blob: 5fdbe9d5870684ce256fd0c923d84427dbb0cbfe [file] [log] [blame]
Denis Vlasenko1203c9b2007-03-11 22:16:02 +00001/*
2 * libbb/selinux_common.c
3 * -- common SELinux utility functions
Denis Vlasenkoc86e0522007-03-20 11:30:28 +00004 *
Denis Vlasenko1203c9b2007-03-11 22:16:02 +00005 * Copyright 2007 KaiGai Kohei <kaigai@kaigai.gr.jp>
6 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00007#include "libbb.h"
Denis Vlasenko1203c9b2007-03-11 22:16:02 +00008#include <selinux/context.h>
9
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000010context_t FAST_FUNC set_security_context_component(security_context_t cur_context,
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000011 char *user, char *role, char *type, char *range)
12{
13 context_t con = context_new(cur_context);
14 if (!con)
15 return NULL;
16
17 if (user && context_user_set(con, user))
18 goto error;
19 if (type && context_type_set(con, type))
20 goto error;
21 if (range && context_range_set(con, range))
22 goto error;
23 if (role && context_role_set(con, role))
24 goto error;
25 return con;
26
27error:
28 context_free(con);
29 return NULL;
30}
Denis Vlasenko39c651e2007-03-12 18:22:55 +000031
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000032void FAST_FUNC setfscreatecon_or_die(security_context_t scontext)
Denis Vlasenko39c651e2007-03-12 18:22:55 +000033{
34 if (setfscreatecon(scontext) < 0) {
35 /* Can be NULL. All known printf implementations
36 * display "(null)", "<null>" etc */
37 bb_perror_msg_and_die("cannot set default "
38 "file creation context to %s", scontext);
39 }
40}
Denis Vlasenko2edbc2a2007-10-20 02:00:49 +000041
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000042void FAST_FUNC selinux_preserve_fcontext(int fdesc)
Denis Vlasenko2edbc2a2007-10-20 02:00:49 +000043{
44 security_context_t context;
45
46 if (fgetfilecon(fdesc, &context) < 0) {
47 if (errno == ENODATA || errno == ENOTSUP)
48 return;
49 bb_perror_msg_and_die("fgetfilecon failed");
50 }
51 setfscreatecon_or_die(context);
52 freecon(context);
53}
54