2007-03-11 22:16:02 +00:00
|
|
|
/*
|
|
|
|
* libbb/selinux_common.c
|
|
|
|
* -- common SELinux utility functions
|
2007-03-20 11:30:28 +00:00
|
|
|
*
|
2007-03-11 22:16:02 +00:00
|
|
|
* Copyright 2007 KaiGai Kohei <kaigai@kaigai.gr.jp>
|
2008-12-07 00:52:58 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2007-03-11 22:16:02 +00:00
|
|
|
*/
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2007-03-11 22:16:02 +00:00
|
|
|
#include <selinux/context.h>
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
context_t FAST_FUNC set_security_context_component(security_context_t cur_context,
|
2007-03-11 22:16:02 +00:00
|
|
|
char *user, char *role, char *type, char *range)
|
|
|
|
{
|
|
|
|
context_t con = context_new(cur_context);
|
|
|
|
if (!con)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (user && context_user_set(con, user))
|
|
|
|
goto error;
|
|
|
|
if (type && context_type_set(con, type))
|
|
|
|
goto error;
|
|
|
|
if (range && context_range_set(con, range))
|
|
|
|
goto error;
|
|
|
|
if (role && context_role_set(con, role))
|
|
|
|
goto error;
|
|
|
|
return con;
|
|
|
|
|
|
|
|
error:
|
|
|
|
context_free(con);
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-03-12 18:22:55 +00:00
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
void FAST_FUNC setfscreatecon_or_die(security_context_t scontext)
|
2007-03-12 18:22:55 +00:00
|
|
|
{
|
|
|
|
if (setfscreatecon(scontext) < 0) {
|
|
|
|
/* Can be NULL. All known printf implementations
|
|
|
|
* display "(null)", "<null>" etc */
|
2009-11-13 08:08:27 +00:00
|
|
|
bb_perror_msg_and_die("can't set default "
|
2007-03-12 18:22:55 +00:00
|
|
|
"file creation context to %s", scontext);
|
|
|
|
}
|
|
|
|
}
|
2007-10-20 02:00:49 +00:00
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
void FAST_FUNC selinux_preserve_fcontext(int fdesc)
|
2007-10-20 02:00:49 +00:00
|
|
|
{
|
|
|
|
security_context_t context;
|
|
|
|
|
|
|
|
if (fgetfilecon(fdesc, &context) < 0) {
|
|
|
|
if (errno == ENODATA || errno == ENOTSUP)
|
|
|
|
return;
|
|
|
|
bb_perror_msg_and_die("fgetfilecon failed");
|
|
|
|
}
|
|
|
|
setfscreatecon_or_die(context);
|
|
|
|
freecon(context);
|
|
|
|
}
|