1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1980, 1988, 1993 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 "namespace.h" 33 #include <sys/param.h> 34 #include <sys/mount.h> 35 #include <sys/stat.h> 36 37 #include <errno.h> 38 #include <fstab.h> 39 #include <paths.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <unistd.h> 44 #include <vis.h> 45 #include "un-namespace.h" 46 47 static FILE *_fs_fp; 48 static struct fstab _fs_fstab; 49 static int LineNo = 0; 50 static char *path_fstab; 51 static char fstab_path[PATH_MAX]; 52 static int fsp_set = 0; 53 54 static void error(int); 55 static void fixfsfile(void); 56 static int fstabscan(void); 57 58 void 59 setfstab(const char *file) 60 { 61 62 if (file == NULL) { 63 path_fstab = _PATH_FSTAB; 64 } else { 65 strncpy(fstab_path, file, PATH_MAX); 66 fstab_path[PATH_MAX - 1] = '\0'; 67 path_fstab = fstab_path; 68 } 69 fsp_set = 1; 70 71 return; 72 } 73 74 const char * 75 getfstab(void) 76 { 77 78 if (fsp_set) 79 return (path_fstab); 80 else 81 return (_PATH_FSTAB); 82 } 83 84 static void 85 fixfsfile(void) 86 { 87 static char buf[sizeof(_PATH_DEV) + MNAMELEN]; 88 struct stat sb; 89 struct statfs sf; 90 91 if (_fs_fstab.fs_file != NULL && strcmp(_fs_fstab.fs_file, "/") != 0) 92 return; 93 if (statfs("/", &sf) != 0) 94 return; 95 if (sf.f_mntfromname[0] == '/') 96 buf[0] = '\0'; 97 else 98 strcpy(buf, _PATH_DEV); 99 strcat(buf, sf.f_mntfromname); 100 if (stat(buf, &sb) != 0 || 101 (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode))) 102 return; 103 _fs_fstab.fs_spec = buf; 104 } 105 106 static int 107 fstabscan(void) 108 { 109 char *cp, *p; 110 #define MAXLINELENGTH 1024 111 static char line[MAXLINELENGTH]; 112 char subline[MAXLINELENGTH]; 113 int typexx; 114 115 for (;;) { 116 117 if (!(p = fgets(line, sizeof(line), _fs_fp))) 118 return (0); 119 /* OLD_STYLE_FSTAB */ 120 ++LineNo; 121 if (*line == '#' || *line == '\n') 122 continue; 123 if (!strpbrk(p, " \t")) { 124 _fs_fstab.fs_spec = strsep(&p, ":\n"); 125 _fs_fstab.fs_file = strsep(&p, ":\n"); 126 fixfsfile(); 127 _fs_fstab.fs_type = strsep(&p, ":\n"); 128 if (_fs_fstab.fs_type) { 129 if (!strcmp(_fs_fstab.fs_type, FSTAB_XX)) 130 continue; 131 _fs_fstab.fs_mntops = _fs_fstab.fs_type; 132 _fs_fstab.fs_vfstype = 133 strcmp(_fs_fstab.fs_type, FSTAB_SW) ? 134 "ufs" : "swap"; 135 if ((cp = strsep(&p, ":\n")) != NULL) { 136 _fs_fstab.fs_freq = atoi(cp); 137 if ((cp = strsep(&p, ":\n")) != NULL) { 138 _fs_fstab.fs_passno = atoi(cp); 139 return (1); 140 } 141 } 142 } 143 goto bad; 144 } 145 /* OLD_STYLE_FSTAB */ 146 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') 147 ; 148 _fs_fstab.fs_spec = cp; 149 if (_fs_fstab.fs_spec == NULL || *_fs_fstab.fs_spec == '#') 150 continue; 151 if (strunvis(_fs_fstab.fs_spec, _fs_fstab.fs_spec) < 0) 152 goto bad; 153 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') 154 ; 155 _fs_fstab.fs_file = cp; 156 if (_fs_fstab.fs_file == NULL) 157 goto bad; 158 if (strunvis(_fs_fstab.fs_file, _fs_fstab.fs_file) < 0) 159 goto bad; 160 fixfsfile(); 161 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') 162 ; 163 _fs_fstab.fs_vfstype = cp; 164 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') 165 ; 166 _fs_fstab.fs_mntops = cp; 167 if (_fs_fstab.fs_mntops == NULL) 168 goto bad; 169 _fs_fstab.fs_freq = 0; 170 _fs_fstab.fs_passno = 0; 171 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') 172 ; 173 if (cp != NULL) { 174 _fs_fstab.fs_freq = atoi(cp); 175 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') 176 ; 177 if (cp != NULL) 178 _fs_fstab.fs_passno = atoi(cp); 179 } 180 (void)strlcpy(subline, _fs_fstab.fs_mntops, sizeof(subline)); 181 p = subline; 182 for (typexx = 0, cp = strsep(&p, ","); cp; 183 cp = strsep(&p, ",")) { 184 if (strlen(cp) != 2) 185 continue; 186 if (!strcmp(cp, FSTAB_RW)) { 187 _fs_fstab.fs_type = FSTAB_RW; 188 break; 189 } 190 if (!strcmp(cp, FSTAB_RQ)) { 191 _fs_fstab.fs_type = FSTAB_RQ; 192 break; 193 } 194 if (!strcmp(cp, FSTAB_RO)) { 195 _fs_fstab.fs_type = FSTAB_RO; 196 break; 197 } 198 if (!strcmp(cp, FSTAB_SW)) { 199 _fs_fstab.fs_type = FSTAB_SW; 200 break; 201 } 202 if (!strcmp(cp, FSTAB_XX)) { 203 _fs_fstab.fs_type = FSTAB_XX; 204 typexx++; 205 break; 206 } 207 } 208 if (typexx) 209 continue; 210 if (cp != NULL) 211 return (1); 212 213 bad: /* no way to distinguish between EOF and syntax error */ 214 error(EFTYPE); 215 } 216 /* NOTREACHED */ 217 } 218 219 struct fstab * 220 getfsent(void) 221 { 222 223 if ((!_fs_fp && !setfsent()) || !fstabscan()) 224 return (NULL); 225 return (&_fs_fstab); 226 } 227 228 struct fstab * 229 getfsspec(const char *name) 230 { 231 232 if (setfsent()) 233 while (fstabscan()) 234 if (!strcmp(_fs_fstab.fs_spec, name)) 235 return (&_fs_fstab); 236 return (NULL); 237 } 238 239 struct fstab * 240 getfsfile(const char *name) 241 { 242 243 if (setfsent()) 244 while (fstabscan()) 245 if (!strcmp(_fs_fstab.fs_file, name)) 246 return (&_fs_fstab); 247 return (NULL); 248 } 249 250 int 251 setfsent(void) 252 { 253 if (_fs_fp) { 254 rewind(_fs_fp); 255 LineNo = 0; 256 return (1); 257 } 258 if (fsp_set == 0) 259 setfstab(secure_getenv("PATH_FSTAB")); 260 if ((_fs_fp = fopen(path_fstab, "re")) != NULL) { 261 LineNo = 0; 262 return (1); 263 } 264 error(errno); 265 return (0); 266 } 267 268 void 269 endfsent(void) 270 { 271 272 if (_fs_fp) { 273 (void)fclose(_fs_fp); 274 _fs_fp = NULL; 275 } 276 277 fsp_set = 0; 278 } 279 280 static void 281 error(int err) 282 { 283 char *p; 284 char num[30]; 285 286 (void)_write(STDERR_FILENO, "fstab: ", 7); 287 (void)_write(STDERR_FILENO, path_fstab, strlen(path_fstab)); 288 (void)_write(STDERR_FILENO, ":", 1); 289 sprintf(num, "%d: ", LineNo); 290 (void)_write(STDERR_FILENO, num, strlen(num)); 291 p = strerror(err); 292 (void)_write(STDERR_FILENO, p, strlen(p)); 293 (void)_write(STDERR_FILENO, "\n", 1); 294 } 295