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