xref: /dflybsd-src/contrib/file/src/magic.c (revision 0eb2eccd5a86ef7dd7492d2651de55c3589f23d7)
1 /*
2  * Copyright (c) Christos Zoulas 2003.
3  * All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice immediately at the beginning of the file, without modification,
10  *    this list of conditions, and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifdef WIN32
29 #include <windows.h>
30 #include <shlwapi.h>
31 #endif
32 
33 #include "file.h"
34 
35 #ifndef	lint
36 FILE_RCSID("@(#)$File: magic.c,v 1.72 2011/03/20 20:36:52 christos Exp $")
37 #endif	/* lint */
38 
39 #include "magic.h"
40 
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <string.h>
44 #ifdef QUICK
45 #include <sys/mman.h>
46 #endif
47 #ifdef HAVE_LIMITS_H
48 #include <limits.h>	/* for PIPE_BUF */
49 #endif
50 
51 #if defined(HAVE_UTIMES)
52 # include <sys/time.h>
53 #elif defined(HAVE_UTIME)
54 # if defined(HAVE_SYS_UTIME_H)
55 #  include <sys/utime.h>
56 # elif defined(HAVE_UTIME_H)
57 #  include <utime.h>
58 # endif
59 #endif
60 
61 #ifdef HAVE_UNISTD_H
62 #include <unistd.h>	/* for read() */
63 #endif
64 
65 #include "patchlevel.h"
66 
67 #ifndef PIPE_BUF
68 /* Get the PIPE_BUF from pathconf */
69 #ifdef _PC_PIPE_BUF
70 #define PIPE_BUF pathconf(".", _PC_PIPE_BUF)
71 #else
72 #define PIPE_BUF 512
73 #endif
74 #endif
75 
76 private void free_mlist(struct mlist *);
77 private void close_and_restore(const struct magic_set *, const char *, int,
78     const struct stat *);
79 private int unreadable_info(struct magic_set *, mode_t, const char *);
80 private const char* get_default_magic(void);
81 #ifndef COMPILE_ONLY
82 private const char *file_or_fd(struct magic_set *, const char *, int);
83 #endif
84 
85 #ifndef	STDIN_FILENO
86 #define	STDIN_FILENO	0
87 #endif
88 
89 #ifdef WIN32
90 BOOL WINAPI DllMain(HINSTANCE hinstDLL,
91     DWORD fdwReason __attribute__((__unused__)),
92     LPVOID lpvReserved __attribute__((__unused__)));
93 
94 CHAR dllpath[MAX_PATH + 1] = { 0 };
95 
96 BOOL WINAPI DllMain(HINSTANCE hinstDLL,
97     DWORD fdwReason __attribute__((__unused__)),
98     LPVOID lpvReserved __attribute__((__unused__)))
99 {
100 	if (dllpath[0] == 0 &&
101 	    GetModuleFileNameA(hinstDLL, dllpath, MAX_PATH) != 0)
102 		PathRemoveFileSpecA(dllpath);
103 	return TRUE;
104 }
105 #endif
106 
107 private const char *
108 get_default_magic(void)
109 {
110 	static const char hmagic[] = "/.magic/magic.mgc";
111 	static char *default_magic;
112 	char *home, *hmagicpath;
113 
114 #ifndef WIN32
115 	struct stat st;
116 
117 	if (default_magic) {
118 		free(default_magic);
119 		default_magic = NULL;
120 	}
121 	if ((home = getenv("HOME")) == NULL)
122 		return MAGIC;
123 
124 	if (asprintf(&hmagicpath, "%s/.magic", home) < 0)
125 		return MAGIC;
126 	if (stat(hmagicpath, &st) == -1)
127 		goto out;
128 	if (S_ISDIR(st.st_mode)) {
129 		free(hmagicpath);
130 		if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0)
131 			return MAGIC;
132 		if (access(hmagicpath, R_OK) == -1)
133 			goto out;
134 	}
135 
136 	if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0)
137 		goto out;
138 	free(hmagicpath);
139 	return default_magic;
140 out:
141 	default_magic = NULL;
142 	free(hmagicpath);
143 	return MAGIC;
144 #else
145 	char *hmagicp = hmagicpath;
146 	char *tmppath = NULL;
147 
148 #define APPENDPATH() \
149 	do { \
150 		if (tmppath && access(tmppath, R_OK) != -1) { \
151 			if (hmagicpath == NULL) { \
152 				hmagicpath = tmppath; \
153 				tmppath = NULL; \
154 			} else { \
155 				free(tmppath); \
156 				if (asprintf(&hmagicp, "%s%c%s", hmagicpath, \
157 				    PATHSEP, tmppath) >= 0) { \
158 					free(hmagicpath); \
159 					hmagicpath = hmagicp; \
160 				} \
161 			} \
162 	} while (/*CONSTCOND*/0)
163 
164 	if (default_magic) {
165 		free(default_magic);
166 		default_magic = NULL;
167 	}
168 
169 	/* First, try to get user-specific magic file */
170 	if ((home = getenv("LOCALAPPDATA")) == NULL) {
171 		if ((home = getenv("USERPROFILE")) != NULL)
172 			if (asprintf(&tmppath,
173 			    "%s/Local Settings/Application Data%s", home,
174 			    hmagic) < 0)
175 				tmppath = NULL;
176 	} else {
177 		if (asprintf(&tmppath, "%s%s", home, hmagic) < 0)
178 			tmppath = NULL;
179 	}
180 
181 	APPENDPATH();
182 
183 	/* Second, try to get a magic file from Common Files */
184 	if ((home = getenv("COMMONPROGRAMFILES")) != NULL) {
185 		if (asprintf(&tmppath, "%s%s", home, hmagic) >= 0)
186 			APPENDPATH();
187 	}
188 
189 
190 	/* Third, try to get magic file relative to dll location */
191 	if (dllpath[0] != 0) {
192 		if (strlen(dllpath) > 3 &&
193 		    stricmp(&dllpath[strlen(dllpath) - 3], "bin") == 0) {
194 			if (asprintf(&tmppath,
195 			    "%s/../share/misc/magic.mgc", dllpath) >= 0)
196 				APPENDPATH();
197 		} else {
198 			if (asprintf(&tmppath,
199 			    "%s/share/misc/magic.mgc", dllpath) >= 0)
200 				APPENDPATH();
201 			else if (asprintf(&tmppath,
202 			    "%s/magic.mgc", dllpath) >= 0)
203 				APPENDPATH();
204 		}
205 	}
206 
207 	/* Don't put MAGIC constant - it likely points to a file within MSys
208 	tree */
209 	default_magic = hmagicpath;
210 	return default_magic;
211 #endif
212 }
213 
214 public const char *
215 magic_getpath(const char *magicfile, int action)
216 {
217 	if (magicfile != NULL)
218 		return magicfile;
219 
220 	magicfile = getenv("MAGIC");
221 	if (magicfile != NULL)
222 		return magicfile;
223 
224 	return action == FILE_LOAD ? get_default_magic() : MAGIC;
225 }
226 
227 public struct magic_set *
228 magic_open(int flags)
229 {
230 	struct magic_set *ms;
231 	size_t len;
232 
233 	if ((ms = CAST(struct magic_set *, calloc((size_t)1,
234 	    sizeof(struct magic_set)))) == NULL)
235 		return NULL;
236 
237 	if (magic_setflags(ms, flags) == -1) {
238 		errno = EINVAL;
239 		goto free;
240 	}
241 
242 	ms->o.buf = ms->o.pbuf = NULL;
243 	len = (ms->c.len = 10) * sizeof(*ms->c.li);
244 
245 	if ((ms->c.li = CAST(struct level_info *, malloc(len))) == NULL)
246 		goto free;
247 
248 	ms->event_flags = 0;
249 	ms->error = -1;
250 	ms->mlist = NULL;
251 	ms->file = "unknown";
252 	ms->line = 0;
253 	return ms;
254 free:
255 	free(ms);
256 	return NULL;
257 }
258 
259 private void
260 free_mlist(struct mlist *mlist)
261 {
262 	struct mlist *ml;
263 
264 	if (mlist == NULL)
265 		return;
266 
267 	for (ml = mlist->next; ml != mlist;) {
268 		struct mlist *next = ml->next;
269 		struct magic *mg = ml->magic;
270 		file_delmagic(mg, ml->mapped, ml->nmagic);
271 		free(ml);
272 		ml = next;
273 	}
274 	free(ml);
275 }
276 
277 private int
278 unreadable_info(struct magic_set *ms, mode_t md, const char *file)
279 {
280 	/* We cannot open it, but we were able to stat it. */
281 	if (access(file, W_OK) == 0)
282 		if (file_printf(ms, "writable, ") == -1)
283 			return -1;
284 	if (access(file, X_OK) == 0)
285 		if (file_printf(ms, "executable, ") == -1)
286 			return -1;
287 	if (S_ISREG(md))
288 		if (file_printf(ms, "regular file, ") == -1)
289 			return -1;
290 	if (file_printf(ms, "no read permission") == -1)
291 		return -1;
292 	return 0;
293 }
294 
295 public void
296 magic_close(struct magic_set *ms)
297 {
298 	free_mlist(ms->mlist);
299 	free(ms->o.pbuf);
300 	free(ms->o.buf);
301 	free(ms->c.li);
302 	free(ms);
303 }
304 
305 /*
306  * load a magic file
307  */
308 public int
309 magic_load(struct magic_set *ms, const char *magicfile)
310 {
311 	struct mlist *ml = file_apprentice(ms, magicfile, FILE_LOAD);
312 	if (ml) {
313 		free_mlist(ms->mlist);
314 		ms->mlist = ml;
315 		return 0;
316 	}
317 	return -1;
318 }
319 
320 public int
321 magic_compile(struct magic_set *ms, const char *magicfile)
322 {
323 	struct mlist *ml = file_apprentice(ms, magicfile, FILE_COMPILE);
324 	free_mlist(ml);
325 	return ml ? 0 : -1;
326 }
327 
328 public int
329 magic_check(struct magic_set *ms, const char *magicfile)
330 {
331 	struct mlist *ml = file_apprentice(ms, magicfile, FILE_CHECK);
332 	free_mlist(ml);
333 	return ml ? 0 : -1;
334 }
335 
336 public int
337 magic_list(struct magic_set *ms, const char *magicfile)
338 {
339 	struct mlist *ml = file_apprentice(ms, magicfile, FILE_LIST);
340 	free_mlist(ml);
341 	return ml ? 0 : -1;
342 }
343 
344 private void
345 close_and_restore(const struct magic_set *ms, const char *name, int fd,
346     const struct stat *sb)
347 {
348 	if (fd == STDIN_FILENO)
349 		return;
350 	(void) close(fd);
351 
352 	if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
353 		/*
354 		 * Try to restore access, modification times if read it.
355 		 * This is really *bad* because it will modify the status
356 		 * time of the file... And of course this will affect
357 		 * backup programs
358 		 */
359 #ifdef HAVE_UTIMES
360 		struct timeval  utsbuf[2];
361 		(void)memset(utsbuf, 0, sizeof(utsbuf));
362 		utsbuf[0].tv_sec = sb->st_atime;
363 		utsbuf[1].tv_sec = sb->st_mtime;
364 
365 		(void) utimes(name, utsbuf); /* don't care if loses */
366 #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
367 		struct utimbuf  utbuf;
368 
369 		(void)memset(&utbuf, 0, sizeof(utbuf));
370 		utbuf.actime = sb->st_atime;
371 		utbuf.modtime = sb->st_mtime;
372 		(void) utime(name, &utbuf); /* don't care if loses */
373 #endif
374 	}
375 }
376 
377 #ifndef COMPILE_ONLY
378 
379 /*
380  * find type of descriptor
381  */
382 public const char *
383 magic_descriptor(struct magic_set *ms, int fd)
384 {
385 	return file_or_fd(ms, NULL, fd);
386 }
387 
388 /*
389  * find type of named file
390  */
391 public const char *
392 magic_file(struct magic_set *ms, const char *inname)
393 {
394 	return file_or_fd(ms, inname, STDIN_FILENO);
395 }
396 
397 private const char *
398 file_or_fd(struct magic_set *ms, const char *inname, int fd)
399 {
400 	int	rv = -1;
401 	unsigned char *buf;
402 	struct stat	sb;
403 	ssize_t nbytes = 0;	/* number of bytes read from a datafile */
404 	int	ispipe = 0;
405 
406 	/*
407 	 * one extra for terminating '\0', and
408 	 * some overlapping space for matches near EOF
409 	 */
410 #define SLOP (1 + sizeof(union VALUETYPE))
411 	if ((buf = CAST(unsigned char *, malloc(HOWMANY + SLOP))) == NULL)
412 		return NULL;
413 
414 	if (file_reset(ms) == -1)
415 		goto done;
416 
417 	switch (file_fsmagic(ms, inname, &sb)) {
418 	case -1:		/* error */
419 		goto done;
420 	case 0:			/* nothing found */
421 		break;
422 	default:		/* matched it and printed type */
423 		rv = 0;
424 		goto done;
425 	}
426 
427 	if (inname == NULL) {
428 		if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
429 			ispipe = 1;
430 	} else {
431 		int flags = O_RDONLY|O_BINARY;
432 
433 		if (stat(inname, &sb) == 0 && S_ISFIFO(sb.st_mode)) {
434 #ifdef O_NONBLOCK
435 			flags |= O_NONBLOCK;
436 #endif
437 			ispipe = 1;
438 		}
439 
440 		errno = 0;
441 		if ((fd = open(inname, flags)) < 0) {
442 			if (unreadable_info(ms, sb.st_mode, inname) == -1)
443 				goto done;
444 			rv = 0;
445 			goto done;
446 		}
447 #ifdef O_NONBLOCK
448 		if ((flags = fcntl(fd, F_GETFL)) != -1) {
449 			flags &= ~O_NONBLOCK;
450 			(void)fcntl(fd, F_SETFL, flags);
451 		}
452 #endif
453 	}
454 
455 	/*
456 	 * try looking at the first HOWMANY bytes
457 	 */
458 	if (ispipe) {
459 		ssize_t r = 0;
460 
461 		while ((r = sread(fd, (void *)&buf[nbytes],
462 		    (size_t)(HOWMANY - nbytes), 1)) > 0) {
463 			nbytes += r;
464 			if (r < PIPE_BUF) break;
465 		}
466 
467 		if (nbytes == 0) {
468 			/* We can not read it, but we were able to stat it. */
469 			if (unreadable_info(ms, sb.st_mode, inname) == -1)
470 				goto done;
471 			rv = 0;
472 			goto done;
473 		}
474 
475 	} else {
476 		if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
477 			file_error(ms, errno, "cannot read `%s'", inname);
478 			goto done;
479 		}
480 	}
481 
482 	(void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
483 	if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1)
484 		goto done;
485 	rv = 0;
486 done:
487 	free(buf);
488 	close_and_restore(ms, inname, fd, &sb);
489 	return rv == 0 ? file_getbuffer(ms) : NULL;
490 }
491 
492 
493 public const char *
494 magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
495 {
496 	if (file_reset(ms) == -1)
497 		return NULL;
498 	/*
499 	 * The main work is done here!
500 	 * We have the file name and/or the data buffer to be identified.
501 	 */
502 	if (file_buffer(ms, -1, NULL, buf, nb) == -1) {
503 		return NULL;
504 	}
505 	return file_getbuffer(ms);
506 }
507 #endif
508 
509 public const char *
510 magic_error(struct magic_set *ms)
511 {
512 	return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
513 }
514 
515 public int
516 magic_errno(struct magic_set *ms)
517 {
518 	return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
519 }
520 
521 public int
522 magic_setflags(struct magic_set *ms, int flags)
523 {
524 #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
525 	if (flags & MAGIC_PRESERVE_ATIME)
526 		return -1;
527 #endif
528 	ms->flags = flags;
529 	return 0;
530 }
531