blob: 741c7bba69c78ef397d5e5c10ae2be4d4b3b6727 [file] [log] [blame]
Denis Vlasenko1203c9b2007-03-11 22:16:02 +00001/*
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 Vlasenko1203c9b2007-03-11 22:16:02 +000029#include <getopt.h>
30#include <selinux/context.h>
31#include <selinux/flask.h>
32
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000033#include "libbb.h"
34
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000035static 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
72static const struct option runcon_options[] = {
73 { "user", 1, NULL, 'u' },
74 { "role", 1, NULL, 'r' },
75 { "type", 1, NULL, 't' },
76 { "range", 1, NULL, 'l' },
77 { "compute", 0, NULL, 'c' },
78 { "help", 0, NULL, 'h' },
79 { NULL, 0, NULL, 0 },
80};
81#endif
82
83#define OPTS_ROLE (1<<0) /* r */
84#define OPTS_TYPE (1<<1) /* t */
85#define OPTS_USER (1<<2) /* u */
86#define OPTS_RANGE (1<<3) /* l */
87#define OPTS_COMPUTE (1<<4) /* c */
88#define OPTS_HELP (1<<5) /* h */
89#define OPTS_CONTEXT_COMPONENT (OPTS_ROLE | OPTS_TYPE | OPTS_USER | OPTS_RANGE)
90
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +000091int runcon_main(int argc, char **argv);
92int runcon_main(int argc, char **argv)
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000093{
94 char *role = NULL;
95 char *range = NULL;
96 char *user = NULL;
97 char *type = NULL;
98 char *context = NULL;
99 unsigned opts;
100 context_t con;
101
102 selinux_or_die();
103
104#if ENABLE_FEATURE_RUNCON_LONG_OPTIONS
105 applet_long_options = runcon_options;
106#endif
107 opt_complementary = "-1";
108 opts = getopt32(argc, argv, "r:t:u:l:ch", &role, &type, &user, &range);
109 argv += optind;
110
111 if (!(opts & OPTS_CONTEXT_COMPONENT)) {
112 context = *argv++;
113 if (!argv[0])
Denis Vlasenkoff131b92007-04-10 15:42:06 +0000114 bb_error_msg_and_die("no command given");
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000115 }
116
117 if (context) {
118 con = context_new(context);
119 if (!con)
120 bb_error_msg_and_die("'%s' is not a valid context", context);
121 } else {
122 con = runcon_compute_new_context(user, role, type, range,
123 argv[0], opts & OPTS_COMPUTE);
124 }
125
126 if (security_check_context(context_str(con)))
127 bb_error_msg_and_die("'%s' is not a valid context",
128 context_str(con));
129
130 if (setexeccon(context_str(con)))
131 bb_error_msg_and_die("cannot set up security context '%s'",
132 context_str(con));
133
134 execvp(argv[0], argv);
135
Denis Vlasenko39c651e2007-03-12 18:22:55 +0000136 bb_perror_msg_and_die("cannot execute '%s'", argv[0]);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000137}