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