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 * file - find type of a file or files - main program.
30 */
31
32#include "file.h"
33
34#ifndef	lint
35FILE_RCSID("@(#)$File: file.c,v 1.187 2020/06/07 17:38:30 christos Exp $")
36#endif	/* lint */
37
38#include "magic.h"
39
40#include <stdlib.h>
41#include <unistd.h>
42#include <string.h>
43#ifdef RESTORE_TIME
44# if (__COHERENT__ >= 0x420)
45#  include <sys/utime.h>
46# else
47#  ifdef USE_UTIMES
48#   include <sys/time.h>
49#  else
50#   include <utime.h>
51#  endif
52# endif
53#endif
54#ifdef HAVE_UNISTD_H
55#include <unistd.h>	/* for read() */
56#endif
57#ifdef HAVE_WCHAR_H
58#include <wchar.h>
59#endif
60
61#if defined(HAVE_GETOPT_H) && defined(HAVE_STRUCT_OPTION)
62# include <getopt.h>
63# ifndef HAVE_GETOPT_LONG
64int getopt_long(int, char * const *, const char *,
65    const struct option *, int *);
66# endif
67# else
68#  include "mygetopt.h"
69#endif
70
71#ifdef S_IFLNK
72# define IFLNK_h "h"
73# define IFLNK_L "L"
74#else
75# define IFLNK_h ""
76# define IFLNK_L ""
77#endif
78
79#define FILE_FLAGS	"bcCdE" IFLNK_h "ik" IFLNK_L "lNnprsSvzZ0"
80#define OPTSTRING	"bcCde:Ef:F:hiklLm:nNpP:rsSvzZ0"
81
82# define USAGE  \
83    "Usage: %s [-" FILE_FLAGS "] [--apple] [--extension] [--mime-encoding]\n" \
84    "            [--mime-type] [-e <testname>] [-F <separator>] " \
85    " [-f <namefile>]\n" \
86    "            [-m <magicfiles>] [-P <parameter=value>] [--exclude-quiet]\n" \
87    "            <file> ...\n" \
88    "       %s -C [-m <magicfiles>]\n" \
89    "       %s [--help]\n"
90
91private int 		/* Global command-line options 		*/
92	bflag = 0,	/* brief output format	 		*/
93	nopad = 0,	/* Don't pad output			*/
94	nobuffer = 0,   /* Do not buffer stdout 		*/
95	nulsep = 0;	/* Append '\0' to the separator		*/
96
97private const char *separator = ":";	/* Default field separator	*/
98private const struct option long_options[] = {
99#define OPT_HELP		1
100#define OPT_APPLE		2
101#define OPT_EXTENSIONS		3
102#define OPT_MIME_TYPE		4
103#define OPT_MIME_ENCODING	5
104#define OPT_EXCLUDE_QUIET	6
105#define OPT(shortname, longname, opt, def, doc)		\
106    {longname, opt, NULL, shortname},
107#define OPT_LONGONLY(longname, opt, def, doc, id)	\
108    {longname, opt, NULL, id},
109#include "file_opts.h"
110#undef OPT
111#undef OPT_LONGONLY
112    {0, 0, NULL, 0}
113    };
114
115private const struct {
116	const char *name;
117	int value;
118} nv[] = {
119	{ "apptype",	MAGIC_NO_CHECK_APPTYPE },
120	{ "ascii",	MAGIC_NO_CHECK_ASCII },
121	{ "cdf",	MAGIC_NO_CHECK_CDF },
122	{ "compress",	MAGIC_NO_CHECK_COMPRESS },
123	{ "csv",	MAGIC_NO_CHECK_CSV },
124	{ "elf",	MAGIC_NO_CHECK_ELF },
125	{ "encoding",	MAGIC_NO_CHECK_ENCODING },
126	{ "soft",	MAGIC_NO_CHECK_SOFT },
127	{ "tar",	MAGIC_NO_CHECK_TAR },
128	{ "json",	MAGIC_NO_CHECK_JSON },
129	{ "text",	MAGIC_NO_CHECK_TEXT },	/* synonym for ascii */
130	{ "tokens",	MAGIC_NO_CHECK_TOKENS }, /* OBSOLETE: ignored for backwards compatibility */
131};
132
133private struct {
134	const char *name;
135	int tag;
136	size_t value;
137	int set;
138	size_t def;
139	const char *desc;
140} pm[] = {
141	{ "bytes",	MAGIC_PARAM_BYTES_MAX, 0, 0, FILE_BYTES_MAX,
142	    "max bytes to look inside file" },
143	{ "elf_notes",	MAGIC_PARAM_ELF_NOTES_MAX, 0, 0, FILE_ELF_NOTES_MAX,
144	    "max ELF notes processed" },
145	{ "elf_phnum",	MAGIC_PARAM_ELF_PHNUM_MAX, 0, 0, FILE_ELF_PHNUM_MAX,
146	    "max ELF prog sections processed" },
147	{ "elf_shnum",	MAGIC_PARAM_ELF_SHNUM_MAX, 0, 0, FILE_ELF_SHNUM_MAX,
148	    "max ELF sections processed" },
149	{ "indir",	MAGIC_PARAM_INDIR_MAX, 0, 0, FILE_INDIR_MAX,
150	    "recursion limit for indirection" },
151	{ "name",	MAGIC_PARAM_NAME_MAX, 0, 0, FILE_NAME_MAX,
152	    "use limit for name/use magic" },
153	{ "regex",	MAGIC_PARAM_REGEX_MAX, 0, 0, FILE_REGEX_MAX,
154	    "length limit for REGEX searches" },
155};
156
157private int posixly;
158
159#ifdef __dead
160__dead
161#endif
162private void usage(void);
163private void docprint(const char *, int);
164#ifdef __dead
165__dead
166#endif
167private void help(void);
168
169private int unwrap(struct magic_set *, const char *);
170private int process(struct magic_set *ms, const char *, int);
171private struct magic_set *load(const char *, int);
172private void setparam(const char *);
173private void applyparam(magic_t);
174
175
176/*
177 * main - parse arguments and handle options
178 */
179int
180main(int argc, char *argv[])
181{
182	int c;
183	size_t i;
184	int action = 0, didsomefiles = 0, errflg = 0;
185	int flags = 0, e = 0;
186#ifdef HAVE_LIBSECCOMP
187	int sandbox = 1;
188#endif
189	struct magic_set *magic = NULL;
190	int longindex;
191	const char *magicfile = NULL;		/* where the magic is	*/
192	char *progname;
193
194	/* makes islower etc work for other langs */
195	(void)setlocale(LC_CTYPE, "");
196
197#ifdef __EMX__
198	/* sh-like wildcard expansion! Shouldn't hurt at least ... */
199	_wildcard(&argc, &argv);
200#endif
201
202	if ((progname = strrchr(argv[0], '/')) != NULL)
203		progname++;
204	else
205		progname = argv[0];
206
207	file_setprogname(progname);
208
209
210#ifdef S_IFLNK
211	posixly = getenv("POSIXLY_CORRECT") != NULL;
212	flags |=  posixly ? MAGIC_SYMLINK : 0;
213#endif
214	while ((c = getopt_long(argc, argv, OPTSTRING, long_options,
215	    &longindex)) != -1)
216		switch (c) {
217		case OPT_HELP:
218			help();
219			break;
220		case OPT_APPLE:
221			flags |= MAGIC_APPLE;
222			break;
223		case OPT_EXTENSIONS:
224			flags |= MAGIC_EXTENSION;
225			break;
226		case OPT_MIME_TYPE:
227			flags |= MAGIC_MIME_TYPE;
228			break;
229		case OPT_MIME_ENCODING:
230			flags |= MAGIC_MIME_ENCODING;
231			break;
232		case '0':
233			nulsep++;
234			break;
235		case 'b':
236			bflag++;
237			break;
238		case 'c':
239			action = FILE_CHECK;
240			break;
241		case 'C':
242			action = FILE_COMPILE;
243			break;
244		case 'd':
245			flags |= MAGIC_DEBUG|MAGIC_CHECK;
246			break;
247		case 'E':
248			flags |= MAGIC_ERROR;
249			break;
250		case 'e':
251		case OPT_EXCLUDE_QUIET:
252			for (i = 0; i < __arraycount(nv); i++)
253				if (strcmp(nv[i].name, optarg) == 0)
254					break;
255
256			if (i == __arraycount(nv)) {
257				if (c != OPT_EXCLUDE_QUIET)
258					errflg++;
259			} else
260				flags |= nv[i].value;
261			break;
262
263		case 'f':
264			if(action)
265				usage();
266			if (magic == NULL)
267				if ((magic = load(magicfile, flags)) == NULL)
268					return 1;
269			applyparam(magic);
270			e |= unwrap(magic, optarg);
271			++didsomefiles;
272			break;
273		case 'F':
274			separator = optarg;
275			break;
276		case 'i':
277			flags |= MAGIC_MIME;
278			break;
279		case 'k':
280			flags |= MAGIC_CONTINUE;
281			break;
282		case 'l':
283			action = FILE_LIST;
284			break;
285		case 'm':
286			magicfile = optarg;
287			break;
288		case 'n':
289			++nobuffer;
290			break;
291		case 'N':
292			++nopad;
293			break;
294#if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
295		case 'p':
296			flags |= MAGIC_PRESERVE_ATIME;
297			break;
298#endif
299		case 'P':
300			setparam(optarg);
301			break;
302		case 'r':
303			flags |= MAGIC_RAW;
304			break;
305		case 's':
306			flags |= MAGIC_DEVICES;
307			break;
308		case 'S':
309#ifdef HAVE_LIBSECCOMP
310			sandbox = 0;
311#endif
312			break;
313		case 'v':
314			if (magicfile == NULL)
315				magicfile = magic_getpath(magicfile, action);
316			(void)fprintf(stdout, "%s-%s\n", file_getprogname(),
317			    VERSION);
318			(void)fprintf(stdout, "magic file from %s\n",
319			    magicfile);
320#ifdef HAVE_LIBSECCOMP
321			(void)fprintf(stdout, "seccomp support included\n");
322#endif
323			return 0;
324		case 'z':
325			flags |= MAGIC_COMPRESS;
326			break;
327
328		case 'Z':
329			flags |= MAGIC_COMPRESS|MAGIC_COMPRESS_TRANSP;
330			break;
331#ifdef S_IFLNK
332		case 'L':
333			flags |= MAGIC_SYMLINK;
334			break;
335		case 'h':
336			flags &= ~MAGIC_SYMLINK;
337			break;
338#endif
339		case '?':
340		default:
341			errflg++;
342			break;
343		}
344
345	if (errflg) {
346		usage();
347	}
348	if (e)
349		return e;
350
351#ifdef HAVE_LIBSECCOMP
352#if 0
353	if (sandbox && enable_sandbox_basic() == -1)
354#else
355	if (sandbox && enable_sandbox_full() == -1)
356#endif
357		file_err(EXIT_FAILURE, "SECCOMP initialisation failed");
358#endif /* HAVE_LIBSECCOMP */
359
360	if (MAGIC_VERSION != magic_version())
361		file_warnx("Compiled magic version [%d] "
362		    "does not match with shared library magic version [%d]\n",
363		    MAGIC_VERSION, magic_version());
364
365	switch(action) {
366	case FILE_CHECK:
367	case FILE_COMPILE:
368	case FILE_LIST:
369		/*
370		 * Don't try to check/compile ~/.magic unless we explicitly
371		 * ask for it.
372		 */
373		magic = magic_open(flags|MAGIC_CHECK);
374		if (magic == NULL) {
375			file_warn("Can't create magic");
376			return 1;
377		}
378
379
380		switch(action) {
381		case FILE_CHECK:
382			c = magic_check(magic, magicfile);
383			break;
384		case FILE_COMPILE:
385			c = magic_compile(magic, magicfile);
386			break;
387		case FILE_LIST:
388			c = magic_list(magic, magicfile);
389			break;
390		default:
391			abort();
392		}
393		if (c == -1) {
394			file_warnx("%s", magic_error(magic));
395			e = 1;
396			goto out;
397		}
398		goto out;
399	default:
400		if (magic == NULL)
401			if ((magic = load(magicfile, flags)) == NULL)
402				return 1;
403		applyparam(magic);
404	}
405
406	if (optind == argc) {
407		if (!didsomefiles)
408			usage();
409	}
410	else {
411		size_t j, wid, nw;
412		for (wid = 0, j = CAST(size_t, optind); j < CAST(size_t, argc);
413		    j++) {
414			nw = file_mbswidth(argv[j]);
415			if (nw > wid)
416				wid = nw;
417		}
418		/*
419		 * If bflag is only set twice, set it depending on
420		 * number of files [this is undocumented, and subject to change]
421		 */
422		if (bflag == 2) {
423			bflag = optind >= argc - 1;
424		}
425		for (; optind < argc; optind++)
426			e |= process(magic, argv[optind], wid);
427	}
428
429out:
430	if (magic)
431		magic_close(magic);
432	return e;
433}
434
435private void
436applyparam(magic_t magic)
437{
438	size_t i;
439
440	for (i = 0; i < __arraycount(pm); i++) {
441		if (!pm[i].set)
442			continue;
443		if (magic_setparam(magic, pm[i].tag, &pm[i].value) == -1)
444			file_err(EXIT_FAILURE, "Can't set %s", pm[i].name);
445	}
446}
447
448private void
449setparam(const char *p)
450{
451	size_t i;
452	char *s;
453
454	if ((s = strchr(p, '=')) == NULL)
455		goto badparm;
456
457	for (i = 0; i < __arraycount(pm); i++) {
458		if (strncmp(p, pm[i].name, s - p) != 0)
459			continue;
460		pm[i].value = atoi(s + 1);
461		pm[i].set = 1;
462		return;
463	}
464badparm:
465	file_errx(EXIT_FAILURE, "Unknown param %s", p);
466}
467
468private struct magic_set *
469/*ARGSUSED*/
470load(const char *magicfile, int flags)
471{
472	struct magic_set *magic = magic_open(flags);
473	const char *e;
474
475	if (magic == NULL) {
476		file_warn("Can't create magic");
477		return NULL;
478	}
479	if (magic_load(magic, magicfile) == -1) {
480		file_warn("%s", magic_error(magic));
481		magic_close(magic);
482		return NULL;
483	}
484	if ((e = magic_error(magic)) != NULL)
485		file_warn("%s", e);
486	return magic;
487}
488
489/*
490 * unwrap -- read a file of filenames, do each one.
491 */
492private int
493unwrap(struct magic_set *ms, const char *fn)
494{
495	FILE *f;
496	ssize_t len;
497	char *line = NULL;
498	size_t llen = 0;
499	int wid = 0, cwid;
500	int e = 0;
501
502	if (strcmp("-", fn) == 0) {
503		f = stdin;
504		wid = 1;
505	} else {
506		if ((f = fopen(fn, "r")) == NULL) {
507			file_warn("Cannot open `%s'", fn);
508			return 1;
509		}
510
511		while ((len = getline(&line, &llen, f)) > 0) {
512			if (line[len - 1] == '\n')
513				line[len - 1] = '\0';
514			cwid = file_mbswidth(line);
515			if (cwid > wid)
516				wid = cwid;
517		}
518
519		rewind(f);
520	}
521
522	while ((len = getline(&line, &llen, f)) > 0) {
523		if (line[len - 1] == '\n')
524			line[len - 1] = '\0';
525		e |= process(ms, line, wid);
526		if(nobuffer)
527			(void)fflush(stdout);
528	}
529
530	free(line);
531	(void)fclose(f);
532	return e;
533}
534
535/*
536 * Called for each input file on the command line (or in a list of files)
537 */
538private int
539process(struct magic_set *ms, const char *inname, int wid)
540{
541	const char *type, c = nulsep > 1 ? '\0' : '\n';
542	int std_in = strcmp(inname, "-") == 0;
543
544	if (wid > 0 && !bflag) {
545		(void)printf("%s", std_in ? "/dev/stdin" : inname);
546		if (nulsep)
547			(void)putc('\0', stdout);
548		if (nulsep < 2) {
549			(void)printf("%s", separator);
550			(void)printf("%*s ", CAST(int, nopad ? 0
551			    : (wid - file_mbswidth(inname))), "");
552		}
553	}
554
555	type = magic_file(ms, std_in ? NULL : inname);
556
557	if (type == NULL) {
558		(void)printf("ERROR: %s%c", magic_error(ms), c);
559		return 1;
560	} else {
561		(void)printf("%s%c", type, c);
562		return 0;
563	}
564}
565
566protected size_t
567file_mbswidth(const char *s)
568{
569#if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
570	size_t bytesconsumed, old_n, n, width = 0;
571	mbstate_t state;
572	wchar_t nextchar;
573	(void)memset(&state, 0, sizeof(mbstate_t));
574	old_n = n = strlen(s);
575
576	while (n > 0) {
577		bytesconsumed = mbrtowc(&nextchar, s, n, &state);
578		if (bytesconsumed == CAST(size_t, -1) ||
579		    bytesconsumed == CAST(size_t, -2)) {
580			/* Something went wrong, return something reasonable */
581			return old_n;
582		}
583		if (s[0] == '\n') {
584			/*
585			 * do what strlen() would do, so that caller
586			 * is always right
587			 */
588			width++;
589		} else {
590			int w = wcwidth(nextchar);
591			if (w > 0)
592				width += w;
593		}
594
595		s += bytesconsumed, n -= bytesconsumed;
596	}
597	return width;
598#else
599	return strlen(s);
600#endif
601}
602
603private void
604usage(void)
605{
606	const char *pn = file_getprogname();
607	(void)fprintf(stderr, USAGE, pn, pn, pn);
608	exit(EXIT_FAILURE);
609}
610
611private void
612defprint(int def)
613{
614	if (!def)
615		return;
616	if (((def & 1) && posixly) || ((def & 2) && !posixly))
617		fprintf(stdout, " (default)");
618	fputc('\n', stdout);
619}
620
621private void
622docprint(const char *opts, int def)
623{
624	size_t i;
625	int comma, pad;
626	char *sp, *p;
627
628	p = strchr(opts, '%');
629	if (p == NULL) {
630		fprintf(stdout, "%s", opts);
631		defprint(def);
632		return;
633	}
634
635	for (sp = p - 1; sp > opts && *sp == ' '; sp--)
636		continue;
637
638	fprintf(stdout, "%.*s", CAST(int, p - opts), opts);
639	pad = (int)CAST(int, p - sp - 1);
640
641	switch (*++p) {
642	case 'e':
643		comma = 0;
644		for (i = 0; i < __arraycount(nv); i++) {
645			fprintf(stdout, "%s%s", comma++ ? ", " : "", nv[i].name);
646			if (i && i % 5 == 0 && i != __arraycount(nv) - 1) {
647				fprintf(stdout, ",\n%*s", pad, "");
648				comma = 0;
649			}
650		}
651		break;
652	case 'P':
653		for (i = 0; i < __arraycount(pm); i++) {
654			fprintf(stdout, "%9s %7zu %s", pm[i].name, pm[i].def,
655			    pm[i].desc);
656			if (i != __arraycount(pm) - 1)
657				fprintf(stdout, "\n%*s", pad, "");
658		}
659		break;
660	default:
661		file_errx(EXIT_FAILURE, "Unknown escape `%c' in long options",
662		   *p);
663		break;
664	}
665	fprintf(stdout, "%s", opts + (p - opts) + 1);
666
667}
668
669private void
670help(void)
671{
672	(void)fputs(
673"Usage: file [OPTION...] [FILE...]\n"
674"Determine type of FILEs.\n"
675"\n", stdout);
676#define OPT(shortname, longname, opt, def, doc)      \
677	fprintf(stdout, "  -%c, --" longname, shortname), \
678	docprint(doc, def);
679#define OPT_LONGONLY(longname, opt, def, doc, id)    	\
680	fprintf(stdout, "      --" longname),	\
681	docprint(doc, def);
682#include "file_opts.h"
683#undef OPT
684#undef OPT_LONGONLY
685	fprintf(stdout, "\nReport bugs to https://bugs.astron.com/\n");
686	exit(EXIT_SUCCESS);
687}
688
689private const char *file_progname;
690
691protected void
692file_setprogname(const char *progname)
693{
694	file_progname = progname;
695}
696
697protected const char *
698file_getprogname(void)
699{
700	return file_progname;
701}
702
703protected void
704file_err(int e, const char *fmt, ...)
705{
706	va_list ap;
707	int se = errno;
708
709	va_start(ap, fmt);
710	fprintf(stderr, "%s: ", file_progname);
711	vfprintf(stderr, fmt, ap);
712	va_end(ap);
713	if (se)
714		fprintf(stderr, " (%s)\n", strerror(se));
715	else
716		fputc('\n', stderr);
717	exit(e);
718}
719
720protected void
721file_errx(int e, const char *fmt, ...)
722{
723	va_list ap;
724
725	va_start(ap, fmt);
726	fprintf(stderr, "%s: ", file_progname);
727	vfprintf(stderr, fmt, ap);
728	va_end(ap);
729	fprintf(stderr, "\n");
730	exit(e);
731}
732
733protected void
734file_warn(const char *fmt, ...)
735{
736	va_list ap;
737	int se = errno;
738
739	va_start(ap, fmt);
740	fprintf(stderr, "%s: ", file_progname);
741	vfprintf(stderr, fmt, ap);
742	va_end(ap);
743	if (se)
744		fprintf(stderr, " (%s)\n", strerror(se));
745	else
746		fputc('\n', stderr);
747	errno = se;
748}
749
750protected void
751file_warnx(const char *fmt, ...)
752{
753	va_list ap;
754	int se = errno;
755
756	va_start(ap, fmt);
757	fprintf(stderr, "%s: ", file_progname);
758	vfprintf(stderr, fmt, ap);
759	va_end(ap);
760	fprintf(stderr, "\n");
761	errno = se;
762}
763