1 /* $NetBSD: file.c,v 1.11 2018/04/18 01:18:16 christos Exp $ */ 2 3 /* 4 * Copyright (c) Ian F. Darwin 1986-1995. 5 * Software written by Ian F. Darwin and others; 6 * maintained 1995-present by Christos Zoulas and others. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice immediately at the beginning of the file, without modification, 13 * this list of conditions, and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 /* 31 * file - find type of a file or files - main program. 32 */ 33 34 #include "file.h" 35 36 #ifndef lint 37 #if 0 38 FILE_RCSID("@(#)$File: file.c,v 1.175 2018/03/02 16:11:37 christos Exp $") 39 #else 40 __RCSID("$NetBSD: file.c,v 1.11 2018/04/18 01:18:16 christos Exp $"); 41 #endif 42 #endif /* lint */ 43 44 #include "magic.h" 45 46 #include <stdlib.h> 47 #include <unistd.h> 48 #include <string.h> 49 #ifdef RESTORE_TIME 50 # if (__COHERENT__ >= 0x420) 51 # include <sys/utime.h> 52 # else 53 # ifdef USE_UTIMES 54 # include <sys/time.h> 55 # else 56 # include <utime.h> 57 # endif 58 # endif 59 #endif 60 #ifdef HAVE_UNISTD_H 61 #include <unistd.h> /* for read() */ 62 #endif 63 #ifdef HAVE_WCHAR_H 64 #include <wchar.h> 65 #endif 66 67 #if defined(HAVE_GETOPT_H) && defined(HAVE_STRUCT_OPTION) 68 # include <getopt.h> 69 # ifndef HAVE_GETOPT_LONG 70 int getopt_long(int, char * const *, const char *, 71 const struct option *, int *); 72 # endif 73 # else 74 # include "mygetopt.h" 75 #endif 76 77 #ifdef S_IFLNK 78 # define IFLNK_h "h" 79 # define IFLNK_L "L" 80 #else 81 # define IFLNK_h "" 82 # define IFLNK_L "" 83 #endif 84 85 #ifdef HAVE_LIBSECCOMP 86 # define SECCOMP_S "S" 87 #else 88 # define SECCOMP_S "" 89 #endif 90 91 #define FILE_FLAGS "bcCdE" IFLNK_h "ik" IFLNK_L "lNnprs" SECCOMP_S "vzZ0" 92 #define OPTSTRING "bcCde:Ef:F:hiklLm:nNpP:rsSvzZ0" 93 94 # define USAGE \ 95 "Usage: %s [-" FILE_FLAGS "] [--apple] [--extension] [--mime-encoding]\n" \ 96 " [--mime-type] [-e <testname>] [-F <separator>] " \ 97 " [-f <namefile>]\n" \ 98 " [-m <magicfiles>] [-P <parameter=value>] <file> ...\n" \ 99 " %s -C [-m <magicfiles>]\n" \ 100 " %s [--help]\n" 101 102 private int /* Global command-line options */ 103 bflag = 0, /* brief output format */ 104 nopad = 0, /* Don't pad output */ 105 nobuffer = 0, /* Do not buffer stdout */ 106 nulsep = 0; /* Append '\0' to the separator */ 107 108 private const char *separator = ":"; /* Default field separator */ 109 private const struct option long_options[] = { 110 #define OPT_HELP 1 111 #define OPT_APPLE 2 112 #define OPT_EXTENSIONS 3 113 #define OPT_MIME_TYPE 4 114 #define OPT_MIME_ENCODING 5 115 #define OPT(shortname, longname, opt, def, doc) \ 116 {longname, opt, NULL, shortname}, 117 #define OPT_LONGONLY(longname, opt, def, doc, id) \ 118 {longname, opt, NULL, id}, 119 #include "file_opts.h" 120 #undef OPT 121 #undef OPT_LONGONLY 122 {0, 0, NULL, 0} 123 }; 124 125 private const struct { 126 const char *name; 127 int value; 128 } nv[] = { 129 { "apptype", MAGIC_NO_CHECK_APPTYPE }, 130 { "ascii", MAGIC_NO_CHECK_ASCII }, 131 { "cdf", MAGIC_NO_CHECK_CDF }, 132 { "compress", MAGIC_NO_CHECK_COMPRESS }, 133 { "elf", MAGIC_NO_CHECK_ELF }, 134 { "encoding", MAGIC_NO_CHECK_ENCODING }, 135 { "soft", MAGIC_NO_CHECK_SOFT }, 136 { "tar", MAGIC_NO_CHECK_TAR }, 137 { "text", MAGIC_NO_CHECK_TEXT }, /* synonym for ascii */ 138 { "tokens", MAGIC_NO_CHECK_TOKENS }, /* OBSOLETE: ignored for backwards compatibility */ 139 }; 140 141 private struct { 142 const char *name; 143 int tag; 144 size_t value; 145 } pm[] = { 146 { "indir", MAGIC_PARAM_INDIR_MAX, 0 }, 147 { "name", MAGIC_PARAM_NAME_MAX, 0 }, 148 { "elf_phnum", MAGIC_PARAM_ELF_PHNUM_MAX, 0 }, 149 { "elf_shnum", MAGIC_PARAM_ELF_SHNUM_MAX, 0 }, 150 { "elf_notes", MAGIC_PARAM_ELF_NOTES_MAX, 0 }, 151 { "regex", MAGIC_PARAM_REGEX_MAX, 0 }, 152 { "bytes", MAGIC_PARAM_BYTES_MAX, 0 }, 153 }; 154 155 private int posixly; 156 157 #ifdef __dead 158 __dead 159 #endif 160 private void usage(void); 161 private void docprint(const char *, int); 162 #ifdef __dead 163 __dead 164 #endif 165 private void help(void); 166 167 private int unwrap(struct magic_set *, const char *); 168 private int process(struct magic_set *ms, const char *, int); 169 private struct magic_set *load(const char *, int); 170 private void setparam(const char *); 171 private void applyparam(magic_t); 172 173 174 /* 175 * main - parse arguments and handle options 176 */ 177 int 178 main(int argc, char *argv[]) 179 { 180 int c; 181 size_t i; 182 int action = 0, didsomefiles = 0, errflg = 0; 183 int flags = 0, e = 0; 184 #ifdef HAVE_LIBSECCOMP 185 int sandbox = 1; 186 #endif 187 struct magic_set *magic = NULL; 188 int longindex; 189 const char *magicfile = NULL; /* where the magic is */ 190 char *progname; 191 192 /* makes islower etc work for other langs */ 193 #ifdef HAVE_SETLOCALE 194 (void)setlocale(LC_CTYPE, ""); 195 #endif 196 197 #ifdef __EMX__ 198 /* sh-like wildcard expansion! Shouldn't hurt at least ... */ 199 _wildcard(&argc, &argv); 200 #endif 201 202 if ((progname = strrchr(argv[0], '/')) != NULL) 203 progname++; 204 else 205 progname = argv[0]; 206 207 file_setprogname(progname); 208 209 210 #ifdef S_IFLNK 211 posixly = getenv("POSIXLY_CORRECT") != NULL; 212 flags |= posixly ? MAGIC_SYMLINK : 0; 213 #endif 214 while ((c = getopt_long(argc, argv, OPTSTRING, long_options, 215 &longindex)) != -1) 216 switch (c) { 217 case OPT_HELP: 218 help(); 219 break; 220 case OPT_APPLE: 221 flags |= MAGIC_APPLE; 222 break; 223 case OPT_EXTENSIONS: 224 flags |= MAGIC_EXTENSION; 225 break; 226 case OPT_MIME_TYPE: 227 flags |= MAGIC_MIME_TYPE; 228 break; 229 case OPT_MIME_ENCODING: 230 flags |= MAGIC_MIME_ENCODING; 231 break; 232 case '0': 233 nulsep++; 234 break; 235 case 'b': 236 bflag++; 237 break; 238 case 'c': 239 action = FILE_CHECK; 240 break; 241 case 'C': 242 action = FILE_COMPILE; 243 break; 244 case 'd': 245 flags |= MAGIC_DEBUG|MAGIC_CHECK; 246 break; 247 case 'E': 248 flags |= MAGIC_ERROR; 249 break; 250 case 'e': 251 for (i = 0; i < sizeof(nv) / sizeof(nv[0]); i++) 252 if (strcmp(nv[i].name, optarg) == 0) 253 break; 254 255 if (i == sizeof(nv) / sizeof(nv[0])) 256 errflg++; 257 else 258 flags |= nv[i].value; 259 break; 260 261 case 'f': 262 if(action) 263 usage(); 264 if (magic == NULL) 265 if ((magic = load(magicfile, flags)) == NULL) 266 return 1; 267 applyparam(magic); 268 e |= unwrap(magic, optarg); 269 ++didsomefiles; 270 break; 271 case 'F': 272 separator = optarg; 273 break; 274 case 'i': 275 flags |= MAGIC_MIME; 276 break; 277 case 'k': 278 flags |= MAGIC_CONTINUE; 279 break; 280 case 'l': 281 action = FILE_LIST; 282 break; 283 case 'm': 284 magicfile = optarg; 285 break; 286 case 'n': 287 ++nobuffer; 288 break; 289 case 'N': 290 ++nopad; 291 break; 292 #if defined(HAVE_UTIME) || defined(HAVE_UTIMES) 293 case 'p': 294 flags |= MAGIC_PRESERVE_ATIME; 295 break; 296 #endif 297 case 'P': 298 setparam(optarg); 299 break; 300 case 'r': 301 flags |= MAGIC_RAW; 302 break; 303 case 's': 304 flags |= MAGIC_DEVICES; 305 break; 306 #ifdef HAVE_LIBSECCOMP 307 case 'S': 308 sandbox = 0; 309 break; 310 #endif 311 case 'v': 312 if (magicfile == NULL) 313 magicfile = magic_getpath(magicfile, action); 314 (void)fprintf(stdout, "%s-%s\n", file_getprogname(), 315 VERSION); 316 (void)fprintf(stdout, "magic file from %s\n", 317 magicfile); 318 return 0; 319 case 'z': 320 flags |= MAGIC_COMPRESS; 321 break; 322 323 case 'Z': 324 flags |= MAGIC_COMPRESS|MAGIC_COMPRESS_TRANSP; 325 break; 326 #ifdef S_IFLNK 327 case 'L': 328 flags |= MAGIC_SYMLINK; 329 break; 330 case 'h': 331 flags &= ~MAGIC_SYMLINK; 332 break; 333 #endif 334 case '?': 335 default: 336 errflg++; 337 break; 338 } 339 340 if (errflg) { 341 usage(); 342 } 343 if (e) 344 return e; 345 346 #ifdef HAVE_LIBSECCOMP 347 #if 0 348 if (sandbox && enable_sandbox_basic() == -1) 349 #else 350 if (sandbox && enable_sandbox_full() == -1) 351 #endif 352 file_err(EXIT_FAILURE, "SECCOMP initialisation failed"); 353 #endif /* HAVE_LIBSECCOMP */ 354 355 if (MAGIC_VERSION != magic_version()) 356 file_warnx("Compiled magic version [%d] " 357 "does not match with shared library magic version [%d]\n", 358 MAGIC_VERSION, magic_version()); 359 360 switch(action) { 361 case FILE_CHECK: 362 case FILE_COMPILE: 363 case FILE_LIST: 364 /* 365 * Don't try to check/compile ~/.magic unless we explicitly 366 * ask for it. 367 */ 368 magic = magic_open(flags|MAGIC_CHECK); 369 if (magic == NULL) { 370 file_warn("Can't create magic"); 371 return 1; 372 } 373 374 375 switch(action) { 376 case FILE_CHECK: 377 c = magic_check(magic, magicfile); 378 break; 379 case FILE_COMPILE: 380 c = magic_compile(magic, magicfile); 381 break; 382 case FILE_LIST: 383 c = magic_list(magic, magicfile); 384 break; 385 default: 386 abort(); 387 } 388 if (c == -1) { 389 file_warnx("%s", magic_error(magic)); 390 e = 1; 391 goto out; 392 } 393 goto out; 394 default: 395 if (magic == NULL) 396 if ((magic = load(magicfile, flags)) == NULL) 397 return 1; 398 applyparam(magic); 399 } 400 401 if (optind == argc) { 402 if (!didsomefiles) 403 usage(); 404 } 405 else { 406 size_t j, wid, nw; 407 for (wid = 0, j = (size_t)optind; j < (size_t)argc; j++) { 408 nw = file_mbswidth(argv[j]); 409 if (nw > wid) 410 wid = nw; 411 } 412 /* 413 * If bflag is only set twice, set it depending on 414 * number of files [this is undocumented, and subject to change] 415 */ 416 if (bflag == 2) { 417 bflag = optind >= argc - 1; 418 } 419 for (; optind < argc; optind++) 420 e |= process(magic, argv[optind], wid); 421 } 422 423 out: 424 if (magic) 425 magic_close(magic); 426 return e; 427 } 428 429 private void 430 applyparam(magic_t magic) 431 { 432 size_t i; 433 434 for (i = 0; i < __arraycount(pm); i++) { 435 if (pm[i].value == 0) 436 continue; 437 if (magic_setparam(magic, pm[i].tag, &pm[i].value) == -1) 438 file_err(EXIT_FAILURE, "Can't set %s", pm[i].name); 439 } 440 } 441 442 private void 443 setparam(const char *p) 444 { 445 size_t i; 446 char *s; 447 448 if ((s = strchr(p, '=')) == NULL) 449 goto badparm; 450 451 for (i = 0; i < __arraycount(pm); i++) { 452 if (strncmp(p, pm[i].name, s - p) != 0) 453 continue; 454 pm[i].value = atoi(s + 1); 455 return; 456 } 457 badparm: 458 file_errx(EXIT_FAILURE, "Unknown param %s", p); 459 } 460 461 private struct magic_set * 462 /*ARGSUSED*/ 463 load(const char *magicfile, int flags) 464 { 465 struct magic_set *magic = magic_open(flags); 466 const char *e; 467 468 if (magic == NULL) { 469 file_warn("Can't create magic"); 470 return NULL; 471 } 472 if (magic_load(magic, magicfile) == -1) { 473 file_warn("%s", magic_error(magic)); 474 magic_close(magic); 475 return NULL; 476 } 477 if ((e = magic_error(magic)) != NULL) 478 file_warn("%s", e); 479 return magic; 480 } 481 482 /* 483 * unwrap -- read a file of filenames, do each one. 484 */ 485 private int 486 unwrap(struct magic_set *ms, const char *fn) 487 { 488 FILE *f; 489 ssize_t len; 490 char *line = NULL; 491 size_t llen = 0; 492 int wid = 0, cwid; 493 int e = 0; 494 495 if (strcmp("-", fn) == 0) { 496 f = stdin; 497 wid = 1; 498 } else { 499 if ((f = fopen(fn, "r")) == NULL) { 500 file_warn("Cannot open `%s'", fn); 501 return 1; 502 } 503 504 while ((len = getline(&line, &llen, f)) > 0) { 505 if (line[len - 1] == '\n') 506 line[len - 1] = '\0'; 507 cwid = file_mbswidth(line); 508 if (cwid > wid) 509 wid = cwid; 510 } 511 512 rewind(f); 513 } 514 515 while ((len = getline(&line, &llen, f)) > 0) { 516 if (line[len - 1] == '\n') 517 line[len - 1] = '\0'; 518 e |= process(ms, line, wid); 519 if(nobuffer) 520 (void)fflush(stdout); 521 } 522 523 free(line); 524 (void)fclose(f); 525 return e; 526 } 527 528 /* 529 * Called for each input file on the command line (or in a list of files) 530 */ 531 private int 532 process(struct magic_set *ms, const char *inname, int wid) 533 { 534 const char *type, c = nulsep > 1 ? '\0' : '\n'; 535 int std_in = strcmp(inname, "-") == 0; 536 537 if (wid > 0 && !bflag) { 538 (void)printf("%s", std_in ? "/dev/stdin" : inname); 539 if (nulsep) 540 (void)putc('\0', stdout); 541 if (nulsep < 2) { 542 (void)printf("%s", separator); 543 (void)printf("%*s ", 544 (int) (nopad ? 0 : (wid - file_mbswidth(inname))), 545 ""); 546 } 547 } 548 549 type = magic_file(ms, std_in ? NULL : inname); 550 551 if (type == NULL) { 552 (void)printf("ERROR: %s%c", magic_error(ms), c); 553 return 1; 554 } else { 555 (void)printf("%s%c", type, c); 556 return 0; 557 } 558 } 559 560 protected size_t 561 file_mbswidth(const char *s) 562 { 563 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH) 564 size_t bytesconsumed, old_n, n, width = 0; 565 mbstate_t state; 566 wchar_t nextchar; 567 (void)memset(&state, 0, sizeof(mbstate_t)); 568 old_n = n = strlen(s); 569 570 while (n > 0) { 571 bytesconsumed = mbrtowc(&nextchar, s, n, &state); 572 if (bytesconsumed == (size_t)(-1) || 573 bytesconsumed == (size_t)(-2)) { 574 /* Something went wrong, return something reasonable */ 575 return old_n; 576 } 577 if (s[0] == '\n') { 578 /* 579 * do what strlen() would do, so that caller 580 * is always right 581 */ 582 width++; 583 } else { 584 int w = wcwidth(nextchar); 585 if (w > 0) 586 width += w; 587 } 588 589 s += bytesconsumed, n -= bytesconsumed; 590 } 591 return width; 592 #else 593 return strlen(s); 594 #endif 595 } 596 597 private void 598 usage(void) 599 { 600 const char *pn = file_getprogname(); 601 (void)fprintf(stderr, USAGE, pn, pn, pn); 602 exit(EXIT_FAILURE); 603 } 604 605 private void 606 defprint(int def) 607 { 608 if (!def) 609 return; 610 if (((def & 1) && posixly) || ((def & 2) && !posixly)) 611 fprintf(stdout, " (default)"); 612 fputc('\n', stdout); 613 } 614 615 private void 616 docprint(const char *opts, int def) 617 { 618 size_t i; 619 int comma; 620 char *sp, *p; 621 622 p = strstr(opts, "%o"); 623 if (p == NULL) { 624 fprintf(stdout, "%s", opts); 625 defprint(def); 626 return; 627 } 628 629 for (sp = p - 1; sp > opts && *sp == ' '; sp--) 630 continue; 631 632 fprintf(stdout, "%.*s", (int)(p - opts), opts); 633 634 comma = 0; 635 for (i = 0; i < __arraycount(nv); i++) { 636 fprintf(stdout, "%s%s", comma++ ? ", " : "", nv[i].name); 637 if (i && i % 5 == 0) { 638 fprintf(stdout, ",\n%*s", (int)(p - sp - 1), ""); 639 comma = 0; 640 } 641 } 642 643 fprintf(stdout, "%s", opts + (p - opts) + 2); 644 } 645 646 private void 647 help(void) 648 { 649 (void)fputs( 650 "Usage: file [OPTION...] [FILE...]\n" 651 "Determine type of FILEs.\n" 652 "\n", stdout); 653 #define OPT(shortname, longname, opt, def, doc) \ 654 fprintf(stdout, " -%c, --" longname, shortname), \ 655 docprint(doc, def); 656 #define OPT_LONGONLY(longname, opt, def, doc, id) \ 657 fprintf(stdout, " --" longname), \ 658 docprint(doc, def); 659 #include "file_opts.h" 660 #undef OPT 661 #undef OPT_LONGONLY 662 fprintf(stdout, "\nReport bugs to http://bugs.gw.com/\n"); 663 exit(EXIT_SUCCESS); 664 } 665 666 private const char *file_progname; 667 668 protected void 669 file_setprogname(const char *progname) 670 { 671 file_progname = progname; 672 } 673 674 protected const char * 675 file_getprogname(void) 676 { 677 return file_progname; 678 } 679 680 protected void 681 file_err(int e, const char *fmt, ...) 682 { 683 va_list ap; 684 int se = errno; 685 686 va_start(ap, fmt); 687 fprintf(stderr, "%s: ", file_progname); 688 vfprintf(stderr, fmt, ap); 689 va_end(ap); 690 fprintf(stderr, " (%s)\n", strerror(se)); 691 exit(e); 692 } 693 694 protected void 695 file_errx(int e, const char *fmt, ...) 696 { 697 va_list ap; 698 699 va_start(ap, fmt); 700 fprintf(stderr, "%s: ", file_progname); 701 vfprintf(stderr, fmt, ap); 702 va_end(ap); 703 fprintf(stderr, "\n"); 704 exit(e); 705 } 706 707 protected void 708 file_warn(const char *fmt, ...) 709 { 710 va_list ap; 711 int se = errno; 712 713 va_start(ap, fmt); 714 fprintf(stderr, "%s: ", file_progname); 715 vfprintf(stderr, fmt, ap); 716 va_end(ap); 717 fprintf(stderr, " (%s)\n", strerror(se)); 718 errno = se; 719 } 720 721 protected void 722 file_warnx(const char *fmt, ...) 723 { 724 va_list ap; 725 int se = errno; 726 727 va_start(ap, fmt); 728 fprintf(stderr, "%s: ", file_progname); 729 vfprintf(stderr, fmt, ap); 730 va_end(ap); 731 fprintf(stderr, "\n"); 732 errno = se; 733 } 734