Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | #include <fcntl.h> |
| 3 | #include <string.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <errno.h> |
| 6 | #include <sys/xattr.h> |
| 7 | #include "selinux_internal.h" |
| 8 | #include "policy.h" |
| 9 | |
Stephen Smalley | 9eb9c93 | 2014-02-19 09:16:17 -0500 | [diff] [blame] | 10 | int lsetfilecon_raw(const char *path, const char * context) |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 11 | { |
Dan Walsh | 51d9a07 | 2013-10-09 15:15:35 -0400 | [diff] [blame] | 12 | int rc = lsetxattr(path, XATTR_NAME_SELINUX, context, strlen(context) + 1, |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 13 | 0); |
Dan Walsh | 51d9a07 | 2013-10-09 15:15:35 -0400 | [diff] [blame] | 14 | if (rc < 0 && errno == ENOTSUP) { |
Stephen Smalley | 9eb9c93 | 2014-02-19 09:16:17 -0500 | [diff] [blame] | 15 | char * ccontext = NULL; |
Dan Walsh | 51d9a07 | 2013-10-09 15:15:35 -0400 | [diff] [blame] | 16 | int err = errno; |
| 17 | if ((lgetfilecon_raw(path, &ccontext) >= 0) && |
| 18 | (strcmp(context,ccontext) == 0)) { |
| 19 | rc = 0; |
| 20 | } else { |
| 21 | errno = err; |
| 22 | } |
| 23 | freecon(ccontext); |
| 24 | } |
| 25 | return rc; |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | hidden_def(lsetfilecon_raw) |
| 29 | |
Stephen Smalley | 9eb9c93 | 2014-02-19 09:16:17 -0500 | [diff] [blame] | 30 | int lsetfilecon(const char *path, const char *context) |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 31 | { |
| 32 | int ret; |
Stephen Smalley | 9eb9c93 | 2014-02-19 09:16:17 -0500 | [diff] [blame] | 33 | char * rcontext; |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 34 | |
| 35 | if (selinux_trans_to_raw_context(context, &rcontext)) |
| 36 | return -1; |
| 37 | |
| 38 | ret = lsetfilecon_raw(path, rcontext); |
| 39 | |
| 40 | freecon(rcontext); |
| 41 | |
| 42 | return ret; |
| 43 | } |