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