xref: /freebsd/lib/libutil/mntopts.c (revision 8073a5137f223bb481606b15edaa5ecb93ceffcb)
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 /*
149  * Get the mount point information for name. Name may be mount point name
150  * or device name (with or without /dev/ preprended).
151  */
152 struct statfs *
getmntpoint(const char * name)153 getmntpoint(const char *name)
154 {
155 	struct stat devstat, mntdevstat;
156 	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
157 	char *ddevname;
158 	struct statfs *mntbuf, *statfsp;
159 	int i, mntsize, isdev;
160 	u_long len;
161 
162 	if (stat(name, &devstat) != 0)
163 		return (NULL);
164 	if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
165 		isdev = 1;
166 	else
167 		isdev = 0;
168 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
169 	for (i = 0; i < mntsize; i++) {
170 		statfsp = &mntbuf[i];
171 		if (isdev == 0) {
172 			if (strcmp(name, statfsp->f_mntonname))
173 				continue;
174 			return (statfsp);
175 		}
176 		ddevname = statfsp->f_mntfromname;
177 		if (*ddevname != '/') {
178 			if ((len = strlen(_PATH_DEV) + strlen(ddevname) + 1) >
179 			    sizeof(statfsp->f_mntfromname) ||
180 			    len > sizeof(device))
181 				continue;
182 			strncpy(device, _PATH_DEV, len);
183 			strncat(device, ddevname, len);
184 			if (stat(device, &mntdevstat) == 0)
185 				strncpy(statfsp->f_mntfromname, device, len);
186 		}
187 		if (stat(ddevname, &mntdevstat) == 0 &&
188 		    S_ISCHR(mntdevstat.st_mode) &&
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
build_iovec(struct iovec ** iov,int * iovlen,const char * name,void * val,size_t len)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
build_iovec_argf(struct iovec ** iov,int * iovlen,const char * name,const char * fmt,...)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
free_iovec(struct iovec ** iov,int * iovlen)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