tests: remove crufty test=test_name code from old tests
[coreutils/ericb.git] / src / runcon.c
blob29bf0e502b73a5f2fb3832ab09d754991aad9120
1 /* runcon -- run command with specified security context
2 Copyright (C) 2005-2012 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 #ifdef HAVE_SELINUX_FLASK_H
49 # include <selinux/flask.h>
50 #else
51 # define SECCLASS_PROCESS 0
52 #endif
53 #include <sys/types.h>
54 #include "system.h"
55 #include "error.h"
56 #include "quote.h"
57 #include "quotearg.h"
59 /* The official name of this program (e.g., no 'g' prefix). */
60 #define PROGRAM_NAME "runcon"
62 #define AUTHORS proper_name ("Russell Coker")
64 static struct option const long_options[] =
66 {"role", required_argument, NULL, 'r'},
67 {"type", required_argument, NULL, 't'},
68 {"user", required_argument, NULL, 'u'},
69 {"range", required_argument, NULL, 'l'},
70 {"compute", no_argument, NULL, 'c'},
71 {GETOPT_HELP_OPTION_DECL},
72 {GETOPT_VERSION_OPTION_DECL},
73 {NULL, 0, NULL, 0}
76 void
77 usage (int status)
79 if (status != EXIT_SUCCESS)
80 emit_try_help ();
81 else
83 printf (_("\
84 Usage: %s CONTEXT COMMAND [args]\n\
85 or: %s [ -c ] [-u USER] [-r ROLE] [-t TYPE] [-l RANGE] COMMAND [args]\n\
86 "), program_name, program_name);
87 fputs (_("\
88 Run a program in a different security context.\n\
89 With neither CONTEXT nor COMMAND, print the current security context.\n\
90 \n\
91 CONTEXT Complete security context\n\
92 -c, --compute compute process transition context before modifying\n\
93 -t, --type=TYPE type (for same role as parent)\n\
94 -u, --user=USER user identity\n\
95 -r, --role=ROLE role\n\
96 -l, --range=RANGE levelrange\n\
97 \n\
98 "), stdout);
99 fputs (HELP_OPTION_DESCRIPTION, stdout);
100 fputs (VERSION_OPTION_DESCRIPTION, stdout);
101 emit_ancillary_info ();
103 exit (status);
107 main (int argc, char **argv)
109 char *role = NULL;
110 char *range = NULL;
111 char *user = NULL;
112 char *type = NULL;
113 char *context = NULL;
114 security_context_t cur_context = NULL;
115 security_context_t file_context = NULL;
116 security_context_t new_context = NULL;
117 bool compute_trans = false;
119 context_t con;
121 initialize_main (&argc, &argv);
122 set_program_name (argv[0]);
123 setlocale (LC_ALL, "");
124 bindtextdomain (PACKAGE, LOCALEDIR);
125 textdomain (PACKAGE);
127 atexit (close_stdout);
129 while (1)
131 int option_index = 0;
132 int c = getopt_long (argc, argv, "+r:t:u:l:c", long_options,
133 &option_index);
134 if (c == -1)
135 break;
136 switch (c)
138 case 'r':
139 if (role)
140 error (EXIT_FAILURE, 0, _("multiple roles"));
141 role = optarg;
142 break;
143 case 't':
144 if (type)
145 error (EXIT_FAILURE, 0, _("multiple types"));
146 type = optarg;
147 break;
148 case 'u':
149 if (user)
150 error (EXIT_FAILURE, 0, _("multiple users"));
151 user = optarg;
152 break;
153 case 'l':
154 if (range)
155 error (EXIT_FAILURE, 0, _("multiple levelranges"));
156 range = optarg;
157 break;
158 case 'c':
159 compute_trans = true;
160 break;
162 case_GETOPT_HELP_CHAR;
163 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
164 default:
165 usage (EXIT_FAILURE);
166 break;
170 if (argc - optind == 0)
172 if (getcon (&cur_context) < 0)
173 error (EXIT_FAILURE, errno, _("failed to get current context"));
174 fputs (cur_context, stdout);
175 fputc ('\n', stdout);
176 exit (EXIT_SUCCESS);
179 if (!(user || role || type || range || compute_trans))
181 if (optind >= argc)
183 error (0, 0, _("you must specify -c, -t, -u, -l, -r, or context"));
184 usage (EXIT_FAILURE);
186 context = argv[optind++];
189 if (optind >= argc)
191 error (0, 0, _("no command specified"));
192 usage (EXIT_FAILURE);
195 if (is_selinux_enabled () != 1)
196 error (EXIT_FAILURE, 0,
197 _("%s may be used only on a SELinux kernel"), program_name);
199 if (context)
201 con = context_new (context);
202 if (!con)
203 error (EXIT_FAILURE, errno, _("failed to create security context: %s"),
204 quotearg_colon (context));
206 else
208 if (getcon (&cur_context) < 0)
209 error (EXIT_FAILURE, errno, _("failed to get current context"));
211 /* We will generate context based on process transition */
212 if (compute_trans)
214 /* Get context of file to be executed */
215 if (getfilecon (argv[optind], &file_context) == -1)
216 error (EXIT_FAILURE, errno,
217 _("failed to get security context of %s"),
218 quote (argv[optind]));
219 /* compute result of process transition */
220 if (security_compute_create (cur_context, file_context,
221 SECCLASS_PROCESS, &new_context) != 0)
222 error (EXIT_FAILURE, errno,
223 _("failed to compute a new context"));
224 /* free contexts */
225 freecon (file_context);
226 freecon (cur_context);
228 /* set cur_context equal to new_context */
229 cur_context = new_context;
232 con = context_new (cur_context);
233 if (!con)
234 error (EXIT_FAILURE, errno, _("failed to create security context: %s"),
235 quotearg_colon (cur_context));
236 if (user && context_user_set (con, user))
237 error (EXIT_FAILURE, errno, _("failed to set new user %s"), user);
238 if (type && context_type_set (con, type))
239 error (EXIT_FAILURE, errno, _("failed to set new type %s"), type);
240 if (range && context_range_set (con, range))
241 error (EXIT_FAILURE, errno, _("failed to set new range %s"), range);
242 if (role && context_role_set (con, role))
243 error (EXIT_FAILURE, errno, _("failed to set new role %s"), role);
246 if (security_check_context (context_str (con)) < 0)
247 error (EXIT_FAILURE, errno, _("invalid context: %s"),
248 quotearg_colon (context_str (con)));
250 if (setexeccon (context_str (con)) != 0)
251 error (EXIT_FAILURE, errno, _("unable to set security context %s"),
252 quote (context_str (con)));
253 if (cur_context != NULL)
254 freecon (cur_context);
256 execvp (argv[optind], argv + optind);
259 int exit_status = (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
260 error (0, errno, "%s", argv[optind]);
261 exit (exit_status);