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 | */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 8 | #include "libbb.h" |
Denis Vlasenko | d46d3c2 | 2007-02-06 19:28:50 +0000 | [diff] [blame] | 9 | |
| 10 | static int print_matchpathcon(char *path, int noprint) |
| 11 | { |
| 12 | char *buf; |
| 13 | int rc = matchpathcon(path, 0, &buf); |
| 14 | if (rc < 0) { |
| 15 | bb_perror_msg("matchpathcon(%s) failed", path); |
| 16 | return 1; |
| 17 | } |
| 18 | if (!noprint) |
| 19 | printf("%s\t%s\n", path, buf); |
| 20 | else |
Denis Vlasenko | feb7ae7 | 2007-10-01 12:05:12 +0000 | [diff] [blame] | 21 | puts(buf); |
Denis Vlasenko | d46d3c2 | 2007-02-06 19:28:50 +0000 | [diff] [blame] | 22 | |
| 23 | freecon(buf); |
| 24 | return 0; |
| 25 | } |
| 26 | |
| 27 | #define OPT_NOT_PRINT (1<<0) /* -n */ |
| 28 | #define OPT_NOT_TRANS (1<<1) /* -N */ |
| 29 | #define OPT_FCONTEXT (1<<2) /* -f */ |
| 30 | #define OPT_PREFIX (1<<3) /* -p */ |
| 31 | #define OPT_VERIFY (1<<4) /* -V */ |
| 32 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 33 | int matchpathcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 34 | int matchpathcon_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | d46d3c2 | 2007-02-06 19:28:50 +0000 | [diff] [blame] | 35 | { |
| 36 | int error = 0; |
| 37 | unsigned opts; |
| 38 | char *fcontext, *prefix, *path; |
| 39 | |
Bernhard Reutner-Fischer | 12eda0a | 2007-03-09 08:44:30 +0000 | [diff] [blame] | 40 | opt_complementary = "-1" /* at least one param reqd */ |
| 41 | ":?:f--p:p--f"; /* mutually exclusive */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 42 | opts = getopt32(argv, "nNf:p:V", &fcontext, &prefix); |
Denis Vlasenko | d46d3c2 | 2007-02-06 19:28:50 +0000 | [diff] [blame] | 43 | argv += optind; |
| 44 | |
| 45 | if (opts & OPT_NOT_TRANS) { |
Denis Vlasenko | 8c6c6e9 | 2007-02-07 22:08:42 +0000 | [diff] [blame] | 46 | set_matchpathcon_flags(MATCHPATHCON_NOTRANS); |
Denis Vlasenko | d46d3c2 | 2007-02-06 19:28:50 +0000 | [diff] [blame] | 47 | } |
| 48 | if (opts & OPT_FCONTEXT) { |
| 49 | if (matchpathcon_init(fcontext)) |
| 50 | bb_perror_msg_and_die("error while processing %s", fcontext); |
| 51 | } |
| 52 | if (opts & OPT_PREFIX) { |
| 53 | if (matchpathcon_init_prefix(NULL, prefix)) |
| 54 | bb_perror_msg_and_die("error while processing %s", prefix); |
| 55 | } |
| 56 | |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 57 | while ((path = *argv++) != NULL) { |
Denis Vlasenko | d46d3c2 | 2007-02-06 19:28:50 +0000 | [diff] [blame] | 58 | security_context_t con; |
| 59 | int rc; |
| 60 | |
| 61 | if (!(opts & OPT_VERIFY)) { |
Denis Vlasenko | 8c6c6e9 | 2007-02-07 22:08:42 +0000 | [diff] [blame] | 62 | error += print_matchpathcon(path, opts & OPT_NOT_PRINT); |
Denis Vlasenko | d46d3c2 | 2007-02-06 19:28:50 +0000 | [diff] [blame] | 63 | continue; |
| 64 | } |
| 65 | |
| 66 | if (selinux_file_context_verify(path, 0)) { |
| 67 | printf("%s verified\n", path); |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | if (opts & OPT_NOT_TRANS) |
| 72 | rc = lgetfilecon_raw(path, &con); |
| 73 | else |
| 74 | rc = lgetfilecon(path, &con); |
| 75 | |
| 76 | if (rc >= 0) { |
| 77 | printf("%s has context %s, should be ", path, con); |
| 78 | error += print_matchpathcon(path, 1); |
| 79 | freecon(con); |
| 80 | continue; |
| 81 | } |
| 82 | printf("actual context unknown: %s, should be ", strerror(errno)); |
| 83 | error += print_matchpathcon(path, 1); |
| 84 | } |
| 85 | matchpathcon_fini(); |
| 86 | return error; |
| 87 | } |