fsmagic.c revision 191736
168349Sobrien/*
2133359Sobrien * Copyright (c) Ian F. Darwin 1986-1995.
3133359Sobrien * Software written by Ian F. Darwin and others;
4133359Sobrien * maintained 1995-present by Christos Zoulas and others.
5133359Sobrien *
6133359Sobrien * Redistribution and use in source and binary forms, with or without
7133359Sobrien * modification, are permitted provided that the following conditions
8133359Sobrien * are met:
9133359Sobrien * 1. Redistributions of source code must retain the above copyright
10133359Sobrien *    notice immediately at the beginning of the file, without modification,
11133359Sobrien *    this list of conditions, and the following disclaimer.
12133359Sobrien * 2. Redistributions in binary form must reproduce the above copyright
13133359Sobrien *    notice, this list of conditions and the following disclaimer in the
14133359Sobrien *    documentation and/or other materials provided with the distribution.
15133359Sobrien *
16133359Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17133359Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18133359Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19133359Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20133359Sobrien * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21133359Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22133359Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23133359Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24133359Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25133359Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26133359Sobrien * SUCH DAMAGE.
27133359Sobrien */
28133359Sobrien/*
2968349Sobrien * fsmagic - magic based on filesystem info - directory, special files, etc.
3068349Sobrien */
3168349Sobrien
3268349Sobrien#include "file.h"
33191736Sobrien
34191736Sobrien#ifndef	lint
35191736SobrienFILE_RCSID("@(#)$File: fsmagic.c,v 1.59 2009/02/03 20:27:51 christos Exp $")
36191736Sobrien#endif	/* lint */
37191736Sobrien
38133359Sobrien#include "magic.h"
3968349Sobrien#include <string.h>
4068349Sobrien#ifdef HAVE_UNISTD_H
4168349Sobrien#include <unistd.h>
4268349Sobrien#endif
4368349Sobrien#include <stdlib.h>
44133359Sobrien/* Since major is a function on SVR4, we cannot use `ifndef major'.  */
4568349Sobrien#ifdef MAJOR_IN_MKDEV
4668349Sobrien# include <sys/mkdev.h>
4768349Sobrien# define HAVE_MAJOR
4868349Sobrien#endif
4968349Sobrien#ifdef MAJOR_IN_SYSMACROS
5068349Sobrien# include <sys/sysmacros.h>
5168349Sobrien# define HAVE_MAJOR
5268349Sobrien#endif
5368349Sobrien#ifdef major			/* Might be defined in sys/types.h.  */
5468349Sobrien# define HAVE_MAJOR
5568349Sobrien#endif
5668349Sobrien
5768349Sobrien#ifndef HAVE_MAJOR
5868349Sobrien# define major(dev)  (((dev) >> 8) & 0xff)
5968349Sobrien# define minor(dev)  ((dev) & 0xff)
6068349Sobrien#endif
6168349Sobrien#undef HAVE_MAJOR
6268349Sobrien
63186690Sobrienprivate int
64186690Sobrienbad_link(struct magic_set *ms, int err, char *buf)
65186690Sobrien{
66186690Sobrien	const char *errfmt;
67186690Sobrien	int mime = ms->flags & MAGIC_MIME;
68186690Sobrien	if ((mime & MAGIC_MIME_TYPE) &&
69186690Sobrien	    file_printf(ms, "application/x-symlink")
70186690Sobrien	    == -1)
71186690Sobrien		return -1;
72186690Sobrien	else if (!mime) {
73186690Sobrien		if (err == ELOOP)
74186690Sobrien			errfmt = "symbolic link in a loop";
75186690Sobrien		else
76186690Sobrien			errfmt = "broken symbolic link to `%s'";
77186690Sobrien		if (ms->flags & MAGIC_ERROR) {
78186690Sobrien			file_error(ms, err, errfmt, buf);
79186690Sobrien			return -1;
80186690Sobrien		}
81186690Sobrien		if (file_printf(ms, errfmt, buf) == -1)
82186690Sobrien			return -1;
83186690Sobrien	}
84186690Sobrien	return 1;
85186690Sobrien}
86186690Sobrien
87191736Sobrienprivate int
88191736Sobrienhandle_mime(struct magic_set *ms, int mime, const char *str)
89191736Sobrien{
90191736Sobrien	if ((mime & MAGIC_MIME_TYPE)) {
91191736Sobrien		if (file_printf(ms, "application/%s", str) == -1)
92191736Sobrien			return -1;
93191736Sobrien		if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms,
94191736Sobrien		    "; charset=") == -1)
95191736Sobrien			return -1;
96191736Sobrien	}
97191736Sobrien	if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms, "binary") == -1)
98191736Sobrien		return -1;
99191736Sobrien	return 0;
100191736Sobrien}
101191736Sobrien
102133359Sobrienprotected int
103133359Sobrienfile_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
10468349Sobrien{
10568349Sobrien	int ret = 0;
106175296Sobrien	int mime = ms->flags & MAGIC_MIME;
107133359Sobrien#ifdef	S_IFLNK
108133359Sobrien	char buf[BUFSIZ+4];
109133359Sobrien	int nch;
110133359Sobrien	struct stat tstatbuf;
111133359Sobrien#endif
11268349Sobrien
113191736Sobrien	if (ms->flags & MAGIC_APPLE)
114191736Sobrien		return 0;
115133359Sobrien	if (fn == NULL)
116133359Sobrien		return 0;
117133359Sobrien
11868349Sobrien	/*
11968349Sobrien	 * Fstat is cheaper but fails for files you don't have read perms on.
12068349Sobrien	 * On 4.2BSD and similar systems, use lstat() to identify symlinks.
12168349Sobrien	 */
12268349Sobrien#ifdef	S_IFLNK
123133359Sobrien	if ((ms->flags & MAGIC_SYMLINK) == 0)
12468349Sobrien		ret = lstat(fn, sb);
12568349Sobrien	else
12668349Sobrien#endif
12768349Sobrien	ret = stat(fn, sb);	/* don't merge into if; see "ret =" above */
12868349Sobrien
12968349Sobrien	if (ret) {
130133359Sobrien		if (ms->flags & MAGIC_ERROR) {
131133359Sobrien			file_error(ms, errno, "cannot stat `%s'", fn);
132133359Sobrien			return -1;
133133359Sobrien		}
134139368Sobrien		if (file_printf(ms, "cannot open `%s' (%s)",
135133359Sobrien		    fn, strerror(errno)) == -1)
136133359Sobrien			return -1;
13768349Sobrien		return 1;
13868349Sobrien	}
13968349Sobrien
140186690Sobrien	if (!mime) {
14168349Sobrien#ifdef S_ISUID
142133359Sobrien		if (sb->st_mode & S_ISUID)
143133359Sobrien			if (file_printf(ms, "setuid ") == -1)
144133359Sobrien				return -1;
14568349Sobrien#endif
14668349Sobrien#ifdef S_ISGID
147133359Sobrien		if (sb->st_mode & S_ISGID)
148133359Sobrien			if (file_printf(ms, "setgid ") == -1)
149133359Sobrien				return -1;
15068349Sobrien#endif
15168349Sobrien#ifdef S_ISVTX
152133359Sobrien		if (sb->st_mode & S_ISVTX)
153133359Sobrien			if (file_printf(ms, "sticky ") == -1)
154133359Sobrien				return -1;
15568349Sobrien#endif
15668349Sobrien	}
15768349Sobrien
15868349Sobrien	switch (sb->st_mode & S_IFMT) {
15968349Sobrien	case S_IFDIR:
160191736Sobrien		if (mime) {
161191736Sobrien			if (handle_mime(ms, mime, "x-directory") == -1)
162191736Sobrien				return -1;
163191736Sobrien		} else if (file_printf(ms, "directory") == -1)
164133359Sobrien			return -1;
16568349Sobrien		return 1;
16668349Sobrien#ifdef S_IFCHR
16768349Sobrien	case S_IFCHR:
16868349Sobrien		/*
16968349Sobrien		 * If -s has been specified, treat character special files
17068349Sobrien		 * like ordinary files.  Otherwise, just report that they
17168349Sobrien		 * are block special files and go on to the next file.
17268349Sobrien		 */
173133359Sobrien		if ((ms->flags & MAGIC_DEVICES) != 0)
17468349Sobrien			break;
175191736Sobrien		if (mime) {
176191736Sobrien			if (handle_mime(ms, mime, "x-character-device") == -1)
177191736Sobrien				return -1;
178191736Sobrien		} else {
179186690Sobrien#ifdef HAVE_STAT_ST_RDEV
18068349Sobrien# ifdef dv_unit
181186690Sobrien			if (file_printf(ms, "character special (%d/%d/%d)",
182191736Sobrien			    major(sb->st_rdev), dv_unit(sb->st_rdev),
183186690Sobrien					dv_subunit(sb->st_rdev)) == -1)
184186690Sobrien				return -1;
18568349Sobrien# else
186186690Sobrien			if (file_printf(ms, "character special (%ld/%ld)",
187191736Sobrien			    (long)major(sb->st_rdev), (long)minor(sb->st_rdev))
188191736Sobrien			    == -1)
189186690Sobrien				return -1;
19068349Sobrien# endif
19168349Sobrien#else
192186690Sobrien			if (file_printf(ms, "character special") == -1)
193186690Sobrien				return -1;
19468349Sobrien#endif
195186690Sobrien		}
19668349Sobrien		return 1;
19768349Sobrien#endif
19868349Sobrien#ifdef S_IFBLK
19968349Sobrien	case S_IFBLK:
20068349Sobrien		/*
20168349Sobrien		 * If -s has been specified, treat block special files
20268349Sobrien		 * like ordinary files.  Otherwise, just report that they
20368349Sobrien		 * are block special files and go on to the next file.
20468349Sobrien		 */
205133359Sobrien		if ((ms->flags & MAGIC_DEVICES) != 0)
20668349Sobrien			break;
207191736Sobrien		if (mime) {
208191736Sobrien			if (handle_mime(ms, mime, "x-block-device") == -1)
209191736Sobrien				return -1;
210191736Sobrien		} else {
211186690Sobrien#ifdef HAVE_STAT_ST_RDEV
21268349Sobrien# ifdef dv_unit
213186690Sobrien			if (file_printf(ms, "block special (%d/%d/%d)",
214186690Sobrien					major(sb->st_rdev), dv_unit(sb->st_rdev),
215186690Sobrien					dv_subunit(sb->st_rdev)) == -1)
216186690Sobrien				return -1;
21768349Sobrien# else
218186690Sobrien			if (file_printf(ms, "block special (%ld/%ld)",
219186690Sobrien					(long)major(sb->st_rdev), (long)minor(sb->st_rdev)) == -1)
220186690Sobrien				return -1;
22168349Sobrien# endif
22268349Sobrien#else
223186690Sobrien			if (file_printf(ms, "block special") == -1)
224186690Sobrien				return -1;
22568349Sobrien#endif
226186690Sobrien		}
22768349Sobrien		return 1;
22868349Sobrien#endif
22968349Sobrien	/* TODO add code to handle V7 MUX and Blit MUX files */
23068349Sobrien#ifdef	S_IFIFO
23168349Sobrien	case S_IFIFO:
232159764Sobrien		if((ms->flags & MAGIC_DEVICES) != 0)
233159764Sobrien			break;
234191736Sobrien		if (mime) {
235191736Sobrien			if (handle_mime(ms, mime, "x-fifo") == -1)
236191736Sobrien				return -1;
237191736Sobrien		} else if (file_printf(ms, "fifo (named pipe)") == -1)
238133359Sobrien			return -1;
23968349Sobrien		return 1;
24068349Sobrien#endif
24168349Sobrien#ifdef	S_IFDOOR
24268349Sobrien	case S_IFDOOR:
243191736Sobrien		if (mime) {
244191736Sobrien			if (handle_mime(ms, mime, "x-door") == -1)
245191736Sobrien				return -1;
246191736Sobrien		} else if (file_printf(ms, "door") == -1)
247133359Sobrien			return -1;
24868349Sobrien		return 1;
24968349Sobrien#endif
25068349Sobrien#ifdef	S_IFLNK
25168349Sobrien	case S_IFLNK:
252133359Sobrien		if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
253133359Sobrien			if (ms->flags & MAGIC_ERROR) {
254133359Sobrien			    file_error(ms, errno, "unreadable symlink `%s'",
255133359Sobrien				fn);
256133359Sobrien			    return -1;
25768349Sobrien			}
258191736Sobrien			if (mime) {
259191736Sobrien				if (handle_mime(ms, mime, "x-symlink") == -1)
260191736Sobrien					return -1;
261191736Sobrien			} else if (file_printf(ms,
262133359Sobrien			    "unreadable symlink `%s' (%s)", fn,
263133359Sobrien			    strerror(errno)) == -1)
264133359Sobrien				return -1;
265133359Sobrien			return 1;
266133359Sobrien		}
267186690Sobrien		buf[nch] = '\0';	/* readlink(2) does not do this */
26868349Sobrien
269133359Sobrien		/* If broken symlink, say so and quit early. */
270133359Sobrien		if (*buf == '/') {
271186690Sobrien			if (stat(buf, &tstatbuf) < 0)
272186690Sobrien				return bad_link(ms, errno, buf);
273186690Sobrien		} else {
274133359Sobrien			char *tmp;
275133359Sobrien			char buf2[BUFSIZ+BUFSIZ+4];
27668349Sobrien
277133359Sobrien			if ((tmp = strrchr(fn,  '/')) == NULL) {
27868349Sobrien				tmp = buf; /* in current directory anyway */
279133359Sobrien			} else {
280133359Sobrien				if (tmp - fn + 1 > BUFSIZ) {
281133359Sobrien					if (ms->flags & MAGIC_ERROR) {
282133359Sobrien						file_error(ms, 0,
283133359Sobrien						    "path too long: `%s'", buf);
284133359Sobrien						return -1;
285133359Sobrien					}
286191736Sobrien					if (mime) {
287191736Sobrien						if (handle_mime(ms, mime,
288191736Sobrien						    "x-path-too-long") == -1)
289191736Sobrien							return -1;
290191736Sobrien					} else if (file_printf(ms,
291133359Sobrien					    "path too long: `%s'", fn) == -1)
292133359Sobrien						return -1;
293133359Sobrien					return 1;
294133359Sobrien				}
295191736Sobrien				/* take dir part */
296191736Sobrien				(void)strlcpy(buf2, fn, sizeof buf2);
297133359Sobrien				buf2[tmp - fn + 1] = '\0';
298191736Sobrien				/* plus (rel) link */
299191736Sobrien				(void)strlcat(buf2, buf, sizeof buf2);
30068349Sobrien				tmp = buf2;
301133359Sobrien			}
302186690Sobrien			if (stat(tmp, &tstatbuf) < 0)
303186690Sobrien				return bad_link(ms, errno, buf);
30468349Sobrien		}
305133359Sobrien
306133359Sobrien		/* Otherwise, handle it. */
307133359Sobrien		if ((ms->flags & MAGIC_SYMLINK) != 0) {
308133359Sobrien			const char *p;
309133359Sobrien			ms->flags &= MAGIC_SYMLINK;
310133359Sobrien			p = magic_file(ms, buf);
311133359Sobrien			ms->flags |= MAGIC_SYMLINK;
312133359Sobrien			return p != NULL ? 1 : -1;
313133359Sobrien		} else { /* just print what it points to */
314191736Sobrien			if (mime) {
315191736Sobrien				if (handle_mime(ms, mime, "x-symlink") == -1)
316191736Sobrien					return -1;
317191736Sobrien			} else if (file_printf(ms, "symbolic link to `%s'",
318133359Sobrien			    buf) == -1)
319133359Sobrien				return -1;
320133359Sobrien		}
321186690Sobrien		return 1;
32268349Sobrien#endif
32368349Sobrien#ifdef	S_IFSOCK
32468349Sobrien#ifndef __COHERENT__
32568349Sobrien	case S_IFSOCK:
326191736Sobrien		if (mime) {
327191736Sobrien			if (handle_mime(ms, mime, "x-socket") == -1)
328191736Sobrien				return -1;
329191736Sobrien		} else if (file_printf(ms, "socket") == -1)
330133359Sobrien			return -1;
33168349Sobrien		return 1;
33268349Sobrien#endif
33368349Sobrien#endif
33468349Sobrien	case S_IFREG:
33568349Sobrien		break;
33668349Sobrien	default:
337133359Sobrien		file_error(ms, 0, "invalid mode 0%o", sb->st_mode);
338133359Sobrien		return -1;
33968349Sobrien		/*NOTREACHED*/
34068349Sobrien	}
34168349Sobrien
34268349Sobrien	/*
34368349Sobrien	 * regular file, check next possibility
34468349Sobrien	 *
34568349Sobrien	 * If stat() tells us the file has zero length, report here that
34668349Sobrien	 * the file is empty, so we can skip all the work of opening and
34768349Sobrien	 * reading the file.
34868349Sobrien	 * But if the -s option has been given, we skip this optimization,
34968349Sobrien	 * since on some systems, stat() reports zero size for raw disk
35068349Sobrien	 * partitions.  (If the block special device really has zero length,
35168349Sobrien	 * the fact that it is empty will be detected and reported correctly
35268349Sobrien	 * when we read the file.)
35368349Sobrien	 */
354133359Sobrien	if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) {
355191736Sobrien		if (mime) {
356191736Sobrien			if (handle_mime(ms, mime, "x-empty") == -1)
357191736Sobrien				return -1;
358191736Sobrien		} else if (file_printf(ms, "empty") == -1)
359133359Sobrien			return -1;
36068349Sobrien		return 1;
36168349Sobrien	}
36268349Sobrien	return 0;
36368349Sobrien}
364