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
44
sepol_bool_key_unpack(const sepol_bool_key_t * key,const char ** name)45 void sepol_bool_key_unpack(const sepol_bool_key_t * key, const char **name)
46 {
47
48 *name = key->name;
49 }
50
51
sepol_bool_key_extract(sepol_handle_t * handle,const sepol_bool_t * boolean,sepol_bool_key_t ** key_ptr)52 int sepol_bool_key_extract(sepol_handle_t * handle,
53 const sepol_bool_t * boolean,
54 sepol_bool_key_t ** key_ptr)
55 {
56
57 if (sepol_bool_key_create(handle, boolean->name, key_ptr) < 0) {
58 ERR(handle, "could not extract key from boolean %s",
59 boolean->name);
60 return STATUS_ERR;
61 }
62
63 return STATUS_SUCCESS;
64 }
65
sepol_bool_key_free(sepol_bool_key_t * key)66 void sepol_bool_key_free(sepol_bool_key_t * key)
67 {
68 if (!key)
69 return;
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
95
sepol_bool_set_name(sepol_handle_t * handle,sepol_bool_t * boolean,const char * name)96 int sepol_bool_set_name(sepol_handle_t * handle,
97 sepol_bool_t * boolean, const char *name)
98 {
99
100 char *tmp_name = strdup(name);
101 if (!tmp_name) {
102 ERR(handle, "out of memory, could not set boolean name");
103 return STATUS_ERR;
104 }
105 free(boolean->name);
106 boolean->name = tmp_name;
107 return STATUS_SUCCESS;
108 }
109
110
111 /* Value */
sepol_bool_get_value(const sepol_bool_t * boolean)112 int sepol_bool_get_value(const sepol_bool_t * boolean)
113 {
114
115 return boolean->value;
116 }
117
118
sepol_bool_set_value(sepol_bool_t * boolean,int value)119 void sepol_bool_set_value(sepol_bool_t * boolean, int value)
120 {
121
122 boolean->value = value;
123 }
124
125
126 /* Create */
sepol_bool_create(sepol_handle_t * handle,sepol_bool_t ** bool_ptr)127 int sepol_bool_create(sepol_handle_t * handle, sepol_bool_t ** bool_ptr)
128 {
129
130 sepol_bool_t *boolean = (sepol_bool_t *) malloc(sizeof(sepol_bool_t));
131
132 if (!boolean) {
133 ERR(handle, "out of memory, "
134 "could not create boolean record");
135 return STATUS_ERR;
136 }
137
138 boolean->name = NULL;
139 boolean->value = 0;
140
141 *bool_ptr = boolean;
142 return STATUS_SUCCESS;
143 }
144
145
146 /* Deep copy clone */
sepol_bool_clone(sepol_handle_t * handle,const sepol_bool_t * boolean,sepol_bool_t ** bool_ptr)147 int sepol_bool_clone(sepol_handle_t * handle,
148 const sepol_bool_t * boolean, sepol_bool_t ** bool_ptr)
149 {
150
151 sepol_bool_t *new_bool = NULL;
152
153 if (sepol_bool_create(handle, &new_bool) < 0)
154 goto err;
155
156 if (sepol_bool_set_name(handle, new_bool, boolean->name) < 0)
157 goto err;
158
159 new_bool->value = boolean->value;
160
161 *bool_ptr = new_bool;
162 return STATUS_SUCCESS;
163
164 err:
165 ERR(handle, "could not clone boolean record");
166 sepol_bool_free(new_bool);
167 return STATUS_ERR;
168 }
169
170 /* Destroy */
sepol_bool_free(sepol_bool_t * boolean)171 void sepol_bool_free(sepol_bool_t * boolean)
172 {
173
174 if (!boolean)
175 return;
176
177 free(boolean->name);
178 free(boolean);
179 }
180
181