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.128 2026/05/09 22:34:30 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 #include <limits.h> /* for PIPE_BUF */ 48 49 #if defined(HAVE_UTIMES) 50 # include <sys/time.h> 51 #elif defined(HAVE_UTIME) 52 # if defined(HAVE_SYS_UTIME_H) 53 # include <sys/utime.h> 54 # elif defined(HAVE_UTIME_H) 55 # include <utime.h> 56 # endif 57 #endif 58 59 #ifdef HAVE_UNISTD_H 60 #include <unistd.h> /* for read() */ 61 #endif 62 63 #ifndef PIPE_BUF 64 /* Get the PIPE_BUF from pathconf */ 65 #ifdef _PC_PIPE_BUF 66 #define PIPE_BUF pathconf(".", _PC_PIPE_BUF) 67 #else 68 #define PIPE_BUF 512 69 #endif 70 #endif 71 72 file_private void close_and_restore(const struct magic_set *, const char *, int, 73 const struct stat *); 74 file_private int unreadable_info(struct magic_set *, mode_t, const char *); 75 file_private const char *get_default_magic(void); 76 #ifndef COMPILE_ONLY 77 file_private const char *file_or_fd(struct magic_set *, const char *, int); 78 #endif 79 80 #ifndef STDIN_FILENO 81 #define STDIN_FILENO 0 82 #endif 83 84 #ifdef WIN32 85 /* HINSTANCE of this shared library. Needed for get_default_magic() */ 86 static HINSTANCE _w32_dll_instance = NULL; 87 88 static void 89 _w32_append_path(char **hmagicpath, const char *fmt, ...) 90 { 91 char *tmppath; 92 char *newpath; 93 va_list ap; 94 95 va_start(ap, fmt); 96 if (vasprintf(&tmppath, fmt, ap) < 0) { 97 va_end(ap); 98 return; 99 } 100 va_end(ap); 101 102 if (access(tmppath, R_OK) == -1) 103 goto out; 104 105 if (*hmagicpath == NULL) { 106 *hmagicpath = tmppath; 107 return; 108 } 109 110 if (asprintf(&newpath, "%s%c%s", *hmagicpath, PATHSEP, tmppath) < 0) 111 goto out; 112 113 free(*hmagicpath); 114 free(tmppath); 115 *hmagicpath = newpath; 116 return; 117 out: 118 free(tmppath); 119 } 120 121 static void 122 _w32_get_magic_relative_to(char **hmagicpath, HINSTANCE module) 123 { 124 static const char *trypaths[] = { 125 "%s/share/misc/magic.mgc", 126 "%s/magic.mgc", 127 }; 128 LPSTR dllpath; 129 size_t sp; 130 131 dllpath = calloc(MAX_PATH + 1, sizeof(*dllpath)); 132 133 if (!GetModuleFileNameA(module, dllpath, MAX_PATH)) 134 goto out; 135 136 PathRemoveFileSpecA(dllpath); 137 138 if (module) { 139 char exepath[MAX_PATH]; 140 GetModuleFileNameA(NULL, exepath, MAX_PATH); 141 PathRemoveFileSpecA(exepath); 142 if (stricmp(exepath, dllpath) == 0) 143 goto out; 144 } 145 146 sp = strlen(dllpath); 147 if (sp > 3 && stricmp(&dllpath[sp - 3], "bin") == 0) { 148 _w32_append_path(hmagicpath, 149 "%s/../share/misc/magic.mgc", dllpath); 150 goto out; 151 } 152 153 for (sp = 0; sp < __arraycount(trypaths); sp++) 154 _w32_append_path(hmagicpath, trypaths[sp], dllpath); 155 out: 156 free(dllpath); 157 } 158 159 #ifndef BUILD_AS_WINDOWS_STATIC_LIBARAY 160 /* Placate GCC by offering a sacrificial previous prototype */ 161 BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID); 162 163 BOOL WINAPI 164 DllMain(HINSTANCE hinstDLL, DWORD fdwReason, 165 LPVOID lpvReserved __attribute__((__unused__))) 166 { 167 if (fdwReason == DLL_PROCESS_ATTACH) 168 _w32_dll_instance = hinstDLL; 169 return 1; 170 } 171 #endif 172 #endif 173 174 file_private const char * 175 get_default_magic(void) 176 { 177 static const char hmagic[] = "/.magic/magic.mgc"; 178 static char *default_magic; 179 char *home, *hmagicpath, *tmp_magic; 180 181 #ifndef WIN32 182 struct stat st; 183 184 if ((home = getenv("HOME")) == NULL) 185 return MAGIC; 186 187 if (asprintf(&hmagicpath, "%s/.magic.mgc", home) < 0) 188 return MAGIC; 189 if (stat(hmagicpath, &st) == -1) { 190 free(hmagicpath); 191 if (asprintf(&hmagicpath, "%s/.magic", home) < 0) 192 return MAGIC; 193 if (stat(hmagicpath, &st) == -1) 194 goto out; 195 if (S_ISDIR(st.st_mode)) { 196 free(hmagicpath); 197 if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0) 198 return MAGIC; 199 if (access(hmagicpath, R_OK) == -1) 200 goto out; 201 } 202 } 203 204 if (asprintf(&tmp_magic, "%s%c%s", hmagicpath, PATHSEP, MAGIC) < 0) 205 goto out; 206 free(hmagicpath); 207 hmagicpath = default_magic; 208 default_magic = tmp_magic; 209 free(hmagicpath); 210 return default_magic; 211 out: 212 default_magic = NULL; 213 free(hmagicpath); 214 return MAGIC; 215 #else 216 hmagicpath = NULL; 217 218 if (default_magic) { 219 free(default_magic); 220 default_magic = NULL; 221 } 222 223 /* Before anything else, try to get a magic file from user HOME */ 224 if ((home = getenv("HOME")) != NULL) 225 _w32_append_path(&hmagicpath, "%s%s", home, hmagic); 226 227 /* First, try to get a magic file from user-application data */ 228 if ((home = getenv("LOCALAPPDATA")) != NULL) 229 _w32_append_path(&hmagicpath, "%s%s", home, hmagic); 230 231 /* Second, try to get a magic file from the user profile data */ 232 if ((home = getenv("USERPROFILE")) != NULL) 233 _w32_append_path(&hmagicpath, 234 "%s/Local Settings/Application Data%s", home, hmagic); 235 236 /* Third, try to get a magic file from Common Files */ 237 if ((home = getenv("COMMONPROGRAMFILES")) != NULL) 238 _w32_append_path(&hmagicpath, "%s%s", home, hmagic); 239 240 /* Fourth, try to get magic file relative to exe location */ 241 _w32_get_magic_relative_to(&hmagicpath, NULL); 242 243 /* Fifth, try to get magic file relative to dll location */ 244 _w32_get_magic_relative_to(&hmagicpath, _w32_dll_instance); 245 246 /* Avoid MAGIC constant - it likely points to a file within MSys tree */ 247 tmp_magic = default_magic; 248 default_magic = hmagicpath; 249 free(tmp_magic); 250 return default_magic; 251 #endif 252 } 253 254 file_public const char * 255 magic_getpath(const char *magicfile, int action) 256 { 257 if (magicfile != NULL) 258 return magicfile; 259 260 magicfile = getenv("MAGIC"); 261 if (magicfile != NULL) 262 return magicfile; 263 264 return action == FILE_LOAD ? get_default_magic() : MAGIC; 265 } 266 267 file_public struct magic_set * 268 magic_open(int flags) 269 { 270 return file_ms_alloc(flags); 271 } 272 273 file_private int 274 unreadable_info(struct magic_set *ms, mode_t md, const char *file) 275 { 276 if (file) { 277 /* We cannot open it, but we were able to stat it. */ 278 if (access(file, W_OK) == 0) 279 if (file_printf(ms, "writable, ") == -1) 280 return -1; 281 #ifndef WIN32 282 if (access(file, X_OK) == 0) 283 if (file_printf(ms, "executable, ") == -1) 284 return -1; 285 #else 286 /* X_OK doesn't work well on MS-Windows */ 287 { 288 const char *p = strrchr(file, '.'); 289 if (p && (stricmp(p, ".exe") 290 || stricmp(p, ".dll") 291 || stricmp(p, ".bat") 292 || stricmp(p, ".cmd"))) 293 if (file_printf(ms, "writable, ") == -1) 294 return -1; 295 } 296 #endif 297 } 298 if (S_ISREG(md)) 299 if (file_printf(ms, "regular file, ") == -1) 300 return -1; 301 if (file_printf(ms, "no read permission") == -1) 302 return -1; 303 return 0; 304 } 305 306 file_public void 307 magic_close(struct magic_set *ms) 308 { 309 if (ms == NULL) 310 return; 311 file_ms_free(ms); 312 } 313 314 /* 315 * load a magic file 316 */ 317 file_public int 318 magic_load(struct magic_set *ms, const char *magicfile) 319 { 320 if (ms == NULL) 321 return -1; 322 return file_apprentice(ms, magicfile, FILE_LOAD); 323 } 324 325 #ifndef COMPILE_ONLY 326 /* 327 * Install a set of compiled magic buffers. 328 */ 329 file_public int 330 magic_load_buffers(struct magic_set *ms, void **bufs, size_t *sizes, 331 size_t nbufs) 332 { 333 if (ms == NULL) 334 return -1; 335 return buffer_apprentice(ms, RCAST(struct magic **, bufs), 336 sizes, nbufs); 337 } 338 #endif 339 340 file_public int 341 magic_compile(struct magic_set *ms, const char *magicfile) 342 { 343 if (ms == NULL) 344 return -1; 345 return file_apprentice(ms, magicfile, FILE_COMPILE); 346 } 347 348 file_public int 349 magic_check(struct magic_set *ms, const char *magicfile) 350 { 351 if (ms == NULL) 352 return -1; 353 return file_apprentice(ms, magicfile, FILE_CHECK); 354 } 355 356 file_public int 357 magic_list(struct magic_set *ms, const char *magicfile) 358 { 359 if (ms == NULL) 360 return -1; 361 return file_apprentice(ms, magicfile, FILE_LIST); 362 } 363 364 file_private void 365 close_and_restore(const struct magic_set *ms, const char *name, int fd, 366 const struct stat *sb) 367 { 368 if (fd == STDIN_FILENO || name == NULL) 369 return; 370 (void) close(fd); 371 372 if (sb == NULL || (ms->flags & MAGIC_PRESERVE_ATIME) == 0) 373 return; 374 375 /* 376 * Try to restore access, modification times if read it. 377 * This is really *bad* because it will modify the status 378 * time of the file... And of course this will affect 379 * backup programs 380 */ 381 #ifdef HAVE_UTIMES 382 struct timeval utsbuf[2]; 383 (void)memset(utsbuf, 0, sizeof(utsbuf)); 384 utsbuf[0].tv_sec = sb->st_atime; 385 utsbuf[1].tv_sec = sb->st_mtime; 386 387 (void) utimes(name, utsbuf); /* don't care if loses */ 388 #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H) 389 struct utimbuf utbuf; 390 391 (void)memset(&utbuf, 0, sizeof(utbuf)); 392 utbuf.actime = sb->st_atime; 393 utbuf.modtime = sb->st_mtime; 394 (void) utime(name, &utbuf); /* don't care if loses */ 395 #endif 396 } 397 398 #ifndef COMPILE_ONLY 399 400 /* 401 * find type of descriptor 402 */ 403 file_public const char * 404 magic_descriptor(struct magic_set *ms, int fd) 405 { 406 if (ms == NULL) 407 return NULL; 408 return file_or_fd(ms, NULL, fd); 409 } 410 411 /* 412 * find type of named file 413 */ 414 file_public const char * 415 magic_file(struct magic_set *ms, const char *inname) 416 { 417 if (ms == NULL) 418 return NULL; 419 return file_or_fd(ms, inname, STDIN_FILENO); 420 } 421 422 file_private const char * 423 file_or_fd(struct magic_set *ms, const char *inname, int fd) 424 { 425 int rv = -1; 426 unsigned char *buf; 427 struct stat sb; 428 ssize_t nbytes = 0; /* number of bytes read from a datafile */ 429 int ispipe = 0; 430 int okstat = 0; 431 off_t pos = CAST(off_t, -1); 432 433 if (file_reset(ms, 1) == -1) 434 goto out; 435 436 /* 437 * one extra for terminating '\0', and 438 * some overlapping space for matches near EOF 439 */ 440 #define SLOP (1 + sizeof(union VALUETYPE)) 441 if ((buf = CAST(unsigned char *, malloc(ms->bytes_max + SLOP))) == NULL) 442 return NULL; 443 444 switch (file_fsmagic(ms, inname, &sb)) { 445 case -1: /* error */ 446 goto done; 447 case 0: /* nothing found */ 448 break; 449 default: /* matched it and printed type */ 450 rv = 0; 451 goto done; 452 } 453 454 #ifdef WIN32 455 /* Place stdin in binary mode, so EOF (Ctrl+Z) doesn't stop early. */ 456 if (fd == STDIN_FILENO) 457 _setmode(STDIN_FILENO, O_BINARY); 458 #endif 459 if (inname != NULL) { 460 int flags = O_RDONLY|O_BINARY|O_NONBLOCK|O_CLOEXEC; 461 errno = 0; 462 if ((fd = open(inname, flags)) < 0) { 463 okstat = stat(inname, &sb) == 0; 464 #ifdef WIN32 465 /* 466 * Can't stat, can't open. It may have been opened in 467 * fsmagic, so if the user doesn't have read permission, 468 * allow it to say so; otherwise an error was probably 469 * displayed in fsmagic. 470 */ 471 if (!okstat && errno == EACCES) { 472 sb.st_mode = S_IFBLK; 473 okstat = 1; 474 } 475 #endif 476 if (okstat && 477 unreadable_info(ms, sb.st_mode, inname) == -1) 478 goto done; 479 rv = 0; 480 goto done; 481 } 482 #if O_CLOEXEC == 0 && defined(F_SETFD) 483 (void)fcntl(fd, F_SETFD, FD_CLOEXEC); 484 #endif 485 } 486 487 if (fd != -1) { 488 okstat = fstat(fd, &sb) == 0; 489 if (okstat && S_ISFIFO(sb.st_mode)) 490 ispipe = 1; 491 if (inname == NULL) 492 pos = lseek(fd, CAST(off_t, 0), SEEK_CUR); 493 } 494 495 /* 496 * try looking at the first ms->bytes_max bytes 497 */ 498 if (ispipe) { 499 if (fd != -1) { 500 ssize_t r = 0; 501 502 while ((r = sread(fd, RCAST(void *, &buf[nbytes]), 503 CAST(size_t, ms->bytes_max - nbytes), 1)) > 0) { 504 nbytes += r; 505 if (r < PIPE_BUF) break; 506 } 507 } 508 509 if (nbytes == 0 && inname) { 510 /* We can not read it, but we were able to stat it. */ 511 if (unreadable_info(ms, sb.st_mode, inname) == -1) 512 goto done; 513 rv = 0; 514 goto done; 515 } 516 517 } else if (fd != -1) { 518 /* Windows refuses to read from a big console buffer. */ 519 size_t howmany = 520 #ifdef WIN32 521 _isatty(fd) ? 8 * 1024 : 522 #endif 523 ms->bytes_max; 524 if ((nbytes = read(fd, RCAST(void *, buf), howmany)) == -1) { 525 if (inname == NULL && fd != STDIN_FILENO) 526 file_error(ms, errno, "cannot read fd %d", fd); 527 else 528 file_error(ms, errno, "cannot read `%s'", 529 inname == NULL ? "/dev/stdin" : inname); 530 goto done; 531 } 532 } 533 534 (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */ 535 if (file_buffer(ms, fd, okstat ? &sb : NULL, inname, buf, 536 CAST(size_t, nbytes)) == -1) 537 goto done; 538 rv = 0; 539 done: 540 free(buf); 541 if (fd != -1) { 542 if (pos != CAST(off_t, -1)) 543 (void)lseek(fd, pos, SEEK_SET); 544 close_and_restore(ms, inname, fd, okstat ? &sb : NULL); 545 } 546 out: 547 return rv == 0 ? file_getbuffer(ms) : NULL; 548 } 549 550 551 file_public const char * 552 magic_buffer(struct magic_set *ms, const void *buf, size_t nb) 553 { 554 if (ms == NULL) 555 return NULL; 556 if (file_reset(ms, 1) == -1) 557 return NULL; 558 /* 559 * The main work is done here! 560 * We have the file name and/or the data buffer to be identified. 561 */ 562 if (file_buffer(ms, -1, NULL, NULL, buf, nb) == -1) { 563 return NULL; 564 } 565 return file_getbuffer(ms); 566 } 567 #endif 568 569 file_public const char * 570 magic_error(struct magic_set *ms) 571 { 572 if (ms == NULL) 573 return "Magic database is not open"; 574 return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL; 575 } 576 577 file_public int 578 magic_errno(struct magic_set *ms) 579 { 580 if (ms == NULL) 581 return EINVAL; 582 return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0; 583 } 584 585 file_public int 586 magic_getflags(struct magic_set *ms) 587 { 588 if (ms == NULL) 589 return -1; 590 591 return ms->flags; 592 } 593 594 file_public int 595 magic_setflags(struct magic_set *ms, int flags) 596 { 597 if (ms == NULL) 598 return -1; 599 #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES) 600 if (flags & MAGIC_PRESERVE_ATIME) 601 return -1; 602 #endif 603 ms->flags = flags; 604 return 0; 605 } 606 607 file_public int 608 magic_version(void) 609 { 610 return MAGIC_VERSION; 611 } 612 613 file_public ssize_t 614 magic_getmaxparam(int param) 615 { 616 switch (param) { 617 case MAGIC_PARAM_INDIR_MAX: 618 case MAGIC_PARAM_NAME_MAX: 619 case MAGIC_PARAM_ELF_PHNUM_MAX: 620 case MAGIC_PARAM_ELF_SHNUM_MAX: 621 case MAGIC_PARAM_ELF_SHSIZE_MAX: 622 case MAGIC_PARAM_ELF_NOTES_MAX: 623 case MAGIC_PARAM_REGEX_MAX: 624 return 0xffff; 625 case MAGIC_PARAM_BYTES_MAX: 626 case MAGIC_PARAM_ENCODING_MAX: 627 case MAGIC_PARAM_MAGWARN_MAX: 628 return 0x7fffffff; 629 default: 630 errno = EINVAL; 631 return -1; 632 } 633 } 634 635 file_public int 636 magic_setparam(struct magic_set *ms, int param, const void *val) 637 { 638 if (ms == NULL || val == NULL) { 639 errno = EFAULT; 640 return -1; 641 } 642 const size_t v = *CAST(const size_t *, val); 643 switch (param) { 644 case MAGIC_PARAM_INDIR_MAX: 645 ms->indir_max = CAST(uint16_t, v); 646 return 0; 647 case MAGIC_PARAM_NAME_MAX: 648 ms->name_max = CAST(uint16_t, v); 649 return 0; 650 case MAGIC_PARAM_ELF_PHNUM_MAX: 651 ms->elf_phnum_max = CAST(uint16_t, v); 652 return 0; 653 case MAGIC_PARAM_ELF_SHNUM_MAX: 654 ms->elf_shnum_max = CAST(uint16_t, v); 655 return 0; 656 case MAGIC_PARAM_ELF_SHSIZE_MAX: 657 ms->elf_shsize_max = v; 658 return 0; 659 case MAGIC_PARAM_ELF_NOTES_MAX: 660 ms->elf_notes_max = CAST(uint16_t, v); 661 return 0; 662 case MAGIC_PARAM_REGEX_MAX: 663 ms->regex_max = CAST(uint16_t, v); 664 return 0; 665 case MAGIC_PARAM_BYTES_MAX: 666 ms->bytes_max = v; 667 return 0; 668 case MAGIC_PARAM_ENCODING_MAX: 669 ms->encoding_max = v; 670 return 0; 671 case MAGIC_PARAM_MAGWARN_MAX: 672 ms->magwarn_max = v; 673 return 0; 674 default: 675 errno = EINVAL; 676 return -1; 677 } 678 } 679 680 file_public int 681 magic_getparam(struct magic_set *ms, int param, void *val) 682 { 683 if (ms == NULL || val == NULL) { 684 errno = EFAULT; 685 return -1; 686 } 687 switch (param) { 688 case MAGIC_PARAM_INDIR_MAX: 689 *CAST(size_t *, val) = ms->indir_max; 690 return 0; 691 case MAGIC_PARAM_NAME_MAX: 692 *CAST(size_t *, val) = ms->name_max; 693 return 0; 694 case MAGIC_PARAM_ELF_PHNUM_MAX: 695 *CAST(size_t *, val) = ms->elf_phnum_max; 696 return 0; 697 case MAGIC_PARAM_ELF_SHNUM_MAX: 698 *CAST(size_t *, val) = ms->elf_shnum_max; 699 return 0; 700 case MAGIC_PARAM_ELF_SHSIZE_MAX: 701 *CAST(size_t *, val) = ms->elf_shsize_max; 702 return 0; 703 case MAGIC_PARAM_ELF_NOTES_MAX: 704 *CAST(size_t *, val) = ms->elf_notes_max; 705 return 0; 706 case MAGIC_PARAM_REGEX_MAX: 707 *CAST(size_t *, val) = ms->regex_max; 708 return 0; 709 case MAGIC_PARAM_BYTES_MAX: 710 *CAST(size_t *, val) = ms->bytes_max; 711 return 0; 712 case MAGIC_PARAM_ENCODING_MAX: 713 *CAST(size_t *, val) = ms->encoding_max; 714 return 0; 715 case MAGIC_PARAM_MAGWARN_MAX: 716 *CAST(size_t *, val) = ms->magwarn_max; 717 return 0; 718 default: 719 errno = EINVAL; 720 return -1; 721 } 722 } 723