fsmagic.c revision 226048
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" 33191736Sobrien 34191736Sobrien#ifndef lint 35226048SobrienFILE_RCSID("@(#)$File: fsmagic.c,v 1.64 2011/08/14 09:03:12 christos Exp $") 36191736Sobrien#endif /* lint */ 37191736Sobrien 38133359Sobrien#include "magic.h" 3968349Sobrien#include <string.h> 4068349Sobrien#ifdef HAVE_UNISTD_H 4168349Sobrien#include <unistd.h> 4268349Sobrien#endif 4368349Sobrien#include <stdlib.h> 44133359Sobrien/* Since major is a function on SVR4, we cannot use `ifndef major'. */ 4568349Sobrien#ifdef MAJOR_IN_MKDEV 4668349Sobrien# include <sys/mkdev.h> 4768349Sobrien# define HAVE_MAJOR 4868349Sobrien#endif 4968349Sobrien#ifdef MAJOR_IN_SYSMACROS 5068349Sobrien# include <sys/sysmacros.h> 5168349Sobrien# define HAVE_MAJOR 5268349Sobrien#endif 5368349Sobrien#ifdef major /* Might be defined in sys/types.h. */ 5468349Sobrien# define HAVE_MAJOR 5568349Sobrien#endif 5668349Sobrien 5768349Sobrien#ifndef HAVE_MAJOR 5868349Sobrien# define major(dev) (((dev) >> 8) & 0xff) 5968349Sobrien# define minor(dev) ((dev) & 0xff) 6068349Sobrien#endif 6168349Sobrien#undef HAVE_MAJOR 62226048Sobrien#ifdef S_IFLNK 63186690Sobrienprivate int 64186690Sobrienbad_link(struct magic_set *ms, int err, char *buf) 65186690Sobrien{ 66186690Sobrien int mime = ms->flags & MAGIC_MIME; 67186690Sobrien if ((mime & MAGIC_MIME_TYPE) && 68226048Sobrien file_printf(ms, "inode/symlink") 69186690Sobrien == -1) 70186690Sobrien return -1; 71186690Sobrien else if (!mime) { 72186690Sobrien if (ms->flags & MAGIC_ERROR) { 73226048Sobrien file_error(ms, err, 74226048Sobrien "broken symbolic link to `%s'", buf); 75186690Sobrien return -1; 76186690Sobrien } 77226048Sobrien if (file_printf(ms, "broken symbolic link to `%s'", buf) == -1) 78186690Sobrien return -1; 79186690Sobrien } 80186690Sobrien return 1; 81186690Sobrien} 82226048Sobrien#endif 83191736Sobrienprivate int 84191736Sobrienhandle_mime(struct magic_set *ms, int mime, const char *str) 85191736Sobrien{ 86191736Sobrien if ((mime & MAGIC_MIME_TYPE)) { 87226048Sobrien if (file_printf(ms, "inode/%s", str) == -1) 88191736Sobrien return -1; 89191736Sobrien if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms, 90191736Sobrien "; charset=") == -1) 91191736Sobrien return -1; 92191736Sobrien } 93191736Sobrien if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms, "binary") == -1) 94191736Sobrien return -1; 95191736Sobrien return 0; 96191736Sobrien} 97191736Sobrien 98133359Sobrienprotected int 99133359Sobrienfile_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb) 10068349Sobrien{ 10168349Sobrien int ret = 0; 102175296Sobrien int mime = ms->flags & MAGIC_MIME; 103133359Sobrien#ifdef S_IFLNK 104133359Sobrien char buf[BUFSIZ+4]; 105226048Sobrien ssize_t nch; 106133359Sobrien struct stat tstatbuf; 107133359Sobrien#endif 10868349Sobrien 109191736Sobrien if (ms->flags & MAGIC_APPLE) 110191736Sobrien return 0; 111133359Sobrien if (fn == NULL) 112133359Sobrien return 0; 113133359Sobrien 11468349Sobrien /* 11568349Sobrien * Fstat is cheaper but fails for files you don't have read perms on. 11668349Sobrien * On 4.2BSD and similar systems, use lstat() to identify symlinks. 11768349Sobrien */ 11868349Sobrien#ifdef S_IFLNK 119133359Sobrien if ((ms->flags & MAGIC_SYMLINK) == 0) 12068349Sobrien ret = lstat(fn, sb); 12168349Sobrien else 12268349Sobrien#endif 12368349Sobrien ret = stat(fn, sb); /* don't merge into if; see "ret =" above */ 12468349Sobrien 12568349Sobrien if (ret) { 126133359Sobrien if (ms->flags & MAGIC_ERROR) { 127133359Sobrien file_error(ms, errno, "cannot stat `%s'", fn); 128133359Sobrien return -1; 129133359Sobrien } 130139368Sobrien if (file_printf(ms, "cannot open `%s' (%s)", 131133359Sobrien fn, strerror(errno)) == -1) 132133359Sobrien return -1; 133226048Sobrien ms->event_flags |= EVENT_HAD_ERR; 134226048Sobrien return -1; 13568349Sobrien } 13668349Sobrien 137186690Sobrien if (!mime) { 13868349Sobrien#ifdef S_ISUID 139133359Sobrien if (sb->st_mode & S_ISUID) 140133359Sobrien if (file_printf(ms, "setuid ") == -1) 141133359Sobrien return -1; 14268349Sobrien#endif 14368349Sobrien#ifdef S_ISGID 144133359Sobrien if (sb->st_mode & S_ISGID) 145133359Sobrien if (file_printf(ms, "setgid ") == -1) 146133359Sobrien return -1; 14768349Sobrien#endif 14868349Sobrien#ifdef S_ISVTX 149133359Sobrien if (sb->st_mode & S_ISVTX) 150133359Sobrien if (file_printf(ms, "sticky ") == -1) 151133359Sobrien return -1; 15268349Sobrien#endif 15368349Sobrien } 15468349Sobrien 15568349Sobrien switch (sb->st_mode & S_IFMT) { 15668349Sobrien case S_IFDIR: 157191736Sobrien if (mime) { 158226048Sobrien if (handle_mime(ms, mime, "directory") == -1) 159191736Sobrien return -1; 160191736Sobrien } else if (file_printf(ms, "directory") == -1) 161133359Sobrien return -1; 16268349Sobrien return 1; 16368349Sobrien#ifdef S_IFCHR 16468349Sobrien case S_IFCHR: 16568349Sobrien /* 16668349Sobrien * If -s has been specified, treat character special files 16768349Sobrien * like ordinary files. Otherwise, just report that they 16868349Sobrien * are block special files and go on to the next file. 16968349Sobrien */ 170133359Sobrien if ((ms->flags & MAGIC_DEVICES) != 0) 17168349Sobrien break; 172191736Sobrien if (mime) { 173226048Sobrien if (handle_mime(ms, mime, "chardevice") == -1) 174191736Sobrien return -1; 175191736Sobrien } else { 176186690Sobrien#ifdef HAVE_STAT_ST_RDEV 17768349Sobrien# ifdef dv_unit 178186690Sobrien if (file_printf(ms, "character special (%d/%d/%d)", 179191736Sobrien major(sb->st_rdev), dv_unit(sb->st_rdev), 180186690Sobrien dv_subunit(sb->st_rdev)) == -1) 181186690Sobrien return -1; 18268349Sobrien# else 183186690Sobrien if (file_printf(ms, "character special (%ld/%ld)", 184191736Sobrien (long)major(sb->st_rdev), (long)minor(sb->st_rdev)) 185191736Sobrien == -1) 186186690Sobrien return -1; 18768349Sobrien# endif 18868349Sobrien#else 189186690Sobrien if (file_printf(ms, "character special") == -1) 190186690Sobrien return -1; 19168349Sobrien#endif 192186690Sobrien } 19368349Sobrien return 1; 19468349Sobrien#endif 19568349Sobrien#ifdef S_IFBLK 19668349Sobrien case S_IFBLK: 19768349Sobrien /* 19868349Sobrien * If -s has been specified, treat block special files 19968349Sobrien * like ordinary files. Otherwise, just report that they 20068349Sobrien * are block special files and go on to the next file. 20168349Sobrien */ 202133359Sobrien if ((ms->flags & MAGIC_DEVICES) != 0) 20368349Sobrien break; 204191736Sobrien if (mime) { 205226048Sobrien if (handle_mime(ms, mime, "blockdevice") == -1) 206191736Sobrien return -1; 207191736Sobrien } else { 208186690Sobrien#ifdef HAVE_STAT_ST_RDEV 20968349Sobrien# ifdef dv_unit 210186690Sobrien if (file_printf(ms, "block special (%d/%d/%d)", 211186690Sobrien major(sb->st_rdev), dv_unit(sb->st_rdev), 212186690Sobrien dv_subunit(sb->st_rdev)) == -1) 213186690Sobrien return -1; 21468349Sobrien# else 215186690Sobrien if (file_printf(ms, "block special (%ld/%ld)", 216186690Sobrien (long)major(sb->st_rdev), (long)minor(sb->st_rdev)) == -1) 217186690Sobrien return -1; 21868349Sobrien# endif 21968349Sobrien#else 220186690Sobrien if (file_printf(ms, "block special") == -1) 221186690Sobrien return -1; 22268349Sobrien#endif 223186690Sobrien } 22468349Sobrien return 1; 22568349Sobrien#endif 22668349Sobrien /* TODO add code to handle V7 MUX and Blit MUX files */ 22768349Sobrien#ifdef S_IFIFO 22868349Sobrien case S_IFIFO: 229159764Sobrien if((ms->flags & MAGIC_DEVICES) != 0) 230159764Sobrien break; 231191736Sobrien if (mime) { 232226048Sobrien if (handle_mime(ms, mime, "fifo") == -1) 233191736Sobrien return -1; 234191736Sobrien } else if (file_printf(ms, "fifo (named pipe)") == -1) 235133359Sobrien return -1; 23668349Sobrien return 1; 23768349Sobrien#endif 23868349Sobrien#ifdef S_IFDOOR 23968349Sobrien case S_IFDOOR: 240191736Sobrien if (mime) { 241226048Sobrien if (handle_mime(ms, mime, "door") == -1) 242191736Sobrien return -1; 243191736Sobrien } else if (file_printf(ms, "door") == -1) 244133359Sobrien return -1; 24568349Sobrien return 1; 24668349Sobrien#endif 24768349Sobrien#ifdef S_IFLNK 24868349Sobrien case S_IFLNK: 249133359Sobrien if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) { 250133359Sobrien if (ms->flags & MAGIC_ERROR) { 251133359Sobrien file_error(ms, errno, "unreadable symlink `%s'", 252133359Sobrien fn); 253133359Sobrien return -1; 25468349Sobrien } 255191736Sobrien if (mime) { 256226048Sobrien if (handle_mime(ms, mime, "symlink") == -1) 257191736Sobrien return -1; 258191736Sobrien } else if (file_printf(ms, 259133359Sobrien "unreadable symlink `%s' (%s)", fn, 260133359Sobrien strerror(errno)) == -1) 261133359Sobrien return -1; 262133359Sobrien return 1; 263133359Sobrien } 264186690Sobrien buf[nch] = '\0'; /* readlink(2) does not do this */ 26568349Sobrien 266133359Sobrien /* If broken symlink, say so and quit early. */ 267133359Sobrien if (*buf == '/') { 268186690Sobrien if (stat(buf, &tstatbuf) < 0) 269186690Sobrien return bad_link(ms, errno, buf); 270186690Sobrien } else { 271133359Sobrien char *tmp; 272133359Sobrien char buf2[BUFSIZ+BUFSIZ+4]; 27368349Sobrien 274133359Sobrien if ((tmp = strrchr(fn, '/')) == NULL) { 27568349Sobrien tmp = buf; /* in current directory anyway */ 276133359Sobrien } else { 277133359Sobrien if (tmp - fn + 1 > BUFSIZ) { 278133359Sobrien if (ms->flags & MAGIC_ERROR) { 279133359Sobrien file_error(ms, 0, 280133359Sobrien "path too long: `%s'", buf); 281133359Sobrien return -1; 282133359Sobrien } 283191736Sobrien if (mime) { 284191736Sobrien if (handle_mime(ms, mime, 285191736Sobrien "x-path-too-long") == -1) 286191736Sobrien return -1; 287191736Sobrien } else if (file_printf(ms, 288133359Sobrien "path too long: `%s'", fn) == -1) 289133359Sobrien return -1; 290133359Sobrien return 1; 291133359Sobrien } 292191736Sobrien /* take dir part */ 293191736Sobrien (void)strlcpy(buf2, fn, sizeof buf2); 294133359Sobrien buf2[tmp - fn + 1] = '\0'; 295191736Sobrien /* plus (rel) link */ 296191736Sobrien (void)strlcat(buf2, buf, sizeof buf2); 29768349Sobrien tmp = buf2; 298133359Sobrien } 299186690Sobrien if (stat(tmp, &tstatbuf) < 0) 300186690Sobrien return bad_link(ms, errno, buf); 30168349Sobrien } 302133359Sobrien 303133359Sobrien /* Otherwise, handle it. */ 304133359Sobrien if ((ms->flags & MAGIC_SYMLINK) != 0) { 305133359Sobrien const char *p; 306133359Sobrien ms->flags &= MAGIC_SYMLINK; 307133359Sobrien p = magic_file(ms, buf); 308133359Sobrien ms->flags |= MAGIC_SYMLINK; 309133359Sobrien return p != NULL ? 1 : -1; 310133359Sobrien } else { /* just print what it points to */ 311191736Sobrien if (mime) { 312226048Sobrien if (handle_mime(ms, mime, "symlink") == -1) 313191736Sobrien return -1; 314191736Sobrien } else if (file_printf(ms, "symbolic link to `%s'", 315133359Sobrien buf) == -1) 316133359Sobrien return -1; 317133359Sobrien } 318186690Sobrien return 1; 31968349Sobrien#endif 32068349Sobrien#ifdef S_IFSOCK 32168349Sobrien#ifndef __COHERENT__ 32268349Sobrien case S_IFSOCK: 323191736Sobrien if (mime) { 324226048Sobrien if (handle_mime(ms, mime, "socket") == -1) 325191736Sobrien return -1; 326191736Sobrien } else if (file_printf(ms, "socket") == -1) 327133359Sobrien return -1; 32868349Sobrien return 1; 32968349Sobrien#endif 33068349Sobrien#endif 33168349Sobrien case S_IFREG: 33268349Sobrien break; 33368349Sobrien default: 334133359Sobrien file_error(ms, 0, "invalid mode 0%o", sb->st_mode); 335133359Sobrien return -1; 33668349Sobrien /*NOTREACHED*/ 33768349Sobrien } 33868349Sobrien 33968349Sobrien /* 34068349Sobrien * regular file, check next possibility 34168349Sobrien * 34268349Sobrien * If stat() tells us the file has zero length, report here that 34368349Sobrien * the file is empty, so we can skip all the work of opening and 34468349Sobrien * reading the file. 34568349Sobrien * But if the -s option has been given, we skip this optimization, 34668349Sobrien * since on some systems, stat() reports zero size for raw disk 34768349Sobrien * partitions. (If the block special device really has zero length, 34868349Sobrien * the fact that it is empty will be detected and reported correctly 34968349Sobrien * when we read the file.) 35068349Sobrien */ 351133359Sobrien if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) { 352191736Sobrien if (mime) { 353191736Sobrien if (handle_mime(ms, mime, "x-empty") == -1) 354191736Sobrien return -1; 355191736Sobrien } else if (file_printf(ms, "empty") == -1) 356133359Sobrien return -1; 35768349Sobrien return 1; 35868349Sobrien } 35968349Sobrien return 0; 36068349Sobrien} 361