magic.c revision 1.1.1.2
1/* $NetBSD: magic.c,v 1.1.1.2 2011/05/12 20:46:50 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.73 2011/05/10 17:08:14 christos Exp $") 40#else 41__RCSID("$NetBSD: magic.c,v 1.1.1.2 2011/05/12 20:46:50 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 80private void free_mlist(struct mlist *); 81private void close_and_restore(const struct magic_set *, const char *, int, 82 const struct stat *); 83private int unreadable_info(struct magic_set *, mode_t, const char *); 84private const char* get_default_magic(void); 85#ifndef COMPILE_ONLY 86private const char *file_or_fd(struct magic_set *, const char *, int); 87#endif 88 89#ifndef STDIN_FILENO 90#define STDIN_FILENO 0 91#endif 92 93#ifdef WIN32 94BOOL WINAPI DllMain(HINSTANCE hinstDLL, 95 DWORD fdwReason __attribute__((__unused__)), 96 LPVOID lpvReserved __attribute__((__unused__))); 97 98CHAR dllpath[MAX_PATH + 1] = { 0 }; 99 100BOOL WINAPI DllMain(HINSTANCE hinstDLL, 101 DWORD fdwReason __attribute__((__unused__)), 102 LPVOID lpvReserved __attribute__((__unused__))) 103{ 104 if (dllpath[0] == 0 && 105 GetModuleFileNameA(hinstDLL, dllpath, MAX_PATH) != 0) 106 PathRemoveFileSpecA(dllpath); 107 return TRUE; 108} 109#endif 110 111private const char * 112get_default_magic(void) 113{ 114 static const char hmagic[] = "/.magic/magic.mgc"; 115 static char *default_magic; 116 char *home, *hmagicpath; 117 118#ifndef WIN32 119 struct stat st; 120 121 if (default_magic) { 122 free(default_magic); 123 default_magic = NULL; 124 } 125 if ((home = getenv("HOME")) == NULL) 126 return MAGIC; 127 128 if (asprintf(&hmagicpath, "%s/.magic", home) < 0) 129 return MAGIC; 130 if (stat(hmagicpath, &st) == -1) 131 goto out; 132 if (S_ISDIR(st.st_mode)) { 133 free(hmagicpath); 134 if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0) 135 return MAGIC; 136 if (access(hmagicpath, R_OK) == -1) 137 goto out; 138 } 139 140 if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0) 141 goto out; 142 free(hmagicpath); 143 return default_magic; 144out: 145 default_magic = NULL; 146 free(hmagicpath); 147 return MAGIC; 148#else 149 char *hmagicp = hmagicpath; 150 char *tmppath = NULL; 151 152#define APPENDPATH() \ 153 do { \ 154 if (tmppath && access(tmppath, R_OK) != -1) { \ 155 if (hmagicpath == NULL) { \ 156 hmagicpath = tmppath; \ 157 tmppath = NULL; \ 158 } else { \ 159 free(tmppath); \ 160 if (asprintf(&hmagicp, "%s%c%s", hmagicpath, \ 161 PATHSEP, tmppath) >= 0) { \ 162 free(hmagicpath); \ 163 hmagicpath = hmagicp; \ 164 } \ 165 } \ 166 } while (/*CONSTCOND*/0) 167 168 if (default_magic) { 169 free(default_magic); 170 default_magic = NULL; 171 } 172 173 /* First, try to get user-specific magic file */ 174 if ((home = getenv("LOCALAPPDATA")) == NULL) { 175 if ((home = getenv("USERPROFILE")) != NULL) 176 if (asprintf(&tmppath, 177 "%s/Local Settings/Application Data%s", home, 178 hmagic) < 0) 179 tmppath = NULL; 180 } else { 181 if (asprintf(&tmppath, "%s%s", home, hmagic) < 0) 182 tmppath = NULL; 183 } 184 185 APPENDPATH(); 186 187 /* Second, try to get a magic file from Common Files */ 188 if ((home = getenv("COMMONPROGRAMFILES")) != NULL) { 189 if (asprintf(&tmppath, "%s%s", home, hmagic) >= 0) 190 APPENDPATH(); 191 } 192 193 194 /* Third, try to get magic file relative to dll location */ 195 if (dllpath[0] != 0) { 196 if (strlen(dllpath) > 3 && 197 stricmp(&dllpath[strlen(dllpath) - 3], "bin") == 0) { 198 if (asprintf(&tmppath, 199 "%s/../share/misc/magic.mgc", dllpath) >= 0) 200 APPENDPATH(); 201 } else { 202 if (asprintf(&tmppath, 203 "%s/share/misc/magic.mgc", dllpath) >= 0) 204 APPENDPATH(); 205 else if (asprintf(&tmppath, 206 "%s/magic.mgc", dllpath) >= 0) 207 APPENDPATH(); 208 } 209 } 210 211 /* Don't put MAGIC constant - it likely points to a file within MSys 212 tree */ 213 default_magic = hmagicpath; 214 return default_magic; 215#endif 216} 217 218public const char * 219magic_getpath(const char *magicfile, int action) 220{ 221 if (magicfile != NULL) 222 return magicfile; 223 224 magicfile = getenv("MAGIC"); 225 if (magicfile != NULL) 226 return magicfile; 227 228 return action == FILE_LOAD ? get_default_magic() : MAGIC; 229} 230 231public struct magic_set * 232magic_open(int flags) 233{ 234 struct magic_set *ms; 235 size_t len; 236 237 if ((ms = CAST(struct magic_set *, calloc((size_t)1, 238 sizeof(struct magic_set)))) == NULL) 239 return NULL; 240 241 if (magic_setflags(ms, flags) == -1) { 242 errno = EINVAL; 243 goto free; 244 } 245 246 ms->o.buf = ms->o.pbuf = NULL; 247 len = (ms->c.len = 10) * sizeof(*ms->c.li); 248 249 if ((ms->c.li = CAST(struct level_info *, malloc(len))) == NULL) 250 goto free; 251 252 ms->event_flags = 0; 253 ms->error = -1; 254 ms->mlist = NULL; 255 ms->file = "unknown"; 256 ms->line = 0; 257 return ms; 258free: 259 free(ms); 260 return NULL; 261} 262 263private void 264free_mlist(struct mlist *mlist) 265{ 266 struct mlist *ml; 267 268 if (mlist == NULL) 269 return; 270 271 for (ml = mlist->next; ml != mlist;) { 272 struct mlist *next = ml->next; 273 struct magic *mg = ml->magic; 274 file_delmagic(mg, ml->mapped, ml->nmagic); 275 free(ml); 276 ml = next; 277 } 278 free(ml); 279} 280 281private int 282unreadable_info(struct magic_set *ms, mode_t md, const char *file) 283{ 284 /* We cannot open it, but we were able to stat it. */ 285 if (access(file, W_OK) == 0) 286 if (file_printf(ms, "writable, ") == -1) 287 return -1; 288 if (access(file, X_OK) == 0) 289 if (file_printf(ms, "executable, ") == -1) 290 return -1; 291 if (S_ISREG(md)) 292 if (file_printf(ms, "regular file, ") == -1) 293 return -1; 294 if (file_printf(ms, "no read permission") == -1) 295 return -1; 296 return 0; 297} 298 299public void 300magic_close(struct magic_set *ms) 301{ 302 free_mlist(ms->mlist); 303 free(ms->o.pbuf); 304 free(ms->o.buf); 305 free(ms->c.li); 306 free(ms); 307} 308 309/* 310 * load a magic file 311 */ 312public int 313magic_load(struct magic_set *ms, const char *magicfile) 314{ 315 struct mlist *ml = file_apprentice(ms, magicfile, FILE_LOAD); 316 if (ml) { 317 free_mlist(ms->mlist); 318 ms->mlist = ml; 319 return 0; 320 } 321 return -1; 322} 323 324public int 325magic_compile(struct magic_set *ms, const char *magicfile) 326{ 327 struct mlist *ml = file_apprentice(ms, magicfile, FILE_COMPILE); 328 free_mlist(ml); 329 return ml ? 0 : -1; 330} 331 332public int 333magic_check(struct magic_set *ms, const char *magicfile) 334{ 335 struct mlist *ml = file_apprentice(ms, magicfile, FILE_CHECK); 336 free_mlist(ml); 337 return ml ? 0 : -1; 338} 339 340public int 341magic_list(struct magic_set *ms, const char *magicfile) 342{ 343 struct mlist *ml = file_apprentice(ms, magicfile, FILE_LIST); 344 free_mlist(ml); 345 return ml ? 0 : -1; 346} 347 348private void 349close_and_restore(const struct magic_set *ms, const char *name, int fd, 350 const struct stat *sb) 351{ 352 if (fd == STDIN_FILENO) 353 return; 354 (void) close(fd); 355 356 if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) { 357 /* 358 * Try to restore access, modification times if read it. 359 * This is really *bad* because it will modify the status 360 * time of the file... And of course this will affect 361 * backup programs 362 */ 363#ifdef HAVE_UTIMES 364 struct timeval utsbuf[2]; 365 (void)memset(utsbuf, 0, sizeof(utsbuf)); 366 utsbuf[0].tv_sec = sb->st_atime; 367 utsbuf[1].tv_sec = sb->st_mtime; 368 369 (void) utimes(name, utsbuf); /* don't care if loses */ 370#elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H) 371 struct utimbuf utbuf; 372 373 (void)memset(&utbuf, 0, sizeof(utbuf)); 374 utbuf.actime = sb->st_atime; 375 utbuf.modtime = sb->st_mtime; 376 (void) utime(name, &utbuf); /* don't care if loses */ 377#endif 378 } 379} 380 381#ifndef COMPILE_ONLY 382 383/* 384 * find type of descriptor 385 */ 386public const char * 387magic_descriptor(struct magic_set *ms, int fd) 388{ 389 return file_or_fd(ms, NULL, fd); 390} 391 392/* 393 * find type of named file 394 */ 395public const char * 396magic_file(struct magic_set *ms, const char *inname) 397{ 398 return file_or_fd(ms, inname, STDIN_FILENO); 399} 400 401private const char * 402file_or_fd(struct magic_set *ms, const char *inname, int fd) 403{ 404 int rv = -1; 405 unsigned char *buf; 406 struct stat sb; 407 ssize_t nbytes = 0; /* number of bytes read from a datafile */ 408 int ispipe = 0; 409 410 /* 411 * one extra for terminating '\0', and 412 * some overlapping space for matches near EOF 413 */ 414#define SLOP (1 + sizeof(union VALUETYPE)) 415 if ((buf = CAST(unsigned char *, malloc(HOWMANY + SLOP))) == NULL) 416 return NULL; 417 418 if (file_reset(ms) == -1) 419 goto done; 420 421 switch (file_fsmagic(ms, inname, &sb)) { 422 case -1: /* error */ 423 goto done; 424 case 0: /* nothing found */ 425 break; 426 default: /* matched it and printed type */ 427 rv = 0; 428 goto done; 429 } 430 431 if (inname == NULL) { 432 if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode)) 433 ispipe = 1; 434 } else { 435 int flags = O_RDONLY|O_BINARY; 436 437 if (stat(inname, &sb) == 0 && S_ISFIFO(sb.st_mode)) { 438#ifdef O_NONBLOCK 439 flags |= O_NONBLOCK; 440#endif 441 ispipe = 1; 442 } 443 444 errno = 0; 445 if ((fd = open(inname, flags)) < 0) { 446 if (unreadable_info(ms, sb.st_mode, inname) == -1) 447 goto done; 448 rv = 0; 449 goto done; 450 } 451#ifdef O_NONBLOCK 452 if ((flags = fcntl(fd, F_GETFL)) != -1) { 453 flags &= ~O_NONBLOCK; 454 (void)fcntl(fd, F_SETFL, flags); 455 } 456#endif 457 } 458 459 /* 460 * try looking at the first HOWMANY bytes 461 */ 462 if (ispipe) { 463 ssize_t r = 0; 464 465 while ((r = sread(fd, (void *)&buf[nbytes], 466 (size_t)(HOWMANY - nbytes), 1)) > 0) { 467 nbytes += r; 468 if (r < PIPE_BUF) break; 469 } 470 471 if (nbytes == 0) { 472 /* We can not read it, but we were able to stat it. */ 473 if (unreadable_info(ms, sb.st_mode, inname) == -1) 474 goto done; 475 rv = 0; 476 goto done; 477 } 478 479 } else { 480 if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) { 481 file_error(ms, errno, "cannot read `%s'", inname); 482 goto done; 483 } 484 } 485 486 (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */ 487 if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1) 488 goto done; 489 rv = 0; 490done: 491 free(buf); 492 close_and_restore(ms, inname, fd, &sb); 493 return rv == 0 ? file_getbuffer(ms) : NULL; 494} 495 496 497public const char * 498magic_buffer(struct magic_set *ms, const void *buf, size_t nb) 499{ 500 if (file_reset(ms) == -1) 501 return NULL; 502 /* 503 * The main work is done here! 504 * We have the file name and/or the data buffer to be identified. 505 */ 506 if (file_buffer(ms, -1, NULL, buf, nb) == -1) { 507 return NULL; 508 } 509 return file_getbuffer(ms); 510} 511#endif 512 513public const char * 514magic_error(struct magic_set *ms) 515{ 516 return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL; 517} 518 519public int 520magic_errno(struct magic_set *ms) 521{ 522 return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0; 523} 524 525public int 526magic_setflags(struct magic_set *ms, int flags) 527{ 528#if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES) 529 if (flags & MAGIC_PRESERVE_ATIME) 530 return -1; 531#endif 532 ms->flags = flags; 533 return 0; 534} 535