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.81 2019/07/16 13:30:32 christos Exp $")
44 /* Since major is a function on SVR4, we cannot use `ifndef major'. */
46 # include <sys/mkdev.h>
49 #ifdef HAVE_SYS_SYSMACROS_H
50 # include <sys/sysmacros.h>
52 #ifdef MAJOR_IN_SYSMACROS
55 #if defined(major) && !defined(HAVE_MAJOR)
56 /* Might be defined in sys/types.h. */
60 # define WIN32_LEAN_AND_MEAN
65 # define major(dev) (((dev) >> 8) & 0xff)
66 # define minor(dev) ((dev) & 0xff)
71 bad_link(struct magic_set *ms, int err, char *buf)
73 int mime = ms->flags & MAGIC_MIME;
74 if ((mime & MAGIC_MIME_TYPE) &&
75 file_printf(ms, "inode/symlink")
79 if (ms->flags & MAGIC_ERROR) {
81 "broken symbolic link to %s", buf);
84 if (file_printf(ms, "broken symbolic link to %s", buf) == -1)
91 handle_mime(struct magic_set *ms, int mime, const char *str)
93 if ((mime & MAGIC_MIME_TYPE)) {
94 if (file_printf(ms, "inode/%s", str) == -1)
96 if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms,
100 if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms, "binary") == -1)
106 file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
109 int mime = ms->flags & MAGIC_MIME;
110 int silent = ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION);
114 struct stat tstatbuf;
120 #define COMMA (did++ ? ", " : "")
122 * Fstat is cheaper but fails for files you don't have read perms on.
123 * On 4.2BSD and similar systems, use lstat() to identify symlinks.
126 if ((ms->flags & MAGIC_SYMLINK) == 0)
130 ret = stat(fn, sb); /* don't merge into if; see "ret =" above */
134 HANDLE hFile = CreateFile((LPCSTR)fn, 0, FILE_SHARE_DELETE |
135 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,
137 if (hFile != INVALID_HANDLE_VALUE) {
139 * Stat failed, but we can still open it - assume it's
140 * a block device, if nothing else.
143 sb->st_mode = S_IFBLK;
146 switch (GetFileType(hFile)) {
148 sb->st_mode |= S_IFCHR;
149 sb->st_mode &= ~S_IFREG;
152 sb->st_mode |= S_IFIFO;
153 sb->st_mode &= ~S_IFREG;
162 if (ms->flags & MAGIC_ERROR) {
163 file_error(ms, errno, "cannot stat `%s'", fn);
166 if (file_printf(ms, "cannot open `%s' (%s)",
167 fn, strerror(errno)) == -1)
173 if (!mime && !silent) {
175 if (sb->st_mode & S_ISUID)
176 if (file_printf(ms, "%ssetuid", COMMA) == -1)
180 if (sb->st_mode & S_ISGID)
181 if (file_printf(ms, "%ssetgid", COMMA) == -1)
185 if (sb->st_mode & S_ISVTX)
186 if (file_printf(ms, "%ssticky", COMMA) == -1)
191 switch (sb->st_mode & S_IFMT) {
194 if (handle_mime(ms, mime, "directory") == -1)
197 } else if (file_printf(ms, "%sdirectory", COMMA) == -1)
203 * If -s has been specified, treat character special files
204 * like ordinary files. Otherwise, just report that they
205 * are block special files and go on to the next file.
207 if ((ms->flags & MAGIC_DEVICES) != 0) {
212 if (handle_mime(ms, mime, "chardevice") == -1)
216 #ifdef HAVE_STRUCT_STAT_ST_RDEV
218 if (file_printf(ms, "%scharacter special (%d/%d/%d)",
219 COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
220 dv_subunit(sb->st_rdev)) == -1)
223 if (file_printf(ms, "%scharacter special (%ld/%ld)",
224 COMMA, (long)major(sb->st_rdev),
225 (long)minor(sb->st_rdev)) == -1)
229 if (file_printf(ms, "%scharacter special", COMMA) == -1)
238 * If -s has been specified, treat block special files
239 * like ordinary files. Otherwise, just report that they
240 * are block special files and go on to the next file.
242 if ((ms->flags & MAGIC_DEVICES) != 0) {
247 if (handle_mime(ms, mime, "blockdevice") == -1)
251 #ifdef HAVE_STRUCT_STAT_ST_RDEV
253 if (file_printf(ms, "%sblock special (%d/%d/%d)",
254 COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
255 dv_subunit(sb->st_rdev)) == -1)
258 if (file_printf(ms, "%sblock special (%ld/%ld)",
259 COMMA, (long)major(sb->st_rdev),
260 (long)minor(sb->st_rdev)) == -1)
264 if (file_printf(ms, "%sblock special", COMMA) == -1)
270 /* TODO add code to handle V7 MUX and Blit MUX files */
273 if((ms->flags & MAGIC_DEVICES) != 0)
276 if (handle_mime(ms, mime, "fifo") == -1)
279 } else if (file_printf(ms, "%sfifo (named pipe)", COMMA) == -1)
286 if (handle_mime(ms, mime, "door") == -1)
289 } else if (file_printf(ms, "%sdoor", COMMA) == -1)
295 if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
296 if (ms->flags & MAGIC_ERROR) {
297 file_error(ms, errno, "unreadable symlink `%s'",
302 if (handle_mime(ms, mime, "symlink") == -1)
305 } else if (file_printf(ms,
306 "%sunreadable symlink `%s' (%s)", COMMA, fn,
307 strerror(errno)) == -1)
311 buf[nch] = '\0'; /* readlink(2) does not do this */
313 /* If broken symlink, say so and quit early. */
316 * linux procfs/devfs makes symlinks like pipe:[3515864880]
317 * that we can't stat their readlink output, so stat the
318 * original filename instead.
320 if (stat(fn, &tstatbuf) < 0)
321 return bad_link(ms, errno, buf);
324 if (stat(buf, &tstatbuf) < 0)
325 return bad_link(ms, errno, buf);
328 char buf2[BUFSIZ+BUFSIZ+4];
330 if ((tmp = strrchr(fn, '/')) == NULL) {
331 tmp = buf; /* in current directory anyway */
333 if (tmp - fn + 1 > BUFSIZ) {
334 if (ms->flags & MAGIC_ERROR) {
336 "path too long: `%s'", buf);
340 if (handle_mime(ms, mime,
341 "x-path-too-long") == -1)
344 } else if (file_printf(ms,
345 "%spath too long: `%s'", COMMA,
351 (void)strlcpy(buf2, fn, sizeof buf2);
352 buf2[tmp - fn + 1] = '\0';
353 /* plus (rel) link */
354 (void)strlcat(buf2, buf, sizeof buf2);
357 if (stat(tmp, &tstatbuf) < 0)
358 return bad_link(ms, errno, buf);
362 /* Otherwise, handle it. */
363 if ((ms->flags & MAGIC_SYMLINK) != 0) {
365 ms->flags &= MAGIC_SYMLINK;
366 p = magic_file(ms, buf);
367 ms->flags |= MAGIC_SYMLINK;
370 } else { /* just print what it points to */
372 if (handle_mime(ms, mime, "symlink") == -1)
375 } else if (file_printf(ms, "%ssymbolic link to %s",
385 if (handle_mime(ms, mime, "socket") == -1)
388 } else if (file_printf(ms, "%ssocket", COMMA) == -1)
395 * regular file, check next possibility
397 * If stat() tells us the file has zero length, report here that
398 * the file is empty, so we can skip all the work of opening and
400 * But if the -s option has been given, we skip this
401 * optimization, since on some systems, stat() reports zero
402 * size for raw disk partitions. (If the block special device
403 * really has zero length, the fact that it is empty will be
404 * detected and reported correctly when we read the file.)
406 if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) {
408 if (handle_mime(ms, mime, "x-empty") == -1)
411 } else if (file_printf(ms, "%sempty", COMMA) == -1)
419 file_error(ms, 0, "invalid mode 0%o", sb->st_mode);
424 if (!silent && !mime && did && ret == 0) {
425 if (file_printf(ms, " ") == -1)
429 * If we were looking for extensions or apple (silent) it is not our
430 * job to print here, so don't count this as a match.
432 if (ret == 1 && silent)