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