blob: 6945a029572f62dc0dc1eb14e1bb5f249189d1b9 [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 */
Denys Vlasenkoa8e52da2016-11-23 18:46:40 +01008//config:config MATCHPATHCON
Denys Vlasenkoae178ce2017-07-19 14:32:54 +02009//config: bool "matchpathcon (6.1 kb)"
Denys Vlasenkoa8e52da2016-11-23 18:46:40 +010010//config: default n
11//config: depends on SELINUX
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: Enable support to get default security context of the
14//config: specified path from the file contexts configuration.
Denys Vlasenkoa8e52da2016-11-23 18:46:40 +010015
16//applet:IF_MATCHPATHCON(APPLET(matchpathcon, BB_DIR_USR_SBIN, BB_SUID_DROP))
17
18//kbuild:lib-$(CONFIG_MATCHPATHCON) += matchpathcon.o
Pere Orga5bc8c002011-04-11 03:29:49 +020019
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"
28
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000029#include "libbb.h"
Denis Vlasenkod46d3c22007-02-06 19:28:50 +000030
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
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +000042 puts(buf);
Denis Vlasenkod46d3c22007-02-06 19:28:50 +000043
44 freecon(buf);
45 return 0;
46}
47
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 */
53
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000054int matchpathcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000055int matchpathcon_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkod46d3c22007-02-06 19:28:50 +000056{
57 int error = 0;
58 unsigned opts;
59 char *fcontext, *prefix, *path;
60
Denys Vlasenko22542ec2017-08-08 21:55:02 +020061 opts = getopt32(argv, "^"
62 "nNf:p:V"
63 "\0"
64 "-1" /* at least one param reqd */
65 ":?:f--p:p--f" /* mutually exclusive */
66 , &fcontext, &prefix
67 );
Denis Vlasenkod46d3c22007-02-06 19:28:50 +000068 argv += optind;
69
70 if (opts & OPT_NOT_TRANS) {
Denis Vlasenko8c6c6e92007-02-07 22:08:42 +000071 set_matchpathcon_flags(MATCHPATHCON_NOTRANS);
Denis Vlasenkod46d3c22007-02-06 19:28:50 +000072 }
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
Denis Vlasenko51742f42007-04-12 00:32:05 +000082 while ((path = *argv++) != NULL) {
Denis Vlasenkod46d3c22007-02-06 19:28:50 +000083 security_context_t con;
84 int rc;
85
86 if (!(opts & OPT_VERIFY)) {
Denis Vlasenko8c6c6e92007-02-07 22:08:42 +000087 error += print_matchpathcon(path, opts & OPT_NOT_PRINT);
Denis Vlasenkod46d3c22007-02-06 19:28:50 +000088 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 }
Denys Vlasenko6f97b302017-09-29 18:17:25 +0200107 printf("actual context unknown: "STRERROR_FMT", should be " STRERROR_ERRNO);
Denis Vlasenkod46d3c22007-02-06 19:28:50 +0000108 error += print_matchpathcon(path, 1);
109 }
110 matchpathcon_fini();
111 return error;
112}