xref: /freebsd/sbin/mount/getmntopts.c (revision f374ba41f55c1a127303d92d830dd58eef2f5243)
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 #if 0
33 #ifndef lint
34 static char sccsid[] = "@(#)getmntopts.c	8.3 (Berkeley) 3/29/95";
35 #endif /* not lint */
36 #endif
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/param.h>
41 #include <sys/mount.h>
42 #include <sys/stat.h>
43 #include <sys/uio.h>
44 
45 #include <err.h>
46 #include <errno.h>
47 #include <paths.h>
48 #include <stdarg.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 
53 #include "mntopts.h"
54 
55 int getmnt_silent = 0;
56 
57 void
58 getmntopts(const char *options, const struct mntopt *m0, int *flagp,
59 	int *altflagp)
60 {
61 	const struct mntopt *m;
62 	int negative, len;
63 	char *opt, *optbuf, *p;
64 	int *thisflagp;
65 
66 	/* Copy option string, since it is about to be torn asunder... */
67 	if ((optbuf = strdup(options)) == NULL)
68 		err(1, NULL);
69 
70 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
71 		/* Check for "no" prefix. */
72 		if (opt[0] == 'n' && opt[1] == 'o') {
73 			negative = 1;
74 			opt += 2;
75 		} else
76 			negative = 0;
77 
78 		/*
79 		 * for options with assignments in them (ie. quotas)
80 		 * ignore the assignment as it's handled elsewhere
81 		 */
82 		p = strchr(opt, '=');
83 		if (p != NULL)
84 			 *++p = '\0';
85 
86 		/* Scan option table. */
87 		for (m = m0; m->m_option != NULL; ++m) {
88 			len = strlen(m->m_option);
89 			if (strncasecmp(opt, m->m_option, len) == 0)
90 				if (opt[len] == '\0' || opt[len] == '=')
91 					break;
92 		}
93 
94 		/* Save flag, or fail if option is not recognized. */
95 		if (m->m_option) {
96 			thisflagp = m->m_altloc ? altflagp : flagp;
97 			if (negative == m->m_inverse)
98 				*thisflagp |= m->m_flag;
99 			else
100 				*thisflagp &= ~m->m_flag;
101 		} else if (!getmnt_silent) {
102 			errx(1, "-o %s: option not supported", opt);
103 		}
104 	}
105 
106 	free(optbuf);
107 }
108 
109 void
110 rmslashes(char *rrpin, char *rrpout)
111 {
112 	char *rrpoutstart;
113 
114 	*rrpout = *rrpin;
115 	for (rrpoutstart = rrpout; *rrpin != '\0'; *rrpout++ = *rrpin++) {
116 
117 		/* skip all double slashes */
118 		while (*rrpin == '/' && *(rrpin + 1) == '/')
119 			 rrpin++;
120 	}
121 
122 	/* remove trailing slash if necessary */
123 	if (rrpout - rrpoutstart > 1 && *(rrpout - 1) == '/')
124 		*(rrpout - 1) = '\0';
125 	else
126 		*rrpout = '\0';
127 }
128 
129 int
130 checkpath(const char *path, char *resolved)
131 {
132 	struct stat sb;
133 
134 	if (realpath(path, resolved) == NULL || stat(resolved, &sb) != 0)
135 		return (1);
136 	if (!S_ISDIR(sb.st_mode)) {
137 		errno = ENOTDIR;
138 		return (1);
139 	}
140 	return (0);
141 }
142 
143 int
144 checkpath_allow_file(const char *path, char *resolved)
145 {
146 	struct stat sb;
147 
148 	if (realpath(path, resolved) == NULL || stat(resolved, &sb) != 0)
149 		return (1);
150 	if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode)) {
151 		errno = ENOTDIR;
152 		return (1);
153 	}
154 	return (0);
155 }
156 
157 /*
158  * Get the mount point information for name. Name may be mount point name
159  * or device name (with or without /dev/ preprended).
160  */
161 struct statfs *
162 getmntpoint(const char *name)
163 {
164 	struct stat devstat, mntdevstat;
165 	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
166 	char *ddevname;
167 	struct statfs *mntbuf, *statfsp;
168 	int i, mntsize, isdev;
169 	u_long len;
170 
171 	if (stat(name, &devstat) != 0)
172 		return (NULL);
173 	if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
174 		isdev = 1;
175 	else
176 		isdev = 0;
177 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
178 	for (i = 0; i < mntsize; i++) {
179 		statfsp = &mntbuf[i];
180 		if (isdev == 0) {
181 			if (strcmp(name, statfsp->f_mntonname))
182 				continue;
183 			return (statfsp);
184 		}
185 		ddevname = statfsp->f_mntfromname;
186 		if (*ddevname != '/') {
187 			if ((len = strlen(_PATH_DEV) + strlen(ddevname) + 1) >
188 			    sizeof(statfsp->f_mntfromname) ||
189 			    len > sizeof(device))
190 				continue;
191 			strncpy(device, _PATH_DEV, len);
192 			strncat(device, ddevname, len);
193 			if (stat(device, &mntdevstat) == 0)
194 				strncpy(statfsp->f_mntfromname, device, len);
195 		}
196 		if (stat(ddevname, &mntdevstat) == 0 &&
197 		    mntdevstat.st_rdev == devstat.st_rdev)
198 			return (statfsp);
199 	}
200 	return (NULL);
201 }
202 
203 /*
204  * If possible reload a mounted filesystem.
205  * When prtmsg != NULL print a warning if a reload is attempted, but fails.
206  * Return 0 on success, 1 on failure.
207  */
208 int
209 chkdoreload(struct statfs *mntp,
210 	void (*prtmsg)(const char *, ...) __printflike(1,2))
211 {
212 	struct iovec *iov;
213 	int iovlen, error;
214 	char errmsg[255];
215 
216 	/*
217 	 * If the filesystem is not mounted it does not need to be reloaded.
218 	 * If it is mounted for writing, then it could not have been opened
219 	 * for writing by a utility, so does not need to be reloaded.
220 	 */
221 	if (mntp == NULL || (mntp->f_flags & MNT_RDONLY) == 0)
222 		return (0);
223 
224 	/*
225 	 * We modified a mounted file system.  Do a mount update on
226 	 * it so we can continue using it as safely as possible.
227 	 */
228 	iov = NULL;
229 	iovlen = 0;
230 	errmsg[0] = '\0';
231 	build_iovec(&iov, &iovlen, "fstype", __DECONST(void *, "ffs"), 4);
232 	build_iovec(&iov, &iovlen, "from", mntp->f_mntfromname, (size_t)-1);
233 	build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname, (size_t)-1);
234 	build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
235 	build_iovec(&iov, &iovlen, "update", NULL, 0);
236 	build_iovec(&iov, &iovlen, "reload", NULL, 0);
237 	/*
238 	 * XX: We need the following line until we clean up
239 	 * nmount parsing of root mounts and NFS root mounts.
240 	 */
241 	build_iovec(&iov, &iovlen, "ro", NULL, 0);
242 	error = nmount(iov, iovlen, mntp->f_flags);
243 	free_iovec(&iov, &iovlen);
244 	if (error == 0)
245 		return (0);
246 	if (prtmsg != NULL)
247 		prtmsg("mount reload of '%s' failed: %s %s\n\n",
248 		    mntp->f_mntonname, strerror(errno), errmsg);
249 	return (1);
250 }
251 
252 void
253 build_iovec(struct iovec **iov, int *iovlen, const char *name, void *val,
254 	    size_t len)
255 {
256 	int i;
257 
258 	if (*iovlen < 0)
259 		return;
260 	i = *iovlen;
261 	*iov = realloc(*iov, sizeof **iov * (i + 2));
262 	if (*iov == NULL) {
263 		*iovlen = -1;
264 		return;
265 	}
266 	(*iov)[i].iov_base = strdup(name);
267 	(*iov)[i].iov_len = strlen(name) + 1;
268 	i++;
269 	(*iov)[i].iov_base = val;
270 	if (len == (size_t)-1) {
271 		if (val != NULL)
272 			len = strlen(val) + 1;
273 		else
274 			len = 0;
275 	}
276 	(*iov)[i].iov_len = (int)len;
277 	*iovlen = ++i;
278 }
279 
280 /*
281  * This function is needed for compatibility with parameters
282  * which used to use the mount_argf() command for the old mount() syscall.
283  */
284 void
285 build_iovec_argf(struct iovec **iov, int *iovlen, const char *name,
286     const char *fmt, ...)
287 {
288 	va_list ap;
289 	char val[255] = { 0 };
290 
291 	va_start(ap, fmt);
292 	vsnprintf(val, sizeof(val), fmt, ap);
293 	va_end(ap);
294 	build_iovec(iov, iovlen, name, strdup(val), (size_t)-1);
295 }
296 
297 /*
298  * Free the iovec and reset to NULL with zero length.  Useful for calling
299  * nmount in a loop.
300  */
301 void
302 free_iovec(struct iovec **iov, int *iovlen)
303 {
304 	int i;
305 
306 	for (i = 0; i < *iovlen; i += 2)
307 		free((*iov)[i].iov_base);
308 	free(*iov);
309 }
310