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