1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1994
5 * The Regents of the University of California. 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 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/param.h>
33 #include <sys/mount.h>
34 #include <sys/stat.h>
35 #include <sys/uio.h>
36
37 #include <err.h>
38 #include <errno.h>
39 #include <mntopts.h>
40 #include <paths.h>
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 int getmnt_silent = 0;
47
48 void
getmntopts(const char * options,const struct mntopt * m0,int * flagp,int * altflagp)49 getmntopts(const char *options, const struct mntopt *m0, int *flagp,
50 int *altflagp)
51 {
52 const struct mntopt *m;
53 int negative, len;
54 char *opt, *optbuf, *p;
55 int *thisflagp;
56
57 /* Copy option string, since it is about to be torn asunder... */
58 if ((optbuf = strdup(options)) == NULL)
59 err(1, NULL);
60
61 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
62 /* Check for "no" prefix. */
63 if (opt[0] == 'n' && opt[1] == 'o') {
64 negative = 1;
65 opt += 2;
66 } else
67 negative = 0;
68
69 /*
70 * for options with assignments in them (ie. quotas)
71 * ignore the assignment as it's handled elsewhere
72 */
73 p = strchr(opt, '=');
74 if (p != NULL)
75 *++p = '\0';
76
77 /* Scan option table. */
78 for (m = m0; m->m_option != NULL; ++m) {
79 len = strlen(m->m_option);
80 if (strncasecmp(opt, m->m_option, len) == 0)
81 if (opt[len] == '\0' || opt[len] == '=')
82 break;
83 }
84
85 /* Save flag, or fail if option is not recognized. */
86 if (m->m_option) {
87 thisflagp = m->m_altloc ? altflagp : flagp;
88 if (negative == m->m_inverse)
89 *thisflagp |= m->m_flag;
90 else
91 *thisflagp &= ~m->m_flag;
92 } else if (!getmnt_silent) {
93 errx(1, "-o %s: option not supported", opt);
94 }
95 }
96
97 free(optbuf);
98 }
99
100 void
rmslashes(char * rrpin,char * rrpout)101 rmslashes(char *rrpin, char *rrpout)
102 {
103 char *rrpoutstart;
104
105 *rrpout = *rrpin;
106 for (rrpoutstart = rrpout; *rrpin != '\0'; *rrpout++ = *rrpin++) {
107
108 /* skip all double slashes */
109 while (*rrpin == '/' && *(rrpin + 1) == '/')
110 rrpin++;
111 }
112
113 /* remove trailing slash if necessary */
114 if (rrpout - rrpoutstart > 1 && *(rrpout - 1) == '/')
115 *(rrpout - 1) = '\0';
116 else
117 *rrpout = '\0';
118 }
119
120 int
checkpath(const char * path,char * resolved)121 checkpath(const char *path, char *resolved)
122 {
123 struct stat sb;
124
125 if (realpath(path, resolved) == NULL || stat(resolved, &sb) != 0)
126 return (1);
127 if (!S_ISDIR(sb.st_mode)) {
128 errno = ENOTDIR;
129 return (1);
130 }
131 return (0);
132 }
133
134 int
checkpath_allow_file(const char * path,char * resolved)135 checkpath_allow_file(const char *path, char *resolved)
136 {
137 struct stat sb;
138
139 if (realpath(path, resolved) == NULL || stat(resolved, &sb) != 0)
140 return (1);
141 if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode)) {
142 errno = ENOTDIR;
143 return (1);
144 }
145 return (0);
146 }
147
148 static char *
prependdevtopath(const char * path,char * buf,u_long buflen)149 prependdevtopath(const char *path, char *buf, u_long buflen)
150 {
151 u_long len;
152
153 if ((len = strlen(_PATH_DEV) + strlen(path) + 1) > buflen)
154 return NULL;
155 strncpy(buf, _PATH_DEV, len);
156 strncat(buf, path, len - sizeof(_PATH_DEV));
157 return (buf);
158 }
159
160 /*
161 * Get the mount point information for name. Name may be mount point name
162 * or device name (with or without /dev/ preprended).
163 */
164 struct statfs *
getmntpoint(const char * name)165 getmntpoint(const char *name)
166 {
167 struct stat devstat, mntdevstat;
168 char *devname;
169 struct statfs *mntbuf, *statfsp;
170 int i, len, isdev, mntsize, mntfromnamesize;
171 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
172 u_long devlen;
173
174 devlen = sizeof(device);
175 /*
176 * Note that stat(NULL, &statbuf) returns -1 (EBADF) which will
177 * cause us to return NULL if prependdevtopath() returns NULL.
178 */
179 if (stat(name, &devstat) != 0 &&
180 (name[0] != '/' &&
181 stat(prependdevtopath(name, device, devlen), &devstat) != 0))
182 return (NULL);
183 if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
184 isdev = 1;
185 else
186 isdev = 0;
187 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
188 mntfromnamesize = sizeof(statfsp->f_mntfromname);
189 for (i = 0; i < mntsize; i++) {
190 statfsp = &mntbuf[i];
191 if (isdev == 0) {
192 if (strcmp(name, statfsp->f_mntonname))
193 continue;
194 return (statfsp);
195 }
196 devname = statfsp->f_mntfromname;
197 if (*devname == '/') {
198 if (stat(devname, &mntdevstat) != 0)
199 continue;
200 } else {
201 devname = prependdevtopath(devname, device, devlen);
202 if (devname == NULL ||
203 (len = strlen(devname)) > mntfromnamesize)
204 continue;
205 if (stat(devname, &mntdevstat) != 0)
206 continue;
207 strncpy(statfsp->f_mntfromname, devname, len);
208 }
209 if (S_ISCHR(mntdevstat.st_mode) &&
210 mntdevstat.st_rdev == devstat.st_rdev)
211 return (statfsp);
212 }
213 return (NULL);
214 }
215
216 /*
217 * If possible reload a mounted filesystem.
218 * When prtmsg != NULL print a warning if a reload is attempted, but fails.
219 * Return 0 on success, 1 on failure.
220 */
221 int
222 chkdoreload(struct statfs *mntp,
223 void (*prtmsg)(const char *, ...) __printflike(1,2))
224 {
225 struct iovec *iov;
226 int iovlen, error;
227 char errmsg[255];
228
229 /*
230 * If the filesystem is not mounted it does not need to be reloaded.
231 * If it is mounted for writing, then it could not have been opened
232 * for writing by a utility, so does not need to be reloaded.
233 */
234 if (mntp == NULL || (mntp->f_flags & MNT_RDONLY) == 0)
235 return (0);
236
237 /*
238 * We modified a mounted file system. Do a mount update on
239 * it so we can continue using it as safely as possible.
240 */
241 iov = NULL;
242 iovlen = 0;
243 errmsg[0] = '\0';
244 build_iovec(&iov, &iovlen, "fstype", __DECONST(void *, "ffs"), 4);
245 build_iovec(&iov, &iovlen, "from", mntp->f_mntfromname, (size_t)-1);
246 build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname, (size_t)-1);
247 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
248 build_iovec(&iov, &iovlen, "update", NULL, 0);
249 build_iovec(&iov, &iovlen, "reload", NULL, 0);
250 /*
251 * XX: We need the following line until we clean up
252 * nmount parsing of root mounts and NFS root mounts.
253 */
254 build_iovec(&iov, &iovlen, "ro", NULL, 0);
255 error = nmount(iov, iovlen, mntp->f_flags);
256 free_iovec(&iov, &iovlen);
257 if (error == 0)
258 return (0);
259 if (prtmsg != NULL)
260 prtmsg("mount reload of '%s' failed: %s %s\n\n",
261 mntp->f_mntonname, strerror(errno), errmsg);
262 return (1);
263 }
264
265 void
build_iovec(struct iovec ** iov,int * iovlen,const char * name,void * val,size_t len)266 build_iovec(struct iovec **iov, int *iovlen, const char *name, void *val,
267 size_t len)
268 {
269 int i;
270
271 if (*iovlen < 0)
272 return;
273 i = *iovlen;
274 *iov = realloc(*iov, sizeof **iov * (i + 2));
275 if (*iov == NULL) {
276 *iovlen = -1;
277 return;
278 }
279 (*iov)[i].iov_base = strdup(name);
280 (*iov)[i].iov_len = strlen(name) + 1;
281 i++;
282 (*iov)[i].iov_base = val;
283 if (len == (size_t)-1) {
284 if (val != NULL)
285 len = strlen(val) + 1;
286 else
287 len = 0;
288 }
289 (*iov)[i].iov_len = (int)len;
290 *iovlen = ++i;
291 }
292
293 /*
294 * This function is needed for compatibility with parameters
295 * which used to use the mount_argf() command for the old mount() syscall.
296 */
297 void
build_iovec_argf(struct iovec ** iov,int * iovlen,const char * name,const char * fmt,...)298 build_iovec_argf(struct iovec **iov, int *iovlen, const char *name,
299 const char *fmt, ...)
300 {
301 va_list ap;
302 char val[255] = { 0 };
303
304 va_start(ap, fmt);
305 vsnprintf(val, sizeof(val), fmt, ap);
306 va_end(ap);
307 build_iovec(iov, iovlen, name, strdup(val), (size_t)-1);
308 }
309
310 /*
311 * Free the iovec and reset to NULL with zero length. Useful for calling
312 * nmount in a loop.
313 */
314 void
free_iovec(struct iovec ** iov,int * iovlen)315 free_iovec(struct iovec **iov, int *iovlen)
316 {
317 int i;
318
319 for (i = 0; i < *iovlen; i += 2)
320 free((*iov)[i].iov_base);
321 free(*iov);
322 }
323