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: stable/12/sbin/mount/getmntopts.c 326025 2017-11-20 19:49:47Z pfg $");
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 <stdarg.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51
52 #include "mntopts.h"
53
54 int getmnt_silent = 0;
55
56 void
getmntopts(const char * options,const struct mntopt * m0,int * flagp,int * altflagp)57 getmntopts(const char *options, const struct mntopt *m0, int *flagp,
58 int *altflagp)
59 {
60 const struct mntopt *m;
61 int negative, len;
62 char *opt, *optbuf, *p;
63 int *thisflagp;
64
65 /* Copy option string, since it is about to be torn asunder... */
66 if ((optbuf = strdup(options)) == NULL)
67 err(1, NULL);
68
69 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
70 /* Check for "no" prefix. */
71 if (opt[0] == 'n' && opt[1] == 'o') {
72 negative = 1;
73 opt += 2;
74 } else
75 negative = 0;
76
77 /*
78 * for options with assignments in them (ie. quotas)
79 * ignore the assignment as it's handled elsewhere
80 */
81 p = strchr(opt, '=');
82 if (p != NULL)
83 *++p = '\0';
84
85 /* Scan option table. */
86 for (m = m0; m->m_option != NULL; ++m) {
87 len = strlen(m->m_option);
88 if (strncasecmp(opt, m->m_option, len) == 0)
89 if (opt[len] == '\0' || opt[len] == '=')
90 break;
91 }
92
93 /* Save flag, or fail if option is not recognized. */
94 if (m->m_option) {
95 thisflagp = m->m_altloc ? altflagp : flagp;
96 if (negative == m->m_inverse)
97 *thisflagp |= m->m_flag;
98 else
99 *thisflagp &= ~m->m_flag;
100 } else if (!getmnt_silent) {
101 errx(1, "-o %s: option not supported", opt);
102 }
103 }
104
105 free(optbuf);
106 }
107
108 void
rmslashes(char * rrpin,char * rrpout)109 rmslashes(char *rrpin, char *rrpout)
110 {
111 char *rrpoutstart;
112
113 *rrpout = *rrpin;
114 for (rrpoutstart = rrpout; *rrpin != '\0'; *rrpout++ = *rrpin++) {
115
116 /* skip all double slashes */
117 while (*rrpin == '/' && *(rrpin + 1) == '/')
118 rrpin++;
119 }
120
121 /* remove trailing slash if necessary */
122 if (rrpout - rrpoutstart > 1 && *(rrpout - 1) == '/')
123 *(rrpout - 1) = '\0';
124 else
125 *rrpout = '\0';
126 }
127
128 int
checkpath(const char * path,char * resolved)129 checkpath(const char *path, char *resolved)
130 {
131 struct stat sb;
132
133 if (realpath(path, resolved) == NULL || stat(resolved, &sb) != 0)
134 return (1);
135 if (!S_ISDIR(sb.st_mode)) {
136 errno = ENOTDIR;
137 return (1);
138 }
139 return (0);
140 }
141
142 void
build_iovec(struct iovec ** iov,int * iovlen,const char * name,void * val,size_t len)143 build_iovec(struct iovec **iov, int *iovlen, const char *name, void *val,
144 size_t len)
145 {
146 int i;
147
148 if (*iovlen < 0)
149 return;
150 i = *iovlen;
151 *iov = realloc(*iov, sizeof **iov * (i + 2));
152 if (*iov == NULL) {
153 *iovlen = -1;
154 return;
155 }
156 (*iov)[i].iov_base = strdup(name);
157 (*iov)[i].iov_len = strlen(name) + 1;
158 i++;
159 (*iov)[i].iov_base = val;
160 if (len == (size_t)-1) {
161 if (val != NULL)
162 len = strlen(val) + 1;
163 else
164 len = 0;
165 }
166 (*iov)[i].iov_len = (int)len;
167 *iovlen = ++i;
168 }
169
170 /*
171 * This function is needed for compatibility with parameters
172 * which used to use the mount_argf() command for the old mount() syscall.
173 */
174 void
build_iovec_argf(struct iovec ** iov,int * iovlen,const char * name,const char * fmt,...)175 build_iovec_argf(struct iovec **iov, int *iovlen, const char *name,
176 const char *fmt, ...)
177 {
178 va_list ap;
179 char val[255] = { 0 };
180
181 va_start(ap, fmt);
182 vsnprintf(val, sizeof(val), fmt, ap);
183 va_end(ap);
184 build_iovec(iov, iovlen, name, strdup(val), (size_t)-1);
185 }
186
187 /*
188 * Free the iovec and reset to NULL with zero length. Useful for calling
189 * nmount in a loop.
190 */
191 void
free_iovec(struct iovec ** iov,int * iovlen)192 free_iovec(struct iovec **iov, int *iovlen)
193 {
194 int i;
195
196 for (i = 0; i < *iovlen; i++)
197 free((*iov)[i].iov_base);
198 free(*iov);
199 }
200