xref: /freebsd/usr.sbin/fstyp/fstyp.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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/cdefs.h>
31 #include <sys/capsicum.h>
32 #include <sys/disk.h>
33 #include <sys/ioctl.h>
34 #include <sys/stat.h>
35 #include <capsicum_helpers.h>
36 #include <err.h>
37 #include <errno.h>
38 #ifdef WITH_ICONV
39 #include <iconv.h>
40 #endif
41 #include <locale.h>
42 #include <stdbool.h>
43 #include <stddef.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <vis.h>
49 
50 #include "fstyp.h"
51 
52 #define	LABEL_LEN	256
53 
54 bool show_label = false;
55 
56 typedef int (*fstyp_function)(FILE *, char *, size_t);
57 
58 static struct {
59 	const char	*name;
60 	fstyp_function	function;
61 	bool		unmountable;
62 	const char	*precache_encoding;
63 } fstypes[] = {
64 	{ "apfs", &fstyp_apfs, true, NULL },
65 	{ "befs", &fstyp_befs, false, NULL },
66 	{ "cd9660", &fstyp_cd9660, false, NULL },
67 	{ "exfat", &fstyp_exfat, false, EXFAT_ENC },
68 	{ "ext2fs", &fstyp_ext2fs, false, NULL },
69 	{ "geli", &fstyp_geli, true, NULL },
70 	{ "hammer", &fstyp_hammer, true, NULL },
71 	{ "hammer2", &fstyp_hammer2, true, NULL },
72 	{ "hfs+", &fstyp_hfsp, false, NULL },
73 	{ "msdosfs", &fstyp_msdosfs, false, NULL },
74 	{ "ntfs", &fstyp_ntfs, false, NTFS_ENC },
75 	{ "ufs", &fstyp_ufs, false, NULL },
76 #ifdef HAVE_ZFS
77 	{ "zfs", &fstyp_zfs, true, NULL },
78 #endif
79 	{ NULL, NULL, NULL, NULL }
80 };
81 
82 void *
83 read_buf(FILE *fp, off_t off, size_t len)
84 {
85 	int error;
86 	size_t nread;
87 	void *buf;
88 
89 	error = fseek(fp, off, SEEK_SET);
90 	if (error != 0) {
91 		warn("cannot seek to %jd", (uintmax_t)off);
92 		return (NULL);
93 	}
94 
95 	buf = malloc(len);
96 	if (buf == NULL) {
97 		warn("cannot malloc %zd bytes of memory", len);
98 		return (NULL);
99 	}
100 
101 	nread = fread(buf, len, 1, fp);
102 	if (nread != 1) {
103 		free(buf);
104 		if (feof(fp) == 0)
105 			warn("fread");
106 		return (NULL);
107 	}
108 
109 	return (buf);
110 }
111 
112 char *
113 checked_strdup(const char *s)
114 {
115 	char *c;
116 
117 	c = strdup(s);
118 	if (c == NULL)
119 		err(1, "strdup");
120 	return (c);
121 }
122 
123 void
124 rtrim(char *label, size_t size)
125 {
126 	ptrdiff_t i;
127 
128 	for (i = size - 1; i >= 0; i--) {
129 		if (label[i] == '\0')
130 			continue;
131 		else if (label[i] == ' ')
132 			label[i] = '\0';
133 		else
134 			break;
135 	}
136 }
137 
138 static void
139 usage(void)
140 {
141 
142 	fprintf(stderr, "usage: fstyp [-l] [-s] [-u] special\n");
143 	exit(1);
144 }
145 
146 static void
147 type_check(const char *path, FILE *fp)
148 {
149 	int error, fd;
150 	off_t mediasize;
151 	struct stat sb;
152 
153 	fd = fileno(fp);
154 
155 	error = fstat(fd, &sb);
156 	if (error != 0)
157 		err(1, "%s: fstat", path);
158 
159 	if (S_ISREG(sb.st_mode))
160 		return;
161 
162 	error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
163 	if (error != 0)
164 		errx(1, "%s: not a disk", path);
165 }
166 
167 int
168 main(int argc, char **argv)
169 {
170 	int ch, error, i, nbytes;
171 	bool ignore_type = false, show_unmountable = false;
172 	char label[LABEL_LEN + 1], strvised[LABEL_LEN * 4 + 1];
173 	char *path;
174 	FILE *fp;
175 	fstyp_function fstyp_f;
176 
177 	while ((ch = getopt(argc, argv, "lsu")) != -1) {
178 		switch (ch) {
179 		case 'l':
180 			show_label = true;
181 			break;
182 		case 's':
183 			ignore_type = true;
184 			break;
185 		case 'u':
186 			show_unmountable = true;
187 			break;
188 		default:
189 			usage();
190 		}
191 	}
192 
193 	argc -= optind;
194 	argv += optind;
195 	if (argc != 1)
196 		usage();
197 
198 	path = argv[0];
199 
200 	if (setlocale(LC_CTYPE, "") == NULL)
201 		err(1, "setlocale");
202 	caph_cache_catpages();
203 
204 #ifdef WITH_ICONV
205 	/* Cache iconv conversion data before entering capability mode. */
206 	if (show_label) {
207 		for (i = 0; i < (int)nitems(fstypes); i++) {
208 			iconv_t cd;
209 
210 			if (fstypes[i].precache_encoding == NULL)
211 				continue;
212 			cd = iconv_open("", fstypes[i].precache_encoding);
213 			if (cd == (iconv_t)-1)
214 				err(1, "%s: iconv_open %s", fstypes[i].name,
215 				    fstypes[i].precache_encoding);
216 			/* Iconv keeps a small cache of unused encodings. */
217 			iconv_close(cd);
218 		}
219 	}
220 #endif
221 
222 	fp = fopen(path, "r");
223 	if (fp == NULL)
224 		err(1, "%s", path);
225 
226 	if (caph_enter() < 0)
227 		err(1, "cap_enter");
228 
229 	if (ignore_type == false)
230 		type_check(path, fp);
231 
232 	memset(label, '\0', sizeof(label));
233 
234 	for (i = 0;; i++) {
235 		if (show_unmountable == false && fstypes[i].unmountable == true)
236 			continue;
237 		fstyp_f = fstypes[i].function;
238 		if (fstyp_f == NULL)
239 			break;
240 
241 		error = fstyp_f(fp, label, sizeof(label));
242 		if (error == 0)
243 			break;
244 	}
245 
246 	if (fstypes[i].name == NULL) {
247 		warnx("%s: filesystem not recognized", path);
248 		return (1);
249 	}
250 
251 	if (show_label && label[0] != '\0') {
252 		/*
253 		 * XXX: I'd prefer VIS_HTTPSTYLE, but it unconditionally
254 		 *      encodes spaces.
255 		 */
256 		nbytes = strsnvis(strvised, sizeof(strvised), label,
257 		    VIS_GLOB | VIS_NL, "\"'$");
258 		if (nbytes == -1)
259 			err(1, "strsnvis");
260 
261 		printf("%s %s\n", fstypes[i].name, strvised);
262 	} else {
263 		printf("%s\n", fstypes[i].name);
264 	}
265 
266 	return (0);
267 }
268