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