Denis Vlasenko | d46d3c2 | 2007-02-06 19:28:50 +0000 | [diff] [blame] | 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> |
| 5 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | d46d3c2 | 2007-02-06 19:28:50 +0000 | [diff] [blame] | 7 | */ |
Denys Vlasenko | a8e52da | 2016-11-23 18:46:40 +0100 | [diff] [blame] | 8 | //config:config MATCHPATHCON |
Denys Vlasenko | ae178ce | 2017-07-19 14:32:54 +0200 | [diff] [blame] | 9 | //config: bool "matchpathcon (6.1 kb)" |
Denys Vlasenko | a8e52da | 2016-11-23 18:46:40 +0100 | [diff] [blame] | 10 | //config: default n |
| 11 | //config: depends on SELINUX |
| 12 | //config: help |
| 13 | //config: Enable support to get default security context of the |
| 14 | //config: specified path from the file contexts configuration. |
| 15 | |
| 16 | //applet:IF_MATCHPATHCON(APPLET(matchpathcon, BB_DIR_USR_SBIN, BB_SUID_DROP)) |
| 17 | |
| 18 | //kbuild:lib-$(CONFIG_MATCHPATHCON) += matchpathcon.o |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 19 | |
| 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 Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 29 | #include "libbb.h" |
Denis Vlasenko | d46d3c2 | 2007-02-06 19:28:50 +0000 | [diff] [blame] | 30 | |
| 31 | static 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 Vlasenko | feb7ae7 | 2007-10-01 12:05:12 +0000 | [diff] [blame] | 42 | puts(buf); |
Denis Vlasenko | d46d3c2 | 2007-02-06 19:28:50 +0000 | [diff] [blame] | 43 | |
| 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 Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 54 | int matchpathcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 55 | int matchpathcon_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | d46d3c2 | 2007-02-06 19:28:50 +0000 | [diff] [blame] | 56 | { |
| 57 | int error = 0; |
| 58 | unsigned opts; |
| 59 | char *fcontext, *prefix, *path; |
| 60 | |
Bernhard Reutner-Fischer | 12eda0a | 2007-03-09 08:44:30 +0000 | [diff] [blame] | 61 | opt_complementary = "-1" /* at least one param reqd */ |
| 62 | ":?:f--p:p--f"; /* mutually exclusive */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 63 | opts = getopt32(argv, "nNf:p:V", &fcontext, &prefix); |
Denis Vlasenko | d46d3c2 | 2007-02-06 19:28:50 +0000 | [diff] [blame] | 64 | argv += optind; |
| 65 | |
| 66 | if (opts & OPT_NOT_TRANS) { |
Denis Vlasenko | 8c6c6e9 | 2007-02-07 22:08:42 +0000 | [diff] [blame] | 67 | set_matchpathcon_flags(MATCHPATHCON_NOTRANS); |
Denis Vlasenko | d46d3c2 | 2007-02-06 19:28:50 +0000 | [diff] [blame] | 68 | } |
| 69 | if (opts & OPT_FCONTEXT) { |
| 70 | if (matchpathcon_init(fcontext)) |
| 71 | bb_perror_msg_and_die("error while processing %s", fcontext); |
| 72 | } |
| 73 | if (opts & OPT_PREFIX) { |
| 74 | if (matchpathcon_init_prefix(NULL, prefix)) |
| 75 | bb_perror_msg_and_die("error while processing %s", prefix); |
| 76 | } |
| 77 | |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 78 | while ((path = *argv++) != NULL) { |
Denis Vlasenko | d46d3c2 | 2007-02-06 19:28:50 +0000 | [diff] [blame] | 79 | security_context_t con; |
| 80 | int rc; |
| 81 | |
| 82 | if (!(opts & OPT_VERIFY)) { |
Denis Vlasenko | 8c6c6e9 | 2007-02-07 22:08:42 +0000 | [diff] [blame] | 83 | error += print_matchpathcon(path, opts & OPT_NOT_PRINT); |
Denis Vlasenko | d46d3c2 | 2007-02-06 19:28:50 +0000 | [diff] [blame] | 84 | continue; |
| 85 | } |
| 86 | |
| 87 | if (selinux_file_context_verify(path, 0)) { |
| 88 | printf("%s verified\n", path); |
| 89 | continue; |
| 90 | } |
| 91 | |
| 92 | if (opts & OPT_NOT_TRANS) |
| 93 | rc = lgetfilecon_raw(path, &con); |
| 94 | else |
| 95 | rc = lgetfilecon(path, &con); |
| 96 | |
| 97 | if (rc >= 0) { |
| 98 | printf("%s has context %s, should be ", path, con); |
| 99 | error += print_matchpathcon(path, 1); |
| 100 | freecon(con); |
| 101 | continue; |
| 102 | } |
| 103 | printf("actual context unknown: %s, should be ", strerror(errno)); |
| 104 | error += print_matchpathcon(path, 1); |
| 105 | } |
| 106 | matchpathcon_fini(); |
| 107 | return error; |
| 108 | } |