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