fsmagic.c revision 186690
168349Sobrien/* 2133359Sobrien * Copyright (c) Ian F. Darwin 1986-1995. 3133359Sobrien * Software written by Ian F. Darwin and others; 4133359Sobrien * maintained 1995-present by Christos Zoulas and others. 5133359Sobrien * 6133359Sobrien * Redistribution and use in source and binary forms, with or without 7133359Sobrien * modification, are permitted provided that the following conditions 8133359Sobrien * are met: 9133359Sobrien * 1. Redistributions of source code must retain the above copyright 10133359Sobrien * notice immediately at the beginning of the file, without modification, 11133359Sobrien * this list of conditions, and the following disclaimer. 12133359Sobrien * 2. Redistributions in binary form must reproduce the above copyright 13133359Sobrien * notice, this list of conditions and the following disclaimer in the 14133359Sobrien * documentation and/or other materials provided with the distribution. 15133359Sobrien * 16133359Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17133359Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18133359Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19133359Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20133359Sobrien * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21133359Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22133359Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23133359Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24133359Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25133359Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26133359Sobrien * SUCH DAMAGE. 27133359Sobrien */ 28133359Sobrien/* 2968349Sobrien * fsmagic - magic based on filesystem info - directory, special files, etc. 3068349Sobrien */ 3168349Sobrien 3268349Sobrien#include "file.h" 33133359Sobrien#include "magic.h" 3468349Sobrien#include <string.h> 3568349Sobrien#ifdef HAVE_UNISTD_H 3668349Sobrien#include <unistd.h> 3768349Sobrien#endif 3868349Sobrien#include <stdlib.h> 39133359Sobrien#include <sys/stat.h> 40133359Sobrien/* Since major is a function on SVR4, we cannot use `ifndef major'. */ 4168349Sobrien#ifdef MAJOR_IN_MKDEV 4268349Sobrien# include <sys/mkdev.h> 4368349Sobrien# define HAVE_MAJOR 4468349Sobrien#endif 4568349Sobrien#ifdef MAJOR_IN_SYSMACROS 4668349Sobrien# include <sys/sysmacros.h> 4768349Sobrien# define HAVE_MAJOR 4868349Sobrien#endif 4968349Sobrien#ifdef major /* Might be defined in sys/types.h. */ 5068349Sobrien# define HAVE_MAJOR 5168349Sobrien#endif 5268349Sobrien 5368349Sobrien#ifndef HAVE_MAJOR 5468349Sobrien# define major(dev) (((dev) >> 8) & 0xff) 5568349Sobrien# define minor(dev) ((dev) & 0xff) 5668349Sobrien#endif 5768349Sobrien#undef HAVE_MAJOR 5868349Sobrien 5968349Sobrien#ifndef lint 60186690SobrienFILE_RCSID("@(#)$File: fsmagic.c,v 1.52 2008/07/25 23:59:01 rrt Exp $") 6168349Sobrien#endif /* lint */ 6268349Sobrien 63186690Sobrienprivate int 64186690Sobrienbad_link(struct magic_set *ms, int err, char *buf) 65186690Sobrien{ 66186690Sobrien const char *errfmt; 67186690Sobrien int mime = ms->flags & MAGIC_MIME; 68186690Sobrien if ((mime & MAGIC_MIME_TYPE) && 69186690Sobrien file_printf(ms, "application/x-symlink") 70186690Sobrien == -1) 71186690Sobrien return -1; 72186690Sobrien else if (!mime) { 73186690Sobrien if (err == ELOOP) 74186690Sobrien errfmt = "symbolic link in a loop"; 75186690Sobrien else 76186690Sobrien errfmt = "broken symbolic link to `%s'"; 77186690Sobrien if (ms->flags & MAGIC_ERROR) { 78186690Sobrien file_error(ms, err, errfmt, buf); 79186690Sobrien return -1; 80186690Sobrien } 81186690Sobrien if (file_printf(ms, errfmt, buf) == -1) 82186690Sobrien return -1; 83186690Sobrien } 84186690Sobrien return 1; 85186690Sobrien} 86186690Sobrien 87133359Sobrienprotected int 88133359Sobrienfile_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb) 8968349Sobrien{ 9068349Sobrien int ret = 0; 91175296Sobrien int mime = ms->flags & MAGIC_MIME; 92133359Sobrien#ifdef S_IFLNK 93133359Sobrien char buf[BUFSIZ+4]; 94133359Sobrien int nch; 95133359Sobrien struct stat tstatbuf; 96133359Sobrien#endif 9768349Sobrien 98133359Sobrien if (fn == NULL) 99133359Sobrien return 0; 100133359Sobrien 10168349Sobrien /* 10268349Sobrien * Fstat is cheaper but fails for files you don't have read perms on. 10368349Sobrien * On 4.2BSD and similar systems, use lstat() to identify symlinks. 10468349Sobrien */ 10568349Sobrien#ifdef S_IFLNK 106133359Sobrien if ((ms->flags & MAGIC_SYMLINK) == 0) 10768349Sobrien ret = lstat(fn, sb); 10868349Sobrien else 10968349Sobrien#endif 11068349Sobrien ret = stat(fn, sb); /* don't merge into if; see "ret =" above */ 11168349Sobrien 11268349Sobrien if (ret) { 113133359Sobrien if (ms->flags & MAGIC_ERROR) { 114133359Sobrien file_error(ms, errno, "cannot stat `%s'", fn); 115133359Sobrien return -1; 116133359Sobrien } 117139368Sobrien if (file_printf(ms, "cannot open `%s' (%s)", 118133359Sobrien fn, strerror(errno)) == -1) 119133359Sobrien return -1; 12068349Sobrien return 1; 12168349Sobrien } 12268349Sobrien 123186690Sobrien if (!mime) { 12468349Sobrien#ifdef S_ISUID 125133359Sobrien if (sb->st_mode & S_ISUID) 126133359Sobrien if (file_printf(ms, "setuid ") == -1) 127133359Sobrien return -1; 12868349Sobrien#endif 12968349Sobrien#ifdef S_ISGID 130133359Sobrien if (sb->st_mode & S_ISGID) 131133359Sobrien if (file_printf(ms, "setgid ") == -1) 132133359Sobrien return -1; 13368349Sobrien#endif 13468349Sobrien#ifdef S_ISVTX 135133359Sobrien if (sb->st_mode & S_ISVTX) 136133359Sobrien if (file_printf(ms, "sticky ") == -1) 137133359Sobrien return -1; 13868349Sobrien#endif 13968349Sobrien } 14068349Sobrien 14168349Sobrien switch (sb->st_mode & S_IFMT) { 14268349Sobrien case S_IFDIR: 143186690Sobrien if ((mime & MAGIC_MIME_TYPE) && 144186690Sobrien file_printf(ms, "application/x-directory") 145186690Sobrien == -1) 146133359Sobrien return -1; 147186690Sobrien if (!mime && file_printf(ms, "directory") == -1) 148186690Sobrien return -1; 14968349Sobrien return 1; 15068349Sobrien#ifdef S_IFCHR 15168349Sobrien case S_IFCHR: 15268349Sobrien /* 15368349Sobrien * If -s has been specified, treat character special files 15468349Sobrien * like ordinary files. Otherwise, just report that they 15568349Sobrien * are block special files and go on to the next file. 15668349Sobrien */ 157133359Sobrien if ((ms->flags & MAGIC_DEVICES) != 0) 15868349Sobrien break; 159186690Sobrien if ((mime & MAGIC_MIME_TYPE) && 160186690Sobrien file_printf(ms, "application/x-character-device") 161186690Sobrien == -1) 162186690Sobrien return -1; 163186690Sobrien if (!mime) { 164186690Sobrien#ifdef HAVE_STAT_ST_RDEV 16568349Sobrien# ifdef dv_unit 166186690Sobrien if (file_printf(ms, "character special (%d/%d/%d)", 167186690Sobrien major(sb->st_rdev), dv_unit(sb->st_rdev), 168186690Sobrien dv_subunit(sb->st_rdev)) == -1) 169186690Sobrien return -1; 17068349Sobrien# else 171186690Sobrien if (file_printf(ms, "character special (%ld/%ld)", 172186690Sobrien (long) major(sb->st_rdev), (long) minor(sb->st_rdev)) == -1) 173186690Sobrien return -1; 17468349Sobrien# endif 17568349Sobrien#else 176186690Sobrien if (file_printf(ms, "character special") == -1) 177186690Sobrien return -1; 17868349Sobrien#endif 179186690Sobrien } 18068349Sobrien return 1; 18168349Sobrien#endif 18268349Sobrien#ifdef S_IFBLK 18368349Sobrien case S_IFBLK: 18468349Sobrien /* 18568349Sobrien * If -s has been specified, treat block special files 18668349Sobrien * like ordinary files. Otherwise, just report that they 18768349Sobrien * are block special files and go on to the next file. 18868349Sobrien */ 189133359Sobrien if ((ms->flags & MAGIC_DEVICES) != 0) 19068349Sobrien break; 191186690Sobrien if ((mime & MAGIC_MIME_TYPE) && 192186690Sobrien file_printf(ms, "application/x-block-device") 193186690Sobrien == -1) 194186690Sobrien return -1; 195186690Sobrien if (!mime) { 196186690Sobrien#ifdef HAVE_STAT_ST_RDEV 19768349Sobrien# ifdef dv_unit 198186690Sobrien if (file_printf(ms, "block special (%d/%d/%d)", 199186690Sobrien major(sb->st_rdev), dv_unit(sb->st_rdev), 200186690Sobrien dv_subunit(sb->st_rdev)) == -1) 201186690Sobrien return -1; 20268349Sobrien# else 203186690Sobrien if (file_printf(ms, "block special (%ld/%ld)", 204186690Sobrien (long)major(sb->st_rdev), (long)minor(sb->st_rdev)) == -1) 205186690Sobrien return -1; 20668349Sobrien# endif 20768349Sobrien#else 208186690Sobrien if (file_printf(ms, "block special") == -1) 209186690Sobrien return -1; 21068349Sobrien#endif 211186690Sobrien } 21268349Sobrien return 1; 21368349Sobrien#endif 21468349Sobrien /* TODO add code to handle V7 MUX and Blit MUX files */ 21568349Sobrien#ifdef S_IFIFO 21668349Sobrien case S_IFIFO: 217159764Sobrien if((ms->flags & MAGIC_DEVICES) != 0) 218159764Sobrien break; 219186690Sobrien if ((mime & MAGIC_MIME_TYPE) && 220186690Sobrien file_printf(ms, "application/x-fifo") 221186690Sobrien == -1) 222133359Sobrien return -1; 223186690Sobrien if (!mime && file_printf(ms, "fifo (named pipe)") == -1) 224186690Sobrien return -1; 22568349Sobrien return 1; 22668349Sobrien#endif 22768349Sobrien#ifdef S_IFDOOR 22868349Sobrien case S_IFDOOR: 229186690Sobrien if ((mime & MAGIC_MIME_TYPE) && 230186690Sobrien file_printf(ms, "application/x-door") 231186690Sobrien == -1) 232133359Sobrien return -1; 233186690Sobrien if (!mime && file_printf(ms, "door") == -1) 234186690Sobrien return -1; 23568349Sobrien return 1; 23668349Sobrien#endif 23768349Sobrien#ifdef S_IFLNK 23868349Sobrien case S_IFLNK: 239133359Sobrien if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) { 240133359Sobrien if (ms->flags & MAGIC_ERROR) { 241133359Sobrien file_error(ms, errno, "unreadable symlink `%s'", 242133359Sobrien fn); 243133359Sobrien return -1; 24468349Sobrien } 245186690Sobrien if ((mime & MAGIC_MIME_TYPE) && 246186690Sobrien file_printf(ms, "application/x-symlink") 247186690Sobrien == -1) 248186690Sobrien return -1; 249186690Sobrien if (!mime && file_printf(ms, 250133359Sobrien "unreadable symlink `%s' (%s)", fn, 251133359Sobrien strerror(errno)) == -1) 252133359Sobrien return -1; 253133359Sobrien return 1; 254133359Sobrien } 255186690Sobrien buf[nch] = '\0'; /* readlink(2) does not do this */ 25668349Sobrien 257133359Sobrien /* If broken symlink, say so and quit early. */ 258133359Sobrien if (*buf == '/') { 259186690Sobrien if (stat(buf, &tstatbuf) < 0) 260186690Sobrien return bad_link(ms, errno, buf); 261186690Sobrien } else { 262133359Sobrien char *tmp; 263133359Sobrien char buf2[BUFSIZ+BUFSIZ+4]; 26468349Sobrien 265133359Sobrien if ((tmp = strrchr(fn, '/')) == NULL) { 26668349Sobrien tmp = buf; /* in current directory anyway */ 267133359Sobrien } else { 268133359Sobrien if (tmp - fn + 1 > BUFSIZ) { 269133359Sobrien if (ms->flags & MAGIC_ERROR) { 270133359Sobrien file_error(ms, 0, 271133359Sobrien "path too long: `%s'", buf); 272133359Sobrien return -1; 273133359Sobrien } 274186690Sobrien if ((mime & MAGIC_MIME_TYPE) && 275186690Sobrien file_printf(ms, "application/x-path-too-long") 276186690Sobrien == -1) 277186690Sobrien return -1; 278186690Sobrien if (!mime && file_printf(ms, 279133359Sobrien "path too long: `%s'", fn) == -1) 280133359Sobrien return -1; 281133359Sobrien return 1; 282133359Sobrien } 283133359Sobrien (void)strcpy(buf2, fn); /* take dir part */ 284133359Sobrien buf2[tmp - fn + 1] = '\0'; 285133359Sobrien (void)strcat(buf2, buf); /* plus (rel) link */ 28668349Sobrien tmp = buf2; 287133359Sobrien } 288186690Sobrien if (stat(tmp, &tstatbuf) < 0) 289186690Sobrien return bad_link(ms, errno, buf); 29068349Sobrien } 291133359Sobrien 292133359Sobrien /* Otherwise, handle it. */ 293133359Sobrien if ((ms->flags & MAGIC_SYMLINK) != 0) { 294133359Sobrien const char *p; 295133359Sobrien ms->flags &= MAGIC_SYMLINK; 296133359Sobrien p = magic_file(ms, buf); 297133359Sobrien ms->flags |= MAGIC_SYMLINK; 298133359Sobrien return p != NULL ? 1 : -1; 299133359Sobrien } else { /* just print what it points to */ 300186690Sobrien if ((mime & MAGIC_MIME_TYPE) && 301186690Sobrien file_printf(ms, "application/x-symlink") 302186690Sobrien == -1) 303186690Sobrien return -1; 304186690Sobrien if (!mime && file_printf(ms, "symbolic link to `%s'", 305133359Sobrien buf) == -1) 306133359Sobrien return -1; 307133359Sobrien } 308186690Sobrien return 1; 30968349Sobrien#endif 31068349Sobrien#ifdef S_IFSOCK 31168349Sobrien#ifndef __COHERENT__ 31268349Sobrien case S_IFSOCK: 313186690Sobrien if ((mime & MAGIC_MIME_TYPE) && 314186690Sobrien file_printf(ms, "application/x-socket") 315186690Sobrien == -1) 316133359Sobrien return -1; 317186690Sobrien if (!mime && file_printf(ms, "socket") == -1) 318186690Sobrien return -1; 31968349Sobrien return 1; 32068349Sobrien#endif 32168349Sobrien#endif 32268349Sobrien case S_IFREG: 32368349Sobrien break; 32468349Sobrien default: 325133359Sobrien file_error(ms, 0, "invalid mode 0%o", sb->st_mode); 326133359Sobrien return -1; 32768349Sobrien /*NOTREACHED*/ 32868349Sobrien } 32968349Sobrien 33068349Sobrien /* 33168349Sobrien * regular file, check next possibility 33268349Sobrien * 33368349Sobrien * If stat() tells us the file has zero length, report here that 33468349Sobrien * the file is empty, so we can skip all the work of opening and 33568349Sobrien * reading the file. 33668349Sobrien * But if the -s option has been given, we skip this optimization, 33768349Sobrien * since on some systems, stat() reports zero size for raw disk 33868349Sobrien * partitions. (If the block special device really has zero length, 33968349Sobrien * the fact that it is empty will be detected and reported correctly 34068349Sobrien * when we read the file.) 34168349Sobrien */ 342133359Sobrien if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) { 343175296Sobrien if ((!mime || (mime & MAGIC_MIME_TYPE)) && 344175296Sobrien file_printf(ms, mime ? "application/x-empty" : 345175296Sobrien "empty") == -1) 346133359Sobrien return -1; 34768349Sobrien return 1; 34868349Sobrien } 34968349Sobrien return 0; 35068349Sobrien} 351