xref: /netbsd-src/external/bsd/file/dist/src/fsmagic.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: fsmagic.c,v 1.2 2009/05/08 17:28:01 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.59 2009/02/03 20:27:51 christos Exp $")
39 #else
40 __RCSID("$NetBSD: fsmagic.c,v 1.2 2009/05/08 17:28:01 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 
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 
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 		return 1;
144 	}
145 
146 	if (!mime) {
147 #ifdef S_ISUID
148 		if (sb->st_mode & S_ISUID)
149 			if (file_printf(ms, "setuid ") == -1)
150 				return -1;
151 #endif
152 #ifdef S_ISGID
153 		if (sb->st_mode & S_ISGID)
154 			if (file_printf(ms, "setgid ") == -1)
155 				return -1;
156 #endif
157 #ifdef S_ISVTX
158 		if (sb->st_mode & S_ISVTX)
159 			if (file_printf(ms, "sticky ") == -1)
160 				return -1;
161 #endif
162 	}
163 
164 	switch (sb->st_mode & S_IFMT) {
165 	case S_IFDIR:
166 		if (mime) {
167 			if (handle_mime(ms, mime, "x-directory") == -1)
168 				return -1;
169 		} else if (file_printf(ms, "directory") == -1)
170 			return -1;
171 		return 1;
172 #ifdef S_IFCHR
173 	case S_IFCHR:
174 		/*
175 		 * If -s has been specified, treat character special files
176 		 * like ordinary files.  Otherwise, just report that they
177 		 * are block special files and go on to the next file.
178 		 */
179 		if ((ms->flags & MAGIC_DEVICES) != 0)
180 			break;
181 		if (mime) {
182 			if (handle_mime(ms, mime, "x-character-device") == -1)
183 				return -1;
184 		} else {
185 #ifdef HAVE_STAT_ST_RDEV
186 # ifdef dv_unit
187 			if (file_printf(ms, "character special (%d/%d/%d)",
188 			    major(sb->st_rdev), dv_unit(sb->st_rdev),
189 					dv_subunit(sb->st_rdev)) == -1)
190 				return -1;
191 # else
192 			if (file_printf(ms, "character special (%ld/%ld)",
193 			    (long)major(sb->st_rdev), (long)minor(sb->st_rdev))
194 			    == -1)
195 				return -1;
196 # endif
197 #else
198 			if (file_printf(ms, "character special") == -1)
199 				return -1;
200 #endif
201 		}
202 		return 1;
203 #endif
204 #ifdef S_IFBLK
205 	case S_IFBLK:
206 		/*
207 		 * If -s has been specified, treat block special files
208 		 * like ordinary files.  Otherwise, just report that they
209 		 * are block special files and go on to the next file.
210 		 */
211 		if ((ms->flags & MAGIC_DEVICES) != 0)
212 			break;
213 		if (mime) {
214 			if (handle_mime(ms, mime, "x-block-device") == -1)
215 				return -1;
216 		} else {
217 #ifdef HAVE_STAT_ST_RDEV
218 # ifdef dv_unit
219 			if (file_printf(ms, "block special (%d/%d/%d)",
220 					major(sb->st_rdev), dv_unit(sb->st_rdev),
221 					dv_subunit(sb->st_rdev)) == -1)
222 				return -1;
223 # else
224 			if (file_printf(ms, "block special (%ld/%ld)",
225 					(long)major(sb->st_rdev), (long)minor(sb->st_rdev)) == -1)
226 				return -1;
227 # endif
228 #else
229 			if (file_printf(ms, "block special") == -1)
230 				return -1;
231 #endif
232 		}
233 		return 1;
234 #endif
235 	/* TODO add code to handle V7 MUX and Blit MUX files */
236 #ifdef	S_IFIFO
237 	case S_IFIFO:
238 		if((ms->flags & MAGIC_DEVICES) != 0)
239 			break;
240 		if (mime) {
241 			if (handle_mime(ms, mime, "x-fifo") == -1)
242 				return -1;
243 		} else if (file_printf(ms, "fifo (named pipe)") == -1)
244 			return -1;
245 		return 1;
246 #endif
247 #ifdef	S_IFDOOR
248 	case S_IFDOOR:
249 		if (mime) {
250 			if (handle_mime(ms, mime, "x-door") == -1)
251 				return -1;
252 		} else if (file_printf(ms, "door") == -1)
253 			return -1;
254 		return 1;
255 #endif
256 #ifdef	S_IFLNK
257 	case S_IFLNK:
258 		if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
259 			if (ms->flags & MAGIC_ERROR) {
260 			    file_error(ms, errno, "unreadable symlink `%s'",
261 				fn);
262 			    return -1;
263 			}
264 			if (mime) {
265 				if (handle_mime(ms, mime, "x-symlink") == -1)
266 					return -1;
267 			} else if (file_printf(ms,
268 			    "unreadable symlink `%s' (%s)", fn,
269 			    strerror(errno)) == -1)
270 				return -1;
271 			return 1;
272 		}
273 		buf[nch] = '\0';	/* readlink(2) does not do this */
274 
275 		/* If broken symlink, say so and quit early. */
276 		if (*buf == '/') {
277 			if (stat(buf, &tstatbuf) < 0)
278 				return bad_link(ms, errno, buf);
279 		} else {
280 			char *tmp;
281 			char buf2[BUFSIZ+BUFSIZ+4];
282 
283 			if ((tmp = strrchr(fn,  '/')) == NULL) {
284 				tmp = buf; /* in current directory anyway */
285 			} else {
286 				if (tmp - fn + 1 > BUFSIZ) {
287 					if (ms->flags & MAGIC_ERROR) {
288 						file_error(ms, 0,
289 						    "path too long: `%s'", buf);
290 						return -1;
291 					}
292 					if (mime) {
293 						if (handle_mime(ms, mime,
294 						    "x-path-too-long") == -1)
295 							return -1;
296 					} else if (file_printf(ms,
297 					    "path too long: `%s'", 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, "x-symlink") == -1)
322 					return -1;
323 			} else if (file_printf(ms, "symbolic link to `%s'",
324 			    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, "x-socket") == -1)
334 				return -1;
335 		} else if (file_printf(ms, "socket") == -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, "empty") == -1)
365 			return -1;
366 		return 1;
367 	}
368 	return 0;
369 }
370