1 #include <errno.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "boolean_internal.h"
6 #include "debug.h"
7
8 struct sepol_bool {
9 /* This boolean's name */
10 char *name;
11
12 /* Its value */
13 int value;
14 };
15
16 struct sepol_bool_key {
17 /* This boolean's name */
18 char *name;
19 };
20
sepol_bool_key_create(sepol_handle_t * handle,const char * name,sepol_bool_key_t ** key_ptr)21 int sepol_bool_key_create(sepol_handle_t * handle,
22 const char *name, sepol_bool_key_t ** key_ptr)
23 {
24
25 sepol_bool_key_t *tmp_key =
26 (sepol_bool_key_t *) malloc(sizeof(struct sepol_bool_key));
27
28 if (!tmp_key) {
29 ERR(handle, "out of memory, " "could not create boolean key");
30 return STATUS_ERR;
31 }
32
33 tmp_key->name = strdup(name);
34 if (!tmp_key->name) {
35 ERR(handle, "out of memory, " "could not create boolean key");
36 free(tmp_key);
37 return STATUS_ERR;
38 }
39
40 *key_ptr = tmp_key;
41 return STATUS_SUCCESS;
42 }
43
hidden_def(sepol_bool_key_create)44 hidden_def(sepol_bool_key_create)
45
46 void sepol_bool_key_unpack(const sepol_bool_key_t * key, const char **name)
47 {
48
49 *name = key->name;
50 }
51
hidden_def(sepol_bool_key_unpack)52 hidden_def(sepol_bool_key_unpack)
53
54 int sepol_bool_key_extract(sepol_handle_t * handle,
55 const sepol_bool_t * boolean,
56 sepol_bool_key_t ** key_ptr)
57 {
58
59 if (sepol_bool_key_create(handle, boolean->name, key_ptr) < 0) {
60 ERR(handle, "could not extract key from boolean %s",
61 boolean->name);
62 return STATUS_ERR;
63 }
64
65 return STATUS_SUCCESS;
66 }
67
sepol_bool_key_free(sepol_bool_key_t * key)68 void sepol_bool_key_free(sepol_bool_key_t * key)
69 {
70 free(key->name);
71 free(key);
72 }
73
sepol_bool_compare(const sepol_bool_t * boolean,const sepol_bool_key_t * key)74 int sepol_bool_compare(const sepol_bool_t * boolean,
75 const sepol_bool_key_t * key)
76 {
77
78 return strcmp(boolean->name, key->name);
79 }
80
sepol_bool_compare2(const sepol_bool_t * boolean,const sepol_bool_t * boolean2)81 int sepol_bool_compare2(const sepol_bool_t * boolean,
82 const sepol_bool_t * boolean2)
83 {
84
85 return strcmp(boolean->name, boolean2->name);
86 }
87
88 /* Name */
sepol_bool_get_name(const sepol_bool_t * boolean)89 const char *sepol_bool_get_name(const sepol_bool_t * boolean)
90 {
91
92 return boolean->name;
93 }
94
hidden_def(sepol_bool_get_name)95 hidden_def(sepol_bool_get_name)
96
97 int sepol_bool_set_name(sepol_handle_t * handle,
98 sepol_bool_t * boolean, const char *name)
99 {
100
101 char *tmp_name = strdup(name);
102 if (!tmp_name) {
103 ERR(handle, "out of memory, could not set boolean name");
104 return STATUS_ERR;
105 }
106 free(boolean->name);
107 boolean->name = tmp_name;
108 return STATUS_SUCCESS;
109 }
110
hidden_def(sepol_bool_set_name)111 hidden_def(sepol_bool_set_name)
112
113 /* Value */
114 int sepol_bool_get_value(const sepol_bool_t * boolean)
115 {
116
117 return boolean->value;
118 }
119
hidden_def(sepol_bool_get_value)120 hidden_def(sepol_bool_get_value)
121
122 void sepol_bool_set_value(sepol_bool_t * boolean, int value)
123 {
124
125 boolean->value = value;
126 }
127
hidden_def(sepol_bool_set_value)128 hidden_def(sepol_bool_set_value)
129
130 /* Create */
131 int sepol_bool_create(sepol_handle_t * handle, sepol_bool_t ** bool_ptr)
132 {
133
134 sepol_bool_t *boolean = (sepol_bool_t *) malloc(sizeof(sepol_bool_t));
135
136 if (!boolean) {
137 ERR(handle, "out of memory, "
138 "could not create boolean record");
139 return STATUS_ERR;
140 }
141
142 boolean->name = NULL;
143 boolean->value = 0;
144
145 *bool_ptr = boolean;
146 return STATUS_SUCCESS;
147 }
148
hidden_def(sepol_bool_create)149 hidden_def(sepol_bool_create)
150
151 /* Deep copy clone */
152 int sepol_bool_clone(sepol_handle_t * handle,
153 const sepol_bool_t * boolean, sepol_bool_t ** bool_ptr)
154 {
155
156 sepol_bool_t *new_bool = NULL;
157
158 if (sepol_bool_create(handle, &new_bool) < 0)
159 goto err;
160
161 if (sepol_bool_set_name(handle, new_bool, boolean->name) < 0)
162 goto err;
163
164 new_bool->value = boolean->value;
165
166 *bool_ptr = new_bool;
167 return STATUS_SUCCESS;
168
169 err:
170 ERR(handle, "could not clone boolean record");
171 sepol_bool_free(new_bool);
172 return STATUS_ERR;
173 }
174
175 /* Destroy */
sepol_bool_free(sepol_bool_t * boolean)176 void sepol_bool_free(sepol_bool_t * boolean)
177 {
178
179 if (!boolean)
180 return;
181
182 free(boolean->name);
183 free(boolean);
184 }
185
186 hidden_def(sepol_bool_free)
187