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