1 /* $NetBSD: fsmagic.c,v 1.1.1.1 2009/05/08 16:35:06 christos Exp $ */
4 * Copyright (c) Ian F. Darwin 1986-1995.
5 * Software written by Ian F. Darwin and others;
6 * maintained 1995-present by Christos Zoulas and others.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice immediately at the beginning of the file, without modification,
13 * 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.
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 FOR
22 * 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
31 * fsmagic - magic based on filesystem info - directory, special files, etc.
38 FILE_RCSID("@(#)$File: fsmagic.c,v 1.59 2009/02/03 20:27:51 christos Exp $")
40 __RCSID("$NetBSD: fsmagic.c,v 1.1.1.1 2009/05/08 16:35:06 christos Exp $");
50 /* Since major is a function on SVR4, we cannot use `ifndef major'. */
52 # include <sys/mkdev.h>
55 #ifdef MAJOR_IN_SYSMACROS
56 # include <sys/sysmacros.h>
59 #ifdef major /* Might be defined in sys/types.h. */
64 # define major(dev) (((dev) >> 8) & 0xff)
65 # define minor(dev) ((dev) & 0xff)
70 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
, "application/x-symlink")
80 errfmt
= "symbolic link in a loop";
82 errfmt
= "broken symbolic link to `%s'";
83 if (ms
->flags
& MAGIC_ERROR
) {
84 file_error(ms
, err
, errfmt
, buf
);
87 if (file_printf(ms
, errfmt
, buf
) == -1)
94 handle_mime(struct magic_set
*ms
, int mime
, const char *str
)
96 if ((mime
& MAGIC_MIME_TYPE
)) {
97 if (file_printf(ms
, "application/%s", str
) == -1)
99 if ((mime
& MAGIC_MIME_ENCODING
) && file_printf(ms
,
103 if ((mime
& MAGIC_MIME_ENCODING
) && file_printf(ms
, "binary") == -1)
109 file_fsmagic(struct magic_set
*ms
, const char *fn
, struct stat
*sb
)
112 int mime
= ms
->flags
& MAGIC_MIME
;
116 struct stat tstatbuf
;
119 if (ms
->flags
& MAGIC_APPLE
)
125 * Fstat is cheaper but fails for files you don't have read perms on.
126 * On 4.2BSD and similar systems, use lstat() to identify symlinks.
129 if ((ms
->flags
& MAGIC_SYMLINK
) == 0)
133 ret
= stat(fn
, sb
); /* don't merge into if; see "ret =" above */
136 if (ms
->flags
& MAGIC_ERROR
) {
137 file_error(ms
, errno
, "cannot stat `%s'", fn
);
140 if (file_printf(ms
, "cannot open `%s' (%s)",
141 fn
, strerror(errno
)) == -1)
148 if (sb
->st_mode
& S_ISUID
)
149 if (file_printf(ms
, "setuid ") == -1)
153 if (sb
->st_mode
& S_ISGID
)
154 if (file_printf(ms
, "setgid ") == -1)
158 if (sb
->st_mode
& S_ISVTX
)
159 if (file_printf(ms
, "sticky ") == -1)
164 switch (sb
->st_mode
& S_IFMT
) {
167 if (handle_mime(ms
, mime
, "x-directory") == -1)
169 } else if (file_printf(ms
, "directory") == -1)
175 * If -s has been specified, treat character special files
176 * like ordinary files. Otherwise, just report that they
177 * are block special files and go on to the next file.
179 if ((ms
->flags
& MAGIC_DEVICES
) != 0)
182 if (handle_mime(ms
, mime
, "x-character-device") == -1)
185 #ifdef HAVE_STAT_ST_RDEV
187 if (file_printf(ms
, "character special (%d/%d/%d)",
188 major(sb
->st_rdev
), dv_unit(sb
->st_rdev
),
189 dv_subunit(sb
->st_rdev
)) == -1)
192 if (file_printf(ms
, "character special (%ld/%ld)",
193 (long)major(sb
->st_rdev
), (long)minor(sb
->st_rdev
))
198 if (file_printf(ms
, "character special") == -1)
207 * If -s has been specified, treat block special files
208 * like ordinary files. Otherwise, just report that they
209 * are block special files and go on to the next file.
211 if ((ms
->flags
& MAGIC_DEVICES
) != 0)
214 if (handle_mime(ms
, mime
, "x-block-device") == -1)
217 #ifdef HAVE_STAT_ST_RDEV
219 if (file_printf(ms
, "block special (%d/%d/%d)",
220 major(sb
->st_rdev
), dv_unit(sb
->st_rdev
),
221 dv_subunit(sb
->st_rdev
)) == -1)
224 if (file_printf(ms
, "block special (%ld/%ld)",
225 (long)major(sb
->st_rdev
), (long)minor(sb
->st_rdev
)) == -1)
229 if (file_printf(ms
, "block special") == -1)
235 /* TODO add code to handle V7 MUX and Blit MUX files */
238 if((ms
->flags
& MAGIC_DEVICES
) != 0)
241 if (handle_mime(ms
, mime
, "x-fifo") == -1)
243 } else if (file_printf(ms
, "fifo (named pipe)") == -1)
250 if (handle_mime(ms
, mime
, "x-door") == -1)
252 } else if (file_printf(ms
, "door") == -1)
258 if ((nch
= readlink(fn
, buf
, BUFSIZ
-1)) <= 0) {
259 if (ms
->flags
& MAGIC_ERROR
) {
260 file_error(ms
, errno
, "unreadable symlink `%s'",
265 if (handle_mime(ms
, mime
, "x-symlink") == -1)
267 } else if (file_printf(ms
,
268 "unreadable symlink `%s' (%s)", fn
,
269 strerror(errno
)) == -1)
273 buf
[nch
] = '\0'; /* readlink(2) does not do this */
275 /* If broken symlink, say so and quit early. */
277 if (stat(buf
, &tstatbuf
) < 0)
278 return bad_link(ms
, errno
, buf
);
281 char buf2
[BUFSIZ
+BUFSIZ
+4];
283 if ((tmp
= strrchr(fn
, '/')) == NULL
) {
284 tmp
= buf
; /* in current directory anyway */
286 if (tmp
- fn
+ 1 > BUFSIZ
) {
287 if (ms
->flags
& MAGIC_ERROR
) {
289 "path too long: `%s'", buf
);
293 if (handle_mime(ms
, mime
,
294 "x-path-too-long") == -1)
296 } else if (file_printf(ms
,
297 "path too long: `%s'", fn
) == -1)
302 (void)strlcpy(buf2
, fn
, sizeof buf2
);
303 buf2
[tmp
- fn
+ 1] = '\0';
304 /* plus (rel) link */
305 (void)strlcat(buf2
, buf
, sizeof buf2
);
308 if (stat(tmp
, &tstatbuf
) < 0)
309 return bad_link(ms
, errno
, buf
);
312 /* Otherwise, handle it. */
313 if ((ms
->flags
& MAGIC_SYMLINK
) != 0) {
315 ms
->flags
&= MAGIC_SYMLINK
;
316 p
= magic_file(ms
, buf
);
317 ms
->flags
|= MAGIC_SYMLINK
;
318 return p
!= NULL
? 1 : -1;
319 } else { /* just print what it points to */
321 if (handle_mime(ms
, mime
, "x-symlink") == -1)
323 } else if (file_printf(ms
, "symbolic link to `%s'",
333 if (handle_mime(ms
, mime
, "x-socket") == -1)
335 } else if (file_printf(ms
, "socket") == -1)
343 file_error(ms
, 0, "invalid mode 0%o", sb
->st_mode
);
349 * regular file, check next possibility
351 * If stat() tells us the file has zero length, report here that
352 * the file is empty, so we can skip all the work of opening and
354 * But if the -s option has been given, we skip this optimization,
355 * since on some systems, stat() reports zero size for raw disk
356 * partitions. (If the block special device really has zero length,
357 * the fact that it is empty will be detected and reported correctly
358 * when we read the file.)
360 if ((ms
->flags
& MAGIC_DEVICES
) == 0 && sb
->st_size
== 0) {
362 if (handle_mime(ms
, mime
, "x-empty") == -1)
364 } else if (file_printf(ms
, "empty") == -1)