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