1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include "libbb.h"
30
31static int print_matchpathcon(char *path, int noprint)
32{
33 char *buf;
34 int rc = matchpathcon(path, 0, &buf);
35 if (rc < 0) {
36 bb_perror_msg("matchpathcon(%s) failed", path);
37 return 1;
38 }
39 if (!noprint)
40 printf("%s\t%s\n", path, buf);
41 else
42 puts(buf);
43
44 freecon(buf);
45 return 0;
46}
47
48#define OPT_NOT_PRINT (1<<0)
49#define OPT_NOT_TRANS (1<<1)
50#define OPT_FCONTEXT (1<<2)
51#define OPT_PREFIX (1<<3)
52#define OPT_VERIFY (1<<4)
53
54int matchpathcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
55int matchpathcon_main(int argc UNUSED_PARAM, char **argv)
56{
57 int error = 0;
58 unsigned opts;
59 char *fcontext, *prefix, *path;
60
61 opts = getopt32(argv, "^"
62 "nNf:p:V"
63 "\0"
64 "-1"
65 ":?:f--p:p--f"
66 , &fcontext, &prefix
67 );
68 argv += optind;
69
70 if (opts & OPT_NOT_TRANS) {
71 set_matchpathcon_flags(MATCHPATHCON_NOTRANS);
72 }
73 if (opts & OPT_FCONTEXT) {
74 if (matchpathcon_init(fcontext))
75 bb_perror_msg_and_die("error while processing %s", fcontext);
76 }
77 if (opts & OPT_PREFIX) {
78 if (matchpathcon_init_prefix(NULL, prefix))
79 bb_perror_msg_and_die("error while processing %s", prefix);
80 }
81
82 while ((path = *argv++) != NULL) {
83 security_context_t con;
84 int rc;
85
86 if (!(opts & OPT_VERIFY)) {
87 error += print_matchpathcon(path, opts & OPT_NOT_PRINT);
88 continue;
89 }
90
91 if (selinux_file_context_verify(path, 0)) {
92 printf("%s verified\n", path);
93 continue;
94 }
95
96 if (opts & OPT_NOT_TRANS)
97 rc = lgetfilecon_raw(path, &con);
98 else
99 rc = lgetfilecon(path, &con);
100
101 if (rc >= 0) {
102 printf("%s has context %s, should be ", path, con);
103 error += print_matchpathcon(path, 1);
104 freecon(con);
105 continue;
106 }
107 printf("actual context unknown: "STRERROR_FMT", should be " STRERROR_ERRNO);
108 error += print_matchpathcon(path, 1);
109 }
110 matchpathcon_fini();
111 return error;
112}
113