blob: f0338927e35fe5d2d5719408dd281fc8181c7eeb [file] [log] [blame]
Denis Vlasenkod46d3c22007-02-06 19:28:50 +00001/* matchpathcon - get the default security context for the specified
2 * path from the file contexts configuration.
3 * based on libselinux-1.32
4 * Port to busybox: KaiGai Kohei <kaigai@kaigai.gr.jp>
5 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkod46d3c22007-02-06 19:28:50 +00007 */
Pere Orga5bc8c002011-04-11 03:29:49 +02008
9//usage:#define matchpathcon_trivial_usage
10//usage: "[-n] [-N] [-f file_contexts_file] [-p prefix] [-V]"
11//usage:#define matchpathcon_full_usage "\n\n"
12//usage: " -n Don't display path"
13//usage: "\n -N Don't use translations"
14//usage: "\n -f Use alternate file_context file"
15//usage: "\n -p Use prefix to speed translations"
16//usage: "\n -V Verify file context on disk matches defaults"
17
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000018#include "libbb.h"
Denis Vlasenkod46d3c22007-02-06 19:28:50 +000019
20static int print_matchpathcon(char *path, int noprint)
21{
22 char *buf;
23 int rc = matchpathcon(path, 0, &buf);
24 if (rc < 0) {
25 bb_perror_msg("matchpathcon(%s) failed", path);
26 return 1;
27 }
28 if (!noprint)
29 printf("%s\t%s\n", path, buf);
30 else
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +000031 puts(buf);
Denis Vlasenkod46d3c22007-02-06 19:28:50 +000032
33 freecon(buf);
34 return 0;
35}
36
37#define OPT_NOT_PRINT (1<<0) /* -n */
38#define OPT_NOT_TRANS (1<<1) /* -N */
39#define OPT_FCONTEXT (1<<2) /* -f */
40#define OPT_PREFIX (1<<3) /* -p */
41#define OPT_VERIFY (1<<4) /* -V */
42
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000043int matchpathcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000044int matchpathcon_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkod46d3c22007-02-06 19:28:50 +000045{
46 int error = 0;
47 unsigned opts;
48 char *fcontext, *prefix, *path;
49
Bernhard Reutner-Fischer12eda0a2007-03-09 08:44:30 +000050 opt_complementary = "-1" /* at least one param reqd */
51 ":?:f--p:p--f"; /* mutually exclusive */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000052 opts = getopt32(argv, "nNf:p:V", &fcontext, &prefix);
Denis Vlasenkod46d3c22007-02-06 19:28:50 +000053 argv += optind;
54
55 if (opts & OPT_NOT_TRANS) {
Denis Vlasenko8c6c6e92007-02-07 22:08:42 +000056 set_matchpathcon_flags(MATCHPATHCON_NOTRANS);
Denis Vlasenkod46d3c22007-02-06 19:28:50 +000057 }
58 if (opts & OPT_FCONTEXT) {
59 if (matchpathcon_init(fcontext))
60 bb_perror_msg_and_die("error while processing %s", fcontext);
61 }
62 if (opts & OPT_PREFIX) {
63 if (matchpathcon_init_prefix(NULL, prefix))
64 bb_perror_msg_and_die("error while processing %s", prefix);
65 }
66
maxwen27116ba2015-08-14 21:41:28 +020067#ifdef ANDROID
68 if (!(opts & (OPT_FCONTEXT | OPT_PREFIX))) {
69 matchpathcon_init(selinux_file_contexts_path());
70 }
71#endif
Denis Vlasenko51742f42007-04-12 00:32:05 +000072 while ((path = *argv++) != NULL) {
Denis Vlasenkod46d3c22007-02-06 19:28:50 +000073 security_context_t con;
74 int rc;
75
76 if (!(opts & OPT_VERIFY)) {
Denis Vlasenko8c6c6e92007-02-07 22:08:42 +000077 error += print_matchpathcon(path, opts & OPT_NOT_PRINT);
Denis Vlasenkod46d3c22007-02-06 19:28:50 +000078 continue;
79 }
80
81 if (selinux_file_context_verify(path, 0)) {
82 printf("%s verified\n", path);
83 continue;
84 }
85
86 if (opts & OPT_NOT_TRANS)
87 rc = lgetfilecon_raw(path, &con);
88 else
89 rc = lgetfilecon(path, &con);
90
91 if (rc >= 0) {
92 printf("%s has context %s, should be ", path, con);
93 error += print_matchpathcon(path, 1);
94 freecon(con);
95 continue;
96 }
97 printf("actual context unknown: %s, should be ", strerror(errno));
98 error += print_matchpathcon(path, 1);
99 }
100 matchpathcon_fini();
101 return error;
102}