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