add help text
[oweals/busybox.git] / selinux / runcon.c
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  *
29  * Licensed under GPLv2, see file LICENSE in this source tree.
30  */
31
32 //usage:#define runcon_trivial_usage
33 //usage:       "[-c] [-u USER] [-r ROLE] [-t TYPE] [-l RANGE] PROG ARGS\n"
34 //usage:       "runcon CONTEXT PROG ARGS"
35 //usage:#define runcon_full_usage "\n\n"
36 //usage:       "Run PROG in a different security context\n"
37 //usage:     "\n        CONTEXT         Complete security context\n"
38 //usage:        IF_FEATURE_RUNCON_LONG_OPTIONS(
39 //usage:     "\n        -c,--compute    Compute process transition context before modifying"
40 //usage:     "\n        -t,--type=TYPE  Type (for same role as parent)"
41 //usage:     "\n        -u,--user=USER  User identity"
42 //usage:     "\n        -r,--role=ROLE  Role"
43 //usage:     "\n        -l,--range=RNG  Levelrange"
44 //usage:        )
45 //usage:        IF_NOT_FEATURE_RUNCON_LONG_OPTIONS(
46 //usage:     "\n        -c      Compute process transition context before modifying"
47 //usage:     "\n        -t TYPE Type (for same role as parent)"
48 //usage:     "\n        -u USER User identity"
49 //usage:     "\n        -r ROLE Role"
50 //usage:     "\n        -l RNG  Levelrange"
51 //usage:        )
52
53 #include <getopt.h>
54 #include <selinux/context.h>
55 #include <selinux/flask.h>
56
57 #include "libbb.h"
58
59 static context_t runcon_compute_new_context(char *user, char *role, char *type, char *range,
60                                             char *command, int compute_trans)
61 {
62         context_t con;
63         security_context_t cur_context;
64
65         if (getcon(&cur_context))
66                 bb_error_msg_and_die("can't get current context");
67
68         if (compute_trans) {
69                 security_context_t file_context, new_context;
70
71                 if (getfilecon(command, &file_context) < 0)
72                         bb_error_msg_and_die("can't retrieve attributes of '%s'",
73                                              command);
74                 if (security_compute_create(cur_context, file_context,
75                                             SECCLASS_PROCESS, &new_context))
76                         bb_error_msg_and_die("unable to compute a new context");
77                 cur_context = new_context;
78         }
79
80         con = context_new(cur_context);
81         if (!con)
82                 bb_error_msg_and_die("'%s' is not a valid context", cur_context);
83         if (user && context_user_set(con, user))
84                 bb_error_msg_and_die("can't set new user '%s'", user);
85         if (type && context_type_set(con, type))
86                 bb_error_msg_and_die("can't set new type '%s'", type);
87         if (range && context_range_set(con, range))
88                 bb_error_msg_and_die("can't set new range '%s'", range);
89         if (role && context_role_set(con, role))
90                 bb_error_msg_and_die("can't set new role '%s'", role);
91
92         return con;
93 }
94
95 #if ENABLE_FEATURE_RUNCON_LONG_OPTIONS
96 static const char runcon_longopts[] ALIGN1 =
97         "user\0"    Required_argument "u"
98         "role\0"    Required_argument "r"
99         "type\0"    Required_argument "t"
100         "range\0"   Required_argument "l"
101         "compute\0" No_argument "c"
102         "help\0"    No_argument "h"
103         ;
104 #endif
105
106 #define OPTS_ROLE       (1<<0)  /* r */
107 #define OPTS_TYPE       (1<<1)  /* t */
108 #define OPTS_USER       (1<<2)  /* u */
109 #define OPTS_RANGE      (1<<3)  /* l */
110 #define OPTS_COMPUTE    (1<<4)  /* c */
111 #define OPTS_HELP       (1<<5)  /* h */
112 #define OPTS_CONTEXT_COMPONENT          (OPTS_ROLE | OPTS_TYPE | OPTS_USER | OPTS_RANGE)
113
114 int runcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
115 int runcon_main(int argc UNUSED_PARAM, char **argv)
116 {
117         char *role = NULL;
118         char *range = NULL;
119         char *user = NULL;
120         char *type = NULL;
121         char *context = NULL;
122         unsigned opts;
123         context_t con;
124
125         selinux_or_die();
126
127 #if ENABLE_FEATURE_RUNCON_LONG_OPTIONS
128         applet_long_options = runcon_longopts;
129 #endif
130         opt_complementary = "-1";
131         opts = getopt32(argv, "r:t:u:l:ch", &role, &type, &user, &range);
132         argv += optind;
133
134         if (!(opts & OPTS_CONTEXT_COMPONENT)) {
135                 context = *argv++;
136                 if (!argv[0])
137                         bb_error_msg_and_die("no command given");
138         }
139
140         if (context) {
141                 con = context_new(context);
142                 if (!con)
143                         bb_error_msg_and_die("'%s' is not a valid context", context);
144         } else {
145                 con = runcon_compute_new_context(user, role, type, range,
146                                 argv[0], opts & OPTS_COMPUTE);
147         }
148
149         if (security_check_context(context_str(con)))
150                 bb_error_msg_and_die("'%s' is not a valid context",
151                                      context_str(con));
152
153         if (setexeccon(context_str(con)))
154                 bb_error_msg_and_die("can't set up security context '%s'",
155                                      context_str(con));
156
157         BB_EXECVP_or_die(argv);
158 }