xref: /netbsd-src/external/bsd/file/dist/src/fsmagic.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: fsmagic.c,v 1.9 2014/06/13 02:08:06 christos Exp $	*/
2 /*
3  * Copyright (c) Ian F. Darwin 1986-1995.
4  * Software written by Ian F. Darwin and others;
5  * maintained 1995-present by Christos Zoulas and others.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice immediately at the beginning of the file, without modification,
12  *    this list of conditions, and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 /*
30  * fsmagic - magic based on filesystem info - directory, special files, etc.
31  */
32 
33 #include "file.h"
34 
35 #ifndef	lint
36 #if 0
37 FILE_RCSID("@(#)$File: fsmagic.c,v 1.73 2014/05/14 23:15:42 christos Exp $")
38 #else
39 __RCSID("$NetBSD: fsmagic.c,v 1.9 2014/06/13 02:08:06 christos Exp $");
40 #endif
41 #endif	/* lint */
42 
43 #include "magic.h"
44 #include <string.h>
45 #ifdef HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
48 #include <stdlib.h>
49 /* Since major is a function on SVR4, we cannot use `ifndef major'.  */
50 #ifdef MAJOR_IN_MKDEV
51 # include <sys/mkdev.h>
52 # define HAVE_MAJOR
53 #endif
54 #ifdef MAJOR_IN_SYSMACROS
55 # include <sys/sysmacros.h>
56 # define HAVE_MAJOR
57 #endif
58 #ifdef major			/* Might be defined in sys/types.h.  */
59 # define HAVE_MAJOR
60 #endif
61 #ifdef WIN32
62 # define WIN32_LEAN_AND_MEAN
63 # include <windows.h>
64 #endif
65 
66 #ifndef HAVE_MAJOR
67 # define major(dev)  (((dev) >> 8) & 0xff)
68 # define minor(dev)  ((dev) & 0xff)
69 #endif
70 #undef HAVE_MAJOR
71 #ifdef	S_IFLNK
72 private int
73 bad_link(struct magic_set *ms, int err, char *buf)
74 {
75 	int mime = ms->flags & MAGIC_MIME;
76 	if ((mime & MAGIC_MIME_TYPE) &&
77 	    file_printf(ms, "inode/symlink")
78 	    == -1)
79 		return -1;
80 	else if (!mime) {
81 		if (ms->flags & MAGIC_ERROR) {
82 			file_error(ms, err,
83 				   "broken symbolic link to `%s'", buf);
84 			return -1;
85 		}
86 		if (file_printf(ms, "broken symbolic link to `%s'", buf) == -1)
87 			return -1;
88 	}
89 	return 1;
90 }
91 #endif
92 private int
93 handle_mime(struct magic_set *ms, int mime, const char *str)
94 {
95 	if ((mime & MAGIC_MIME_TYPE)) {
96 		if (file_printf(ms, "inode/%s", str) == -1)
97 			return -1;
98 		if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms,
99 		    "; charset=") == -1)
100 			return -1;
101 	}
102 	if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms, "binary") == -1)
103 		return -1;
104 	return 0;
105 }
106 
107 protected int
108 file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
109 {
110 	int ret, did = 0;
111 	int mime = ms->flags & MAGIC_MIME;
112 #ifdef	S_IFLNK
113 	char buf[BUFSIZ+4];
114 	ssize_t nch;
115 	struct stat tstatbuf;
116 #endif
117 
118 	if (ms->flags & MAGIC_APPLE)
119 		return 0;
120 	if (fn == NULL)
121 		return 0;
122 
123 #define COMMA	(did++ ? ", " : "")
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 #ifdef WIN32
136 	{
137 		HANDLE hFile = CreateFile(fn, 0, FILE_SHARE_DELETE |
138 		    FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,
139 		    NULL);
140 		if (hFile != INVALID_HANDLE_VALUE) {
141 			/*
142 			 * Stat failed, but we can still open it - assume it's
143 			 * a block device, if nothing else.
144 			 */
145 			if (ret) {
146 				sb->st_mode = S_IFBLK;
147 				ret = 0;
148 			}
149 			switch (GetFileType(hFile)) {
150 			case FILE_TYPE_CHAR:
151 				sb->st_mode |= S_IFCHR;
152 				sb->st_mode &= ~S_IFREG;
153 				break;
154 			case FILE_TYPE_PIPE:
155 				sb->st_mode |= S_IFIFO;
156 				sb->st_mode &= ~S_IFREG;
157 				break;
158 			}
159 			CloseHandle(hFile);
160 		}
161 	}
162 #endif
163 
164 	if (ret) {
165 		if (ms->flags & MAGIC_ERROR) {
166 			file_error(ms, errno, "cannot stat `%s'", fn);
167 			return -1;
168 		}
169 		if (file_printf(ms, "cannot open `%s' (%s)",
170 		    fn, strerror(errno)) == -1)
171 			return -1;
172 		return 0;
173 	}
174 
175 	ret = 1;
176 	if (!mime) {
177 #ifdef S_ISUID
178 		if (sb->st_mode & S_ISUID)
179 			if (file_printf(ms, "%ssetuid", COMMA) == -1)
180 				return -1;
181 #endif
182 #ifdef S_ISGID
183 		if (sb->st_mode & S_ISGID)
184 			if (file_printf(ms, "%ssetgid", COMMA) == -1)
185 				return -1;
186 #endif
187 #ifdef S_ISVTX
188 		if (sb->st_mode & S_ISVTX)
189 			if (file_printf(ms, "%ssticky", COMMA) == -1)
190 				return -1;
191 #endif
192 	}
193 
194 	switch (sb->st_mode & S_IFMT) {
195 	case S_IFDIR:
196 		if (mime) {
197 			if (handle_mime(ms, mime, "directory") == -1)
198 				return -1;
199 		} else if (file_printf(ms, "%sdirectory", COMMA) == -1)
200 			return -1;
201 		break;
202 #ifdef S_IFCHR
203 	case S_IFCHR:
204 		/*
205 		 * If -s has been specified, treat character special files
206 		 * like ordinary files.  Otherwise, just report that they
207 		 * are block special files and go on to the next file.
208 		 */
209 		if ((ms->flags & MAGIC_DEVICES) != 0) {
210 			ret = 0;
211 			break;
212 		}
213 		if (mime) {
214 			if (handle_mime(ms, mime, "chardevice") == -1)
215 				return -1;
216 		} else {
217 #ifdef HAVE_STRUCT_STAT_ST_RDEV
218 # ifdef dv_unit
219 			if (file_printf(ms, "%scharacter special (%d/%d/%d)",
220 			    COMMA, 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, "%scharacter special (%ld/%ld)",
225 			    COMMA, (long)major(sb->st_rdev),
226 			    (long)minor(sb->st_rdev)) == -1)
227 				return -1;
228 # endif
229 #else
230 			if (file_printf(ms, "%scharacter special", COMMA) == -1)
231 				return -1;
232 #endif
233 		}
234 		break;
235 #endif
236 #ifdef S_IFBLK
237 	case S_IFBLK:
238 		/*
239 		 * If -s has been specified, treat block special files
240 		 * like ordinary files.  Otherwise, just report that they
241 		 * are block special files and go on to the next file.
242 		 */
243 		if ((ms->flags & MAGIC_DEVICES) != 0) {
244 			ret = 0;
245 			break;
246 		}
247 		if (mime) {
248 			if (handle_mime(ms, mime, "blockdevice") == -1)
249 				return -1;
250 		} else {
251 #ifdef HAVE_STRUCT_STAT_ST_RDEV
252 # ifdef dv_unit
253 			if (file_printf(ms, "%sblock special (%d/%d/%d)",
254 			    COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
255 			    dv_subunit(sb->st_rdev)) == -1)
256 				return -1;
257 # else
258 			if (file_printf(ms, "%sblock special (%ld/%ld)",
259 			    COMMA, (long)major(sb->st_rdev),
260 			    (long)minor(sb->st_rdev)) == -1)
261 				return -1;
262 # endif
263 #else
264 			if (file_printf(ms, "%sblock special", COMMA) == -1)
265 				return -1;
266 #endif
267 		}
268 		break;
269 #endif
270 	/* TODO add code to handle V7 MUX and Blit MUX files */
271 #ifdef	S_IFIFO
272 	case S_IFIFO:
273 		if((ms->flags & MAGIC_DEVICES) != 0)
274 			break;
275 		if (mime) {
276 			if (handle_mime(ms, mime, "fifo") == -1)
277 				return -1;
278 		} else if (file_printf(ms, "%sfifo (named pipe)", COMMA) == -1)
279 			return -1;
280 		break;
281 #endif
282 #ifdef	S_IFDOOR
283 	case S_IFDOOR:
284 		if (mime) {
285 			if (handle_mime(ms, mime, "door") == -1)
286 				return -1;
287 		} else if (file_printf(ms, "%sdoor", COMMA) == -1)
288 			return -1;
289 		break;
290 #endif
291 #ifdef	S_IFLNK
292 	case S_IFLNK:
293 		if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
294 			if (ms->flags & MAGIC_ERROR) {
295 			    file_error(ms, errno, "unreadable symlink `%s'",
296 				fn);
297 			    return -1;
298 			}
299 			if (mime) {
300 				if (handle_mime(ms, mime, "symlink") == -1)
301 					return -1;
302 			} else if (file_printf(ms,
303 			    "%sunreadable symlink `%s' (%s)", COMMA, fn,
304 			    strerror(errno)) == -1)
305 				return -1;
306 			break;
307 		}
308 		buf[nch] = '\0';	/* readlink(2) does not do this */
309 
310 		/* If broken symlink, say so and quit early. */
311 		if (*buf == '/') {
312 			if (stat(buf, &tstatbuf) < 0)
313 				return bad_link(ms, errno, buf);
314 		} else {
315 			char *tmp;
316 			char buf2[BUFSIZ+BUFSIZ+4];
317 
318 			if ((tmp = strrchr(fn,  '/')) == NULL) {
319 				tmp = buf; /* in current directory anyway */
320 			} else {
321 				if (tmp - fn + 1 > BUFSIZ) {
322 					if (ms->flags & MAGIC_ERROR) {
323 						file_error(ms, 0,
324 						    "path too long: `%s'", buf);
325 						return -1;
326 					}
327 					if (mime) {
328 						if (handle_mime(ms, mime,
329 						    "x-path-too-long") == -1)
330 							return -1;
331 					} else if (file_printf(ms,
332 					    "%spath too long: `%s'", COMMA,
333 					    fn) == -1)
334 						return -1;
335 					break;
336 				}
337 				/* take dir part */
338 				(void)strlcpy(buf2, fn, sizeof buf2);
339 				buf2[tmp - fn + 1] = '\0';
340 				/* plus (rel) link */
341 				(void)strlcat(buf2, buf, sizeof buf2);
342 				tmp = buf2;
343 			}
344 			if (stat(tmp, &tstatbuf) < 0)
345 				return bad_link(ms, errno, buf);
346 		}
347 
348 		/* Otherwise, handle it. */
349 		if ((ms->flags & MAGIC_SYMLINK) != 0) {
350 			const char *p;
351 			ms->flags &= MAGIC_SYMLINK;
352 			p = magic_file(ms, buf);
353 			ms->flags |= MAGIC_SYMLINK;
354 			if (p == NULL)
355 				return -1;
356 		} else { /* just print what it points to */
357 			if (mime) {
358 				if (handle_mime(ms, mime, "symlink") == -1)
359 					return -1;
360 			} else if (file_printf(ms, "%ssymbolic link to `%s'",
361 			    COMMA, buf) == -1)
362 				return -1;
363 		}
364 		break;
365 #endif
366 #ifdef	S_IFSOCK
367 #ifndef __COHERENT__
368 	case S_IFSOCK:
369 		if (mime) {
370 			if (handle_mime(ms, mime, "socket") == -1)
371 				return -1;
372 		} else if (file_printf(ms, "%ssocket", COMMA) == -1)
373 			return -1;
374 		break;
375 #endif
376 #endif
377 	case S_IFREG:
378 		/*
379 		 * regular file, check next possibility
380 		 *
381 		 * If stat() tells us the file has zero length, report here that
382 		 * the file is empty, so we can skip all the work of opening and
383 		 * reading the file.
384 		 * But if the -s option has been given, we skip this
385 		 * optimization, since on some systems, stat() reports zero
386 		 * size for raw disk partitions. (If the block special device
387 		 * really has zero length, the fact that it is empty will be
388 		 * detected and reported correctly when we read the file.)
389 		 */
390 		if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) {
391 			if (mime) {
392 				if (handle_mime(ms, mime, "x-empty") == -1)
393 					return -1;
394 			} else if (file_printf(ms, "%sempty", COMMA) == -1)
395 				return -1;
396 			break;
397 		}
398 		ret = 0;
399 		break;
400 
401 	default:
402 		file_error(ms, 0, "invalid mode 0%o", sb->st_mode);
403 		return -1;
404 		/*NOTREACHED*/
405 	}
406 
407 	if (!mime && did && ret == 0) {
408 	    if (file_printf(ms, " ") == -1)
409 		    return -1;
410 	}
411 	return ret;
412 }
413