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 35 FILE_RCSID("@(#)$File: fsmagic.c,v 1.75 2014/12/04 15:56:46 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 #ifdef WIN32 57 # define WIN32_LEAN_AND_MEAN 58 # include <windows.h> 59 #endif 60 61 #ifndef HAVE_MAJOR 62 # define major(dev) (((dev) >> 8) & 0xff) 63 # define minor(dev) ((dev) & 0xff) 64 #endif 65 #undef HAVE_MAJOR 66 #ifdef S_IFLNK 67 private int 68 bad_link(struct magic_set *ms, int err, char *buf) 69 { 70 int mime = ms->flags & MAGIC_MIME; 71 if ((mime & MAGIC_MIME_TYPE) && 72 file_printf(ms, "inode/symlink") 73 == -1) 74 return -1; 75 else if (!mime) { 76 if (ms->flags & MAGIC_ERROR) { 77 file_error(ms, err, 78 "broken symbolic link to %s", buf); 79 return -1; 80 } 81 if (file_printf(ms, "broken symbolic link to %s", buf) == -1) 82 return -1; 83 } 84 return 1; 85 } 86 #endif 87 private int 88 handle_mime(struct magic_set *ms, int mime, const char *str) 89 { 90 if ((mime & MAGIC_MIME_TYPE)) { 91 if (file_printf(ms, "inode/%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 102 protected int 103 file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb) 104 { 105 int ret, did = 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 #define COMMA (did++ ? ", " : "") 119 /* 120 * Fstat is cheaper but fails for files you don't have read perms on. 121 * On 4.2BSD and similar systems, use lstat() to identify symlinks. 122 */ 123 #ifdef S_IFLNK 124 if ((ms->flags & MAGIC_SYMLINK) == 0) 125 ret = lstat(fn, sb); 126 else 127 #endif 128 ret = stat(fn, sb); /* don't merge into if; see "ret =" above */ 129 130 #ifdef WIN32 131 { 132 HANDLE hFile = CreateFile((LPCSTR)fn, 0, FILE_SHARE_DELETE | 133 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 134 NULL); 135 if (hFile != INVALID_HANDLE_VALUE) { 136 /* 137 * Stat failed, but we can still open it - assume it's 138 * a block device, if nothing else. 139 */ 140 if (ret) { 141 sb->st_mode = S_IFBLK; 142 ret = 0; 143 } 144 switch (GetFileType(hFile)) { 145 case FILE_TYPE_CHAR: 146 sb->st_mode |= S_IFCHR; 147 sb->st_mode &= ~S_IFREG; 148 break; 149 case FILE_TYPE_PIPE: 150 sb->st_mode |= S_IFIFO; 151 sb->st_mode &= ~S_IFREG; 152 break; 153 } 154 CloseHandle(hFile); 155 } 156 } 157 #endif 158 159 if (ret) { 160 if (ms->flags & MAGIC_ERROR) { 161 file_error(ms, errno, "cannot stat `%s'", fn); 162 return -1; 163 } 164 if (file_printf(ms, "cannot open `%s' (%s)", 165 fn, strerror(errno)) == -1) 166 return -1; 167 return 0; 168 } 169 170 ret = 1; 171 if (!mime) { 172 #ifdef S_ISUID 173 if (sb->st_mode & S_ISUID) 174 if (file_printf(ms, "%ssetuid", COMMA) == -1) 175 return -1; 176 #endif 177 #ifdef S_ISGID 178 if (sb->st_mode & S_ISGID) 179 if (file_printf(ms, "%ssetgid", COMMA) == -1) 180 return -1; 181 #endif 182 #ifdef S_ISVTX 183 if (sb->st_mode & S_ISVTX) 184 if (file_printf(ms, "%ssticky", COMMA) == -1) 185 return -1; 186 #endif 187 } 188 189 switch (sb->st_mode & S_IFMT) { 190 case S_IFDIR: 191 if (mime) { 192 if (handle_mime(ms, mime, "directory") == -1) 193 return -1; 194 } else if (file_printf(ms, "%sdirectory", COMMA) == -1) 195 return -1; 196 break; 197 #ifdef S_IFCHR 198 case S_IFCHR: 199 /* 200 * If -s has been specified, treat character special files 201 * like ordinary files. Otherwise, just report that they 202 * are block special files and go on to the next file. 203 */ 204 if ((ms->flags & MAGIC_DEVICES) != 0) { 205 ret = 0; 206 break; 207 } 208 if (mime) { 209 if (handle_mime(ms, mime, "chardevice") == -1) 210 return -1; 211 } else { 212 #ifdef HAVE_STRUCT_STAT_ST_RDEV 213 # ifdef dv_unit 214 if (file_printf(ms, "%scharacter special (%d/%d/%d)", 215 COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev), 216 dv_subunit(sb->st_rdev)) == -1) 217 return -1; 218 # else 219 if (file_printf(ms, "%scharacter special (%ld/%ld)", 220 COMMA, (long)major(sb->st_rdev), 221 (long)minor(sb->st_rdev)) == -1) 222 return -1; 223 # endif 224 #else 225 if (file_printf(ms, "%scharacter special", COMMA) == -1) 226 return -1; 227 #endif 228 } 229 break; 230 #endif 231 #ifdef S_IFBLK 232 case S_IFBLK: 233 /* 234 * If -s has been specified, treat block special files 235 * like ordinary files. Otherwise, just report that they 236 * are block special files and go on to the next file. 237 */ 238 if ((ms->flags & MAGIC_DEVICES) != 0) { 239 ret = 0; 240 break; 241 } 242 if (mime) { 243 if (handle_mime(ms, mime, "blockdevice") == -1) 244 return -1; 245 } else { 246 #ifdef HAVE_STRUCT_STAT_ST_RDEV 247 # ifdef dv_unit 248 if (file_printf(ms, "%sblock special (%d/%d/%d)", 249 COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev), 250 dv_subunit(sb->st_rdev)) == -1) 251 return -1; 252 # else 253 if (file_printf(ms, "%sblock special (%ld/%ld)", 254 COMMA, (long)major(sb->st_rdev), 255 (long)minor(sb->st_rdev)) == -1) 256 return -1; 257 # endif 258 #else 259 if (file_printf(ms, "%sblock special", COMMA) == -1) 260 return -1; 261 #endif 262 } 263 break; 264 #endif 265 /* TODO add code to handle V7 MUX and Blit MUX files */ 266 #ifdef S_IFIFO 267 case S_IFIFO: 268 if((ms->flags & MAGIC_DEVICES) != 0) 269 break; 270 if (mime) { 271 if (handle_mime(ms, mime, "fifo") == -1) 272 return -1; 273 } else if (file_printf(ms, "%sfifo (named pipe)", COMMA) == -1) 274 return -1; 275 break; 276 #endif 277 #ifdef S_IFDOOR 278 case S_IFDOOR: 279 if (mime) { 280 if (handle_mime(ms, mime, "door") == -1) 281 return -1; 282 } else if (file_printf(ms, "%sdoor", COMMA) == -1) 283 return -1; 284 break; 285 #endif 286 #ifdef S_IFLNK 287 case S_IFLNK: 288 if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) { 289 if (ms->flags & MAGIC_ERROR) { 290 file_error(ms, errno, "unreadable symlink `%s'", 291 fn); 292 return -1; 293 } 294 if (mime) { 295 if (handle_mime(ms, mime, "symlink") == -1) 296 return -1; 297 } else if (file_printf(ms, 298 "%sunreadable symlink `%s' (%s)", COMMA, fn, 299 strerror(errno)) == -1) 300 return -1; 301 break; 302 } 303 buf[nch] = '\0'; /* readlink(2) does not do this */ 304 305 /* If broken symlink, say so and quit early. */ 306 if (*buf == '/') { 307 if (stat(buf, &tstatbuf) < 0) 308 return bad_link(ms, errno, buf); 309 } else { 310 char *tmp; 311 char buf2[BUFSIZ+BUFSIZ+4]; 312 313 if ((tmp = strrchr(fn, '/')) == NULL) { 314 tmp = buf; /* in current directory anyway */ 315 } else { 316 if (tmp - fn + 1 > BUFSIZ) { 317 if (ms->flags & MAGIC_ERROR) { 318 file_error(ms, 0, 319 "path too long: `%s'", buf); 320 return -1; 321 } 322 if (mime) { 323 if (handle_mime(ms, mime, 324 "x-path-too-long") == -1) 325 return -1; 326 } else if (file_printf(ms, 327 "%spath too long: `%s'", COMMA, 328 fn) == -1) 329 return -1; 330 break; 331 } 332 /* take dir part */ 333 (void)strlcpy(buf2, fn, sizeof buf2); 334 buf2[tmp - fn + 1] = '\0'; 335 /* plus (rel) link */ 336 (void)strlcat(buf2, buf, sizeof buf2); 337 tmp = buf2; 338 } 339 if (stat(tmp, &tstatbuf) < 0) 340 return bad_link(ms, errno, buf); 341 } 342 343 /* Otherwise, handle it. */ 344 if ((ms->flags & MAGIC_SYMLINK) != 0) { 345 const char *p; 346 ms->flags &= MAGIC_SYMLINK; 347 p = magic_file(ms, buf); 348 ms->flags |= MAGIC_SYMLINK; 349 if (p == NULL) 350 return -1; 351 } else { /* just print what it points to */ 352 if (mime) { 353 if (handle_mime(ms, mime, "symlink") == -1) 354 return -1; 355 } else if (file_printf(ms, "%ssymbolic link to %s", 356 COMMA, buf) == -1) 357 return -1; 358 } 359 break; 360 #endif 361 #ifdef S_IFSOCK 362 #ifndef __COHERENT__ 363 case S_IFSOCK: 364 if (mime) { 365 if (handle_mime(ms, mime, "socket") == -1) 366 return -1; 367 } else if (file_printf(ms, "%ssocket", COMMA) == -1) 368 return -1; 369 break; 370 #endif 371 #endif 372 case S_IFREG: 373 /* 374 * regular file, check next possibility 375 * 376 * If stat() tells us the file has zero length, report here that 377 * the file is empty, so we can skip all the work of opening and 378 * reading the file. 379 * But if the -s option has been given, we skip this 380 * optimization, since on some systems, stat() reports zero 381 * size for raw disk partitions. (If the block special device 382 * really has zero length, the fact that it is empty will be 383 * detected and reported correctly when we read the file.) 384 */ 385 if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) { 386 if (mime) { 387 if (handle_mime(ms, mime, "x-empty") == -1) 388 return -1; 389 } else if (file_printf(ms, "%sempty", COMMA) == -1) 390 return -1; 391 break; 392 } 393 ret = 0; 394 break; 395 396 default: 397 file_error(ms, 0, "invalid mode 0%o", sb->st_mode); 398 return -1; 399 /*NOTREACHED*/ 400 } 401 402 if (!mime && did && ret == 0) { 403 if (file_printf(ms, " ") == -1) 404 return -1; 405 } 406 return ret; 407 } 408