1 /* $NetBSD: magic.c,v 1.10 2015/01/02 21:15:32 christos Exp $ */
4 * Copyright (c) Christos Zoulas 2003.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
39 FILE_RCSID("@(#)$File: magic.c,v 1.91 2014/12/16 23:18:40 christos Exp $")
41 __RCSID("$NetBSD: magic.c,v 1.10 2015/01/02 21:15:32 christos Exp $");
54 #include <limits.h> /* for PIPE_BUF */
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)
68 #include <unistd.h> /* for read() */
72 /* Get the PIPE_BUF from pathconf */
74 #define PIPE_BUF pathconf(".", _PC_PIPE_BUF)
80 private void close_and_restore(const struct magic_set
*, const char *, int,
82 private int unreadable_info(struct magic_set
*, mode_t
, const char *);
83 private const char* get_default_magic(void);
85 private const char *file_or_fd(struct magic_set
*, const char *, int);
89 #define STDIN_FILENO 0
93 get_default_magic(void)
95 static const char hmagic
[] = "/.magic/magic.mgc";
96 static char *default_magic
;
97 char *home
, *hmagicpath
;
104 default_magic
= NULL
;
106 if ((home
= getenv("HOME")) == NULL
)
109 if (asprintf(&hmagicpath
, "%s/.magic.mgc", home
) < 0)
111 if (stat(hmagicpath
, &st
) == -1) {
113 if (asprintf(&hmagicpath
, "%s/.magic", home
) < 0)
115 if (stat(hmagicpath
, &st
) == -1)
117 if (S_ISDIR(st
.st_mode
)) {
119 if (asprintf(&hmagicpath
, "%s/%s", home
, hmagic
) < 0)
121 if (access(hmagicpath
, R_OK
) == -1)
126 if (asprintf(&default_magic
, "%s:%s", hmagicpath
, MAGIC
) < 0)
129 return default_magic
;
131 default_magic
= NULL
;
136 char *tmppath
= NULL
;
140 #define APPENDPATH() \
142 if (tmppath && access(tmppath, R_OK) != -1) { \
143 if (hmagicpath == NULL) \
144 hmagicpath = tmppath; \
146 if (asprintf(&hmagicp, "%s%c%s", hmagicpath, \
147 PATHSEP, tmppath) >= 0) { \
149 hmagicpath = hmagicp; \
155 } while (/*CONSTCOND*/0)
159 default_magic
= NULL
;
162 /* First, try to get user-specific magic file */
163 if ((home
= getenv("LOCALAPPDATA")) == NULL
) {
164 if ((home
= getenv("USERPROFILE")) != NULL
)
165 if (asprintf(&tmppath
,
166 "%s/Local Settings/Application Data%s", home
,
170 if (asprintf(&tmppath
, "%s%s", home
, hmagic
) < 0)
176 /* Second, try to get a magic file from Common Files */
177 if ((home
= getenv("COMMONPROGRAMFILES")) != NULL
) {
178 if (asprintf(&tmppath
, "%s%s", home
, hmagic
) >= 0)
182 /* Third, try to get magic file relative to dll location */
183 dllpath
= malloc(sizeof(*dllpath
) * (MAX_PATH
+ 1));
184 dllpath
[MAX_PATH
] = 0; /* just in case long path gets truncated and not null terminated */
185 if (GetModuleFileNameA(NULL
, dllpath
, MAX_PATH
)){
186 PathRemoveFileSpecA(dllpath
);
187 if (strlen(dllpath
) > 3 &&
188 stricmp(&dllpath
[strlen(dllpath
) - 3], "bin") == 0) {
189 if (asprintf(&tmppath
,
190 "%s/../share/misc/magic.mgc", dllpath
) >= 0)
193 if (asprintf(&tmppath
,
194 "%s/share/misc/magic.mgc", dllpath
) >= 0)
196 else if (asprintf(&tmppath
,
197 "%s/magic.mgc", dllpath
) >= 0)
202 /* Don't put MAGIC constant - it likely points to a file within MSys
204 default_magic
= hmagicpath
;
205 return default_magic
;
210 magic_getpath(const char *magicfile
, int action
)
212 if (magicfile
!= NULL
)
215 magicfile
= getenv("MAGIC");
216 if (magicfile
!= NULL
)
219 return action
== FILE_LOAD
? get_default_magic() : MAGIC
;
222 public struct magic_set
*
223 magic_open(int flags
)
225 return file_ms_alloc(flags
);
229 unreadable_info(struct magic_set
*ms
, mode_t md
, const char *file
)
232 /* We cannot open it, but we were able to stat it. */
233 if (access(file
, W_OK
) == 0)
234 if (file_printf(ms
, "writable, ") == -1)
236 if (access(file
, X_OK
) == 0)
237 if (file_printf(ms
, "executable, ") == -1)
241 if (file_printf(ms
, "regular file, ") == -1)
243 if (file_printf(ms
, "no read permission") == -1)
249 magic_close(struct magic_set
*ms
)
260 magic_load(struct magic_set
*ms
, const char *magicfile
)
264 return file_apprentice(ms
, magicfile
, FILE_LOAD
);
269 * Install a set of compiled magic buffers.
272 magic_load_buffers(struct magic_set
*ms
, void **bufs
, size_t *sizes
,
277 return buffer_apprentice(ms
, (struct magic
**)bufs
, sizes
, nbufs
);
282 magic_compile(struct magic_set
*ms
, const char *magicfile
)
286 return file_apprentice(ms
, magicfile
, FILE_COMPILE
);
290 magic_check(struct magic_set
*ms
, const char *magicfile
)
294 return file_apprentice(ms
, magicfile
, FILE_CHECK
);
298 magic_list(struct magic_set
*ms
, const char *magicfile
)
302 return file_apprentice(ms
, magicfile
, FILE_LIST
);
306 close_and_restore(const struct magic_set
*ms
, const char *name
, int fd
,
307 const struct stat
*sb
)
309 if (fd
== STDIN_FILENO
|| name
== NULL
)
313 if ((ms
->flags
& MAGIC_PRESERVE_ATIME
) != 0) {
315 * Try to restore access, modification times if read it.
316 * This is really *bad* because it will modify the status
317 * time of the file... And of course this will affect
321 struct timeval utsbuf
[2];
322 (void)memset(utsbuf
, 0, sizeof(utsbuf
));
323 utsbuf
[0].tv_sec
= sb
->st_atime
;
324 utsbuf
[1].tv_sec
= sb
->st_mtime
;
326 (void) utimes(name
, utsbuf
); /* don't care if loses */
327 #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
328 struct utimbuf utbuf
;
330 (void)memset(&utbuf
, 0, sizeof(utbuf
));
331 utbuf
.actime
= sb
->st_atime
;
332 utbuf
.modtime
= sb
->st_mtime
;
333 (void) utime(name
, &utbuf
); /* don't care if loses */
341 * find type of descriptor
344 magic_descriptor(struct magic_set
*ms
, int fd
)
348 return file_or_fd(ms
, NULL
, fd
);
352 * find type of named file
355 magic_file(struct magic_set
*ms
, const char *inname
)
359 return file_or_fd(ms
, inname
, STDIN_FILENO
);
363 file_or_fd(struct magic_set
*ms
, const char *inname
, int fd
)
368 ssize_t nbytes
= 0; /* number of bytes read from a datafile */
370 off_t pos
= (off_t
)-1;
372 if (file_reset(ms
) == -1)
376 * one extra for terminating '\0', and
377 * some overlapping space for matches near EOF
379 #define SLOP (1 + sizeof(union VALUETYPE))
380 if ((buf
= CAST(unsigned char *, malloc(HOWMANY
+ SLOP
))) == NULL
)
383 switch (file_fsmagic(ms
, inname
, &sb
)) {
386 case 0: /* nothing found */
388 default: /* matched it and printed type */
394 /* Place stdin in binary mode, so EOF (Ctrl+Z) doesn't stop early. */
395 if (fd
== STDIN_FILENO
)
396 _setmode(STDIN_FILENO
, O_BINARY
);
399 if (inname
== NULL
) {
400 if (fstat(fd
, &sb
) == 0 && S_ISFIFO(sb
.st_mode
))
403 pos
= lseek(fd
, (off_t
)0, SEEK_CUR
);
405 int flags
= O_RDONLY
|O_BINARY
;
406 int okstat
= stat(inname
, &sb
) == 0;
408 if (okstat
&& S_ISFIFO(sb
.st_mode
)) {
416 if ((fd
= open(inname
, flags
)) < 0) {
419 * Can't stat, can't open. It may have been opened in
420 * fsmagic, so if the user doesn't have read permission,
421 * allow it to say so; otherwise an error was probably
422 * displayed in fsmagic.
424 if (!okstat
&& errno
== EACCES
) {
425 sb
.st_mode
= S_IFBLK
;
430 unreadable_info(ms
, sb
.st_mode
, inname
) == -1)
436 if ((flags
= fcntl(fd
, F_GETFL
)) != -1) {
437 flags
&= ~O_NONBLOCK
;
438 (void)fcntl(fd
, F_SETFL
, flags
);
444 * try looking at the first HOWMANY bytes
449 while ((r
= sread(fd
, (void *)&buf
[nbytes
],
450 (size_t)(HOWMANY
- nbytes
), 1)) > 0) {
452 if (r
< PIPE_BUF
) break;
456 /* We can not read it, but we were able to stat it. */
457 if (unreadable_info(ms
, sb
.st_mode
, inname
) == -1)
464 /* Windows refuses to read from a big console buffer. */
466 #if defined(WIN32) && HOWMANY > 8 * 1024
467 _isatty(fd
) ? 8 * 1024 :
470 if ((nbytes
= read(fd
, (char *)buf
, howmany
)) == -1) {
471 if (inname
== NULL
&& fd
!= STDIN_FILENO
)
472 file_error(ms
, errno
, "cannot read fd %d", fd
);
474 file_error(ms
, errno
, "cannot read `%s'",
475 inname
== NULL
? "/dev/stdin" : inname
);
480 (void)memset(buf
+ nbytes
, 0, SLOP
); /* NUL terminate */
481 if (file_buffer(ms
, fd
, inname
, buf
, (size_t)nbytes
) == -1)
486 if (pos
!= (off_t
)-1)
487 (void)lseek(fd
, pos
, SEEK_SET
);
488 close_and_restore(ms
, inname
, fd
, &sb
);
490 return rv
== 0 ? file_getbuffer(ms
) : NULL
;
495 magic_buffer(struct magic_set
*ms
, const void *buf
, size_t nb
)
499 if (file_reset(ms
) == -1)
502 * The main work is done here!
503 * We have the file name and/or the data buffer to be identified.
505 if (file_buffer(ms
, -1, NULL
, buf
, nb
) == -1) {
508 return file_getbuffer(ms
);
513 magic_error(struct magic_set
*ms
)
516 return "Magic database is not open";
517 return (ms
->event_flags
& EVENT_HAD_ERR
) ? ms
->o
.buf
: NULL
;
521 magic_errno(struct magic_set
*ms
)
525 return (ms
->event_flags
& EVENT_HAD_ERR
) ? ms
->error
: 0;
529 magic_setflags(struct magic_set
*ms
, int flags
)
533 #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
534 if (flags
& MAGIC_PRESERVE_ATIME
)
544 return MAGIC_VERSION
;
548 magic_setparam(struct magic_set
*ms
, int param
, const void *val
)
551 case MAGIC_PARAM_INDIR_MAX
:
552 ms
->indir_max
= (uint16_t)*(const size_t *)val
;
554 case MAGIC_PARAM_NAME_MAX
:
555 ms
->name_max
= (uint16_t)*(const size_t *)val
;
557 case MAGIC_PARAM_ELF_PHNUM_MAX
:
558 ms
->elf_phnum_max
= (uint16_t)*(const size_t *)val
;
560 case MAGIC_PARAM_ELF_SHNUM_MAX
:
561 ms
->elf_shnum_max
= (uint16_t)*(const size_t *)val
;
563 case MAGIC_PARAM_ELF_NOTES_MAX
:
564 ms
->elf_notes_max
= (uint16_t)*(const size_t *)val
;
573 magic_getparam(struct magic_set
*ms
, int param
, void *val
)
576 case MAGIC_PARAM_INDIR_MAX
:
577 *(size_t *)val
= ms
->indir_max
;
579 case MAGIC_PARAM_NAME_MAX
:
580 *(size_t *)val
= ms
->name_max
;
582 case MAGIC_PARAM_ELF_PHNUM_MAX
:
583 *(size_t *)val
= ms
->elf_phnum_max
;
585 case MAGIC_PARAM_ELF_SHNUM_MAX
:
586 *(size_t *)val
= ms
->elf_shnum_max
;
588 case MAGIC_PARAM_ELF_NOTES_MAX
:
589 *(size_t *)val
= ms
->elf_notes_max
;