Annotation of src/usr.bin/file/file.c, Revision 1.36
1.36 ! nicm 1: /* $OpenBSD: file.c,v 1.35 2015/04/27 13:41:45 nicm Exp $ */
1.27 nicm 2:
1.14 tedu 3: /*
1.27 nicm 4: * Copyright (c) 2015 Nicholas Marriott <nicm@openbsd.org>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.14 tedu 17: */
1.11 ian 18:
1.20 deraadt 19: #include <sys/types.h>
1.27 nicm 20: #include <sys/ioctl.h>
21: #include <sys/mman.h>
1.35 nicm 22: #include <sys/socket.h>
23: #include <sys/queue.h>
24: #include <sys/uio.h>
25: #include <sys/wait.h>
1.20 deraadt 26:
1.27 nicm 27: #include <errno.h>
1.35 nicm 28: #include <imsg.h>
1.27 nicm 29: #include <libgen.h>
30: #include <getopt.h>
31: #include <fcntl.h>
32: #include <pwd.h>
1.1 deraadt 33: #include <stdlib.h>
1.14 tedu 34: #include <unistd.h>
35:
1.27 nicm 36: #include "file.h"
37: #include "magic.h"
38: #include "xmalloc.h"
1.1 deraadt 39:
1.35 nicm 40: struct input_msg
41: {
42: int idx;
43:
44: struct stat sb;
45: int error;
46:
47: char link_path[PATH_MAX];
48: int link_error;
49: int link_target;
50: };
51:
52: struct input_ack
53: {
54: int idx;
55: };
56:
1.27 nicm 57: struct input_file
58: {
1.35 nicm 59: struct magic *m;
60: struct input_msg *msg;
1.1 deraadt 61:
1.35 nicm 62: const char *path;
63: int fd;
1.14 tedu 64:
1.35 nicm 65: void *base;
66: size_t size;
67: int mapped;
68: char *result;
1.27 nicm 69: };
1.1 deraadt 70:
1.27 nicm 71: extern char *__progname;
1.1 deraadt 72:
1.27 nicm 73: __dead void usage(void);
1.1 deraadt 74:
1.35 nicm 75: static void send_message(struct imsgbuf *, void *, size_t, int);
76: static int read_message(struct imsgbuf *, struct imsg *, pid_t);
77:
78: static void read_link(struct input_msg *, const char *);
79:
80: static __dead void child(int, pid_t, int, char **);
81:
82: static void test_file(struct input_file *, size_t);
1.27 nicm 83:
84: static int try_stat(struct input_file *);
85: static int try_empty(struct input_file *);
86: static int try_access(struct input_file *);
87: static int try_text(struct input_file *);
88: static int try_magic(struct input_file *);
89: static int try_unknown(struct input_file *);
90:
91: static int bflag;
92: static int cflag;
93: static int iflag;
94: static int Lflag;
95: static int sflag;
96: static int Wflag;
97:
1.35 nicm 98: static char *magicpath;
99: static FILE *magicfp;
100:
1.27 nicm 101: static struct option longopts[] = {
102: { "mime", no_argument, NULL, 'i' },
103: { "mime-type", no_argument, NULL, 'i' },
104: { NULL, 0, NULL, 0 }
105: };
1.14 tedu 106:
1.27 nicm 107: __dead void
108: usage(void)
109: {
110: fprintf(stderr, "usage: %s [-bchiLsW] [file ...]\n", __progname);
111: exit(1);
112: }
1.14 tedu 113:
1.1 deraadt 114: int
1.27 nicm 115: main(int argc, char **argv)
1.1 deraadt 116: {
1.35 nicm 117: int opt, pair[2], fd, idx;
118: char *home;
1.27 nicm 119: struct passwd *pw;
1.35 nicm 120: struct imsgbuf ibuf;
121: struct imsg imsg;
122: struct input_msg msg;
123: struct input_ack *ack;
124: pid_t pid, parent;
1.27 nicm 125:
126: for (;;) {
127: opt = getopt_long(argc, argv, "bchiLsW", longopts, NULL);
128: if (opt == -1)
1.18 chl 129: break;
1.27 nicm 130: switch (opt) {
1.9 millert 131: case 'b':
1.27 nicm 132: bflag = 1;
1.9 millert 133: break;
1.1 deraadt 134: case 'c':
1.27 nicm 135: cflag = 1;
1.14 tedu 136: break;
1.27 nicm 137: case 'h':
138: Lflag = 0;
1.14 tedu 139: break;
1.19 chl 140: case 'i':
1.27 nicm 141: iflag = 1;
1.19 chl 142: break;
1.27 nicm 143: case 'L':
144: Lflag = 1;
1.14 tedu 145: break;
146: case 's':
1.27 nicm 147: sflag = 1;
1.14 tedu 148: break;
1.27 nicm 149: case 'W':
150: Wflag = 1;
1.18 chl 151: break;
1.1 deraadt 152: default:
1.27 nicm 153: usage();
1.1 deraadt 154: }
1.27 nicm 155: }
156: argc -= optind;
157: argv += optind;
158: if (cflag) {
159: if (argc != 0)
160: usage();
161: } else if (argc == 0)
162: usage();
1.1 deraadt 163:
1.35 nicm 164: magicfp = NULL;
1.32 nicm 165: if (geteuid() != 0 && !issetugid()) {
166: home = getenv("HOME");
167: if (home == NULL || *home == '\0') {
168: pw = getpwuid(getuid());
169: if (pw != NULL)
170: home = pw->pw_dir;
171: else
172: home = NULL;
173: }
174: if (home != NULL) {
1.35 nicm 175: xasprintf(&magicpath, "%s/.magic", home);
176: magicfp = fopen(magicpath, "r");
177: if (magicfp == NULL && errno != ENOENT)
178: err(1, "%s", magicpath);
179: if (magicfp == NULL)
180: free(magicpath);
1.32 nicm 181: }
1.27 nicm 182: }
1.35 nicm 183: if (magicfp == NULL) {
184: magicpath = xstrdup("/etc/magic");
185: magicfp = fopen(magicpath, "r");
186: }
187: if (magicfp == NULL)
188: err(1, "%s", magicpath);
1.36 ! nicm 189: setvbuf(magicfp, NULL, _IOLBF, 0); /* stops stdio calling fstat */
1.35 nicm 190:
191: parent = getpid();
192: if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
193: err(1, "socketpair");
1.36 ! nicm 194: pid = sandbox_fork(FILE_USER);
! 195: if (pid == 0) {
1.35 nicm 196: close(pair[0]);
197: child(pair[1], parent, argc, argv);
198: }
199: close(pair[1]);
200:
201: fclose(magicfp);
202: magicfp = NULL;
203:
204: if (cflag)
205: goto wait_for_child;
206:
207: imsg_init(&ibuf, pair[0]);
208: for (idx = 0; idx < argc; idx++) {
209: memset(&msg, 0, sizeof msg);
210: msg.idx = idx;
211:
212: if (lstat(argv[idx], &msg.sb) == -1) {
213: fd = -1;
214: msg.error = errno;
215: } else {
216: fd = open(argv[idx], O_RDONLY|O_NONBLOCK);
217: if (fd == -1 && (errno == ENFILE || errno == EMFILE))
218: err(1, "open");
219: if (S_ISLNK(msg.sb.st_mode))
220: read_link(&msg, argv[idx]);
221: }
222: send_message(&ibuf, &msg, sizeof msg, fd);
1.1 deraadt 223:
1.35 nicm 224: if (read_message(&ibuf, &imsg, pid) == 0)
225: break;
226: if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof *ack)
227: errx(1, "message too small");
228: ack = imsg.data;
229: if (ack->idx != idx)
230: errx(1, "index not expected");
231: imsg_free(&imsg);
1.1 deraadt 232: }
233:
1.35 nicm 234: wait_for_child:
235: close(pair[0]);
236: while (wait(NULL) == -1 && errno != ECHILD) {
237: if (errno != EINTR)
238: err(1, "wait");
1.30 nicm 239: }
1.35 nicm 240: _exit(0); /* let the child flush */
1.1 deraadt 241: }
242:
1.27 nicm 243: static void
1.35 nicm 244: send_message(struct imsgbuf *ibuf, void *msg, size_t msglen, int fd)
245: {
246: if (imsg_compose(ibuf, -1, -1, 0, fd, msg, msglen) != 1)
247: err(1, "imsg_compose");
248: if (imsg_flush(ibuf) != 0)
249: err(1, "imsg_flush");
250: }
251:
252: static int
253: read_message(struct imsgbuf *ibuf, struct imsg *imsg, pid_t from)
1.27 nicm 254: {
1.35 nicm 255: int n;
1.1 deraadt 256:
1.35 nicm 257: if ((n = imsg_read(ibuf)) == -1)
258: err(1, "imsg_read");
259: if (n == 0)
260: return (0);
1.27 nicm 261:
1.35 nicm 262: if ((n = imsg_get(ibuf, imsg)) == -1)
263: err(1, "imsg_get");
264: if (n == 0)
265: return (0);
1.31 nicm 266:
1.35 nicm 267: if ((pid_t)imsg->hdr.pid != from)
268: errx(1, "PIDs don't match");
1.27 nicm 269:
1.35 nicm 270: return (n);
1.27 nicm 271:
272: }
273:
274: static void
1.35 nicm 275: read_link(struct input_msg *msg, const char *path)
1.14 tedu 276: {
1.27 nicm 277: struct stat sb;
1.35 nicm 278: char lpath[PATH_MAX];
1.27 nicm 279: char *copy, *root;
280: int used;
281: ssize_t size;
282:
1.35 nicm 283: size = readlink(path, lpath, sizeof lpath);
1.27 nicm 284: if (size == -1) {
1.35 nicm 285: msg->link_error = errno;
1.14 tedu 286: return;
1.27 nicm 287: }
1.35 nicm 288: lpath[size] = '\0';
1.27 nicm 289:
1.35 nicm 290: if (*lpath == '/')
291: strlcpy(msg->link_path, lpath, sizeof msg->link_path);
1.27 nicm 292: else {
1.35 nicm 293: copy = xstrdup(path);
1.27 nicm 294:
295: root = dirname(copy);
296: if (*root == '\0' || strcmp(root, ".") == 0 ||
297: strcmp (root, "/") == 0)
1.35 nicm 298: strlcpy(msg->link_path, lpath, sizeof msg->link_path);
1.27 nicm 299: else {
1.35 nicm 300: used = snprintf(msg->link_path, sizeof msg->link_path,
301: "%s/%s", root, lpath);
302: if (used < 0 || (size_t)used >= sizeof msg->link_path) {
303: msg->link_error = ENAMETOOLONG;
1.27 nicm 304: return;
305: }
306: }
307:
308: free(copy);
309: }
310:
311: if (Lflag) {
1.35 nicm 312: if (stat(path, &msg->sb) == -1)
313: msg->error = errno;
1.27 nicm 314: } else {
1.35 nicm 315: if (stat(path, &sb) == -1)
316: msg->link_target = errno;
1.14 tedu 317: }
318: }
319:
1.35 nicm 320: static __dead void
321: child(int fd, pid_t parent, int argc, char **argv)
322: {
323: struct magic *m;
324: struct imsgbuf ibuf;
325: struct imsg imsg;
326: struct input_msg *msg;
327: struct input_ack ack;
328: struct input_file inf;
329: int i, idx;
330: size_t len, width = 0;
331:
332: m = magic_load(magicfp, magicpath, cflag || Wflag);
333: if (cflag) {
334: magic_dump(m);
335: exit(0);
336: }
337:
338: for (i = 0; i < argc; i++) {
339: len = strlen(argv[i]) + 1;
340: if (len > width)
341: width = len;
342: }
343:
344: imsg_init(&ibuf, fd);
345: for (;;) {
346: if (read_message(&ibuf, &imsg, parent) == 0)
347: break;
348: if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof *msg)
349: errx(1, "message too small");
350: msg = imsg.data;
351:
352: idx = msg->idx;
353: if (idx < 0 || idx >= argc)
354: errx(1, "index out of range");
355:
356: memset(&inf, 0, sizeof inf);
357: inf.m = m;
358: inf.msg = msg;
359:
360: inf.path = argv[idx];
361: inf.fd = imsg.fd;
362:
363: test_file(&inf, width);
364:
365: if (imsg.fd != -1)
366: close(imsg.fd);
367: imsg_free(&imsg);
368:
369: ack.idx = idx;
370: send_message(&ibuf, &ack, sizeof ack, -1);
371: }
372: exit(0);
373: }
374:
1.27 nicm 375: static void *
376: fill_buffer(struct input_file *inf)
1.1 deraadt 377: {
1.27 nicm 378: static void *buffer;
379: ssize_t got;
380: size_t left;
381: void *next;
382:
383: if (buffer == NULL)
384: buffer = xmalloc(FILE_READ_SIZE);
385:
386: next = buffer;
387: left = inf->size;
388: while (left != 0) {
389: got = read(inf->fd, next, left);
390: if (got == -1) {
391: if (errno == EINTR)
392: continue;
393: return NULL;
1.5 millert 394: }
1.27 nicm 395: if (got == 0)
396: break;
1.30 nicm 397: next = (char *)next + got;
1.27 nicm 398: left -= got;
399: }
400:
401: return buffer;
402: }
403:
404: static int
405: load_file(struct input_file *inf)
406: {
1.35 nicm 407: inf->size = inf->msg->sb.st_size;
1.27 nicm 408: if (inf->size > FILE_READ_SIZE)
409: inf->size = FILE_READ_SIZE;
1.34 nicm 410: if (inf->size == 0) {
1.35 nicm 411: if (!S_ISREG(inf->msg->sb.st_mode))
1.34 nicm 412: inf->size = FILE_READ_SIZE;
413: else
414: return (0);
415: }
1.27 nicm 416:
417: inf->base = mmap(NULL, inf->size, PROT_READ, MAP_PRIVATE, inf->fd, 0);
418: if (inf->base == MAP_FAILED) {
419: inf->base = fill_buffer(inf);
420: if (inf->base == NULL) {
421: xasprintf(&inf->result, "cannot read '%s' (%s)",
422: inf->path, strerror(errno));
423: return (1);
424: }
425: } else
426: inf->mapped = 1;
427: return (0);
428: }
1.5 millert 429:
1.27 nicm 430: static int
431: try_stat(struct input_file *inf)
432: {
1.35 nicm 433: if (inf->msg->error != 0) {
1.27 nicm 434: xasprintf(&inf->result, "cannot stat '%s' (%s)", inf->path,
1.35 nicm 435: strerror(inf->msg->error));
1.27 nicm 436: return (1);
437: }
438: if (sflag) {
1.35 nicm 439: switch (inf->msg->sb.st_mode & S_IFMT) {
1.27 nicm 440: case S_IFBLK:
441: case S_IFCHR:
442: case S_IFREG:
443: return (0);
1.5 millert 444: }
1.27 nicm 445: }
1.1 deraadt 446:
1.35 nicm 447: if (iflag && (inf->msg->sb.st_mode & S_IFMT) != S_IFREG) {
1.27 nicm 448: xasprintf(&inf->result, "application/x-not-regular-file");
449: return (1);
1.1 deraadt 450: }
451:
1.35 nicm 452: switch (inf->msg->sb.st_mode & S_IFMT) {
1.27 nicm 453: case S_IFDIR:
454: xasprintf(&inf->result, "directory");
455: return (1);
456: case S_IFLNK:
1.35 nicm 457: if (inf->msg->link_error != 0) {
1.27 nicm 458: xasprintf(&inf->result, "unreadable symlink '%s' (%s)",
1.35 nicm 459: inf->path, strerror(inf->msg->link_error));
1.27 nicm 460: return (1);
461: }
1.35 nicm 462: if (inf->msg->link_target == ELOOP)
1.27 nicm 463: xasprintf(&inf->result, "symbolic link in a loop");
1.35 nicm 464: else if (inf->msg->link_target != 0) {
1.27 nicm 465: xasprintf(&inf->result, "broken symbolic link to '%s'",
1.35 nicm 466: inf->msg->link_path);
1.27 nicm 467: } else {
468: xasprintf(&inf->result, "symbolic link to '%s'",
1.35 nicm 469: inf->msg->link_path);
1.27 nicm 470: }
471: return (1);
472: case S_IFSOCK:
473: xasprintf(&inf->result, "socket");
474: return (1);
475: case S_IFBLK:
476: xasprintf(&inf->result, "block special (%ld/%ld)",
1.35 nicm 477: (long)major(inf->msg->sb.st_rdev),
478: (long)minor(inf->msg->sb.st_rdev));
1.27 nicm 479: return (1);
480: case S_IFCHR:
481: xasprintf(&inf->result, "character special (%ld/%ld)",
1.35 nicm 482: (long)major(inf->msg->sb.st_rdev),
483: (long)minor(inf->msg->sb.st_rdev));
1.27 nicm 484: return (1);
485: case S_IFIFO:
486: xasprintf(&inf->result, "fifo (named pipe)");
487: return (1);
1.1 deraadt 488: }
1.27 nicm 489: return (0);
490: }
491:
492: static int
493: try_empty(struct input_file *inf)
494: {
495: if (inf->size != 0)
496: return (0);
497:
498: if (iflag)
499: xasprintf(&inf->result, "application/x-empty");
500: else
501: xasprintf(&inf->result, "empty");
502: return (1);
503: }
504:
505: static int
506: try_access(struct input_file *inf)
507: {
508: char tmp[256] = "";
509:
510: if (inf->fd != -1)
511: return (0);
1.1 deraadt 512:
1.35 nicm 513: if (inf->msg->sb.st_mode & (S_IWUSR|S_IWGRP|S_IWOTH))
1.27 nicm 514: strlcat(tmp, "writable, ", sizeof tmp);
1.35 nicm 515: if (inf->msg->sb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))
1.27 nicm 516: strlcat(tmp, "executable, ", sizeof tmp);
1.35 nicm 517: if (S_ISREG(inf->msg->sb.st_mode))
1.27 nicm 518: strlcat(tmp, "regular file, ", sizeof tmp);
519: strlcat(tmp, "no read permission", sizeof tmp);
520:
521: inf->result = xstrdup(tmp);
522: return (1);
1.1 deraadt 523: }
524:
1.27 nicm 525: static int
526: try_text(struct input_file *inf)
1.14 tedu 527: {
1.27 nicm 528: const char *type, *s;
529: int flags;
530:
531: flags = MAGIC_TEST_TEXT;
532: if (iflag)
533: flags |= MAGIC_TEST_MIME;
534:
535: type = text_get_type(inf->base, inf->size);
536: if (type == NULL)
537: return (0);
1.14 tedu 538:
1.27 nicm 539: s = magic_test(inf->m, inf->base, inf->size, flags);
540: if (s != NULL) {
541: inf->result = xstrdup(s);
542: return (1);
543: }
544:
545: s = text_try_words(inf->base, inf->size, flags);
546: if (s != NULL) {
547: if (iflag)
548: inf->result = xstrdup(s);
1.18 chl 549: else
1.27 nicm 550: xasprintf(&inf->result, "%s %s text", type, s);
551: return (1);
1.18 chl 552: }
1.14 tedu 553:
1.27 nicm 554: if (iflag)
555: inf->result = xstrdup("text/plain");
1.14 tedu 556: else
1.27 nicm 557: xasprintf(&inf->result, "%s text", type);
558: return (1);
1.14 tedu 559: }
560:
1.27 nicm 561: static int
562: try_magic(struct input_file *inf)
1.1 deraadt 563: {
1.27 nicm 564: const char *s;
565: int flags;
1.1 deraadt 566:
1.27 nicm 567: flags = 0;
568: if (iflag)
569: flags |= MAGIC_TEST_MIME;
570:
571: s = magic_test(inf->m, inf->base, inf->size, flags);
572: if (s != NULL) {
573: inf->result = xstrdup(s);
574: return (1);
1.1 deraadt 575: }
1.27 nicm 576: return (0);
1.14 tedu 577: }
1.1 deraadt 578:
1.27 nicm 579: static int
580: try_unknown(struct input_file *inf)
1.14 tedu 581: {
1.27 nicm 582: if (iflag)
583: xasprintf(&inf->result, "application/x-not-regular-file");
584: else
585: xasprintf(&inf->result, "data");
586: return (1);
1.1 deraadt 587: }
588:
1.27 nicm 589: static void
1.35 nicm 590: test_file(struct input_file *inf, size_t width)
1.1 deraadt 591: {
1.35 nicm 592: char *label;
593: int stop;
1.27 nicm 594:
595: stop = 0;
596: if (!stop)
597: stop = try_stat(inf);
598: if (!stop)
599: stop = try_access(inf);
600: if (!stop)
601: stop = load_file(inf);
602: if (!stop)
603: stop = try_empty(inf);
604: if (!stop)
605: stop = try_magic(inf);
606: if (!stop)
607: stop = try_text(inf);
608: if (!stop)
609: stop = try_unknown(inf);
610:
611: if (bflag)
612: printf("%s\n", inf->result);
1.35 nicm 613: else {
614: xasprintf(&label, "%s:", inf->path);
615: printf("%-*s %s\n", (int)width, label, inf->result);
616: free(label);
617: }
1.30 nicm 618: free(inf->result);
1.27 nicm 619:
620: if (inf->mapped && inf->base != NULL)
621: munmap(inf->base, inf->size);
1.1 deraadt 622: }