Line 0
Link Here
|
|
|
1 |
/* |
2 |
rc-selinux.c |
3 |
SELinux helpers to get and set contexts. |
4 |
*/ |
5 |
|
6 |
/* |
7 |
* Copyright (c) 2014 Jason Zaman <jason@perfinion.com> |
8 |
* |
9 |
* Redistribution and use in source and binary forms, with or without |
10 |
* modification, are permitted provided that the following conditions |
11 |
* are met: |
12 |
* 1. Redistributions of source code must retain the above copyright |
13 |
* notice, this list of conditions and the following disclaimer. |
14 |
* 2. Redistributions in binary form must reproduce the above copyright |
15 |
* notice, this list of conditions and the following disclaimer in the |
16 |
* documentation and/or other materials provided with the distribution. |
17 |
* |
18 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
19 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
22 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
24 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
25 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
26 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
27 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
28 |
* SUCH DAMAGE. |
29 |
*/ |
30 |
|
31 |
// TODO: take out the printf's |
32 |
#include <stdio.h> |
33 |
|
34 |
#include <errno.h> |
35 |
#include <sys/stat.h> |
36 |
|
37 |
#include <selinux/selinux.h> |
38 |
#include <selinux/label.h> |
39 |
|
40 |
#include "rc-selinux-util.h" |
41 |
|
42 |
static struct selabel_handle *hnd = NULL; |
43 |
|
44 |
int |
45 |
selinux_util_label(const char *path) |
46 |
{ |
47 |
int retval = 0; |
48 |
int enforce; |
49 |
struct stat st; |
50 |
security_context_t con; |
51 |
|
52 |
printf("Labelling file: %s\n", path); |
53 |
|
54 |
enforce = security_getenforce(); |
55 |
if (retval < 0) |
56 |
return retval; |
57 |
|
58 |
if (NULL == hnd) |
59 |
return (enforce) ? -1 : 0; |
60 |
|
61 |
retval = lstat(path, &st); |
62 |
if (retval < 0) { |
63 |
if (ENOENT == errno) |
64 |
return 0; |
65 |
return (enforce) ? -1 : 0; |
66 |
} |
67 |
|
68 |
/* lookup the context */ |
69 |
retval = selabel_lookup_raw(hnd, &con, path, st.st_mode); |
70 |
if (retval < 0) { |
71 |
if (ENOENT == errno) |
72 |
return 0; |
73 |
return (enforce) ? -1 : 0; |
74 |
} |
75 |
|
76 |
/* apply the context */ |
77 |
retval = lsetfilecon(path, con); |
78 |
freecon(con); |
79 |
if (retval < 0) { |
80 |
if (ENOENT == errno) |
81 |
return 0; |
82 |
if (ENOTSUP == errno) |
83 |
return 0; |
84 |
return (enforce) ? -1 : 0; |
85 |
} |
86 |
|
87 |
return 0; |
88 |
} |
89 |
|
90 |
/* |
91 |
* Open the label handle |
92 |
* returns 1 on success, 0 if no selinux, negative on error |
93 |
*/ |
94 |
int |
95 |
selinux_util_open(void) |
96 |
{ |
97 |
int retval = 0; |
98 |
|
99 |
printf("Opening selinux handle\n"); |
100 |
|
101 |
retval = is_selinux_enabled(); |
102 |
if (retval <= 0) |
103 |
return retval; |
104 |
|
105 |
hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0); |
106 |
if (NULL == hnd) |
107 |
return -2; |
108 |
|
109 |
printf("Opened selinux handle\n"); |
110 |
return 1; |
111 |
} |
112 |
|
113 |
/* |
114 |
* Close the label handle |
115 |
* returns 1 on success, 0 if no selinux, negative on error |
116 |
*/ |
117 |
int |
118 |
selinux_util_close(void) |
119 |
{ |
120 |
int retval = 0; |
121 |
|
122 |
printf("Closing selinux handle\n"); |
123 |
|
124 |
retval = is_selinux_enabled(); |
125 |
if (retval <= 0) |
126 |
return retval; |
127 |
|
128 |
if (hnd) { |
129 |
selabel_close(hnd); |
130 |
hnd = NULL; |
131 |
} |
132 |
|
133 |
return 0; |
134 |
} |
135 |
|