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