1 /* 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>
6 * Licensed under GPLv2, see file LICENSE in this source tree.
8 //config:config MATCHPATHCON
9 //config: bool "matchpathcon (6.1 kb)"
11 //config: depends on SELINUX
13 //config: Enable support to get default security context of the
14 //config: specified path from the file contexts configuration.
16 //applet:IF_MATCHPATHCON(APPLET(matchpathcon, BB_DIR_USR_SBIN, BB_SUID_DROP))
18 //kbuild:lib-$(CONFIG_MATCHPATHCON) += matchpathcon.o
20 //usage:#define matchpathcon_trivial_usage
21 //usage: "[-n] [-N] [-f file_contexts_file] [-p prefix] [-V]"
22 //usage:#define matchpathcon_full_usage "\n\n"
23 //usage: " -n Don't display path"
24 //usage: "\n -N Don't use translations"
25 //usage: "\n -f Use alternate file_context file"
26 //usage: "\n -p Use prefix to speed translations"
27 //usage: "\n -V Verify file context on disk matches defaults"
31 static int print_matchpathcon(char *path, int noprint)
34 int rc = matchpathcon(path, 0, &buf);
36 bb_perror_msg("matchpathcon(%s) failed", path);
40 printf("%s\t%s\n", path, buf);
48 #define OPT_NOT_PRINT (1<<0) /* -n */
49 #define OPT_NOT_TRANS (1<<1) /* -N */
50 #define OPT_FCONTEXT (1<<2) /* -f */
51 #define OPT_PREFIX (1<<3) /* -p */
52 #define OPT_VERIFY (1<<4) /* -V */
54 int matchpathcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
55 int matchpathcon_main(int argc UNUSED_PARAM, char **argv)
59 char *fcontext, *prefix, *path;
61 opts = getopt32(argv, "^"
64 "-1" /* at least one param reqd */
65 ":?:f--p:p--f" /* mutually exclusive */
70 if (opts & OPT_NOT_TRANS) {
71 set_matchpathcon_flags(MATCHPATHCON_NOTRANS);
73 if (opts & OPT_FCONTEXT) {
74 if (matchpathcon_init(fcontext))
75 bb_perror_msg_and_die("error while processing %s", fcontext);
77 if (opts & OPT_PREFIX) {
78 if (matchpathcon_init_prefix(NULL, prefix))
79 bb_perror_msg_and_die("error while processing %s", prefix);
82 while ((path = *argv++) != NULL) {
83 security_context_t con;
86 if (!(opts & OPT_VERIFY)) {
87 error += print_matchpathcon(path, opts & OPT_NOT_PRINT);
91 if (selinux_file_context_verify(path, 0)) {
92 printf("%s verified\n", path);
96 if (opts & OPT_NOT_TRANS)
97 rc = lgetfilecon_raw(path, &con);
99 rc = lgetfilecon(path, &con);
102 printf("%s has context %s, should be ", path, con);
103 error += print_matchpathcon(path, 1);
107 printf("actual context unknown: "STRERROR_FMT", should be " STRERROR_ERRNO);
108 error += print_matchpathcon(path, 1);