2 * Copyright (c) 2010 , Jakub Hrozek <jhrozek@redhat.com>
3 * Copyright (c) 2011 , Peter Vrabec <pvrabec@redhat.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the copyright holders or contributors may not be used to
15 * endorse or promote products derived from this software without
16 * specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <selinux/selinux.h>
41 #include <semanage/semanage.h>
44 #ifndef DEFAULT_SERANGE
45 #define DEFAULT_SERANGE "s0"
49 static void semanage_error_callback (void *varg,
50 semanage_handle_t *handle,
54 char * message = NULL;
59 ret = vasprintf (&message, fmt, ap);
66 switch (semanage_msg_get_level (handle)) {
67 case SEMANAGE_MSG_ERR:
68 case SEMANAGE_MSG_WARN:
69 fprintf (stderr, _("[libsemanage]: %s\n"), message);
71 case SEMANAGE_MSG_INFO:
80 static semanage_handle_t *semanage_init (void)
83 semanage_handle_t *handle = NULL;
85 handle = semanage_handle_create ();
88 _("Cannot create SELinux management handle\n"));
92 semanage_msg_set_callback (handle, semanage_error_callback, NULL);
94 ret = semanage_is_managed (handle);
96 fprintf (stderr, _("SELinux policy not managed\n"));
100 ret = semanage_access_check (handle);
101 if (ret < SEMANAGE_CAN_READ) {
102 fprintf (stderr, _("Cannot read SELinux policy store\n"));
106 ret = semanage_connect (handle);
109 _("Cannot establish SELinux management connection\n"));
113 ret = semanage_begin_transaction (handle);
115 fprintf (stderr, _("Cannot begin SELinux transaction\n"));
122 semanage_handle_destroy (handle);
127 static int semanage_user_mod (semanage_handle_t *handle,
128 semanage_seuser_key_t *key,
129 const char *login_name,
130 const char *seuser_name)
133 semanage_seuser_t *seuser = NULL;
135 semanage_seuser_query (handle, key, &seuser);
136 if (NULL == seuser) {
138 _("Could not query seuser for %s\n"), login_name);
143 ret = semanage_seuser_set_mlsrange (handle, seuser, DEFAULT_SERANGE);
146 _("Could not set serange for %s\n"), login_name);
151 ret = semanage_seuser_set_sename (handle, seuser, seuser_name);
154 _("Could not set sename for %s\n"),
160 ret = semanage_seuser_modify_local (handle, key, seuser);
163 _("Could not modify login mapping for %s\n"),
171 semanage_seuser_free (seuser);
176 static int semanage_user_add (semanage_handle_t *handle,
177 semanage_seuser_key_t *key,
178 const char *login_name,
179 const char *seuser_name)
182 semanage_seuser_t *seuser = NULL;
184 ret = semanage_seuser_create (handle, &seuser);
187 _("Cannot create SELinux login mapping for %s\n"),
193 ret = semanage_seuser_set_name (handle, seuser, login_name);
195 fprintf (stderr, _("Could not set name for %s\n"), login_name);
200 ret = semanage_seuser_set_mlsrange (handle, seuser, DEFAULT_SERANGE);
203 _("Could not set serange for %s\n"),
209 ret = semanage_seuser_set_sename (handle, seuser, seuser_name);
212 _("Could not set SELinux user for %s\n"),
218 ret = semanage_seuser_modify_local (handle, key, seuser);
221 _("Could not add login mapping for %s\n"),
229 semanage_seuser_free (seuser);
234 int set_seuser (const char *login_name, const char *seuser_name)
236 semanage_handle_t *handle = NULL;
237 semanage_seuser_key_t *key = NULL;
239 int seuser_exists = 0;
241 if (NULL == seuser_name) {
242 /* don't care, just let system pick the defaults */
246 handle = semanage_init ();
247 if (NULL == handle) {
248 fprintf (stderr, _("Cannot init SELinux management\n"));
253 ret = semanage_seuser_key_create (handle, login_name, &key);
255 fprintf (stderr, _("Cannot create SELinux user key\n"));
260 ret = semanage_seuser_exists (handle, key, &seuser_exists);
262 fprintf (stderr, _("Cannot verify the SELinux user\n"));
267 if (0 != seuser_exists) {
268 ret = semanage_user_mod (handle, key, login_name, seuser_name);
271 _("Cannot modify SELinux user mapping\n"));
276 ret = semanage_user_add (handle, key, login_name, seuser_name);
279 _("Cannot add SELinux user mapping\n"));
285 ret = semanage_commit (handle);
287 fprintf (stderr, _("Cannot commit SELinux transaction\n"));
295 semanage_seuser_key_free (key);
296 semanage_handle_destroy (handle);
301 int del_seuser (const char *login_name)
303 semanage_handle_t *handle = NULL;
304 semanage_seuser_key_t *key = NULL;
308 handle = semanage_init ();
309 if (NULL == handle) {
310 fprintf (stderr, _("Cannot init SELinux management\n"));
315 ret = semanage_seuser_key_create (handle, login_name, &key);
317 fprintf (stderr, _("Cannot create SELinux user key\n"));
322 ret = semanage_seuser_exists (handle, key, &exists);
324 fprintf (stderr, _("Cannot verify the SELinux user\n"));
331 _("Login mapping for %s is not defined, OK if default mapping was used\n"),
333 ret = 0; /* probably default mapping */
337 ret = semanage_seuser_exists_local (handle, key, &exists);
339 fprintf (stderr, _("Cannot verify the SELinux user\n"));
346 _("Login mapping for %s is defined in policy, cannot be deleted\n"),
348 ret = 0; /* Login mapping defined in policy can't be deleted */
352 ret = semanage_seuser_del_local (handle, key);
355 _("Could not delete login mapping for %s"),
361 ret = semanage_commit (handle);
363 fprintf (stderr, _("Cannot commit SELinux transaction\n"));
370 semanage_handle_destroy (handle);
373 #else /* !WITH_SELINUX */
374 extern int errno; /* warning: ANSI C forbids an empty source file */
375 #endif /* !WITH_SELINUX */