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