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