xref: /openbsd-src/usr.bin/file/file.c (revision c658f39f11ea4ebb0d897d9f89de0577e696afe7)
1 /* $OpenBSD: file.c,v 1.72 2024/11/21 13:20:27 claudio Exp $ */
2 
3 /*
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.
17  */
18 
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 #include <sys/mman.h>
22 #include <sys/queue.h>
23 #include <sys/socket.h>
24 #include <sys/stat.h>
25 #include <sys/uio.h>
26 #include <sys/wait.h>
27 
28 #include <err.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <getopt.h>
32 #include <imsg.h>
33 #include <libgen.h>
34 #include <limits.h>
35 #include <pwd.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <time.h>
39 #include <unistd.h>
40 
41 #include "file.h"
42 #include "magic.h"
43 #include "xmalloc.h"
44 
45 struct input_msg {
46 	int		idx;
47 
48 	struct stat	sb;
49 	int		error;
50 
51 	char		link_path[PATH_MAX];
52 	int		link_error;
53 	int		link_target;
54 };
55 
56 struct input_ack {
57 	int		idx;
58 };
59 
60 struct input_file {
61 	struct magic		*m;
62 	struct input_msg	*msg;
63 
64 	const char		*path;
65 	int			 fd;
66 
67 	void			*base;
68 	size_t			 size;
69 	int			 mapped;
70 	char			*result;
71 };
72 
73 extern char	*__progname;
74 
75 __dead void	 usage(void);
76 
77 static int	 prepare_message(struct input_msg *, int, const char *);
78 static void	 send_message(struct imsgbuf *, void *, size_t, int);
79 static int	 read_message(struct imsgbuf *, struct imsg *, pid_t);
80 
81 static void	 read_link(struct input_msg *, const char *);
82 
83 static __dead void child(int, pid_t, int, char **);
84 
85 static void	 test_file(struct input_file *, size_t);
86 
87 static int	 try_stat(struct input_file *);
88 static int	 try_empty(struct input_file *);
89 static int	 try_access(struct input_file *);
90 static int	 try_text(struct input_file *);
91 static int	 try_magic(struct input_file *);
92 static int	 try_unknown(struct input_file *);
93 
94 static int	 bflag;
95 static int	 cflag;
96 static int	 iflag;
97 static int	 Lflag;
98 static int	 sflag;
99 static int	 Wflag;
100 
101 static char	*magicpath;
102 static FILE	*magicfp;
103 
104 static struct option longopts[] = {
105 	{ "brief",       no_argument, NULL, 'b' },
106 	{ "dereference", no_argument, NULL, 'L' },
107 	{ "mime",        no_argument, NULL, 'i' },
108 	{ "mime-type",   no_argument, NULL, 'i' },
109 	{ NULL,          0,           NULL, 0   }
110 };
111 
112 __dead void
113 usage(void)
114 {
115 	fprintf(stderr, "usage: %s [-bchiLsW] file ...\n", __progname);
116 	exit(1);
117 }
118 
119 int
120 main(int argc, char **argv)
121 {
122 	int			 opt, pair[2], fd, idx;
123 	char			*home;
124 	struct passwd		*pw;
125 	struct imsgbuf		 ibuf;
126 	struct imsg		 imsg;
127 	struct input_msg	 msg;
128 	struct input_ack	 ack;
129 	pid_t			 pid, parent;
130 
131 	tzset();
132 
133 	for (;;) {
134 		opt = getopt_long(argc, argv, "bchiLsW", longopts, NULL);
135 		if (opt == -1)
136 			break;
137 		switch (opt) {
138 		case 'b':
139 			bflag = 1;
140 			break;
141 		case 'c':
142 			cflag = 1;
143 			break;
144 		case 'h':
145 			Lflag = 0;
146 			break;
147 		case 'i':
148 			iflag = 1;
149 			break;
150 		case 'L':
151 			Lflag = 1;
152 			break;
153 		case 's':
154 			sflag = 1;
155 			break;
156 		case 'W':
157 			Wflag = 1;
158 			break;
159 		default:
160 			usage();
161 		}
162 	}
163 	argc -= optind;
164 	argv += optind;
165 	if (cflag) {
166 		if (argc != 0)
167 			usage();
168 	} else if (argc == 0)
169 		usage();
170 
171 	if (pledge("stdio rpath getpw recvfd sendfd id proc", NULL) == -1)
172 		err(1, "pledge");
173 
174 	magicfp = NULL;
175 	if (geteuid() != 0 && !issetugid()) {
176 		home = getenv("HOME");
177 		if (home == NULL || *home == '\0') {
178 			pw = getpwuid(getuid());
179 			if (pw != NULL)
180 				home = pw->pw_dir;
181 			else
182 				home = NULL;
183 		}
184 		if (home != NULL) {
185 			xasprintf(&magicpath, "%s/.magic", home);
186 			magicfp = fopen(magicpath, "r");
187 			if (magicfp == NULL)
188 				free(magicpath);
189 		}
190 	}
191 	if (magicfp == NULL) {
192 		magicpath = xstrdup("/etc/magic");
193 		magicfp = fopen(magicpath, "r");
194 	}
195 	if (magicfp == NULL)
196 		err(1, "%s", magicpath);
197 
198 	parent = getpid();
199 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
200 		err(1, "socketpair");
201 	switch (pid = fork()) {
202 	case -1:
203 		err(1, "fork");
204 	case 0:
205 		close(pair[0]);
206 		child(pair[1], parent, argc, argv);
207 	}
208 	close(pair[1]);
209 
210 	if (pledge("stdio rpath sendfd", NULL) == -1)
211 		err(1, "pledge");
212 
213 	fclose(magicfp);
214 	magicfp = NULL;
215 
216 	if (cflag)
217 		goto wait_for_child;
218 
219 	imsgbuf_init(&ibuf, pair[0]);
220 	for (idx = 0; idx < argc; idx++) {
221 		fd = prepare_message(&msg, idx, argv[idx]);
222 		send_message(&ibuf, &msg, sizeof msg, fd);
223 
224 		if (read_message(&ibuf, &imsg, pid) == 0)
225 			break;
226 		if (imsg_get_data(&imsg, &ack, sizeof ack) == -1)
227 			err(1, "bad message");
228 		if (ack.idx != idx)
229 			errx(1, "index not expected");
230 		imsg_free(&imsg);
231 	}
232 
233 wait_for_child:
234 	close(pair[0]);
235 	while (wait(NULL) == -1 && errno != ECHILD) {
236 		if (errno != EINTR)
237 			err(1, "wait");
238 	}
239 	_exit(0); /* let the child flush */
240 }
241 
242 static int
243 prepare_message(struct input_msg *msg, int idx, const char *path)
244 {
245 	int	fd, mode, error;
246 
247 	memset(msg, 0, sizeof *msg);
248 	msg->idx = idx;
249 
250 	if (strcmp(path, "-") == 0) {
251 		if (fstat(STDIN_FILENO, &msg->sb) == -1) {
252 			msg->error = errno;
253 			return (-1);
254 		}
255 		return (STDIN_FILENO);
256 	}
257 
258 	if (Lflag)
259 		error = stat(path, &msg->sb);
260 	else
261 		error = lstat(path, &msg->sb);
262 	if (error == -1) {
263 		msg->error = errno;
264 		return (-1);
265 	}
266 
267 	/*
268 	 * pledge(2) doesn't let us pass directory file descriptors around -
269 	 * but in fact we don't need them, so just don't open directories or
270 	 * symlinks (which could be to directories).
271 	 */
272 	mode = msg->sb.st_mode;
273 	if (!S_ISDIR(mode) && !S_ISLNK(mode)) {
274 		fd = open(path, O_RDONLY|O_NONBLOCK);
275 		if (fd == -1 && (errno == ENFILE || errno == EMFILE))
276 			err(1, "open");
277 	} else
278 		fd = -1;
279 	if (S_ISLNK(mode))
280 		read_link(msg, path);
281 	return (fd);
282 
283 }
284 
285 static void
286 send_message(struct imsgbuf *ibuf, void *msg, size_t msglen, int fd)
287 {
288 	if (imsg_compose(ibuf, -1, -1, 0, fd, msg, msglen) != 1)
289 		err(1, "imsg_compose");
290 	if (imsgbuf_flush(ibuf) != 0)
291 		err(1, "imsgbuf_flush");
292 }
293 
294 static int
295 read_message(struct imsgbuf *ibuf, struct imsg *imsg, pid_t from)
296 {
297 	int	n;
298 
299 	while ((n = imsgbuf_read(ibuf)) == -1 && errno == EAGAIN)
300 		/* nothing */ ;
301 	if (n == -1)
302 		err(1, "imsgbuf_read");
303 	if (n == 0)
304 		return (0);
305 
306 	if ((n = imsg_get(ibuf, imsg)) == -1)
307 		err(1, "imsg_get");
308 	if (n == 0)
309 		return (0);
310 
311 	if ((pid_t)imsg->hdr.pid != from)
312 		errx(1, "PIDs don't match");
313 
314 	return (n);
315 
316 }
317 
318 static void
319 read_link(struct input_msg *msg, const char *path)
320 {
321 	struct stat	 sb;
322 	char		 lpath[PATH_MAX];
323 	char		*copy, *root;
324 	int		 used;
325 	ssize_t		 size;
326 
327 	size = readlink(path, lpath, sizeof lpath - 1);
328 	if (size == -1) {
329 		msg->link_error = errno;
330 		return;
331 	}
332 	lpath[size] = '\0';
333 
334 	if (*lpath == '/')
335 		strlcpy(msg->link_path, lpath, sizeof msg->link_path);
336 	else {
337 		copy = xstrdup(path);
338 
339 		root = dirname(copy);
340 		if (*root == '\0' || strcmp(root, ".") == 0 ||
341 		    strcmp (root, "/") == 0)
342 			strlcpy(msg->link_path, lpath, sizeof msg->link_path);
343 		else {
344 			used = snprintf(msg->link_path, sizeof msg->link_path,
345 			    "%s/%s", root, lpath);
346 			if (used < 0 || (size_t)used >= sizeof msg->link_path) {
347 				msg->link_error = ENAMETOOLONG;
348 				free(copy);
349 				return;
350 			}
351 		}
352 
353 		free(copy);
354 	}
355 
356 	if (!Lflag && stat(path, &sb) == -1)
357 		msg->link_target = errno;
358 }
359 
360 static __dead void
361 child(int fd, pid_t parent, int argc, char **argv)
362 {
363 	struct passwd		*pw;
364 	struct magic		*m;
365 	struct imsgbuf		 ibuf;
366 	struct imsg		 imsg;
367 	struct input_msg	 msg;
368 	struct input_ack	 ack;
369 	struct input_file	 inf;
370 	int			 i, idx;
371 	size_t			 len, width = 0;
372 
373 	if (pledge("stdio getpw recvfd id", NULL) == -1)
374 		err(1, "pledge");
375 
376 	if (geteuid() == 0) {
377 		pw = getpwnam(FILE_USER);
378 		if (pw == NULL)
379 			errx(1, "unknown user %s", FILE_USER);
380 		if (setgroups(1, &pw->pw_gid) != 0)
381 			err(1, "setgroups");
382 		if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0)
383 			err(1, "setresgid");
384 		if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0)
385 			err(1, "setresuid");
386 	}
387 
388 	if (pledge("stdio recvfd", NULL) == -1)
389 		err(1, "pledge");
390 
391 	m = magic_load(magicfp, magicpath, cflag || Wflag);
392 	if (cflag) {
393 		magic_dump(m);
394 		exit(0);
395 	}
396 
397 	for (i = 0; i < argc; i++) {
398 		len = strlen(argv[i]) + 1;
399 		if (len > width)
400 			width = len;
401 	}
402 
403 	imsgbuf_init(&ibuf, fd);
404 	for (;;) {
405 		if (read_message(&ibuf, &imsg, parent) == 0)
406 			break;
407 		if (imsg_get_data(&imsg, &msg, sizeof msg) == -1)
408 			err(1, "bad message");
409 
410 		idx = msg.idx;
411 		if (idx < 0 || idx >= argc)
412 			errx(1, "index out of range");
413 
414 		memset(&inf, 0, sizeof inf);
415 		inf.m = m;
416 		inf.msg = &msg;
417 
418 		inf.path = argv[idx];
419 		inf.fd = imsg_get_fd(&imsg);
420 
421 		test_file(&inf, width);
422 
423 		if (inf.fd != -1)
424 			close(inf.fd);
425 		imsg_free(&imsg);
426 
427 		ack.idx = idx;
428 		send_message(&ibuf, &ack, sizeof ack, -1);
429 	}
430 	exit(0);
431 }
432 
433 static void *
434 fill_buffer(int fd, size_t size, size_t *used)
435 {
436 	static void	*buffer;
437 	ssize_t		 got;
438 	size_t		 left;
439 	void		*next;
440 
441 	if (buffer == NULL)
442 		buffer = xmalloc(FILE_READ_SIZE);
443 
444 	next = buffer;
445 	left = size;
446 	while (left != 0) {
447 		got = read(fd, next, left);
448 		if (got == -1) {
449 			if (errno == EINTR)
450 				continue;
451 			return (NULL);
452 		}
453 		if (got == 0)
454 			break;
455 		next = (char *)next + got;
456 		left -= got;
457 	}
458 	*used = size - left;
459 	return (buffer);
460 }
461 
462 static int
463 load_file(struct input_file *inf)
464 {
465 	size_t	used;
466 
467 	if (inf->msg->sb.st_size == 0 && S_ISREG(inf->msg->sb.st_mode))
468 		return (0); /* empty file */
469 	if (inf->msg->sb.st_size == 0 || inf->msg->sb.st_size > FILE_READ_SIZE)
470 		inf->size = FILE_READ_SIZE;
471 	else
472 		inf->size = inf->msg->sb.st_size;
473 
474 	if (!S_ISREG(inf->msg->sb.st_mode))
475 		goto try_read;
476 
477 	inf->base = mmap(NULL, inf->size, PROT_READ, MAP_PRIVATE, inf->fd, 0);
478 	if (inf->base == MAP_FAILED)
479 		goto try_read;
480 	inf->mapped = 1;
481 	return (0);
482 
483 try_read:
484 	inf->base = fill_buffer(inf->fd, inf->size, &used);
485 	if (inf->base == NULL) {
486 		xasprintf(&inf->result, "cannot read '%s' (%s)", inf->path,
487 		    strerror(errno));
488 		return (1);
489 	}
490 	inf->size = used;
491 	return (0);
492 }
493 
494 static int
495 try_stat(struct input_file *inf)
496 {
497 	if (inf->msg->error != 0) {
498 		xasprintf(&inf->result, "cannot stat '%s' (%s)", inf->path,
499 		    strerror(inf->msg->error));
500 		return (1);
501 	}
502 	if (sflag || strcmp(inf->path, "-") == 0) {
503 		switch (inf->msg->sb.st_mode & S_IFMT) {
504 		case S_IFIFO:
505 			if (strcmp(inf->path, "-") != 0)
506 				break;
507 		case S_IFBLK:
508 		case S_IFCHR:
509 		case S_IFREG:
510 			return (0);
511 		}
512 	}
513 
514 	if (iflag && (inf->msg->sb.st_mode & S_IFMT) != S_IFREG) {
515 		xasprintf(&inf->result, "application/x-not-regular-file");
516 		return (1);
517 	}
518 
519 	switch (inf->msg->sb.st_mode & S_IFMT) {
520 	case S_IFDIR:
521 		xasprintf(&inf->result, "directory");
522 		return (1);
523 	case S_IFLNK:
524 		if (inf->msg->link_error != 0) {
525 			xasprintf(&inf->result, "unreadable symlink '%s' (%s)",
526 			    inf->path, strerror(inf->msg->link_error));
527 			return (1);
528 		}
529 		if (inf->msg->link_target == ELOOP)
530 			xasprintf(&inf->result, "symbolic link in a loop");
531 		else if (inf->msg->link_target != 0) {
532 			xasprintf(&inf->result, "broken symbolic link to '%s'",
533 			    inf->msg->link_path);
534 		} else {
535 			xasprintf(&inf->result, "symbolic link to '%s'",
536 			    inf->msg->link_path);
537 		}
538 		return (1);
539 	case S_IFSOCK:
540 		xasprintf(&inf->result, "socket");
541 		return (1);
542 	case S_IFBLK:
543 		xasprintf(&inf->result, "block special (%lu/%lu)",
544 		    (long)major(inf->msg->sb.st_rdev),
545 		    (long)minor(inf->msg->sb.st_rdev));
546 		return (1);
547 	case S_IFCHR:
548 		xasprintf(&inf->result, "character special (%lu/%lu)",
549 		    (long)major(inf->msg->sb.st_rdev),
550 		    (long)minor(inf->msg->sb.st_rdev));
551 		return (1);
552 	case S_IFIFO:
553 		xasprintf(&inf->result, "fifo (named pipe)");
554 		return (1);
555 	}
556 	return (0);
557 }
558 
559 static int
560 try_empty(struct input_file *inf)
561 {
562 	if (inf->size != 0)
563 		return (0);
564 
565 	if (iflag)
566 		xasprintf(&inf->result, "application/x-empty");
567 	else
568 		xasprintf(&inf->result, "empty");
569 	return (1);
570 }
571 
572 static int
573 try_access(struct input_file *inf)
574 {
575 	char tmp[256] = "";
576 
577 	if (inf->msg->sb.st_size == 0 && S_ISREG(inf->msg->sb.st_mode))
578 		return (0); /* empty file */
579 	if (inf->fd != -1)
580 		return (0);
581 
582 	if (inf->msg->sb.st_mode & (S_IWUSR|S_IWGRP|S_IWOTH))
583 		strlcat(tmp, "writable, ", sizeof tmp);
584 	if (inf->msg->sb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))
585 		strlcat(tmp, "executable, ", sizeof tmp);
586 	if (S_ISREG(inf->msg->sb.st_mode))
587 		strlcat(tmp, "regular file, ", sizeof tmp);
588 	strlcat(tmp, "no read permission", sizeof tmp);
589 
590 	inf->result = xstrdup(tmp);
591 	return (1);
592 }
593 
594 static int
595 try_text(struct input_file *inf)
596 {
597 	const char	*type, *s;
598 	int		 flags;
599 
600 	flags = MAGIC_TEST_TEXT;
601 	if (iflag)
602 		flags |= MAGIC_TEST_MIME;
603 
604 	type = text_get_type(inf->base, inf->size);
605 	if (type == NULL)
606 		return (0);
607 
608 	s = magic_test(inf->m, inf->base, inf->size, flags);
609 	if (s != NULL) {
610 		inf->result = xstrdup(s);
611 		return (1);
612 	}
613 
614 	s = text_try_words(inf->base, inf->size, flags);
615 	if (s != NULL) {
616 		if (iflag)
617 			inf->result = xstrdup(s);
618 		else
619 			xasprintf(&inf->result, "%s %s text", type, s);
620 		return (1);
621 	}
622 
623 	if (iflag)
624 		inf->result = xstrdup("text/plain");
625 	else
626 		xasprintf(&inf->result, "%s text", type);
627 	return (1);
628 }
629 
630 static int
631 try_magic(struct input_file *inf)
632 {
633 	const char	*s;
634 	int		 flags;
635 
636 	flags = 0;
637 	if (iflag)
638 		flags |= MAGIC_TEST_MIME;
639 
640 	s = magic_test(inf->m, inf->base, inf->size, flags);
641 	if (s != NULL) {
642 		inf->result = xstrdup(s);
643 		return (1);
644 	}
645 	return (0);
646 }
647 
648 static int
649 try_unknown(struct input_file *inf)
650 {
651 	if (iflag)
652 		xasprintf(&inf->result, "application/octet-stream");
653 	else
654 		xasprintf(&inf->result, "data");
655 	return (1);
656 }
657 
658 static void
659 test_file(struct input_file *inf, size_t width)
660 {
661 	char	*label;
662 	int	 stop;
663 
664 	stop = 0;
665 	if (!stop)
666 		stop = try_stat(inf);
667 	if (!stop)
668 		stop = try_access(inf);
669 	if (!stop)
670 		stop = load_file(inf);
671 	if (!stop)
672 		stop = try_empty(inf);
673 	if (!stop)
674 		stop = try_magic(inf);
675 	if (!stop)
676 		stop = try_text(inf);
677 	if (!stop)
678 		stop = try_unknown(inf);
679 
680 	if (bflag)
681 		printf("%s\n", inf->result);
682 	else {
683 		if (strcmp(inf->path, "-") == 0)
684 			xasprintf(&label, "/dev/stdin:");
685 		else
686 			xasprintf(&label, "%s:", inf->path);
687 		printf("%-*s %s\n", (int)width, label, inf->result);
688 		free(label);
689 	}
690 	free(inf->result);
691 
692 	if (inf->mapped && inf->base != NULL)
693 		munmap(inf->base, inf->size);
694 }
695