2 * Copyright (c) Ian F. Darwin 1986-1995.
3 * Software written by Ian F. Darwin and others;
4 * maintained 1995-present by Christos Zoulas and others.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice immediately at the beginning of the file, without modification,
11 * 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.
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 FOR
20 * 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
29 * fsmagic - magic based on filesystem info - directory, special files, etc.
35 FILE_RCSID("@(#)$File: fsmagic.c,v 1.71 2013/12/01 18:01:07 christos Exp $")
44 /* Since major is a function on SVR4, we cannot use `ifndef major'. */
46 # include <sys/mkdev.h>
49 #ifdef MAJOR_IN_SYSMACROS
50 # include <sys/sysmacros.h>
53 #ifdef major /* Might be defined in sys/types.h. */
58 # define major(dev) (((dev) >> 8) & 0xff)
59 # define minor(dev) ((dev) & 0xff)
64 bad_link(struct magic_set *ms, int err, char *buf)
66 int mime = ms->flags & MAGIC_MIME;
67 if ((mime & MAGIC_MIME_TYPE) &&
68 file_printf(ms, "inode/symlink")
72 if (ms->flags & MAGIC_ERROR) {
74 "broken symbolic link to `%s'", buf);
77 if (file_printf(ms, "broken symbolic link to `%s'", buf) == -1)
84 handle_mime(struct magic_set *ms, int mime, const char *str)
86 if ((mime & MAGIC_MIME_TYPE)) {
87 if (file_printf(ms, "inode/%s", str) == -1)
89 if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms,
93 if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms, "binary") == -1)
99 file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
102 int mime = ms->flags & MAGIC_MIME;
106 struct stat tstatbuf;
109 if (ms->flags & MAGIC_APPLE)
114 #define COMMA (did++ ? ", " : "")
116 * Fstat is cheaper but fails for files you don't have read perms on.
117 * On 4.2BSD and similar systems, use lstat() to identify symlinks.
120 if ((ms->flags & MAGIC_SYMLINK) == 0)
124 ret = stat(fn, sb); /* don't merge into if; see "ret =" above */
127 if (ms->flags & MAGIC_ERROR) {
128 file_error(ms, errno, "cannot stat `%s'", fn);
131 if (file_printf(ms, "cannot open `%s' (%s)",
132 fn, strerror(errno)) == -1)
140 if (sb->st_mode & S_ISUID)
141 if (file_printf(ms, "%ssetuid", COMMA) == -1)
145 if (sb->st_mode & S_ISGID)
146 if (file_printf(ms, "%ssetgid", COMMA) == -1)
150 if (sb->st_mode & S_ISVTX)
151 if (file_printf(ms, "%ssticky", COMMA) == -1)
156 switch (sb->st_mode & S_IFMT) {
159 if (handle_mime(ms, mime, "directory") == -1)
161 } else if (file_printf(ms, "%sdirectory", COMMA) == -1)
167 * If -s has been specified, treat character special files
168 * like ordinary files. Otherwise, just report that they
169 * are block special files and go on to the next file.
171 if ((ms->flags & MAGIC_DEVICES) != 0) {
176 if (handle_mime(ms, mime, "chardevice") == -1)
179 #ifdef HAVE_STAT_ST_RDEV
181 if (file_printf(ms, "%scharacter special (%d/%d/%d)",
182 COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
183 dv_subunit(sb->st_rdev)) == -1)
186 if (file_printf(ms, "%scharacter special (%ld/%ld)",
187 COMMA, (long)major(sb->st_rdev),
188 (long)minor(sb->st_rdev)) == -1)
192 if (file_printf(ms, "%scharacter special", COMMA) == -1)
201 * If -s has been specified, treat block special files
202 * like ordinary files. Otherwise, just report that they
203 * are block special files and go on to the next file.
205 if ((ms->flags & MAGIC_DEVICES) != 0) {
210 if (handle_mime(ms, mime, "blockdevice") == -1)
213 #ifdef HAVE_STAT_ST_RDEV
215 if (file_printf(ms, "%sblock special (%d/%d/%d)",
216 COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
217 dv_subunit(sb->st_rdev)) == -1)
220 if (file_printf(ms, "%sblock special (%ld/%ld)",
221 COMMA, (long)major(sb->st_rdev),
222 (long)minor(sb->st_rdev)) == -1)
226 if (file_printf(ms, "%sblock special", COMMA) == -1)
232 /* TODO add code to handle V7 MUX and Blit MUX files */
235 if((ms->flags & MAGIC_DEVICES) != 0)
238 if (handle_mime(ms, mime, "fifo") == -1)
240 } else if (file_printf(ms, "%sfifo (named pipe)", COMMA) == -1)
247 if (handle_mime(ms, mime, "door") == -1)
249 } else if (file_printf(ms, "%sdoor", COMMA) == -1)
255 if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
256 if (ms->flags & MAGIC_ERROR) {
257 file_error(ms, errno, "unreadable symlink `%s'",
262 if (handle_mime(ms, mime, "symlink") == -1)
264 } else if (file_printf(ms,
265 "%sunreadable symlink `%s' (%s)", COMMA, fn,
266 strerror(errno)) == -1)
270 buf[nch] = '\0'; /* readlink(2) does not do this */
272 /* If broken symlink, say so and quit early. */
274 if (stat(buf, &tstatbuf) < 0)
275 return bad_link(ms, errno, buf);
278 char buf2[BUFSIZ+BUFSIZ+4];
280 if ((tmp = strrchr(fn, '/')) == NULL) {
281 tmp = buf; /* in current directory anyway */
283 if (tmp - fn + 1 > BUFSIZ) {
284 if (ms->flags & MAGIC_ERROR) {
286 "path too long: `%s'", buf);
290 if (handle_mime(ms, mime,
291 "x-path-too-long") == -1)
293 } else if (file_printf(ms,
294 "%spath too long: `%s'", COMMA,
300 (void)strlcpy(buf2, fn, sizeof buf2);
301 buf2[tmp - fn + 1] = '\0';
302 /* plus (rel) link */
303 (void)strlcat(buf2, buf, sizeof buf2);
306 if (stat(tmp, &tstatbuf) < 0)
307 return bad_link(ms, errno, buf);
310 /* Otherwise, handle it. */
311 if ((ms->flags & MAGIC_SYMLINK) != 0) {
313 ms->flags &= MAGIC_SYMLINK;
314 p = magic_file(ms, buf);
315 ms->flags |= MAGIC_SYMLINK;
318 } else { /* just print what it points to */
320 if (handle_mime(ms, mime, "symlink") == -1)
322 } else if (file_printf(ms, "%ssymbolic link to `%s'",
332 if (handle_mime(ms, mime, "socket") == -1)
334 } else if (file_printf(ms, "%ssocket", COMMA) == -1)
341 * regular file, check next possibility
343 * If stat() tells us the file has zero length, report here that
344 * the file is empty, so we can skip all the work of opening and
346 * But if the -s option has been given, we skip this
347 * optimization, since on some systems, stat() reports zero
348 * size for raw disk partitions. (If the block special device
349 * really has zero length, the fact that it is empty will be
350 * detected and reported correctly when we read the file.)
352 if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) {
354 if (handle_mime(ms, mime, "x-empty") == -1)
356 } else if (file_printf(ms, "%sempty", COMMA) == -1)
364 file_error(ms, 0, "invalid mode 0%o", sb->st_mode);
369 if (!mime && did && ret == 0) {
370 if (file_printf(ms, " ") == -1)