1/* 2 * Copyright (c) Ian F. Darwin 1986-1995. 3 * Software written by Ian F. Darwin and others; 4 * maintained 1995-present by Christos Zoulas and others. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice immediately at the beginning of the file, without modification, 11 * this list of conditions, and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28/* 29 * fsmagic - magic based on filesystem info - directory, special files, etc. 30 */ 31 32#include "file.h" 33 34#ifndef lint 35FILE_RCSID("@(#)$File: fsmagic.c,v 1.60 2009/05/08 17:41:59 christos Exp $") 36#endif /* lint */ 37 38#include "magic.h" 39#include <string.h> 40#ifdef HAVE_UNISTD_H 41#include <unistd.h> 42#endif 43#include <stdlib.h> 44/* Since major is a function on SVR4, we cannot use `ifndef major'. */ 45#ifdef MAJOR_IN_MKDEV 46# include <sys/mkdev.h> 47# define HAVE_MAJOR 48#endif 49#ifdef MAJOR_IN_SYSMACROS 50# include <sys/sysmacros.h> 51# define HAVE_MAJOR 52#endif 53#ifdef major /* Might be defined in sys/types.h. */ 54# define HAVE_MAJOR 55#endif 56 57#ifndef HAVE_MAJOR 58# define major(dev) (((dev) >> 8) & 0xff) 59# define minor(dev) ((dev) & 0xff) 60#endif 61#undef HAVE_MAJOR 62 63private int 64bad_link(struct magic_set *ms, int err, char *buf) 65{ 66 const char *errfmt; 67 int mime = ms->flags & MAGIC_MIME; 68 if ((mime & MAGIC_MIME_TYPE) && 69 file_printf(ms, "application/x-symlink") 70 == -1) 71 return -1; 72 else if (!mime) { 73 if (err == ELOOP) 74 errfmt = "symbolic link in a loop"; 75 else 76 errfmt = "broken symbolic link to %s"; 77 if (ms->flags & MAGIC_ERROR) { 78 file_error(ms, err, errfmt, buf); 79 return -1; 80 } 81 if (file_printf(ms, errfmt, buf) == -1) 82 return -1; 83 } 84 return 1; 85} 86 87private int 88handle_mime(struct magic_set *ms, int mime, const char *str) 89{ 90 if ((mime & MAGIC_MIME_TYPE)) { 91 if (file_printf(ms, "application/%s", str) == -1) 92 return -1; 93 if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms, 94 "; charset=") == -1) 95 return -1; 96 } 97 if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms, "binary") == -1) 98 return -1; 99 return 0; 100} 101 102protected int 103file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb) 104{ 105 int ret = 0; 106 int mime = ms->flags & MAGIC_MIME; 107#ifdef S_IFLNK 108 char buf[BUFSIZ+4]; 109 ssize_t nch; 110 struct stat tstatbuf; 111#endif 112 113 if (ms->flags & MAGIC_APPLE) 114 return 0; 115 if (fn == NULL) 116 return 0; 117 118 /* 119 * Fstat is cheaper but fails for files you don't have read perms on. 120 * On 4.2BSD and similar systems, use lstat() to identify symlinks. 121 */ 122#ifdef S_IFLNK 123 if ((ms->flags & MAGIC_SYMLINK) == 0) 124 ret = lstat(fn, sb); 125 else 126#endif 127 ret = stat(fn, sb); /* don't merge into if; see "ret =" above */ 128 129#ifdef S_IFLNK 130 /* POSIX says pretend symlinks to nonexistant files 131 have the -h option */ 132 if (ret && (ms->flags & MAGIC_SYMLINK) != 0) { 133 ret = lstat(fn, sb); 134 } 135#endif 136 137 if (ret) { 138 if (ms->flags & MAGIC_ERROR) { 139 file_error(ms, errno, "cannot stat `%s'", fn); 140 return -1; 141 } 142 if (file_printf(ms, "cannot open `%s' (%s)", 143 fn, strerror(errno)) == -1) 144 return -1; 145 return 1; 146 } 147 148 if (!mime) { 149#ifdef S_ISUID 150 if (sb->st_mode & S_ISUID) 151 if (file_printf(ms, "setuid ") == -1) 152 return -1; 153#endif 154#ifdef S_ISGID 155 if (sb->st_mode & S_ISGID) 156 if (file_printf(ms, "setgid ") == -1) 157 return -1; 158#endif 159#ifdef S_ISVTX 160 if (sb->st_mode & S_ISVTX) 161 if (file_printf(ms, "sticky ") == -1) 162 return -1; 163#endif 164 } 165 166 switch (sb->st_mode & S_IFMT) { 167 case S_IFDIR: 168 if (mime) { 169 if (handle_mime(ms, mime, "x-directory") == -1) 170 return -1; 171 } else if (file_printf(ms, "directory") == -1) 172 return -1; 173 return 1; 174#ifdef S_IFCHR 175 case S_IFCHR: 176 /* 177 * If -s has been specified, treat character special files 178 * like ordinary files. Otherwise, just report that they 179 * are block special files and go on to the next file. 180 */ 181 if ((ms->flags & MAGIC_DEVICES) != 0) 182 break; 183 if (mime) { 184 if (handle_mime(ms, mime, "x-character-device") == -1) 185 return -1; 186 } else { 187#ifdef HAVE_STAT_ST_RDEV 188# ifdef dv_unit 189 if (file_printf(ms, "character special (%d/%d/%d)", 190 major(sb->st_rdev), dv_unit(sb->st_rdev), 191 dv_subunit(sb->st_rdev)) == -1) 192 return -1; 193# else 194 if (file_printf(ms, "character special (%ld/%ld)", 195 (long)major(sb->st_rdev), (long)minor(sb->st_rdev)) 196 == -1) 197 return -1; 198# endif 199#else 200 if (file_printf(ms, "character special") == -1) 201 return -1; 202#endif 203 } 204 return 1; 205#endif 206#ifdef S_IFBLK 207 case S_IFBLK: 208 /* 209 * If -s has been specified, treat block special files 210 * like ordinary files. Otherwise, just report that they 211 * are block special files and go on to the next file. 212 */ 213 if ((ms->flags & MAGIC_DEVICES) != 0) 214 break; 215 if (mime) { 216 if (handle_mime(ms, mime, "x-block-device") == -1) 217 return -1; 218 } else { 219#ifdef HAVE_STAT_ST_RDEV 220# ifdef dv_unit 221 if (file_printf(ms, "block special (%d/%d/%d)", 222 major(sb->st_rdev), dv_unit(sb->st_rdev), 223 dv_subunit(sb->st_rdev)) == -1) 224 return -1; 225# else 226 if (file_printf(ms, "block special (%ld/%ld)", 227 (long)major(sb->st_rdev), (long)minor(sb->st_rdev)) == -1) 228 return -1; 229# endif 230#else 231 if (file_printf(ms, "block special") == -1) 232 return -1; 233#endif 234 } 235 return 1; 236#endif 237 /* TODO add code to handle V7 MUX and Blit MUX files */ 238#ifdef S_IFIFO 239 case S_IFIFO: 240 if((ms->flags & MAGIC_DEVICES) != 0) 241 break; 242 if (mime) { 243 if (handle_mime(ms, mime, "x-fifo") == -1) 244 return -1; 245 } else if (file_printf(ms, "fifo (named pipe)") == -1) 246 return -1; 247 return 1; 248#endif 249#ifdef S_IFDOOR 250 case S_IFDOOR: 251 if (mime) { 252 if (handle_mime(ms, mime, "x-door") == -1) 253 return -1; 254 } else if (file_printf(ms, "door") == -1) 255 return -1; 256 return 1; 257#endif 258#ifdef S_IFLNK 259 case S_IFLNK: 260 if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) { 261 if (ms->flags & MAGIC_ERROR) { 262 file_error(ms, errno, "unreadable symlink `%s'", 263 fn); 264 return -1; 265 } 266 if (mime) { 267 if (handle_mime(ms, mime, "x-symlink") == -1) 268 return -1; 269 } else if (file_printf(ms, 270 "unreadable symlink `%s' (%s)", fn, 271 strerror(errno)) == -1) 272 return -1; 273 return 1; 274 } 275 buf[nch] = '\0'; /* readlink(2) does not do this */ 276 277 /* If broken symlink, say so and quit early. */ 278 if (*buf == '/') { 279 if (stat(buf, &tstatbuf) < 0) 280 return bad_link(ms, errno, buf); 281 } else { 282 char *tmp; 283 char buf2[BUFSIZ+BUFSIZ+4]; 284 285 if ((tmp = strrchr(fn, '/')) == NULL) { 286 tmp = buf; /* in current directory anyway */ 287 } else { 288 if (tmp - fn + 1 > BUFSIZ) { 289 if (ms->flags & MAGIC_ERROR) { 290 file_error(ms, 0, 291 "path too long: `%s'", buf); 292 return -1; 293 } 294 if (mime) { 295 if (handle_mime(ms, mime, 296 "x-path-too-long") == -1) 297 return -1; 298 } else if (file_printf(ms, 299 "path too long: `%s'", fn) == -1) 300 return -1; 301 return 1; 302 } 303 /* take dir part */ 304 (void)strlcpy(buf2, fn, sizeof buf2); 305 buf2[tmp - fn + 1] = '\0'; 306 /* plus (rel) link */ 307 (void)strlcat(buf2, buf, sizeof buf2); 308 tmp = buf2; 309 } 310 if (stat(tmp, &tstatbuf) < 0) 311 return bad_link(ms, errno, buf); 312 } 313 314 /* Otherwise, handle it. */ 315 if ((ms->flags & MAGIC_SYMLINK) != 0) { 316 const char *p; 317 ms->flags &= MAGIC_SYMLINK; 318 p = magic_file(ms, buf); 319 ms->flags |= MAGIC_SYMLINK; 320 return p != NULL ? 1 : -1; 321 } else { /* just print what it points to */ 322 if (mime) { 323 if (handle_mime(ms, mime, "x-symlink") == -1) 324 return -1; 325 } else if (file_printf(ms, "symbolic link to %s", 326 buf) == -1) 327 return -1; 328 } 329 return 1; 330#endif 331#ifdef S_IFSOCK 332#ifndef __COHERENT__ 333 case S_IFSOCK: 334 if (mime) { 335 if (handle_mime(ms, mime, "x-socket") == -1) 336 return -1; 337 } else if (file_printf(ms, "socket") == -1) 338 return -1; 339 return 1; 340#endif 341#endif 342 case S_IFREG: 343 if (ms->flags & MAGIC_REGULAR) { 344 if (file_printf(ms, "regular file") == -1) 345 return -1; 346 return 1; 347 } 348 break; 349 default: 350 file_error(ms, 0, "invalid mode 0%o", sb->st_mode); 351 return -1; 352 /*NOTREACHED*/ 353 } 354 355 /* 356 * regular file, check next possibility 357 * 358 * If stat() tells us the file has zero length, report here that 359 * the file is empty, so we can skip all the work of opening and 360 * reading the file. 361 * But if the -s option has been given, we skip this optimization, 362 * since on some systems, stat() reports zero size for raw disk 363 * partitions. (If the block special device really has zero length, 364 * the fact that it is empty will be detected and reported correctly 365 * when we read the file.) 366 */ 367 if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) { 368 if (mime) { 369 if (handle_mime(ms, mime, "x-empty") == -1) 370 return -1; 371 } else if (file_printf(ms, "empty") == -1) 372 return -1; 373 return 1; 374 } 375 return 0; 376} 377