1 | /* abac.c */ |
---|
2 | |
---|
3 | #include <assert.h> |
---|
4 | #include <err.h> |
---|
5 | #include <glob.h> |
---|
6 | |
---|
7 | #include "abac.h" |
---|
8 | #include "abac_graph.h" |
---|
9 | #include "abac_util.h" |
---|
10 | #include "abac_verifier.h" |
---|
11 | |
---|
12 | struct _abac_context_t { |
---|
13 | abac_graph_t *graph; |
---|
14 | }; |
---|
15 | |
---|
16 | /** |
---|
17 | * Init the library. |
---|
18 | */ |
---|
19 | void libabac_init(void) { |
---|
20 | void libabac_deinit(void); |
---|
21 | static int has_been_init = 0; |
---|
22 | |
---|
23 | // called every time a context is created, so only do it once |
---|
24 | if (!has_been_init) { |
---|
25 | abac_verifier_init(); |
---|
26 | atexit(libabac_deinit); |
---|
27 | has_been_init = 1; |
---|
28 | } |
---|
29 | } |
---|
30 | |
---|
31 | /** |
---|
32 | * Deinit the library. |
---|
33 | */ |
---|
34 | void libabac_deinit(void) { |
---|
35 | abac_verifier_deinit(); |
---|
36 | } |
---|
37 | |
---|
38 | /** |
---|
39 | * Create a new abac context. |
---|
40 | */ |
---|
41 | abac_context_t *abac_context_new(void) { |
---|
42 | libabac_init(); |
---|
43 | |
---|
44 | abac_context_t *ctx = abac_xmalloc(sizeof(abac_context_t)); |
---|
45 | ctx->graph = abac_graph_new(); |
---|
46 | return ctx; |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * Deep copy an abac context. |
---|
51 | */ |
---|
52 | abac_context_t *abac_context_dup(abac_context_t *ctx) { |
---|
53 | assert(ctx != NULL); |
---|
54 | |
---|
55 | abac_context_t *dup = abac_xmalloc(sizeof(abac_context_t)); |
---|
56 | dup->graph = abac_graph_dup(ctx->graph); |
---|
57 | |
---|
58 | return dup; |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * Free an abac context. |
---|
63 | */ |
---|
64 | void abac_context_free(abac_context_t *ctx) { |
---|
65 | assert(ctx != NULL); |
---|
66 | |
---|
67 | abac_graph_free(ctx->graph); |
---|
68 | free(ctx); |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Load an ID cert from a file. |
---|
73 | */ |
---|
74 | int abac_context_load_id_file(abac_context_t *ctx, char *filename) { |
---|
75 | assert(ctx != NULL); assert(filename != NULL); |
---|
76 | return abac_verifier_load_id_file(filename); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Load an ID cert from a chunk. |
---|
81 | */ |
---|
82 | int abac_context_load_id_chunk(abac_context_t *ctx, abac_chunk_t cert_chunk) { |
---|
83 | assert(ctx != NULL); |
---|
84 | return abac_verifier_load_id_chunk(cert_chunk); |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * Load an attribute cert from a file. |
---|
89 | */ |
---|
90 | int abac_context_load_attribute_file(abac_context_t *ctx, char *filename) { |
---|
91 | int ret, add_ret; |
---|
92 | abac_list_t *cred_list=abac_list_new(); // could be more than 1 |
---|
93 | abac_credential_t *cred; |
---|
94 | |
---|
95 | assert(ctx != NULL); assert(filename != NULL); |
---|
96 | |
---|
97 | ret = abac_verifier_load_attribute_cert_file(filename, cred_list); |
---|
98 | |
---|
99 | if (ret == ABAC_CERT_SUCCESS) { |
---|
100 | int size = abac_list_size(cred_list); |
---|
101 | if(size) { |
---|
102 | abac_list_foreach(cred_list, cred, |
---|
103 | add_ret = abac_graph_add_credential(ctx->graph, cred); |
---|
104 | assert(add_ret != ABAC_GRAPH_CRED_INVALID); |
---|
105 | abac_credential_free(cred); |
---|
106 | ); |
---|
107 | abac_list_free(cred_list); |
---|
108 | } |
---|
109 | } |
---|
110 | return ret; |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * Load an attribute cert from a chunk. |
---|
115 | */ |
---|
116 | int abac_context_load_attribute_chunk(abac_context_t *ctx, abac_chunk_t cert_chunk) { |
---|
117 | int ret, add_ret; |
---|
118 | abac_list_t *cred_list=NULL; |
---|
119 | abac_credential_t *cred; |
---|
120 | |
---|
121 | assert(ctx != NULL); |
---|
122 | |
---|
123 | ret = abac_verifier_load_attribute_cert_chunk(cert_chunk, cred_list); |
---|
124 | if (ret == ABAC_CERT_SUCCESS) { |
---|
125 | int size = abac_list_size(cred_list); |
---|
126 | if(size) { |
---|
127 | abac_list_foreach(cred_list, cred, |
---|
128 | add_ret = abac_graph_add_credential(ctx->graph, cred); |
---|
129 | assert(add_ret != ABAC_GRAPH_CRED_INVALID); |
---|
130 | abac_credential_free(cred); |
---|
131 | ); |
---|
132 | abac_list_free(cred_list); |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | return ret; |
---|
137 | } |
---|
138 | |
---|
139 | #define ID_PAT "/*_ID.{der,pem}" |
---|
140 | #define ATTR_PAT "/*_attr.xml" |
---|
141 | |
---|
142 | /** |
---|
143 | * Load a directory full of certs. |
---|
144 | */ |
---|
145 | void abac_context_load_directory(abac_context_t *ctx, char *path) { |
---|
146 | char *glob_pat; |
---|
147 | glob_t glob_buf; |
---|
148 | int i, ret; |
---|
149 | |
---|
150 | assert(ctx != NULL); assert(path != NULL); |
---|
151 | |
---|
152 | int dirlen = strlen(path); |
---|
153 | glob_pat = abac_xmalloc(dirlen + sizeof(ID_PAT)); |
---|
154 | memcpy(glob_pat, path, dirlen); |
---|
155 | |
---|
156 | // first load ID certs |
---|
157 | memcpy(glob_pat + dirlen, ID_PAT, sizeof(ID_PAT)); |
---|
158 | glob(glob_pat, GLOB_BRACE, NULL, &glob_buf); // TODO check for error |
---|
159 | for (i = 0; i < glob_buf.gl_pathc; ++i) { |
---|
160 | char *cert_file = glob_buf.gl_pathv[i]; |
---|
161 | |
---|
162 | ret = abac_context_load_id_file(ctx, cert_file); |
---|
163 | if (ret != ABAC_CERT_SUCCESS) |
---|
164 | warnx("Couldn't load ID cert %s", cert_file); |
---|
165 | } |
---|
166 | globfree(&glob_buf); |
---|
167 | |
---|
168 | // then load attr certs |
---|
169 | memcpy(glob_pat + dirlen, ATTR_PAT, sizeof(ATTR_PAT)); |
---|
170 | glob(glob_pat, 0, NULL, &glob_buf); // TODO check for error |
---|
171 | for (i = 0; i < glob_buf.gl_pathc; ++i) { |
---|
172 | char *cert_file = glob_buf.gl_pathv[i]; |
---|
173 | |
---|
174 | ret = abac_context_load_attribute_file(ctx, cert_file); |
---|
175 | if (ret != ABAC_CERT_SUCCESS) |
---|
176 | warnx("Couldn't load attribute cert %s", cert_file); |
---|
177 | } |
---|
178 | globfree(&glob_buf); |
---|
179 | |
---|
180 | free(glob_pat); |
---|
181 | } |
---|
182 | |
---|
183 | /** |
---|
184 | * Run a query on the data in an abac context. Returns a NULL-terminated array |
---|
185 | * of abac_credential_t. Success/failure in *success. |
---|
186 | */ |
---|
187 | abac_credential_t **abac_context_query(abac_context_t *ctx, char *role, char *principal, int *success) { |
---|
188 | abac_credential_t **credentials = NULL, *cur; |
---|
189 | int i = 0; |
---|
190 | |
---|
191 | assert(ctx != NULL); assert(role != NULL); assert(principal != NULL); assert(success != NULL); |
---|
192 | |
---|
193 | abac_graph_t *result_graph = abac_graph_query(ctx->graph, role, principal); |
---|
194 | abac_list_t *result = abac_graph_credentials(result_graph); |
---|
195 | |
---|
196 | abac_graph_free(result_graph); |
---|
197 | |
---|
198 | int size = abac_list_size(result); |
---|
199 | if (size > 0) |
---|
200 | *success = 1; |
---|
201 | |
---|
202 | // if there is no actual path, return everything that can reach the role |
---|
203 | else { |
---|
204 | *success = 0; |
---|
205 | abac_list_free(result); |
---|
206 | result_graph = abac_graph_new(); |
---|
207 | |
---|
208 | // TODO: This can probably be better, but it now returns an |
---|
209 | // approximation of a partial proof. It returns all the attributes the |
---|
210 | // principal can reach and all the attributes that will lead to a |
---|
211 | // success. |
---|
212 | |
---|
213 | /* Get all the attributes of the principal. This calls sub-queries to |
---|
214 | * flesh out the indirect proofs. */ |
---|
215 | result_graph = abac_graph_principal_creds(ctx->graph, principal); |
---|
216 | |
---|
217 | /* This gets all the attributes linked to the target en route to the |
---|
218 | * principal. */ |
---|
219 | result = abac_graph_postorder_credentials(ctx->graph, role); |
---|
220 | |
---|
221 | /* Merge responses */ |
---|
222 | abac_list_foreach(result, cur, |
---|
223 | abac_graph_add_credential(result_graph, cur); |
---|
224 | ); |
---|
225 | abac_list_free(result); |
---|
226 | abac_graph_derive_links(result_graph); |
---|
227 | |
---|
228 | result = abac_graph_credentials(result_graph); |
---|
229 | abac_graph_free(result_graph); |
---|
230 | |
---|
231 | size = abac_list_size(result); |
---|
232 | } |
---|
233 | |
---|
234 | // make the array (leave space to NULL terminate it) |
---|
235 | // n.b., even if the list is empty, we still return an array that |
---|
236 | // only contains the NULL terminator |
---|
237 | credentials = abac_xmalloc(sizeof(abac_credential_t *) * (size + 1)); |
---|
238 | abac_list_foreach(result, cur, |
---|
239 | credentials[i++] = cur; |
---|
240 | ); |
---|
241 | credentials[i] = NULL; |
---|
242 | |
---|
243 | abac_list_free(result); |
---|
244 | |
---|
245 | return credentials; |
---|
246 | } |
---|
247 | |
---|
248 | /** |
---|
249 | * A NULL-terminated array of all the credentials in the context. |
---|
250 | */ |
---|
251 | abac_credential_t **abac_context_credentials(abac_context_t *ctx) { |
---|
252 | abac_credential_t *cred; |
---|
253 | int i = 0; |
---|
254 | |
---|
255 | assert(ctx != NULL); |
---|
256 | |
---|
257 | abac_list_t *cred_list = abac_graph_credentials(ctx->graph); |
---|
258 | int size = abac_list_size(cred_list); |
---|
259 | |
---|
260 | abac_credential_t **credentials = abac_xmalloc(sizeof(abac_credential_t *) * (size + 1)); |
---|
261 | abac_list_foreach(cred_list, cred, |
---|
262 | credentials[i++] = cred; |
---|
263 | ); |
---|
264 | credentials[i] = NULL; |
---|
265 | |
---|
266 | abac_list_free(cred_list); |
---|
267 | |
---|
268 | return credentials; |
---|
269 | } |
---|
270 | |
---|
271 | /** |
---|
272 | * Frees a NULL-terminated list of credentials. |
---|
273 | */ |
---|
274 | void abac_context_credentials_free(abac_credential_t **credentials) { |
---|
275 | int i; |
---|
276 | |
---|
277 | if (credentials == NULL) |
---|
278 | return; |
---|
279 | |
---|
280 | for (i = 0; credentials[i] != NULL; ++i) |
---|
281 | abac_credential_free(credentials[i]); |
---|
282 | free(credentials); |
---|
283 | } |
---|