1 /*- 2 * Copyright (c) 2014 The FreeBSD Foundation 3 * 4 * This software was developed by Edward Tomasz Napierala under sponsorship 5 * from the FreeBSD Foundation. 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 */ 29 30 #include <sys/capsicum.h> 31 #include <sys/disk.h> 32 #include <sys/ioctl.h> 33 #include <sys/stat.h> 34 #include <capsicum_helpers.h> 35 #include <err.h> 36 #include <errno.h> 37 #ifdef WITH_ICONV 38 #include <iconv.h> 39 #endif 40 #include <locale.h> 41 #include <stdbool.h> 42 #include <stddef.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <unistd.h> 47 #include <vis.h> 48 49 #include "fstyp.h" 50 51 #define LABEL_LEN 256 52 53 bool show_label = false; 54 55 typedef int (*fstyp_function)(FILE *, char *, size_t); 56 57 /* 58 * The ordering of fstypes[] is not arbitrary. 59 * 60 * fstyp checks the existence of a file system header to determine the 61 * type of file system on a given device. For different file systems, 62 * these headers reside at different offsets within the device. 63 * 64 * For example, the header for an MS-DOS file system begins at offset 0, 65 * whereas a header for UFS *normally* begins at offset 64k. If a device 66 * was constructed as MS-DOS and then repurposed as UFS (via newfs), it 67 * is possible the MS-DOS header will still be intact. To prevent 68 * misidentifying the file system, it is desirable to check the header 69 * with the largest offset first (i.e., UFS before MS-DOS). 70 */ 71 static struct { 72 const char *name; 73 fstyp_function function; 74 bool unmountable; 75 const char *precache_encoding; 76 } fstypes[] = { 77 /* last sector of geli device */ 78 { "geli", &fstyp_geli, true, NULL }, 79 /* 80 * ufs headers have four different areas, searched in this order: 81 * offsets: 64k, 8k, 0k, 256k + 8192 bytes 82 */ 83 { "ufs", &fstyp_ufs, false, NULL }, 84 /* offset 32768 + 512 bytes */ 85 { "cd9660", &fstyp_cd9660, false, NULL }, 86 /* offset 1024 + 512 bytes */ 87 { "hfs+", &fstyp_hfsp, false, NULL }, 88 /* offset 1024 + 512 bytes */ 89 { "ext2fs", &fstyp_ext2fs, false, NULL }, 90 /* offset 512 + 36 bytes */ 91 { "befs", &fstyp_befs, false, NULL }, 92 /* offset 0 + 40 bytes */ 93 { "apfs", &fstyp_apfs, true, NULL }, 94 /* offset 0 + 512 bytes (for initial signature check) */ 95 { "exfat", &fstyp_exfat, false, EXFAT_ENC }, 96 /* offset 0 + 1928 bytes */ 97 { "hammer", &fstyp_hammer, true, NULL }, 98 /* offset 0 + 65536 bytes (for initial signature check) */ 99 { "hammer2", &fstyp_hammer2, true, NULL }, 100 /* offset 0 + 512 bytes (for initial signature check) */ 101 { "msdosfs", &fstyp_msdosfs, false, NULL }, 102 /* offset 0 + 512 bytes (for initial signature check) */ 103 { "ntfs", &fstyp_ntfs, false, NTFS_ENC }, 104 #ifdef HAVE_ZFS 105 /* offset 0 + 256k */ 106 { "zfs", &fstyp_zfs, true, NULL }, 107 #endif 108 { NULL, NULL, NULL, NULL } 109 }; 110 111 void * 112 read_buf(FILE *fp, off_t off, size_t len) 113 { 114 int error; 115 size_t nread; 116 void *buf; 117 118 error = fseek(fp, off, SEEK_SET); 119 if (error != 0) { 120 warn("cannot seek to %jd", (uintmax_t)off); 121 return (NULL); 122 } 123 124 buf = malloc(len); 125 if (buf == NULL) { 126 warn("cannot malloc %zd bytes of memory", len); 127 return (NULL); 128 } 129 130 nread = fread(buf, len, 1, fp); 131 if (nread != 1) { 132 free(buf); 133 if (feof(fp) == 0) 134 warn("fread"); 135 return (NULL); 136 } 137 138 return (buf); 139 } 140 141 char * 142 checked_strdup(const char *s) 143 { 144 char *c; 145 146 c = strdup(s); 147 if (c == NULL) 148 err(1, "strdup"); 149 return (c); 150 } 151 152 void 153 rtrim(char *label, size_t size) 154 { 155 ptrdiff_t i; 156 157 for (i = size - 1; i >= 0; i--) { 158 if (label[i] == '\0') 159 continue; 160 else if (label[i] == ' ') 161 label[i] = '\0'; 162 else 163 break; 164 } 165 } 166 167 static void 168 usage(void) 169 { 170 171 fprintf(stderr, "usage: fstyp [-l] [-s] [-u] special\n"); 172 exit(1); 173 } 174 175 static void 176 type_check(const char *path, FILE *fp) 177 { 178 int error, fd; 179 off_t mediasize; 180 struct stat sb; 181 182 fd = fileno(fp); 183 184 error = fstat(fd, &sb); 185 if (error != 0) 186 err(1, "%s: fstat", path); 187 188 if (S_ISREG(sb.st_mode)) 189 return; 190 191 error = ioctl(fd, DIOCGMEDIASIZE, &mediasize); 192 if (error != 0) 193 errx(1, "%s: not a disk", path); 194 } 195 196 int 197 main(int argc, char **argv) 198 { 199 int ch, error, i, nbytes; 200 bool ignore_type = false, show_unmountable = false; 201 char label[LABEL_LEN + 1], strvised[LABEL_LEN * 4 + 1]; 202 char *path; 203 FILE *fp; 204 fstyp_function fstyp_f; 205 206 while ((ch = getopt(argc, argv, "lsu")) != -1) { 207 switch (ch) { 208 case 'l': 209 show_label = true; 210 break; 211 case 's': 212 ignore_type = true; 213 break; 214 case 'u': 215 show_unmountable = true; 216 break; 217 default: 218 usage(); 219 } 220 } 221 222 argc -= optind; 223 argv += optind; 224 if (argc != 1) 225 usage(); 226 227 path = argv[0]; 228 229 if (setlocale(LC_CTYPE, "") == NULL) 230 err(1, "setlocale"); 231 caph_cache_catpages(); 232 233 #ifdef WITH_ICONV 234 /* Cache iconv conversion data before entering capability mode. */ 235 if (show_label) { 236 for (i = 0; i < (int)nitems(fstypes); i++) { 237 iconv_t cd; 238 239 if (fstypes[i].precache_encoding == NULL) 240 continue; 241 cd = iconv_open("", fstypes[i].precache_encoding); 242 if (cd == (iconv_t)-1) 243 err(1, "%s: iconv_open %s", fstypes[i].name, 244 fstypes[i].precache_encoding); 245 /* Iconv keeps a small cache of unused encodings. */ 246 iconv_close(cd); 247 } 248 } 249 #endif 250 251 fp = fopen(path, "r"); 252 if (fp == NULL) 253 err(1, "%s", path); 254 255 if (caph_enter() < 0) 256 err(1, "cap_enter"); 257 258 if (ignore_type == false) 259 type_check(path, fp); 260 261 memset(label, '\0', sizeof(label)); 262 263 for (i = 0;; i++) { 264 if (show_unmountable == false && fstypes[i].unmountable == true) 265 continue; 266 fstyp_f = fstypes[i].function; 267 if (fstyp_f == NULL) 268 break; 269 270 error = fstyp_f(fp, label, sizeof(label)); 271 if (error == 0) 272 break; 273 } 274 275 if (fstypes[i].name == NULL) { 276 warnx("%s: filesystem not recognized", path); 277 return (1); 278 } 279 280 if (show_label && label[0] != '\0') { 281 /* 282 * XXX: I'd prefer VIS_HTTPSTYLE, but it unconditionally 283 * encodes spaces. 284 */ 285 nbytes = strsnvis(strvised, sizeof(strvised), label, 286 VIS_GLOB | VIS_NL, "\"'$"); 287 if (nbytes == -1) 288 err(1, "strsnvis"); 289 290 printf("%s %s\n", fstypes[i].name, strvised); 291 } else { 292 printf("%s\n", fstypes[i].name); 293 } 294 295 return (0); 296 } 297