• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 2005 Red Hat, Inc. */
2 
3 /* Object: semanage_bool_t (Policy Boolean)
4  * Object: semanage_bool_key_t (Policy Boolean Key)
5  * Implements: record_t (Database Record)
6  * Implements: record_key_t (Database Record Key)
7  */
8 
9 #include <string.h>
10 #include <sepol/boolean_record.h>
11 #include "handle_internal.h"
12 
13 typedef sepol_bool_t semanage_bool_t;
14 typedef sepol_bool_key_t semanage_bool_key_t;
15 #define _SEMANAGE_BOOL_DEFINED_
16 
17 typedef semanage_bool_t record_t;
18 typedef semanage_bool_key_t record_key_t;
19 #define DBASE_RECORD_DEFINED
20 
21 #include "boolean_internal.h"
22 #include "handle.h"
23 #include "database.h"
24 #include <stdlib.h>
25 #include <selinux/selinux.h>
26 
27 /* Key */
semanage_bool_key_create(semanage_handle_t * handle,const char * name,semanage_bool_key_t ** key)28 int semanage_bool_key_create(semanage_handle_t * handle,
29 			     const char *name, semanage_bool_key_t ** key)
30 {
31 
32 	return sepol_bool_key_create(handle->sepolh, name, key);
33 }
34 
semanage_bool_key_extract(semanage_handle_t * handle,const semanage_bool_t * boolean,semanage_bool_key_t ** key)35 int semanage_bool_key_extract(semanage_handle_t * handle,
36 			      const semanage_bool_t * boolean,
37 			      semanage_bool_key_t ** key)
38 {
39 
40 	return sepol_bool_key_extract(handle->sepolh, boolean, key);
41 }
42 
hidden_def(semanage_bool_key_extract)43 hidden_def(semanage_bool_key_extract)
44 
45 void semanage_bool_key_free(semanage_bool_key_t * key)
46 {
47 	sepol_bool_key_free(key);
48 }
49 
hidden_def(semanage_bool_key_free)50 hidden_def(semanage_bool_key_free)
51 
52 int semanage_bool_compare(const semanage_bool_t * boolean,
53 			  const semanage_bool_key_t * key)
54 {
55 
56 	return sepol_bool_compare(boolean, key);
57 }
58 
hidden_def(semanage_bool_compare)59 hidden_def(semanage_bool_compare)
60 
61 int semanage_bool_compare2(const semanage_bool_t * boolean,
62 			   const semanage_bool_t * boolean2)
63 {
64 
65 	return sepol_bool_compare2(boolean, boolean2);
66 }
67 
hidden_def(semanage_bool_compare2)68 hidden_def(semanage_bool_compare2)
69 
70 static int semanage_bool_compare2_qsort(const semanage_bool_t ** boolean,
71 					const semanage_bool_t ** boolean2)
72 {
73 
74 	return sepol_bool_compare2(*boolean, *boolean2);
75 }
76 
77 /* Name */
semanage_bool_get_name(const semanage_bool_t * boolean)78 const char *semanage_bool_get_name(const semanage_bool_t * boolean)
79 {
80 
81 	return sepol_bool_get_name(boolean);
82 }
83 
hidden_def(semanage_bool_get_name)84 hidden_def(semanage_bool_get_name)
85 
86 int semanage_bool_set_name(semanage_handle_t * handle,
87 			   semanage_bool_t * boolean, const char *name)
88 {
89 	int rc = -1;
90 	const char *prefix = semanage_root();
91 	const char *storename = handle->conf->store_path;
92 	const char *selinux_root = selinux_policy_root();
93 	char *oldroot;
94 	char *olddir;
95 	char *subname = NULL;
96 	char *newroot = NULL;
97 	char *end;
98 
99 	if (!selinux_root)
100 		return -1;
101 
102 	oldroot = strdup(selinux_root);
103 	if (!oldroot)
104 		return -1;
105 	olddir = strdup(oldroot);
106 	if (!olddir)
107 		goto out;
108 	end = strrchr(olddir, '/');
109 	if (!end)
110 		goto out;
111 	end++;
112 	*end = '\0';
113 	rc = asprintf(&newroot, "%s%s%s", prefix, olddir, storename);
114 	if (rc < 0)
115 		goto out;
116 
117 	if (strcmp(oldroot, newroot)) {
118 		rc = selinux_set_policy_root(newroot);
119 		if (rc)
120 			goto out;
121 	}
122 
123 	subname = selinux_boolean_sub(name);
124 	if (!subname) {
125 		rc = -1;
126 		goto out;
127 	}
128 
129 	if (strcmp(oldroot, newroot)) {
130 		rc = selinux_set_policy_root(oldroot);
131 		if (rc)
132 			goto out;
133 	}
134 
135 	rc = sepol_bool_set_name(handle->sepolh, boolean, subname);
136 out:
137 	free(subname);
138 	free(oldroot);
139 	free(olddir);
140 	free(newroot);
141 	return rc;
142 }
143 
hidden_def(semanage_bool_set_name)144 hidden_def(semanage_bool_set_name)
145 
146 /* Value */
147 int semanage_bool_get_value(const semanage_bool_t * boolean)
148 {
149 
150 	return sepol_bool_get_value(boolean);
151 }
152 
hidden_def(semanage_bool_get_value)153 hidden_def(semanage_bool_get_value)
154 
155 void semanage_bool_set_value(semanage_bool_t * boolean, int value)
156 {
157 
158 	sepol_bool_set_value(boolean, value);
159 }
160 
hidden_def(semanage_bool_set_value)161 hidden_def(semanage_bool_set_value)
162 
163 /* Create/Clone/Destroy */
164 int semanage_bool_create(semanage_handle_t * handle,
165 			 semanage_bool_t ** bool_ptr)
166 {
167 
168 	return sepol_bool_create(handle->sepolh, bool_ptr);
169 }
170 
hidden_def(semanage_bool_create)171 hidden_def(semanage_bool_create)
172 
173 int semanage_bool_clone(semanage_handle_t * handle,
174 			const semanage_bool_t * boolean,
175 			semanage_bool_t ** bool_ptr)
176 {
177 
178 	return sepol_bool_clone(handle->sepolh, boolean, bool_ptr);
179 }
180 
hidden_def(semanage_bool_clone)181 hidden_def(semanage_bool_clone)
182 
183 void semanage_bool_free(semanage_bool_t * boolean)
184 {
185 
186 	sepol_bool_free(boolean);
187 }
188 
189 hidden_def(semanage_bool_free)
190 
191 /* Record base functions */
192 record_table_t SEMANAGE_BOOL_RTABLE = {
193 	.create = semanage_bool_create,
194 	.key_extract = semanage_bool_key_extract,
195 	.key_free = semanage_bool_key_free,
196 	.clone = semanage_bool_clone,
197 	.compare = semanage_bool_compare,
198 	.compare2 = semanage_bool_compare2,
199 	.compare2_qsort = semanage_bool_compare2_qsort,
200 	.free = semanage_bool_free,
201 };
202