fsmagic.c revision 186690
1/*
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.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
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 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
26 * SUCH DAMAGE.
27 */
28/*
29 * fsmagic - magic based on filesystem info - directory, special files, etc.
30 */
31
32#include "file.h"
33#include "magic.h"
34#include <string.h>
35#ifdef HAVE_UNISTD_H
36#include <unistd.h>
37#endif
38#include <stdlib.h>
39#include <sys/stat.h>
40/* Since major is a function on SVR4, we cannot use `ifndef major'.  */
41#ifdef MAJOR_IN_MKDEV
42# include <sys/mkdev.h>
43# define HAVE_MAJOR
44#endif
45#ifdef MAJOR_IN_SYSMACROS
46# include <sys/sysmacros.h>
47# define HAVE_MAJOR
48#endif
49#ifdef major			/* Might be defined in sys/types.h.  */
50# define HAVE_MAJOR
51#endif
52
53#ifndef HAVE_MAJOR
54# define major(dev)  (((dev) >> 8) & 0xff)
55# define minor(dev)  ((dev) & 0xff)
56#endif
57#undef HAVE_MAJOR
58
59#ifndef	lint
60FILE_RCSID("@(#)$File: fsmagic.c,v 1.52 2008/07/25 23:59:01 rrt Exp $")
61#endif	/* lint */
62
63private int
64bad_link(struct magic_set *ms, int err, char *buf)
65{
66	const char *errfmt;
67	int mime = ms->flags & MAGIC_MIME;
68	if ((mime & MAGIC_MIME_TYPE) &&
69	    file_printf(ms, "application/x-symlink")
70	    == -1)
71		return -1;
72	else if (!mime) {
73		if (err == ELOOP)
74			errfmt = "symbolic link in a loop";
75		else
76			errfmt = "broken symbolic link to `%s'";
77		if (ms->flags & MAGIC_ERROR) {
78			file_error(ms, err, errfmt, buf);
79			return -1;
80		}
81		if (file_printf(ms, errfmt, buf) == -1)
82			return -1;
83	}
84	return 1;
85}
86
87protected int
88file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
89{
90	int ret = 0;
91	int mime = ms->flags & MAGIC_MIME;
92#ifdef	S_IFLNK
93	char buf[BUFSIZ+4];
94	int nch;
95	struct stat tstatbuf;
96#endif
97
98	if (fn == NULL)
99		return 0;
100
101	/*
102	 * Fstat is cheaper but fails for files you don't have read perms on.
103	 * On 4.2BSD and similar systems, use lstat() to identify symlinks.
104	 */
105#ifdef	S_IFLNK
106	if ((ms->flags & MAGIC_SYMLINK) == 0)
107		ret = lstat(fn, sb);
108	else
109#endif
110	ret = stat(fn, sb);	/* don't merge into if; see "ret =" above */
111
112	if (ret) {
113		if (ms->flags & MAGIC_ERROR) {
114			file_error(ms, errno, "cannot stat `%s'", fn);
115			return -1;
116		}
117		if (file_printf(ms, "cannot open `%s' (%s)",
118		    fn, strerror(errno)) == -1)
119			return -1;
120		return 1;
121	}
122
123	if (!mime) {
124#ifdef S_ISUID
125		if (sb->st_mode & S_ISUID)
126			if (file_printf(ms, "setuid ") == -1)
127				return -1;
128#endif
129#ifdef S_ISGID
130		if (sb->st_mode & S_ISGID)
131			if (file_printf(ms, "setgid ") == -1)
132				return -1;
133#endif
134#ifdef S_ISVTX
135		if (sb->st_mode & S_ISVTX)
136			if (file_printf(ms, "sticky ") == -1)
137				return -1;
138#endif
139	}
140
141	switch (sb->st_mode & S_IFMT) {
142	case S_IFDIR:
143		if ((mime & MAGIC_MIME_TYPE) &&
144		    file_printf(ms, "application/x-directory")
145		    == -1)
146			return -1;
147		if (!mime && file_printf(ms, "directory") == -1)
148			return -1;
149		return 1;
150#ifdef S_IFCHR
151	case S_IFCHR:
152		/*
153		 * If -s has been specified, treat character special files
154		 * like ordinary files.  Otherwise, just report that they
155		 * are block special files and go on to the next file.
156		 */
157		if ((ms->flags & MAGIC_DEVICES) != 0)
158			break;
159		if ((mime & MAGIC_MIME_TYPE) &&
160		    file_printf(ms, "application/x-character-device")
161		    == -1)
162			return -1;
163		if (!mime) {
164#ifdef HAVE_STAT_ST_RDEV
165# ifdef dv_unit
166			if (file_printf(ms, "character special (%d/%d/%d)",
167					major(sb->st_rdev), dv_unit(sb->st_rdev),
168					dv_subunit(sb->st_rdev)) == -1)
169				return -1;
170# else
171			if (file_printf(ms, "character special (%ld/%ld)",
172					(long) major(sb->st_rdev), (long) minor(sb->st_rdev)) == -1)
173				return -1;
174# endif
175#else
176			if (file_printf(ms, "character special") == -1)
177				return -1;
178#endif
179		}
180		return 1;
181#endif
182#ifdef S_IFBLK
183	case S_IFBLK:
184		/*
185		 * If -s has been specified, treat block special files
186		 * like ordinary files.  Otherwise, just report that they
187		 * are block special files and go on to the next file.
188		 */
189		if ((ms->flags & MAGIC_DEVICES) != 0)
190			break;
191		if ((mime & MAGIC_MIME_TYPE) &&
192		    file_printf(ms, "application/x-block-device")
193		    == -1)
194			return -1;
195		if (!mime) {
196#ifdef HAVE_STAT_ST_RDEV
197# ifdef dv_unit
198			if (file_printf(ms, "block special (%d/%d/%d)",
199					major(sb->st_rdev), dv_unit(sb->st_rdev),
200					dv_subunit(sb->st_rdev)) == -1)
201				return -1;
202# else
203			if (file_printf(ms, "block special (%ld/%ld)",
204					(long)major(sb->st_rdev), (long)minor(sb->st_rdev)) == -1)
205				return -1;
206# endif
207#else
208			if (file_printf(ms, "block special") == -1)
209				return -1;
210#endif
211		}
212		return 1;
213#endif
214	/* TODO add code to handle V7 MUX and Blit MUX files */
215#ifdef	S_IFIFO
216	case S_IFIFO:
217		if((ms->flags & MAGIC_DEVICES) != 0)
218			break;
219		if ((mime & MAGIC_MIME_TYPE) &&
220		    file_printf(ms, "application/x-fifo")
221		    == -1)
222			return -1;
223		if (!mime && file_printf(ms, "fifo (named pipe)") == -1)
224			return -1;
225		return 1;
226#endif
227#ifdef	S_IFDOOR
228	case S_IFDOOR:
229		if ((mime & MAGIC_MIME_TYPE) &&
230		    file_printf(ms, "application/x-door")
231		    == -1)
232			return -1;
233		if (!mime && file_printf(ms, "door") == -1)
234			return -1;
235		return 1;
236#endif
237#ifdef	S_IFLNK
238	case S_IFLNK:
239		if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
240			if (ms->flags & MAGIC_ERROR) {
241			    file_error(ms, errno, "unreadable symlink `%s'",
242				fn);
243			    return -1;
244			}
245			if ((mime & MAGIC_MIME_TYPE) &&
246			    file_printf(ms, "application/x-symlink")
247			    == -1)
248				return -1;
249			if (!mime && file_printf(ms,
250			    "unreadable symlink `%s' (%s)", fn,
251			    strerror(errno)) == -1)
252				return -1;
253			return 1;
254		}
255		buf[nch] = '\0';	/* readlink(2) does not do this */
256
257		/* If broken symlink, say so and quit early. */
258		if (*buf == '/') {
259			if (stat(buf, &tstatbuf) < 0)
260				return bad_link(ms, errno, buf);
261		} else {
262			char *tmp;
263			char buf2[BUFSIZ+BUFSIZ+4];
264
265			if ((tmp = strrchr(fn,  '/')) == NULL) {
266				tmp = buf; /* in current directory anyway */
267			} else {
268				if (tmp - fn + 1 > BUFSIZ) {
269					if (ms->flags & MAGIC_ERROR) {
270						file_error(ms, 0,
271						    "path too long: `%s'", buf);
272						return -1;
273					}
274					if ((mime & MAGIC_MIME_TYPE) &&
275					    file_printf(ms, "application/x-path-too-long")
276					    == -1)
277						return -1;
278					if (!mime && file_printf(ms,
279					    "path too long: `%s'", fn) == -1)
280						return -1;
281					return 1;
282				}
283				(void)strcpy(buf2, fn);  /* take dir part */
284				buf2[tmp - fn + 1] = '\0';
285				(void)strcat(buf2, buf); /* plus (rel) link */
286				tmp = buf2;
287			}
288			if (stat(tmp, &tstatbuf) < 0)
289				return bad_link(ms, errno, buf);
290		}
291
292		/* Otherwise, handle it. */
293		if ((ms->flags & MAGIC_SYMLINK) != 0) {
294			const char *p;
295			ms->flags &= MAGIC_SYMLINK;
296			p = magic_file(ms, buf);
297			ms->flags |= MAGIC_SYMLINK;
298			return p != NULL ? 1 : -1;
299		} else { /* just print what it points to */
300			if ((mime & MAGIC_MIME_TYPE) &&
301			    file_printf(ms, "application/x-symlink")
302			    == -1)
303				return -1;
304			if (!mime && file_printf(ms, "symbolic link to `%s'",
305			    buf) == -1)
306				return -1;
307		}
308		return 1;
309#endif
310#ifdef	S_IFSOCK
311#ifndef __COHERENT__
312	case S_IFSOCK:
313		if ((mime & MAGIC_MIME_TYPE) &&
314		    file_printf(ms, "application/x-socket")
315		    == -1)
316			return -1;
317		if (!mime && file_printf(ms, "socket") == -1)
318			return -1;
319		return 1;
320#endif
321#endif
322	case S_IFREG:
323		break;
324	default:
325		file_error(ms, 0, "invalid mode 0%o", sb->st_mode);
326		return -1;
327		/*NOTREACHED*/
328	}
329
330	/*
331	 * regular file, check next possibility
332	 *
333	 * If stat() tells us the file has zero length, report here that
334	 * the file is empty, so we can skip all the work of opening and
335	 * reading the file.
336	 * But if the -s option has been given, we skip this optimization,
337	 * since on some systems, stat() reports zero size for raw disk
338	 * partitions.  (If the block special device really has zero length,
339	 * the fact that it is empty will be detected and reported correctly
340	 * when we read the file.)
341	 */
342	if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) {
343		if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
344		    file_printf(ms, mime ? "application/x-empty" :
345		    "empty") == -1)
346			return -1;
347		return 1;
348	}
349	return 0;
350}
351