1 /* $NetBSD: magic.c,v 1.10 2015/01/02 21:15:32 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.91 2014/12/16 23:18:40 christos Exp $") 40 #else 41 __RCSID("$NetBSD: magic.c,v 1.10 2015/01/02 21:15:32 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 close_and_restore(const struct magic_set *, const char *, int, 81 const struct stat *); 82 private int unreadable_info(struct magic_set *, mode_t, const char *); 83 private const char* get_default_magic(void); 84 #ifndef COMPILE_ONLY 85 private const char *file_or_fd(struct magic_set *, const char *, int); 86 #endif 87 88 #ifndef STDIN_FILENO 89 #define STDIN_FILENO 0 90 #endif 91 92 private const char * 93 get_default_magic(void) 94 { 95 static const char hmagic[] = "/.magic/magic.mgc"; 96 static char *default_magic; 97 char *home, *hmagicpath; 98 99 #ifndef WIN32 100 struct stat st; 101 102 if (default_magic) { 103 free(default_magic); 104 default_magic = NULL; 105 } 106 if ((home = getenv("HOME")) == NULL) 107 return MAGIC; 108 109 if (asprintf(&hmagicpath, "%s/.magic.mgc", home) < 0) 110 return MAGIC; 111 if (stat(hmagicpath, &st) == -1) { 112 free(hmagicpath); 113 if (asprintf(&hmagicpath, "%s/.magic", home) < 0) 114 return MAGIC; 115 if (stat(hmagicpath, &st) == -1) 116 goto out; 117 if (S_ISDIR(st.st_mode)) { 118 free(hmagicpath); 119 if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0) 120 return MAGIC; 121 if (access(hmagicpath, R_OK) == -1) 122 goto out; 123 } 124 } 125 126 if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0) 127 goto out; 128 free(hmagicpath); 129 return default_magic; 130 out: 131 default_magic = NULL; 132 free(hmagicpath); 133 return MAGIC; 134 #else 135 char *hmagicp; 136 char *tmppath = NULL; 137 LPTSTR dllpath; 138 hmagicpath = NULL; 139 140 #define APPENDPATH() \ 141 do { \ 142 if (tmppath && access(tmppath, R_OK) != -1) { \ 143 if (hmagicpath == NULL) \ 144 hmagicpath = tmppath; \ 145 else { \ 146 if (asprintf(&hmagicp, "%s%c%s", hmagicpath, \ 147 PATHSEP, tmppath) >= 0) { \ 148 free(hmagicpath); \ 149 hmagicpath = hmagicp; \ 150 } \ 151 free(tmppath); \ 152 } \ 153 tmppath = NULL; \ 154 } \ 155 } while (/*CONSTCOND*/0) 156 157 if (default_magic) { 158 free(default_magic); 159 default_magic = NULL; 160 } 161 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, 167 hmagic) < 0) 168 tmppath = NULL; 169 } else { 170 if (asprintf(&tmppath, "%s%s", home, hmagic) < 0) 171 tmppath = NULL; 172 } 173 174 APPENDPATH(); 175 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) 179 APPENDPATH(); 180 } 181 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) 191 APPENDPATH(); 192 } else { 193 if (asprintf(&tmppath, 194 "%s/share/misc/magic.mgc", dllpath) >= 0) 195 APPENDPATH(); 196 else if (asprintf(&tmppath, 197 "%s/magic.mgc", dllpath) >= 0) 198 APPENDPATH(); 199 } 200 } 201 202 /* Don't put MAGIC constant - it likely points to a file within MSys 203 tree */ 204 default_magic = hmagicpath; 205 return default_magic; 206 #endif 207 } 208 209 public const char * 210 magic_getpath(const char *magicfile, int action) 211 { 212 if (magicfile != NULL) 213 return magicfile; 214 215 magicfile = getenv("MAGIC"); 216 if (magicfile != NULL) 217 return magicfile; 218 219 return action == FILE_LOAD ? get_default_magic() : MAGIC; 220 } 221 222 public struct magic_set * 223 magic_open(int flags) 224 { 225 return file_ms_alloc(flags); 226 } 227 228 private int 229 unreadable_info(struct magic_set *ms, mode_t md, const char *file) 230 { 231 if (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) 235 return -1; 236 if (access(file, X_OK) == 0) 237 if (file_printf(ms, "executable, ") == -1) 238 return -1; 239 } 240 if (S_ISREG(md)) 241 if (file_printf(ms, "regular file, ") == -1) 242 return -1; 243 if (file_printf(ms, "no read permission") == -1) 244 return -1; 245 return 0; 246 } 247 248 public void 249 magic_close(struct magic_set *ms) 250 { 251 if (ms == NULL) 252 return; 253 file_ms_free(ms); 254 } 255 256 /* 257 * load a magic file 258 */ 259 public int 260 magic_load(struct magic_set *ms, const char *magicfile) 261 { 262 if (ms == NULL) 263 return -1; 264 return file_apprentice(ms, magicfile, FILE_LOAD); 265 } 266 267 #ifndef COMPILE_ONLY 268 /* 269 * Install a set of compiled magic buffers. 270 */ 271 public int 272 magic_load_buffers(struct magic_set *ms, void **bufs, size_t *sizes, 273 size_t nbufs) 274 { 275 if (ms == NULL) 276 return -1; 277 return buffer_apprentice(ms, (struct magic **)bufs, sizes, nbufs); 278 } 279 #endif 280 281 public int 282 magic_compile(struct magic_set *ms, const char *magicfile) 283 { 284 if (ms == NULL) 285 return -1; 286 return file_apprentice(ms, magicfile, FILE_COMPILE); 287 } 288 289 public int 290 magic_check(struct magic_set *ms, const char *magicfile) 291 { 292 if (ms == NULL) 293 return -1; 294 return file_apprentice(ms, magicfile, FILE_CHECK); 295 } 296 297 public int 298 magic_list(struct magic_set *ms, const char *magicfile) 299 { 300 if (ms == NULL) 301 return -1; 302 return file_apprentice(ms, magicfile, FILE_LIST); 303 } 304 305 private void 306 close_and_restore(const struct magic_set *ms, const char *name, int fd, 307 const struct stat *sb) 308 { 309 if (fd == STDIN_FILENO || name == NULL) 310 return; 311 (void) close(fd); 312 313 if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) { 314 /* 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 318 * backup programs 319 */ 320 #ifdef HAVE_UTIMES 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; 325 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; 329 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 */ 334 #endif 335 } 336 } 337 338 #ifndef COMPILE_ONLY 339 340 /* 341 * find type of descriptor 342 */ 343 public const char * 344 magic_descriptor(struct magic_set *ms, int fd) 345 { 346 if (ms == NULL) 347 return NULL; 348 return file_or_fd(ms, NULL, fd); 349 } 350 351 /* 352 * find type of named file 353 */ 354 public const char * 355 magic_file(struct magic_set *ms, const char *inname) 356 { 357 if (ms == NULL) 358 return NULL; 359 return file_or_fd(ms, inname, STDIN_FILENO); 360 } 361 362 private const char * 363 file_or_fd(struct magic_set *ms, const char *inname, int fd) 364 { 365 int rv = -1; 366 unsigned char *buf; 367 struct stat sb; 368 ssize_t nbytes = 0; /* number of bytes read from a datafile */ 369 int ispipe = 0; 370 off_t pos = (off_t)-1; 371 372 if (file_reset(ms) == -1) 373 goto out; 374 375 /* 376 * one extra for terminating '\0', and 377 * some overlapping space for matches near EOF 378 */ 379 #define SLOP (1 + sizeof(union VALUETYPE)) 380 if ((buf = CAST(unsigned char *, malloc(HOWMANY + SLOP))) == NULL) 381 return NULL; 382 383 switch (file_fsmagic(ms, inname, &sb)) { 384 case -1: /* error */ 385 goto done; 386 case 0: /* nothing found */ 387 break; 388 default: /* matched it and printed type */ 389 rv = 0; 390 goto done; 391 } 392 393 #ifdef WIN32 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); 397 #endif 398 399 if (inname == NULL) { 400 if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode)) 401 ispipe = 1; 402 else 403 pos = lseek(fd, (off_t)0, SEEK_CUR); 404 } else { 405 int flags = O_RDONLY|O_BINARY; 406 int okstat = stat(inname, &sb) == 0; 407 408 if (okstat && S_ISFIFO(sb.st_mode)) { 409 #ifdef O_NONBLOCK 410 flags |= O_NONBLOCK; 411 #endif 412 ispipe = 1; 413 } 414 415 errno = 0; 416 if ((fd = open(inname, flags)) < 0) { 417 #ifdef WIN32 418 /* 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. 423 */ 424 if (!okstat && errno == EACCES) { 425 sb.st_mode = S_IFBLK; 426 okstat = 1; 427 } 428 #endif 429 if (okstat && 430 unreadable_info(ms, sb.st_mode, inname) == -1) 431 goto done; 432 rv = 0; 433 goto done; 434 } 435 #ifdef O_NONBLOCK 436 if ((flags = fcntl(fd, F_GETFL)) != -1) { 437 flags &= ~O_NONBLOCK; 438 (void)fcntl(fd, F_SETFL, flags); 439 } 440 #endif 441 } 442 443 /* 444 * try looking at the first HOWMANY bytes 445 */ 446 if (ispipe) { 447 ssize_t r = 0; 448 449 while ((r = sread(fd, (void *)&buf[nbytes], 450 (size_t)(HOWMANY - nbytes), 1)) > 0) { 451 nbytes += r; 452 if (r < PIPE_BUF) break; 453 } 454 455 if (nbytes == 0) { 456 /* We can not read it, but we were able to stat it. */ 457 if (unreadable_info(ms, sb.st_mode, inname) == -1) 458 goto done; 459 rv = 0; 460 goto done; 461 } 462 463 } else { 464 /* Windows refuses to read from a big console buffer. */ 465 size_t howmany = 466 #if defined(WIN32) && HOWMANY > 8 * 1024 467 _isatty(fd) ? 8 * 1024 : 468 #endif 469 HOWMANY; 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); 473 else 474 file_error(ms, errno, "cannot read `%s'", 475 inname == NULL ? "/dev/stdin" : inname); 476 goto done; 477 } 478 } 479 480 (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */ 481 if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1) 482 goto done; 483 rv = 0; 484 done: 485 free(buf); 486 if (pos != (off_t)-1) 487 (void)lseek(fd, pos, SEEK_SET); 488 close_and_restore(ms, inname, fd, &sb); 489 out: 490 return rv == 0 ? file_getbuffer(ms) : NULL; 491 } 492 493 494 public const char * 495 magic_buffer(struct magic_set *ms, const void *buf, size_t nb) 496 { 497 if (ms == NULL) 498 return NULL; 499 if (file_reset(ms) == -1) 500 return NULL; 501 /* 502 * The main work is done here! 503 * We have the file name and/or the data buffer to be identified. 504 */ 505 if (file_buffer(ms, -1, NULL, buf, nb) == -1) { 506 return NULL; 507 } 508 return file_getbuffer(ms); 509 } 510 #endif 511 512 public const char * 513 magic_error(struct magic_set *ms) 514 { 515 if (ms == NULL) 516 return "Magic database is not open"; 517 return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL; 518 } 519 520 public int 521 magic_errno(struct magic_set *ms) 522 { 523 if (ms == NULL) 524 return EINVAL; 525 return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0; 526 } 527 528 public int 529 magic_setflags(struct magic_set *ms, int flags) 530 { 531 if (ms == NULL) 532 return -1; 533 #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES) 534 if (flags & MAGIC_PRESERVE_ATIME) 535 return -1; 536 #endif 537 ms->flags = flags; 538 return 0; 539 } 540 541 public int 542 magic_version(void) 543 { 544 return MAGIC_VERSION; 545 } 546 547 public int 548 magic_setparam(struct magic_set *ms, int param, const void *val) 549 { 550 switch (param) { 551 case MAGIC_PARAM_INDIR_MAX: 552 ms->indir_max = (uint16_t)*(const size_t *)val; 553 return 0; 554 case MAGIC_PARAM_NAME_MAX: 555 ms->name_max = (uint16_t)*(const size_t *)val; 556 return 0; 557 case MAGIC_PARAM_ELF_PHNUM_MAX: 558 ms->elf_phnum_max = (uint16_t)*(const size_t *)val; 559 return 0; 560 case MAGIC_PARAM_ELF_SHNUM_MAX: 561 ms->elf_shnum_max = (uint16_t)*(const size_t *)val; 562 return 0; 563 case MAGIC_PARAM_ELF_NOTES_MAX: 564 ms->elf_notes_max = (uint16_t)*(const size_t *)val; 565 return 0; 566 default: 567 errno = EINVAL; 568 return -1; 569 } 570 } 571 572 public int 573 magic_getparam(struct magic_set *ms, int param, void *val) 574 { 575 switch (param) { 576 case MAGIC_PARAM_INDIR_MAX: 577 *(size_t *)val = ms->indir_max; 578 return 0; 579 case MAGIC_PARAM_NAME_MAX: 580 *(size_t *)val = ms->name_max; 581 return 0; 582 case MAGIC_PARAM_ELF_PHNUM_MAX: 583 *(size_t *)val = ms->elf_phnum_max; 584 return 0; 585 case MAGIC_PARAM_ELF_SHNUM_MAX: 586 *(size_t *)val = ms->elf_shnum_max; 587 return 0; 588 case MAGIC_PARAM_ELF_NOTES_MAX: 589 *(size_t *)val = ms->elf_notes_max; 590 return 0; 591 default: 592 errno = EINVAL; 593 return -1; 594 } 595 } 596