blob: 1d3b28a1c5fc7f9ffeb230fc6c22d7b4ef3eeb8d [file] [log] [blame]
Joshua Brindle13cd4c82008-08-19 15:30:36 -04001#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 Smalley9eb9c932014-02-19 09:16:17 -050010int lsetfilecon_raw(const char *path, const char * context)
Joshua Brindle13cd4c82008-08-19 15:30:36 -040011{
Dan Walsh51d9a072013-10-09 15:15:35 -040012 int rc = lsetxattr(path, XATTR_NAME_SELINUX, context, strlen(context) + 1,
Joshua Brindle13cd4c82008-08-19 15:30:36 -040013 0);
Dan Walsh51d9a072013-10-09 15:15:35 -040014 if (rc < 0 && errno == ENOTSUP) {
Stephen Smalley9eb9c932014-02-19 09:16:17 -050015 char * ccontext = NULL;
Dan Walsh51d9a072013-10-09 15:15:35 -040016 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 Brindle13cd4c82008-08-19 15:30:36 -040026}
27
28hidden_def(lsetfilecon_raw)
29
Stephen Smalley9eb9c932014-02-19 09:16:17 -050030int lsetfilecon(const char *path, const char *context)
Joshua Brindle13cd4c82008-08-19 15:30:36 -040031{
32 int ret;
Stephen Smalley9eb9c932014-02-19 09:16:17 -050033 char * rcontext;
Joshua Brindle13cd4c82008-08-19 15:30:36 -040034
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}