2007-02-07 00:58:50 +05:30
|
|
|
/* matchpathcon - get the default security context for the specified
|
|
|
|
* path from the file contexts configuration.
|
|
|
|
* based on libselinux-1.32
|
|
|
|
* Port to busybox: KaiGai Kohei <kaigai@kaigai.gr.jp>
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2007-02-07 00:58:50 +05:30
|
|
|
*/
|
2016-11-23 23:16:40 +05:30
|
|
|
//config:config MATCHPATHCON
|
2017-07-19 18:02:54 +05:30
|
|
|
//config: bool "matchpathcon (6.1 kb)"
|
2016-11-23 23:16:40 +05:30
|
|
|
//config: default n
|
|
|
|
//config: depends on SELINUX
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Enable support to get default security context of the
|
|
|
|
//config: specified path from the file contexts configuration.
|
2016-11-23 23:16:40 +05:30
|
|
|
|
|
|
|
//applet:IF_MATCHPATHCON(APPLET(matchpathcon, BB_DIR_USR_SBIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_MATCHPATHCON) += matchpathcon.o
|
2011-04-11 06:59:49 +05:30
|
|
|
|
|
|
|
//usage:#define matchpathcon_trivial_usage
|
|
|
|
//usage: "[-n] [-N] [-f file_contexts_file] [-p prefix] [-V]"
|
|
|
|
//usage:#define matchpathcon_full_usage "\n\n"
|
|
|
|
//usage: " -n Don't display path"
|
|
|
|
//usage: "\n -N Don't use translations"
|
|
|
|
//usage: "\n -f Use alternate file_context file"
|
|
|
|
//usage: "\n -p Use prefix to speed translations"
|
|
|
|
//usage: "\n -V Verify file context on disk matches defaults"
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2007-02-07 00:58:50 +05:30
|
|
|
|
|
|
|
static int print_matchpathcon(char *path, int noprint)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
int rc = matchpathcon(path, 0, &buf);
|
|
|
|
if (rc < 0) {
|
|
|
|
bb_perror_msg("matchpathcon(%s) failed", path);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!noprint)
|
|
|
|
printf("%s\t%s\n", path, buf);
|
|
|
|
else
|
2007-10-01 17:35:12 +05:30
|
|
|
puts(buf);
|
2007-02-07 00:58:50 +05:30
|
|
|
|
|
|
|
freecon(buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define OPT_NOT_PRINT (1<<0) /* -n */
|
|
|
|
#define OPT_NOT_TRANS (1<<1) /* -N */
|
|
|
|
#define OPT_FCONTEXT (1<<2) /* -f */
|
|
|
|
#define OPT_PREFIX (1<<3) /* -p */
|
|
|
|
#define OPT_VERIFY (1<<4) /* -V */
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int matchpathcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int matchpathcon_main(int argc UNUSED_PARAM, char **argv)
|
2007-02-07 00:58:50 +05:30
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
unsigned opts;
|
|
|
|
char *fcontext, *prefix, *path;
|
|
|
|
|
2007-03-09 14:14:30 +05:30
|
|
|
opt_complementary = "-1" /* at least one param reqd */
|
|
|
|
":?:f--p:p--f"; /* mutually exclusive */
|
2007-08-18 21:02:12 +05:30
|
|
|
opts = getopt32(argv, "nNf:p:V", &fcontext, &prefix);
|
2007-02-07 00:58:50 +05:30
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (opts & OPT_NOT_TRANS) {
|
2007-02-08 03:38:42 +05:30
|
|
|
set_matchpathcon_flags(MATCHPATHCON_NOTRANS);
|
2007-02-07 00:58:50 +05:30
|
|
|
}
|
|
|
|
if (opts & OPT_FCONTEXT) {
|
|
|
|
if (matchpathcon_init(fcontext))
|
|
|
|
bb_perror_msg_and_die("error while processing %s", fcontext);
|
|
|
|
}
|
|
|
|
if (opts & OPT_PREFIX) {
|
|
|
|
if (matchpathcon_init_prefix(NULL, prefix))
|
|
|
|
bb_perror_msg_and_die("error while processing %s", prefix);
|
|
|
|
}
|
|
|
|
|
2007-04-12 06:02:05 +05:30
|
|
|
while ((path = *argv++) != NULL) {
|
2007-02-07 00:58:50 +05:30
|
|
|
security_context_t con;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (!(opts & OPT_VERIFY)) {
|
2007-02-08 03:38:42 +05:30
|
|
|
error += print_matchpathcon(path, opts & OPT_NOT_PRINT);
|
2007-02-07 00:58:50 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selinux_file_context_verify(path, 0)) {
|
|
|
|
printf("%s verified\n", path);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts & OPT_NOT_TRANS)
|
|
|
|
rc = lgetfilecon_raw(path, &con);
|
|
|
|
else
|
|
|
|
rc = lgetfilecon(path, &con);
|
|
|
|
|
|
|
|
if (rc >= 0) {
|
|
|
|
printf("%s has context %s, should be ", path, con);
|
|
|
|
error += print_matchpathcon(path, 1);
|
|
|
|
freecon(con);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
printf("actual context unknown: %s, should be ", strerror(errno));
|
|
|
|
error += print_matchpathcon(path, 1);
|
|
|
|
}
|
|
|
|
matchpathcon_fini();
|
|
|
|
return error;
|
|
|
|
}
|