tests: avoid false df failure with nfs and lofs
[coreutils.git] / src / runcon.c
blob2827992d9b402a173e1048fd16ca7c3c8b8d68b2
1 /* runcon -- run command with specified security context
2 Copyright (C) 2005-2013 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 SELinux security context.\n\
89 With neither CONTEXT nor COMMAND, print the current security context.\n\
90 "), stdout);
92 emit_mandatory_arg_note ();
94 fputs (_("\
95 CONTEXT Complete security context\n\
96 -c, --compute compute process transition context before modifying\n\
97 -t, --type=TYPE type (for same role as parent)\n\
98 -u, --user=USER user identity\n\
99 -r, --role=ROLE role\n\
100 -l, --range=RANGE levelrange\n\
102 "), stdout);
103 fputs (HELP_OPTION_DESCRIPTION, stdout);
104 fputs (VERSION_OPTION_DESCRIPTION, stdout);
105 emit_ancillary_info ();
107 exit (status);
111 main (int argc, char **argv)
113 char *role = NULL;
114 char *range = NULL;
115 char *user = NULL;
116 char *type = NULL;
117 char *context = NULL;
118 security_context_t cur_context = NULL;
119 security_context_t file_context = NULL;
120 security_context_t new_context = NULL;
121 bool compute_trans = false;
123 context_t con;
125 initialize_main (&argc, &argv);
126 set_program_name (argv[0]);
127 setlocale (LC_ALL, "");
128 bindtextdomain (PACKAGE, LOCALEDIR);
129 textdomain (PACKAGE);
131 atexit (close_stdout);
133 while (1)
135 int option_index = 0;
136 int c = getopt_long (argc, argv, "+r:t:u:l:c", long_options,
137 &option_index);
138 if (c == -1)
139 break;
140 switch (c)
142 case 'r':
143 if (role)
144 error (EXIT_FAILURE, 0, _("multiple roles"));
145 role = optarg;
146 break;
147 case 't':
148 if (type)
149 error (EXIT_FAILURE, 0, _("multiple types"));
150 type = optarg;
151 break;
152 case 'u':
153 if (user)
154 error (EXIT_FAILURE, 0, _("multiple users"));
155 user = optarg;
156 break;
157 case 'l':
158 if (range)
159 error (EXIT_FAILURE, 0, _("multiple levelranges"));
160 range = optarg;
161 break;
162 case 'c':
163 compute_trans = true;
164 break;
166 case_GETOPT_HELP_CHAR;
167 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
168 default:
169 usage (EXIT_FAILURE);
170 break;
174 if (argc - optind == 0)
176 if (getcon (&cur_context) < 0)
177 error (EXIT_FAILURE, errno, _("failed to get current context"));
178 fputs (cur_context, stdout);
179 fputc ('\n', stdout);
180 exit (EXIT_SUCCESS);
183 if (!(user || role || type || range || compute_trans))
185 if (optind >= argc)
187 error (0, 0, _("you must specify -c, -t, -u, -l, -r, or context"));
188 usage (EXIT_FAILURE);
190 context = argv[optind++];
193 if (optind >= argc)
195 error (0, 0, _("no command specified"));
196 usage (EXIT_FAILURE);
199 if (is_selinux_enabled () != 1)
200 error (EXIT_FAILURE, 0, _("%s may be used only on a SELinux kernel"),
201 program_name);
203 if (context)
205 con = context_new (context);
206 if (!con)
207 error (EXIT_FAILURE, errno, _("failed to create security context: %s"),
208 quotearg_colon (context));
210 else
212 if (getcon (&cur_context) < 0)
213 error (EXIT_FAILURE, errno, _("failed to get current context"));
215 /* We will generate context based on process transition */
216 if (compute_trans)
218 /* Get context of file to be executed */
219 if (getfilecon (argv[optind], &file_context) == -1)
220 error (EXIT_FAILURE, errno,
221 _("failed to get security context of %s"),
222 quote (argv[optind]));
223 /* compute result of process transition */
224 if (security_compute_create (cur_context, file_context,
225 SECCLASS_PROCESS, &new_context) != 0)
226 error (EXIT_FAILURE, errno, _("failed to compute a new context"));
227 /* free contexts */
228 freecon (file_context);
229 freecon (cur_context);
231 /* set cur_context equal to new_context */
232 cur_context = new_context;
235 con = context_new (cur_context);
236 if (!con)
237 error (EXIT_FAILURE, errno, _("failed to create security context: %s"),
238 quotearg_colon (cur_context));
239 if (user && context_user_set (con, user))
240 error (EXIT_FAILURE, errno, _("failed to set new user %s"), user);
241 if (type && context_type_set (con, type))
242 error (EXIT_FAILURE, errno, _("failed to set new type %s"), type);
243 if (range && context_range_set (con, range))
244 error (EXIT_FAILURE, errno, _("failed to set new range %s"), range);
245 if (role && context_role_set (con, role))
246 error (EXIT_FAILURE, errno, _("failed to set new role %s"), role);
249 if (security_check_context (context_str (con)) < 0)
250 error (EXIT_FAILURE, errno, _("invalid context: %s"),
251 quotearg_colon (context_str (con)));
253 if (setexeccon (context_str (con)) != 0)
254 error (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);
262 int exit_status = (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
263 error (0, errno, "%s", argv[optind]);
264 exit (exit_status);