1 /* $MirOS: contrib/code/jupp/selinux.c,v 1.7 2014/06/26 18:15:17 tg Exp $ */
4 #if defined(HAVE_SELINUX_HDR) && defined(HAVE_SELINUX_FUN)
11 * Example code to show how to copy the security context from one file to
15 #include <selinux/selinux.h>
16 static int selinux_enabled = -1;
23 copy_security_context(const char *from_file, const char *to_file)
27 security_context_t from_context;
28 security_context_t to_context;
30 if (selinux_enabled == -1)
31 selinux_enabled = (is_selinux_enabled() > 0);
36 if (getfilecon(from_file, &from_context) < 0) {
38 * If the filesystem doesn't support extended
39 * attributes, the original had no special security
40 * context and the target cannot have one either.
42 if (errno == EOPNOTSUPP)
45 warn("Could not get security context for %s",
50 if (getfilecon(to_file, &to_context) < 0) {
52 MSG_PUTS(_("\nCould not get security context for "));
53 msg_outtrans(to_file);
56 warn("Could not get security context for %s",
59 freecon(from_context);
63 if (strcmp(from_context, to_context) != 0) {
64 if (setfilecon(to_file, from_context) < 0) {
66 "Could not set security context for %s",
73 freecon(from_context);
79 match_default_security_context(const char *from_file)
82 security_context_t scontext;
84 if (selinux_enabled == -1)
85 selinux_enabled = (is_selinux_enabled() > 0);
90 if (getfilecon(from_file, &scontext) < 0) {
92 * If the filesystem doesn't support extended
93 * attributes, the original had no special security
94 * context and the target cannot have one either.
96 if (errno == EOPNOTSUPP)
99 warn("Could not get security context for %s",
104 if (setfscreatecon(scontext) < 0) {
106 "Could not set default security context for %s",
118 reset_default_security_context(void)
121 if (selinux_enabled == -1)
122 selinux_enabled = (is_selinux_enabled() > 0);
124 if (!selinux_enabled)
127 if (setfscreatecon(0) < 0) {
128 warn("Could not reset default security context");
137 output_security_context(char *from_file)
140 security_context_t scontext;
142 if (selinux_enabled == -1)
143 selinux_enabled = (is_selinux_enabled() > 0);
144 if (!selinux_enabled)
147 if (getfilecon(from_file, &scontext) < 0) {
149 * If the filesystem doesn't support extended
150 * attributes, the original had no special security
151 * context and the target cannot have one either.
153 if (errno == EOPNOTSUPP)
156 warn("Could not get security context for %s",
161 fprintf(stderr, "%s Security Context %s", from_file, scontext);