Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * runcon [ context | |
| 3 | * ( [ -c ] [ -r role ] [-t type] [ -u user ] [ -l levelrange ] ) |
| 4 | * command [arg1 [arg2 ...] ] |
| 5 | * |
| 6 | * attempt to run the specified command with the specified context. |
| 7 | * |
| 8 | * -r role : use the current context with the specified role |
| 9 | * -t type : use the current context with the specified type |
| 10 | * -u user : use the current context with the specified user |
| 11 | * -l level : use the current context with the specified level range |
| 12 | * -c : compute process transition context before modifying |
| 13 | * |
| 14 | * Contexts are interpreted as follows: |
| 15 | * |
| 16 | * Number of MLS |
| 17 | * components system? |
| 18 | * |
| 19 | * 1 - type |
| 20 | * 2 - role:type |
| 21 | * 3 Y role:type:range |
| 22 | * 3 N user:role:type |
| 23 | * 4 Y user:role:type:range |
| 24 | * 4 N error |
| 25 | * |
| 26 | * Port to busybox: KaiGai Kohei <kaigai@kaigai.gr.jp> |
| 27 | * - based on coreutils-5.97 (in Fedora Core 6) |
| 28 | */ |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 29 | #include <getopt.h> |
| 30 | #include <selinux/context.h> |
| 31 | #include <selinux/flask.h> |
| 32 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 33 | #include "libbb.h" |
| 34 | |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 35 | static context_t runcon_compute_new_context(char *user, char *role, char *type, char *range, |
| 36 | char *command, int compute_trans) |
| 37 | { |
| 38 | context_t con; |
| 39 | security_context_t cur_context; |
| 40 | |
| 41 | if (getcon(&cur_context)) |
| 42 | bb_error_msg_and_die("cannot get current context"); |
| 43 | |
| 44 | if (compute_trans) { |
| 45 | security_context_t file_context, new_context; |
| 46 | |
| 47 | if (getfilecon(command, &file_context) < 0) |
| 48 | bb_error_msg_and_die("cannot retrieve attributes of '%s'", |
| 49 | command); |
| 50 | if (security_compute_create(cur_context, file_context, |
| 51 | SECCLASS_PROCESS, &new_context)) |
| 52 | bb_error_msg_and_die("unable to compute a new context"); |
| 53 | cur_context = new_context; |
| 54 | } |
| 55 | |
| 56 | con = context_new(cur_context); |
| 57 | if (!con) |
| 58 | bb_error_msg_and_die("'%s' is not a valid context", cur_context); |
| 59 | if (user && context_user_set(con, user)) |
| 60 | bb_error_msg_and_die("failed to set new user '%s'", user); |
| 61 | if (type && context_type_set(con, type)) |
| 62 | bb_error_msg_and_die("failed to set new type '%s'", type); |
| 63 | if (range && context_range_set(con, range)) |
| 64 | bb_error_msg_and_die("failed to set new range '%s'", range); |
| 65 | if (role && context_role_set(con, role)) |
| 66 | bb_error_msg_and_die("failed to set new role '%s'", role); |
| 67 | |
| 68 | return con; |
| 69 | } |
| 70 | |
| 71 | #if ENABLE_FEATURE_RUNCON_LONG_OPTIONS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 72 | static const char runcon_longopts[] ALIGN1 = |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 73 | "user\0" Required_argument "u" |
| 74 | "role\0" Required_argument "r" |
| 75 | "type\0" Required_argument "t" |
| 76 | "range\0" Required_argument "l" |
| 77 | "compute\0" No_argument "c" |
Denis Vlasenko | 319f8eb | 2007-08-13 11:09:30 +0000 | [diff] [blame] | 78 | "help\0" No_argument "h" |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 79 | ; |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 80 | #endif |
| 81 | |
| 82 | #define OPTS_ROLE (1<<0) /* r */ |
| 83 | #define OPTS_TYPE (1<<1) /* t */ |
| 84 | #define OPTS_USER (1<<2) /* u */ |
| 85 | #define OPTS_RANGE (1<<3) /* l */ |
| 86 | #define OPTS_COMPUTE (1<<4) /* c */ |
| 87 | #define OPTS_HELP (1<<5) /* h */ |
| 88 | #define OPTS_CONTEXT_COMPONENT (OPTS_ROLE | OPTS_TYPE | OPTS_USER | OPTS_RANGE) |
| 89 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 90 | int runcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 592d4fe | 2008-03-17 09:19:26 +0000 | [diff] [blame] | 91 | int runcon_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 92 | { |
| 93 | char *role = NULL; |
| 94 | char *range = NULL; |
| 95 | char *user = NULL; |
| 96 | char *type = NULL; |
| 97 | char *context = NULL; |
| 98 | unsigned opts; |
| 99 | context_t con; |
| 100 | |
| 101 | selinux_or_die(); |
| 102 | |
| 103 | #if ENABLE_FEATURE_RUNCON_LONG_OPTIONS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 104 | applet_long_options = runcon_longopts; |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 105 | #endif |
| 106 | opt_complementary = "-1"; |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 107 | opts = getopt32(argv, "r:t:u:l:ch", &role, &type, &user, &range); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 108 | argv += optind; |
| 109 | |
| 110 | if (!(opts & OPTS_CONTEXT_COMPONENT)) { |
| 111 | context = *argv++; |
| 112 | if (!argv[0]) |
Denis Vlasenko | ff131b9 | 2007-04-10 15:42:06 +0000 | [diff] [blame] | 113 | bb_error_msg_and_die("no command given"); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | if (context) { |
| 117 | con = context_new(context); |
| 118 | if (!con) |
| 119 | bb_error_msg_and_die("'%s' is not a valid context", context); |
| 120 | } else { |
| 121 | con = runcon_compute_new_context(user, role, type, range, |
| 122 | argv[0], opts & OPTS_COMPUTE); |
| 123 | } |
| 124 | |
| 125 | if (security_check_context(context_str(con))) |
| 126 | bb_error_msg_and_die("'%s' is not a valid context", |
| 127 | context_str(con)); |
| 128 | |
| 129 | if (setexeccon(context_str(con))) |
| 130 | bb_error_msg_and_die("cannot set up security context '%s'", |
| 131 | context_str(con)); |
| 132 | |
| 133 | execvp(argv[0], argv); |
| 134 | |
Denis Vlasenko | 39c651e | 2007-03-12 18:22:55 +0000 | [diff] [blame] | 135 | bb_perror_msg_and_die("cannot execute '%s'", argv[0]); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 136 | } |