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