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