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 170 if (stat(name, &devstat) != 0) 171 return (NULL); 172 if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode)) 173 isdev = 1; 174 else 175 isdev = 0; 176 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 177 for (i = 0; i < mntsize; i++) { 178 statfsp = &mntbuf[i]; 179 ddevname = statfsp->f_mntfromname; 180 if (*ddevname != '/') { 181 if (strlen(_PATH_DEV) + strlen(ddevname) + 1 > 182 sizeof(statfsp->f_mntfromname)) 183 continue; 184 strcpy(device, _PATH_DEV); 185 strcat(device, ddevname); 186 strcpy(statfsp->f_mntfromname, device); 187 } 188 if (isdev == 0) { 189 if (strcmp(name, statfsp->f_mntonname)) 190 continue; 191 return (statfsp); 192 } 193 if (stat(ddevname, &mntdevstat) == 0 && 194 mntdevstat.st_rdev == devstat.st_rdev) 195 return (statfsp); 196 } 197 return (NULL); 198 } 199 200 /* 201 * If possible reload a mounted filesystem. 202 * When prtmsg != NULL print a warning if a reload is attempted, but fails. 203 * Return 0 on success, 1 on failure. 204 */ 205 int 206 chkdoreload(struct statfs *mntp, 207 void (*prtmsg)(const char *, ...) __printflike(1,2)) 208 { 209 struct iovec *iov; 210 int iovlen, error; 211 char errmsg[255]; 212 213 /* 214 * If the filesystem is not mounted it does not need to be reloaded. 215 * If it is mounted for writing, then it could not have been opened 216 * for writing by a utility, so does not need to be reloaded. 217 */ 218 if (mntp == NULL || (mntp->f_flags & MNT_RDONLY) == 0) 219 return (0); 220 221 /* 222 * We modified a mounted file system. Do a mount update on 223 * it so we can continue using it as safely as possible. 224 */ 225 iov = NULL; 226 iovlen = 0; 227 errmsg[0] = '\0'; 228 build_iovec(&iov, &iovlen, "fstype", __DECONST(void *, "ffs"), 4); 229 build_iovec(&iov, &iovlen, "from", mntp->f_mntfromname, (size_t)-1); 230 build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname, (size_t)-1); 231 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); 232 build_iovec(&iov, &iovlen, "update", NULL, 0); 233 build_iovec(&iov, &iovlen, "reload", NULL, 0); 234 /* 235 * XX: We need the following line until we clean up 236 * nmount parsing of root mounts and NFS root mounts. 237 */ 238 build_iovec(&iov, &iovlen, "ro", NULL, 0); 239 error = nmount(iov, iovlen, mntp->f_flags); 240 free_iovec(&iov, &iovlen); 241 if (error == 0) 242 return (0); 243 if (prtmsg != NULL) 244 prtmsg("mount reload of '%s' failed: %s %s\n\n", 245 mntp->f_mntonname, strerror(errno), errmsg); 246 return (1); 247 } 248 249 void 250 build_iovec(struct iovec **iov, int *iovlen, const char *name, void *val, 251 size_t len) 252 { 253 int i; 254 255 if (*iovlen < 0) 256 return; 257 i = *iovlen; 258 *iov = realloc(*iov, sizeof **iov * (i + 2)); 259 if (*iov == NULL) { 260 *iovlen = -1; 261 return; 262 } 263 (*iov)[i].iov_base = strdup(name); 264 (*iov)[i].iov_len = strlen(name) + 1; 265 i++; 266 (*iov)[i].iov_base = val; 267 if (len == (size_t)-1) { 268 if (val != NULL) 269 len = strlen(val) + 1; 270 else 271 len = 0; 272 } 273 (*iov)[i].iov_len = (int)len; 274 *iovlen = ++i; 275 } 276 277 /* 278 * This function is needed for compatibility with parameters 279 * which used to use the mount_argf() command for the old mount() syscall. 280 */ 281 void 282 build_iovec_argf(struct iovec **iov, int *iovlen, const char *name, 283 const char *fmt, ...) 284 { 285 va_list ap; 286 char val[255] = { 0 }; 287 288 va_start(ap, fmt); 289 vsnprintf(val, sizeof(val), fmt, ap); 290 va_end(ap); 291 build_iovec(iov, iovlen, name, strdup(val), (size_t)-1); 292 } 293 294 /* 295 * Free the iovec and reset to NULL with zero length. Useful for calling 296 * nmount in a loop. 297 */ 298 void 299 free_iovec(struct iovec **iov, int *iovlen) 300 { 301 int i; 302 303 for (i = 0; i < *iovlen; i += 2) 304 free((*iov)[i].iov_base); 305 free(*iov); 306 } 307