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