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