xref: /netbsd-src/external/bsd/file/dist/src/fsmagic.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1 /*	$NetBSD: fsmagic.c,v 1.5 2011/09/16 21:06:26 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.64 2011/08/14 09:03:12 christos Exp $")
39 #else
40 __RCSID("$NetBSD: fsmagic.c,v 1.5 2011/09/16 21:06:26 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;
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 	/*
121 	 * Fstat is cheaper but fails for files you don't have read perms on.
122 	 * On 4.2BSD and similar systems, use lstat() to identify symlinks.
123 	 */
124 #ifdef	S_IFLNK
125 	if ((ms->flags & MAGIC_SYMLINK) == 0)
126 		ret = lstat(fn, sb);
127 	else
128 #endif
129 	ret = stat(fn, sb);	/* don't merge into if; see "ret =" above */
130 
131 	if (ret) {
132 		if (ms->flags & MAGIC_ERROR) {
133 			file_error(ms, errno, "cannot stat `%s'", fn);
134 			return -1;
135 		}
136 		if (file_printf(ms, "cannot open `%s' (%s)",
137 		    fn, strerror(errno)) == -1)
138 			return -1;
139 		ms->event_flags |= EVENT_HAD_ERR;
140 		return -1;
141 	}
142 
143 	if (!mime) {
144 #ifdef S_ISUID
145 		if (sb->st_mode & S_ISUID)
146 			if (file_printf(ms, "setuid ") == -1)
147 				return -1;
148 #endif
149 #ifdef S_ISGID
150 		if (sb->st_mode & S_ISGID)
151 			if (file_printf(ms, "setgid ") == -1)
152 				return -1;
153 #endif
154 #ifdef S_ISVTX
155 		if (sb->st_mode & S_ISVTX)
156 			if (file_printf(ms, "sticky ") == -1)
157 				return -1;
158 #endif
159 	}
160 
161 	switch (sb->st_mode & S_IFMT) {
162 	case S_IFDIR:
163 		if (mime) {
164 			if (handle_mime(ms, mime, "directory") == -1)
165 				return -1;
166 		} else if (file_printf(ms, "directory") == -1)
167 			return -1;
168 		return 1;
169 #ifdef S_IFCHR
170 	case S_IFCHR:
171 		/*
172 		 * If -s has been specified, treat character special files
173 		 * like ordinary files.  Otherwise, just report that they
174 		 * are block special files and go on to the next file.
175 		 */
176 		if ((ms->flags & MAGIC_DEVICES) != 0)
177 			break;
178 		if (mime) {
179 			if (handle_mime(ms, mime, "chardevice") == -1)
180 				return -1;
181 		} else {
182 #ifdef HAVE_STAT_ST_RDEV
183 # ifdef dv_unit
184 			if (file_printf(ms, "character special (%d/%d/%d)",
185 			    major(sb->st_rdev), dv_unit(sb->st_rdev),
186 					dv_subunit(sb->st_rdev)) == -1)
187 				return -1;
188 # else
189 			if (file_printf(ms, "character special (%ld/%ld)",
190 			    (long)major(sb->st_rdev), (long)minor(sb->st_rdev))
191 			    == -1)
192 				return -1;
193 # endif
194 #else
195 			if (file_printf(ms, "character special") == -1)
196 				return -1;
197 #endif
198 		}
199 		return 1;
200 #endif
201 #ifdef S_IFBLK
202 	case S_IFBLK:
203 		/*
204 		 * If -s has been specified, treat block special files
205 		 * like ordinary files.  Otherwise, just report that they
206 		 * are block special files and go on to the next file.
207 		 */
208 		if ((ms->flags & MAGIC_DEVICES) != 0)
209 			break;
210 		if (mime) {
211 			if (handle_mime(ms, mime, "blockdevice") == -1)
212 				return -1;
213 		} else {
214 #ifdef HAVE_STAT_ST_RDEV
215 # ifdef dv_unit
216 			if (file_printf(ms, "block special (%d/%d/%d)",
217 					major(sb->st_rdev), dv_unit(sb->st_rdev),
218 					dv_subunit(sb->st_rdev)) == -1)
219 				return -1;
220 # else
221 			if (file_printf(ms, "block special (%ld/%ld)",
222 					(long)major(sb->st_rdev), (long)minor(sb->st_rdev)) == -1)
223 				return -1;
224 # endif
225 #else
226 			if (file_printf(ms, "block special") == -1)
227 				return -1;
228 #endif
229 		}
230 		return 1;
231 #endif
232 	/* TODO add code to handle V7 MUX and Blit MUX files */
233 #ifdef	S_IFIFO
234 	case S_IFIFO:
235 		if((ms->flags & MAGIC_DEVICES) != 0)
236 			break;
237 		if (mime) {
238 			if (handle_mime(ms, mime, "fifo") == -1)
239 				return -1;
240 		} else if (file_printf(ms, "fifo (named pipe)") == -1)
241 			return -1;
242 		return 1;
243 #endif
244 #ifdef	S_IFDOOR
245 	case S_IFDOOR:
246 		if (mime) {
247 			if (handle_mime(ms, mime, "door") == -1)
248 				return -1;
249 		} else if (file_printf(ms, "door") == -1)
250 			return -1;
251 		return 1;
252 #endif
253 #ifdef	S_IFLNK
254 	case S_IFLNK:
255 		if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
256 			if (ms->flags & MAGIC_ERROR) {
257 			    file_error(ms, errno, "unreadable symlink `%s'",
258 				fn);
259 			    return -1;
260 			}
261 			if (mime) {
262 				if (handle_mime(ms, mime, "symlink") == -1)
263 					return -1;
264 			} else if (file_printf(ms,
265 			    "unreadable symlink `%s' (%s)", fn,
266 			    strerror(errno)) == -1)
267 				return -1;
268 			return 1;
269 		}
270 		buf[nch] = '\0';	/* readlink(2) does not do this */
271 
272 		/* If broken symlink, say so and quit early. */
273 		if (*buf == '/') {
274 			if (stat(buf, &tstatbuf) < 0)
275 				return bad_link(ms, errno, buf);
276 		} else {
277 			char *tmp;
278 			char buf2[BUFSIZ+BUFSIZ+4];
279 
280 			if ((tmp = strrchr(fn,  '/')) == NULL) {
281 				tmp = buf; /* in current directory anyway */
282 			} else {
283 				if (tmp - fn + 1 > BUFSIZ) {
284 					if (ms->flags & MAGIC_ERROR) {
285 						file_error(ms, 0,
286 						    "path too long: `%s'", buf);
287 						return -1;
288 					}
289 					if (mime) {
290 						if (handle_mime(ms, mime,
291 						    "x-path-too-long") == -1)
292 							return -1;
293 					} else if (file_printf(ms,
294 					    "path too long: `%s'", fn) == -1)
295 						return -1;
296 					return 1;
297 				}
298 				/* take dir part */
299 				(void)strlcpy(buf2, fn, sizeof buf2);
300 				buf2[tmp - fn + 1] = '\0';
301 				/* plus (rel) link */
302 				(void)strlcat(buf2, buf, sizeof buf2);
303 				tmp = buf2;
304 			}
305 			if (stat(tmp, &tstatbuf) < 0)
306 				return bad_link(ms, errno, buf);
307 		}
308 
309 		/* Otherwise, handle it. */
310 		if ((ms->flags & MAGIC_SYMLINK) != 0) {
311 			const char *p;
312 			ms->flags &= MAGIC_SYMLINK;
313 			p = magic_file(ms, buf);
314 			ms->flags |= MAGIC_SYMLINK;
315 			return p != NULL ? 1 : -1;
316 		} else { /* just print what it points to */
317 			if (mime) {
318 				if (handle_mime(ms, mime, "symlink") == -1)
319 					return -1;
320 			} else if (file_printf(ms, "symbolic link to `%s'",
321 			    buf) == -1)
322 				return -1;
323 		}
324 		return 1;
325 #endif
326 #ifdef	S_IFSOCK
327 #ifndef __COHERENT__
328 	case S_IFSOCK:
329 		if (mime) {
330 			if (handle_mime(ms, mime, "socket") == -1)
331 				return -1;
332 		} else if (file_printf(ms, "socket") == -1)
333 			return -1;
334 		return 1;
335 #endif
336 #endif
337 	case S_IFREG:
338 		break;
339 	default:
340 		file_error(ms, 0, "invalid mode 0%o", sb->st_mode);
341 		return -1;
342 		/*NOTREACHED*/
343 	}
344 
345 	/*
346 	 * regular file, check next possibility
347 	 *
348 	 * If stat() tells us the file has zero length, report here that
349 	 * the file is empty, so we can skip all the work of opening and
350 	 * reading the file.
351 	 * But if the -s option has been given, we skip this optimization,
352 	 * since on some systems, stat() reports zero size for raw disk
353 	 * partitions.  (If the block special device really has zero length,
354 	 * the fact that it is empty will be detected and reported correctly
355 	 * when we read the file.)
356 	 */
357 	if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) {
358 		if (mime) {
359 			if (handle_mime(ms, mime, "x-empty") == -1)
360 				return -1;
361 		} else if (file_printf(ms, "empty") == -1)
362 			return -1;
363 		return 1;
364 	}
365 	return 0;
366 }
367