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