1 /*- 2 * Copyright (c) 2014 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Edward Tomasz Napierala under sponsorship 6 * from the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/capsicum.h> 35 #include <sys/disk.h> 36 #include <sys/ioctl.h> 37 #include <sys/stat.h> 38 #include <capsicum_helpers.h> 39 #include <err.h> 40 #include <errno.h> 41 #include <iconv.h> 42 #include <locale.h> 43 #include <stdbool.h> 44 #include <stddef.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <unistd.h> 49 #include <vis.h> 50 51 #include "fstyp.h" 52 53 #define LABEL_LEN 256 54 55 bool show_label = false; 56 57 typedef int (*fstyp_function)(FILE *, char *, size_t); 58 59 static struct { 60 const char *name; 61 fstyp_function function; 62 bool unmountable; 63 char *precache_encoding; 64 } fstypes[] = { 65 { "cd9660", &fstyp_cd9660, false, NULL }, 66 { "exfat", &fstyp_exfat, false, EXFAT_ENC }, 67 { "ext2fs", &fstyp_ext2fs, false, NULL }, 68 { "geli", &fstyp_geli, true, NULL }, 69 { "msdosfs", &fstyp_msdosfs, false, NULL }, 70 { "ntfs", &fstyp_ntfs, false, NULL }, 71 { "ufs", &fstyp_ufs, false, NULL }, 72 #ifdef HAVE_ZFS 73 { "zfs", &fstyp_zfs, true, NULL }, 74 #endif 75 { NULL, NULL, NULL, NULL } 76 }; 77 78 void * 79 read_buf(FILE *fp, off_t off, size_t len) 80 { 81 int error; 82 size_t nread; 83 void *buf; 84 85 error = fseek(fp, off, SEEK_SET); 86 if (error != 0) { 87 warn("cannot seek to %jd", (uintmax_t)off); 88 return (NULL); 89 } 90 91 buf = malloc(len); 92 if (buf == NULL) { 93 warn("cannot malloc %zd bytes of memory", len); 94 return (NULL); 95 } 96 97 nread = fread(buf, len, 1, fp); 98 if (nread != 1) { 99 free(buf); 100 if (feof(fp) == 0) 101 warn("fread"); 102 return (NULL); 103 } 104 105 return (buf); 106 } 107 108 char * 109 checked_strdup(const char *s) 110 { 111 char *c; 112 113 c = strdup(s); 114 if (c == NULL) 115 err(1, "strdup"); 116 return (c); 117 } 118 119 void 120 rtrim(char *label, size_t size) 121 { 122 ptrdiff_t i; 123 124 for (i = size - 1; i >= 0; i--) { 125 if (label[i] == '\0') 126 continue; 127 else if (label[i] == ' ') 128 label[i] = '\0'; 129 else 130 break; 131 } 132 } 133 134 static void 135 usage(void) 136 { 137 138 fprintf(stderr, "usage: fstyp [-l] [-s] [-u] special\n"); 139 exit(1); 140 } 141 142 static void 143 type_check(const char *path, FILE *fp) 144 { 145 int error, fd; 146 off_t mediasize; 147 struct stat sb; 148 149 fd = fileno(fp); 150 151 error = fstat(fd, &sb); 152 if (error != 0) 153 err(1, "%s: fstat", path); 154 155 if (S_ISREG(sb.st_mode)) 156 return; 157 158 error = ioctl(fd, DIOCGMEDIASIZE, &mediasize); 159 if (error != 0) 160 errx(1, "%s: not a disk", path); 161 } 162 163 int 164 main(int argc, char **argv) 165 { 166 int ch, error, i, nbytes; 167 bool ignore_type = false, show_unmountable = false; 168 char label[LABEL_LEN + 1], strvised[LABEL_LEN * 4 + 1]; 169 char *path; 170 FILE *fp; 171 fstyp_function fstyp_f; 172 173 while ((ch = getopt(argc, argv, "lsu")) != -1) { 174 switch (ch) { 175 case 'l': 176 show_label = true; 177 break; 178 case 's': 179 ignore_type = true; 180 break; 181 case 'u': 182 show_unmountable = true; 183 break; 184 default: 185 usage(); 186 } 187 } 188 189 argc -= optind; 190 argv += optind; 191 if (argc != 1) 192 usage(); 193 194 path = argv[0]; 195 196 if (setlocale(LC_CTYPE, "") == NULL) 197 err(1, "setlocale"); 198 caph_cache_catpages(); 199 200 /* Cache iconv conversion data before entering capability mode. */ 201 if (show_label) { 202 for (i = 0; i < nitems(fstypes); i++) { 203 iconv_t cd; 204 205 if (fstypes[i].precache_encoding == NULL) 206 continue; 207 cd = iconv_open("", fstypes[i].precache_encoding); 208 if (cd == (iconv_t)-1) 209 err(1, "%s: iconv_open %s", fstypes[i].name, 210 fstypes[i].precache_encoding); 211 /* Iconv keeps a small cache of unused encodings. */ 212 iconv_close(cd); 213 } 214 } 215 216 fp = fopen(path, "r"); 217 if (fp == NULL) 218 err(1, "%s", path); 219 220 if (caph_enter() < 0) 221 err(1, "cap_enter"); 222 223 if (ignore_type == false) 224 type_check(path, fp); 225 226 memset(label, '\0', sizeof(label)); 227 228 for (i = 0;; i++) { 229 if (show_unmountable == false && fstypes[i].unmountable == true) 230 continue; 231 fstyp_f = fstypes[i].function; 232 if (fstyp_f == NULL) 233 break; 234 235 error = fstyp_f(fp, label, sizeof(label)); 236 if (error == 0) 237 break; 238 } 239 240 if (fstypes[i].name == NULL) { 241 warnx("%s: filesystem not recognized", path); 242 return (1); 243 } 244 245 if (show_label && label[0] != '\0') { 246 /* 247 * XXX: I'd prefer VIS_HTTPSTYLE, but it unconditionally 248 * encodes spaces. 249 */ 250 nbytes = strsnvis(strvised, sizeof(strvised), label, 251 VIS_GLOB | VIS_NL, "\"'$"); 252 if (nbytes == -1) 253 err(1, "strsnvis"); 254 255 printf("%s %s\n", fstypes[i].name, strvised); 256 } else { 257 printf("%s\n", fstypes[i].name); 258 } 259 260 return (0); 261 } 262