maint: update all copyright year number ranges
[coreutils.git] / src / runcon.c
blob92f519df8a8493eb55ad7f9d207a4d9cd9191cf8
1 /* runcon -- run command with specified security context
2 Copyright (C) 2005-2017 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 * runcon [ context
19 * | ( [ -c ] [ -r role ] [-t type] [ -u user ] [ -l levelrange ] )
20 * command [arg1 [arg2 ...] ]
22 * attempt to run the specified command with the specified context.
24 * -r role : use the current context with the specified role
25 * -t type : use the current context with the specified type
26 * -u user : use the current context with the specified user
27 * -l level : use the current context with the specified level range
28 * -c : compute process transition context before modifying
30 * Contexts are interpreted as follows:
32 * Number of MLS
33 * components system?
35 * 1 - type
36 * 2 - role:type
37 * 3 Y role:type:range
38 * 3 N user:role:type
39 * 4 Y user:role:type:range
40 * 4 N error
43 #include <config.h>
44 #include <stdio.h>
45 #include <getopt.h>
46 #include <selinux/selinux.h>
47 #include <selinux/context.h>
48 #include <sys/types.h>
49 #include "system.h"
50 #include "die.h"
51 #include "error.h"
52 #include "quote.h"
54 /* The official name of this program (e.g., no 'g' prefix). */
55 #define PROGRAM_NAME "runcon"
57 #define AUTHORS proper_name ("Russell Coker")
59 static struct option const long_options[] =
61 {"role", required_argument, NULL, 'r'},
62 {"type", required_argument, NULL, 't'},
63 {"user", required_argument, NULL, 'u'},
64 {"range", required_argument, NULL, 'l'},
65 {"compute", no_argument, NULL, 'c'},
66 {GETOPT_HELP_OPTION_DECL},
67 {GETOPT_VERSION_OPTION_DECL},
68 {NULL, 0, NULL, 0}
71 void
72 usage (int status)
74 if (status != EXIT_SUCCESS)
75 emit_try_help ();
76 else
78 printf (_("\
79 Usage: %s CONTEXT COMMAND [args]\n\
80 or: %s [ -c ] [-u USER] [-r ROLE] [-t TYPE] [-l RANGE] COMMAND [args]\n\
81 "), program_name, program_name);
82 fputs (_("\
83 Run a program in a different SELinux security context.\n\
84 With neither CONTEXT nor COMMAND, print the current security context.\n\
85 "), stdout);
87 emit_mandatory_arg_note ();
89 fputs (_("\
90 CONTEXT Complete security context\n\
91 -c, --compute compute process transition context before modifying\n\
92 -t, --type=TYPE type (for same role as parent)\n\
93 -u, --user=USER user identity\n\
94 -r, --role=ROLE role\n\
95 -l, --range=RANGE levelrange\n\
96 \n\
97 "), stdout);
98 fputs (HELP_OPTION_DESCRIPTION, stdout);
99 fputs (VERSION_OPTION_DESCRIPTION, stdout);
100 emit_ancillary_info (PROGRAM_NAME);
102 exit (status);
106 main (int argc, char **argv)
108 char *role = NULL;
109 char *range = NULL;
110 char *user = NULL;
111 char *type = NULL;
112 char *context = NULL;
113 char *cur_context = NULL;
114 char *file_context = NULL;
115 char *new_context = NULL;
116 bool compute_trans = false;
118 context_t con;
120 initialize_main (&argc, &argv);
121 set_program_name (argv[0]);
122 setlocale (LC_ALL, "");
123 bindtextdomain (PACKAGE, LOCALEDIR);
124 textdomain (PACKAGE);
126 atexit (close_stdout);
128 while (1)
130 int option_index = 0;
131 int c = getopt_long (argc, argv, "+r:t:u:l:c", long_options,
132 &option_index);
133 if (c == -1)
134 break;
135 switch (c)
137 case 'r':
138 if (role)
139 die (EXIT_FAILURE, 0, _("multiple roles"));
140 role = optarg;
141 break;
142 case 't':
143 if (type)
144 die (EXIT_FAILURE, 0, _("multiple types"));
145 type = optarg;
146 break;
147 case 'u':
148 if (user)
149 die (EXIT_FAILURE, 0, _("multiple users"));
150 user = optarg;
151 break;
152 case 'l':
153 if (range)
154 die (EXIT_FAILURE, 0, _("multiple levelranges"));
155 range = optarg;
156 break;
157 case 'c':
158 compute_trans = true;
159 break;
161 case_GETOPT_HELP_CHAR;
162 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
163 default:
164 usage (EXIT_FAILURE);
165 break;
169 if (argc - optind == 0)
171 if (getcon (&cur_context) < 0)
172 die (EXIT_FAILURE, errno, _("failed to get current context"));
173 fputs (cur_context, stdout);
174 fputc ('\n', stdout);
175 return EXIT_SUCCESS;
178 if (!(user || role || type || range || compute_trans))
180 if (optind >= argc)
182 error (0, 0, _("you must specify -c, -t, -u, -l, -r, or context"));
183 usage (EXIT_FAILURE);
185 context = argv[optind++];
188 if (optind >= argc)
190 error (0, 0, _("no command specified"));
191 usage (EXIT_FAILURE);
194 if (is_selinux_enabled () != 1)
195 die (EXIT_FAILURE, 0, _("%s may be used only on a SELinux kernel"),
196 program_name);
198 if (context)
200 con = context_new (context);
201 if (!con)
202 die (EXIT_FAILURE, errno, _("failed to create security context: %s"),
203 quote (context));
205 else
207 if (getcon (&cur_context) < 0)
208 die (EXIT_FAILURE, errno, _("failed to get current context"));
210 /* We will generate context based on process transition */
211 if (compute_trans)
213 /* Get context of file to be executed */
214 if (getfilecon (argv[optind], &file_context) == -1)
215 die (EXIT_FAILURE, errno,
216 _("failed to get security context of %s"),
217 quoteaf (argv[optind]));
218 /* compute result of process transition */
219 if (security_compute_create (cur_context, file_context,
220 string_to_security_class ("process"),
221 &new_context) != 0)
222 die (EXIT_FAILURE, errno, _("failed to compute a new context"));
223 /* free contexts */
224 freecon (file_context);
225 freecon (cur_context);
227 /* set cur_context equal to new_context */
228 cur_context = new_context;
231 con = context_new (cur_context);
232 if (!con)
233 die (EXIT_FAILURE, errno, _("failed to create security context: %s"),
234 quote (cur_context));
235 if (user && context_user_set (con, user))
236 die (EXIT_FAILURE, errno, _("failed to set new user: %s"),
237 quote (user));
238 if (type && context_type_set (con, type))
239 die (EXIT_FAILURE, errno, _("failed to set new type: %s"),
240 quote (type));
241 if (range && context_range_set (con, range))
242 die (EXIT_FAILURE, errno, _("failed to set new range: %s"),
243 quote (range));
244 if (role && context_role_set (con, role))
245 die (EXIT_FAILURE, errno, _("failed to set new role: %s"),
246 quote (role));
249 if (security_check_context (context_str (con)) < 0)
250 die (EXIT_FAILURE, errno, _("invalid context: %s"),
251 quote (context_str (con)));
253 if (setexeccon (context_str (con)) != 0)
254 die (EXIT_FAILURE, errno, _("unable to set security context %s"),
255 quote (context_str (con)));
256 if (cur_context != NULL)
257 freecon (cur_context);
259 execvp (argv[optind], argv + optind);
261 int exit_status = errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE;
262 error (0, errno, "%s", quote (argv[optind]));
263 return exit_status;