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