1 /* Copyright (C) 2005 Red Hat, Inc. */
2
3 /* Object: semanage_seuser_t (Unix User)
4 * Object: semanage_seuser_key_t (Unix User Key)
5 * Implements: record_t (Database Record)
6 * Implements: record_key_t (Database Record Key)
7 */
8
9 struct semanage_seuser;
10 struct semanage_seuser_key;
11 typedef struct semanage_seuser record_t;
12 typedef struct semanage_seuser_key record_key_t;
13 #define DBASE_RECORD_DEFINED
14
15 #include <stdlib.h>
16 #include <string.h>
17 #include "seuser_internal.h"
18 #include "debug.h"
19 #include <semanage/handle.h>
20 #include "database.h"
21
22 struct semanage_seuser {
23 /* This user's name */
24 char *name;
25
26 /* This user's corresponding
27 * seuser ("role set") */
28 char *sename;
29
30 /* This user's mls range (only required for mls) */
31 char *mls_range;
32 };
33
34 struct semanage_seuser_key {
35 /* This user's name */
36 char *name;
37 };
38
semanage_seuser_key_create(semanage_handle_t * handle,const char * name,semanage_seuser_key_t ** key_ptr)39 int semanage_seuser_key_create(semanage_handle_t * handle,
40 const char *name,
41 semanage_seuser_key_t ** key_ptr)
42 {
43
44 semanage_seuser_key_t *tmp_key = (semanage_seuser_key_t *)
45 malloc(sizeof(semanage_seuser_key_t));
46
47 if (!tmp_key) {
48 ERR(handle, "out of memory, could not create seuser key");
49 return STATUS_ERR;
50 }
51 tmp_key->name = strdup(name);
52 if (!tmp_key->name) {
53 ERR(handle, "out of memory, could not create seuser key");
54 free(tmp_key);
55 return STATUS_ERR;
56 }
57
58 *key_ptr = tmp_key;
59 return STATUS_SUCCESS;
60 }
61
hidden_def(semanage_seuser_key_create)62 hidden_def(semanage_seuser_key_create)
63
64 int semanage_seuser_key_extract(semanage_handle_t * handle,
65 const semanage_seuser_t * seuser,
66 semanage_seuser_key_t ** key_ptr)
67 {
68
69 if (semanage_seuser_key_create(handle, seuser->name, key_ptr) < 0)
70 goto err;
71
72 return STATUS_SUCCESS;
73
74 err:
75 ERR(handle, "could not extract seuser key from record");
76 return STATUS_ERR;
77 }
78
hidden_def(semanage_seuser_key_extract)79 hidden_def(semanage_seuser_key_extract)
80
81 void semanage_seuser_key_free(semanage_seuser_key_t * key)
82 {
83 free(key->name);
84 free(key);
85 }
86
hidden_def(semanage_seuser_key_free)87 hidden_def(semanage_seuser_key_free)
88
89 int semanage_seuser_compare(const semanage_seuser_t * seuser,
90 const semanage_seuser_key_t * key)
91 {
92
93 return strcmp(seuser->name, key->name);
94 }
95
hidden_def(semanage_seuser_compare)96 hidden_def(semanage_seuser_compare)
97
98 int semanage_seuser_compare2(const semanage_seuser_t * seuser,
99 const semanage_seuser_t * seuser2)
100 {
101
102 return strcmp(seuser->name, seuser2->name);
103 }
104
hidden_def(semanage_seuser_compare2)105 hidden_def(semanage_seuser_compare2)
106
107 static int semanage_seuser_compare2_qsort(const semanage_seuser_t ** seuser,
108 const semanage_seuser_t ** seuser2)
109 {
110
111 return strcmp((*seuser)->name, (*seuser2)->name);
112 }
113
114 /* Name */
semanage_seuser_get_name(const semanage_seuser_t * seuser)115 const char *semanage_seuser_get_name(const semanage_seuser_t * seuser)
116 {
117
118 return seuser->name;
119 }
120
hidden_def(semanage_seuser_get_name)121 hidden_def(semanage_seuser_get_name)
122
123 int semanage_seuser_set_name(semanage_handle_t * handle,
124 semanage_seuser_t * seuser, const char *name)
125 {
126
127 char *tmp_name = strdup(name);
128 if (!tmp_name) {
129 ERR(handle, "out of memory, could not set seuser (Unix) name");
130 return STATUS_ERR;
131 }
132 free(seuser->name);
133 seuser->name = tmp_name;
134 return STATUS_SUCCESS;
135 }
136
hidden_def(semanage_seuser_set_name)137 hidden_def(semanage_seuser_set_name)
138
139 /* Selinux Name */
140 const char *semanage_seuser_get_sename(const semanage_seuser_t * seuser)
141 {
142
143 return seuser->sename;
144 }
145
hidden_def(semanage_seuser_get_sename)146 hidden_def(semanage_seuser_get_sename)
147
148 int semanage_seuser_set_sename(semanage_handle_t * handle,
149 semanage_seuser_t * seuser, const char *sename)
150 {
151
152 char *tmp_sename = strdup(sename);
153 if (!tmp_sename) {
154 ERR(handle,
155 "out of memory, could not set seuser (SELinux) name");
156 return STATUS_ERR;
157 }
158 free(seuser->sename);
159 seuser->sename = tmp_sename;
160 return STATUS_SUCCESS;
161 }
162
hidden_def(semanage_seuser_set_sename)163 hidden_def(semanage_seuser_set_sename)
164
165 /* MLS Range */
166 const char *semanage_seuser_get_mlsrange(const semanage_seuser_t * seuser)
167 {
168
169 return seuser->mls_range;
170 }
171
hidden_def(semanage_seuser_get_mlsrange)172 hidden_def(semanage_seuser_get_mlsrange)
173
174 int semanage_seuser_set_mlsrange(semanage_handle_t * handle,
175 semanage_seuser_t * seuser,
176 const char *mls_range)
177 {
178
179 char *tmp_mls_range = strdup(mls_range);
180 if (!tmp_mls_range) {
181 ERR(handle, "out of memory, could not set seuser MLS range");
182 return STATUS_ERR;
183 }
184 free(seuser->mls_range);
185 seuser->mls_range = tmp_mls_range;
186 return STATUS_SUCCESS;
187 }
188
hidden_def(semanage_seuser_set_mlsrange)189 hidden_def(semanage_seuser_set_mlsrange)
190
191 /* Create */
192 int semanage_seuser_create(semanage_handle_t * handle,
193 semanage_seuser_t ** seuser_ptr)
194 {
195
196 semanage_seuser_t *seuser =
197 (semanage_seuser_t *) malloc(sizeof(semanage_seuser_t));
198
199 if (!seuser) {
200 ERR(handle, "out of memory, could not create seuser");
201 return STATUS_ERR;
202 }
203
204 seuser->name = NULL;
205 seuser->sename = NULL;
206 seuser->mls_range = NULL;
207
208 *seuser_ptr = seuser;
209 return STATUS_SUCCESS;
210 }
211
hidden_def(semanage_seuser_create)212 hidden_def(semanage_seuser_create)
213
214 /* Deep copy clone */
215 int semanage_seuser_clone(semanage_handle_t * handle,
216 const semanage_seuser_t * seuser,
217 semanage_seuser_t ** seuser_ptr)
218 {
219
220 semanage_seuser_t *new_seuser = NULL;
221
222 if (semanage_seuser_create(handle, &new_seuser) < 0)
223 goto err;
224
225 if (semanage_seuser_set_name(handle, new_seuser, seuser->name) < 0)
226 goto err;
227
228 if (semanage_seuser_set_sename(handle, new_seuser, seuser->sename) < 0)
229 goto err;
230
231 if (seuser->mls_range &&
232 (semanage_seuser_set_mlsrange(handle, new_seuser, seuser->mls_range)
233 < 0))
234 goto err;
235
236 *seuser_ptr = new_seuser;
237 return STATUS_SUCCESS;
238
239 err:
240 ERR(handle, "could not clone seuser");
241 semanage_seuser_free(new_seuser);
242 return STATUS_ERR;
243 }
244
hidden_def(semanage_seuser_clone)245 hidden_def(semanage_seuser_clone)
246
247 /* Destroy */
248 void semanage_seuser_free(semanage_seuser_t * seuser)
249 {
250
251 if (!seuser)
252 return;
253
254 free(seuser->name);
255 free(seuser->sename);
256 free(seuser->mls_range);
257 free(seuser);
258 }
259
260 hidden_def(semanage_seuser_free)
261
262 /* Record base functions */
263 record_table_t SEMANAGE_SEUSER_RTABLE = {
264 .create = semanage_seuser_create,
265 .key_extract = semanage_seuser_key_extract,
266 .key_free = semanage_seuser_key_free,
267 .clone = semanage_seuser_clone,
268 .compare = semanage_seuser_compare,
269 .compare2 = semanage_seuser_compare2,
270 .compare2_qsort = semanage_seuser_compare2_qsort,
271 .free = semanage_seuser_free,
272 };
273