[d4cbf71] | 1 | #include <assert.h> |
---|
[379a53f] | 2 | #include <stdlib.h> |
---|
[d4cbf71] | 3 | |
---|
[06293d1] | 4 | #include "abac_graph.h" |
---|
[cb0f2b2] | 5 | |
---|
[2fd24c7] | 6 | #include "abac_set.h" |
---|
[3c251d0] | 7 | #include "abac_util.h" |
---|
[d4cbf71] | 8 | |
---|
[f46412c] | 9 | #include "uthash.h" |
---|
| 10 | |
---|
| 11 | // vertex |
---|
[06293d1] | 12 | struct _abac_vertex_t { |
---|
[1743825] | 13 | abac_role_t *role; |
---|
[f46412c] | 14 | char *name; |
---|
| 15 | |
---|
[6d5623e] | 16 | abac_list_t *edges; |
---|
[f46412c] | 17 | |
---|
| 18 | UT_hash_handle hh; |
---|
| 19 | }; |
---|
| 20 | |
---|
[ae72c5d] | 21 | // edge |
---|
[06293d1] | 22 | typedef struct _abac_edge_t { |
---|
| 23 | abac_vertex_t *vertex; |
---|
[401a054] | 24 | abac_credential_t *credential; |
---|
[06293d1] | 25 | } abac_edge_t; |
---|
[ae72c5d] | 26 | |
---|
[f46412c] | 27 | // derived edge |
---|
[06293d1] | 28 | typedef struct _abac_derived_key_t { |
---|
| 29 | abac_vertex_t *head; |
---|
| 30 | abac_edge_t *tail; |
---|
| 31 | } abac_derived_key_t; |
---|
[f46412c] | 32 | |
---|
[06293d1] | 33 | typedef struct _abac_derived_t { |
---|
| 34 | abac_derived_key_t key; |
---|
[f46412c] | 35 | UT_hash_handle hh; |
---|
[06293d1] | 36 | } abac_derived_t; |
---|
[f46412c] | 37 | |
---|
| 38 | // graph |
---|
[06293d1] | 39 | struct _abac_graph_t { |
---|
| 40 | abac_vertex_t *vertices; |
---|
| 41 | abac_derived_t *derived; |
---|
[f46412c] | 42 | int dirty; |
---|
| 43 | }; |
---|
[0ec9e25] | 44 | |
---|
| 45 | /** |
---|
| 46 | * Create a new graph. |
---|
| 47 | */ |
---|
[06293d1] | 48 | abac_graph_t *abac_graph_new(void) { |
---|
[3c251d0] | 49 | abac_graph_t *graph = abac_xmalloc(sizeof(abac_graph_t)); |
---|
[82aeefa] | 50 | |
---|
| 51 | graph->vertices = NULL; |
---|
[6188f4e] | 52 | graph->derived = NULL; |
---|
[82aeefa] | 53 | graph->dirty = 0; |
---|
| 54 | |
---|
| 55 | return graph; |
---|
| 56 | } |
---|
[d4cbf71] | 57 | |
---|
[b26942d] | 58 | /** |
---|
| 59 | * Deep copy a graph. |
---|
| 60 | */ |
---|
[06293d1] | 61 | abac_graph_t *abac_graph_dup(abac_graph_t *graph) { |
---|
| 62 | abac_vertex_t *vertex; |
---|
| 63 | abac_edge_t *edge; |
---|
[b26942d] | 64 | |
---|
[06293d1] | 65 | abac_graph_t *clone = abac_graph_new(); |
---|
[b26942d] | 66 | |
---|
| 67 | // copy the vertices edge by edge |
---|
| 68 | for (vertex = graph->vertices; vertex != NULL; vertex = vertex->hh.next) |
---|
[6d5623e] | 69 | abac_list_foreach(vertex->edges, edge, |
---|
[b26942d] | 70 | // only copy non-derived edges |
---|
[401a054] | 71 | if (edge->credential != NULL) |
---|
| 72 | abac_graph_add_credential(clone, edge->credential); |
---|
[b26942d] | 73 | ); |
---|
| 74 | |
---|
| 75 | return clone; |
---|
| 76 | } |
---|
| 77 | |
---|
[d4cbf71] | 78 | /** |
---|
[401a054] | 79 | * Add a vertex to the graph. Should only be called by abac_graph_add_credential. |
---|
[d4cbf71] | 80 | */ |
---|
[06293d1] | 81 | static abac_vertex_t *_get_vertex(abac_graph_t *graph, abac_role_t *role) { |
---|
| 82 | abac_vertex_t *vertex; |
---|
[c28bd8b] | 83 | char *string; |
---|
| 84 | |
---|
[dcc1a8e] | 85 | string = abac_role_string(role); |
---|
[82aeefa] | 86 | HASH_FIND_STR(graph->vertices, string, vertex); |
---|
[c28bd8b] | 87 | |
---|
| 88 | // add the vertex if it doesn't exist |
---|
| 89 | if (vertex == NULL) { |
---|
[3c251d0] | 90 | vertex = abac_xmalloc(sizeof(abac_vertex_t)); |
---|
[dcc1a8e] | 91 | vertex->role = abac_role_dup(role); |
---|
| 92 | vertex->name = abac_role_string(vertex->role); |
---|
[d4cbf71] | 93 | |
---|
[379a53f] | 94 | // create the list of edges |
---|
[6d5623e] | 95 | vertex->edges = abac_list_new(); |
---|
[d4cbf71] | 96 | |
---|
[c28bd8b] | 97 | // add it to the vertices |
---|
[82aeefa] | 98 | HASH_ADD_KEYPTR(hh, graph->vertices, vertex->name, strlen(vertex->name), vertex); |
---|
[c28bd8b] | 99 | } |
---|
[d4cbf71] | 100 | |
---|
| 101 | return vertex; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /** |
---|
[401a054] | 105 | * Add a credential to the credential graph. |
---|
[d4cbf71] | 106 | */ |
---|
[401a054] | 107 | int abac_graph_add_credential(abac_graph_t *graph, abac_credential_t *cred) { |
---|
[06293d1] | 108 | abac_vertex_t *head_vertex, *tail_vertex; |
---|
[314869f] | 109 | abac_edge_t *edge; |
---|
[d4cbf71] | 110 | |
---|
[401a054] | 111 | assert(cred != NULL); |
---|
[ae72c5d] | 112 | |
---|
[401a054] | 113 | abac_role_t *head = abac_credential_head(cred); |
---|
| 114 | abac_role_t *tail = abac_credential_tail(cred); |
---|
[d4cbf71] | 115 | |
---|
[401a054] | 116 | // a valid credential must have a role for the head |
---|
[dcc1a8e] | 117 | if (!abac_role_is_role(head)) return 0; |
---|
[d4cbf71] | 118 | |
---|
[82aeefa] | 119 | head_vertex = _get_vertex(graph, head); |
---|
| 120 | tail_vertex = _get_vertex(graph, tail); |
---|
[d4cbf71] | 121 | |
---|
[314869f] | 122 | // make sure we don't insert the same edge twice (ugh) |
---|
| 123 | abac_list_foreach(head_vertex->edges, edge, |
---|
| 124 | if (edge->vertex == tail_vertex) |
---|
| 125 | return 0; |
---|
| 126 | ); |
---|
| 127 | |
---|
[ae72c5d] | 128 | // create the edge and add it |
---|
[314869f] | 129 | edge = abac_xmalloc(sizeof(abac_edge_t)); |
---|
[ae72c5d] | 130 | edge->vertex = tail_vertex; |
---|
[401a054] | 131 | edge->credential = abac_credential_dup(cred); |
---|
[ae72c5d] | 132 | |
---|
[6d5623e] | 133 | abac_list_add(head_vertex->edges, edge); |
---|
[d4cbf71] | 134 | |
---|
[82aeefa] | 135 | // must re-derive edges |
---|
| 136 | graph->dirty = 1; |
---|
| 137 | |
---|
[d4cbf71] | 138 | return 1; |
---|
| 139 | } |
---|
| 140 | |
---|
[ebde9dd] | 141 | // find the principals that have a role |
---|
[06293d1] | 142 | static abac_set_t *_find_principals(abac_graph_t *graph, abac_vertex_t *start_vertex) { |
---|
[2fd24c7] | 143 | abac_set_t *principals = abac_set_new(); |
---|
[d4cbf71] | 144 | |
---|
[06293d1] | 145 | abac_list_t *traversal = abac_graph_postorder(graph, start_vertex->role); |
---|
| 146 | abac_vertex_t *vertex; |
---|
[d4cbf71] | 147 | |
---|
[6d5623e] | 148 | abac_list_foreach(traversal, vertex, |
---|
[dcc1a8e] | 149 | if (abac_role_is_principal(vertex->role)) |
---|
| 150 | abac_set_add(principals, abac_role_string(vertex->role)); |
---|
[ebde9dd] | 151 | ); |
---|
[d4cbf71] | 152 | |
---|
[6d5623e] | 153 | abac_list_free(traversal); |
---|
[ebde9dd] | 154 | return principals; |
---|
| 155 | } |
---|
[d4cbf71] | 156 | |
---|
[6188f4e] | 157 | // remove any derived edges from the graph |
---|
[06293d1] | 158 | void _clear_derived(abac_graph_t *graph) { |
---|
| 159 | abac_derived_t *current; |
---|
[6188f4e] | 160 | |
---|
| 161 | while (graph->derived) { |
---|
| 162 | current = graph->derived; |
---|
| 163 | |
---|
| 164 | HASH_DEL(graph->derived, current); |
---|
| 165 | |
---|
[06293d1] | 166 | abac_vertex_t *head = current->key.head; |
---|
| 167 | abac_edge_t *tail = current->key.tail; |
---|
[401a054] | 168 | assert(tail->credential == NULL); |
---|
[6188f4e] | 169 | |
---|
| 170 | // this can fail, but we assume the data structures are consistent |
---|
[6d5623e] | 171 | abac_list_remove(head->edges, tail); |
---|
[6188f4e] | 172 | |
---|
| 173 | free(current); |
---|
[186cb75] | 174 | free(tail); |
---|
[6188f4e] | 175 | } |
---|
| 176 | } |
---|
| 177 | |
---|
[06293d1] | 178 | void abac_graph_derive_links(abac_graph_t *graph) { |
---|
| 179 | abac_vertex_t *vertex; |
---|
[d4cbf71] | 180 | |
---|
[ebde9dd] | 181 | if (!graph->dirty) |
---|
| 182 | return; |
---|
[379a53f] | 183 | |
---|
[ebde9dd] | 184 | for (vertex = graph->vertices; vertex != NULL; vertex = vertex->hh.next) { |
---|
| 185 | // we only care about linking roles |
---|
[dcc1a8e] | 186 | if (!abac_role_is_linking(vertex->role)) |
---|
[ebde9dd] | 187 | continue; |
---|
| 188 | |
---|
| 189 | // linking roles take the form A.r1.r2 |
---|
[dcc1a8e] | 190 | char *A_r1 = abac_role_linked_role(vertex->role); |
---|
| 191 | char *r2 = abac_role_role_name(vertex->role); |
---|
[ebde9dd] | 192 | |
---|
| 193 | // find the linked role in the graph |
---|
[06293d1] | 194 | abac_vertex_t *A_r1_vertex; |
---|
[ebde9dd] | 195 | HASH_FIND_STR(graph->vertices, A_r1, A_r1_vertex); |
---|
| 196 | if (A_r1_vertex == NULL) |
---|
| 197 | continue; |
---|
| 198 | |
---|
| 199 | // find the principals that have A.r1 |
---|
[2fd24c7] | 200 | abac_set_t *principals = _find_principals(graph, A_r1_vertex); |
---|
[ebde9dd] | 201 | char *B; |
---|
| 202 | |
---|
[2fd24c7] | 203 | abac_list_t *elts = abac_set_elements(principals); |
---|
[ebde9dd] | 204 | |
---|
| 205 | // and add a link for each B.r2 to A.r1.r2 |
---|
[6d5623e] | 206 | abac_list_foreach(elts, B, |
---|
[ebde9dd] | 207 | int B_len = strlen(B); |
---|
| 208 | int r2_len = strlen(r2); |
---|
| 209 | |
---|
| 210 | // create the string B.r2, thx C |
---|
| 211 | char *B_r2 = malloc(B_len + r2_len + 2); |
---|
| 212 | memcpy(B_r2, B, B_len); |
---|
| 213 | B_r2[B_len] = '.'; |
---|
| 214 | memcpy(B_r2 + B_len + 1, r2, r2_len); |
---|
| 215 | B_r2[B_len + r2_len + 1] = 0; |
---|
| 216 | |
---|
[06293d1] | 217 | abac_vertex_t *B_r2_vertex; |
---|
[ebde9dd] | 218 | HASH_FIND_STR(graph->vertices, B_r2, B_r2_vertex); |
---|
| 219 | |
---|
| 220 | // add an edge if the principal's granted it to someone |
---|
| 221 | if (B_r2_vertex) { |
---|
[dcc1a8e] | 222 | debug_printf("adding edge from %s to %s\n", B_r2, abac_role_string(vertex->role)); |
---|
[3c251d0] | 223 | abac_edge_t *edge = abac_xmalloc(sizeof(abac_edge_t)); |
---|
[ae72c5d] | 224 | edge->vertex = B_r2_vertex; |
---|
[401a054] | 225 | edge->credential = NULL; |
---|
[6d5623e] | 226 | abac_list_add(vertex->edges, edge); |
---|
[ebde9dd] | 227 | |
---|
[6188f4e] | 228 | // add to list of derived edges |
---|
[3c251d0] | 229 | abac_derived_t *derived = abac_xmalloc(sizeof(abac_derived_t)); |
---|
[6188f4e] | 230 | derived->key.head = vertex; |
---|
[ae72c5d] | 231 | derived->key.tail = edge; |
---|
[06293d1] | 232 | HASH_ADD(hh, graph->derived, key, sizeof(abac_derived_key_t), derived); |
---|
[ebde9dd] | 233 | } |
---|
| 234 | |
---|
[dbbf777] | 235 | #ifdef DEBUG |
---|
[dcc1a8e] | 236 | debug_printf(" incoming edges for %s\n", abac_role_string(vertex->role)); |
---|
[06293d1] | 237 | abac_edge_t *cur; |
---|
[6d5623e] | 238 | abac_list_foreach(vertex->edges, cur, |
---|
[dcc1a8e] | 239 | debug_printf(" %s (%s)\n", abac_role_string(cur->vertex->role), cur->vertex->name); |
---|
[379a53f] | 240 | ); |
---|
[dbbf777] | 241 | #endif |
---|
[d4cbf71] | 242 | |
---|
[ebde9dd] | 243 | free(B_r2); |
---|
| 244 | ); |
---|
| 245 | |
---|
[6d5623e] | 246 | abac_list_free(elts); |
---|
[2fd24c7] | 247 | abac_set_free(principals); |
---|
[ebde9dd] | 248 | } |
---|
[6188f4e] | 249 | |
---|
| 250 | graph->dirty = 0; |
---|
[d4cbf71] | 251 | } |
---|
[cb0f2b2] | 252 | |
---|
[06293d1] | 253 | static void _order_recurse(abac_vertex_t *vertex, abac_set_t *seen, int preorder, abac_list_t *stack) { |
---|
| 254 | abac_edge_t *incoming; |
---|
[cb0f2b2] | 255 | |
---|
| 256 | // don't revisit nodes |
---|
[dcc1a8e] | 257 | if (!abac_set_add(seen, abac_role_string(vertex->role))) |
---|
[cb0f2b2] | 258 | return; |
---|
| 259 | |
---|
| 260 | if (preorder) |
---|
[6d5623e] | 261 | abac_list_add(stack, vertex); |
---|
[cb0f2b2] | 262 | |
---|
[9536712] | 263 | // recurse along the incoming vertices |
---|
[6d5623e] | 264 | abac_list_foreach(vertex->edges, incoming, |
---|
[ae72c5d] | 265 | _order_recurse(incoming->vertex, seen, preorder, stack); |
---|
[379a53f] | 266 | ); |
---|
[cb0f2b2] | 267 | |
---|
| 268 | if (!preorder) |
---|
[6d5623e] | 269 | abac_list_add(stack, vertex); |
---|
[cb0f2b2] | 270 | } |
---|
| 271 | |
---|
[06293d1] | 272 | static abac_list_t *_order(abac_graph_t *graph, abac_role_t *start, int preorder) { |
---|
[dcc1a8e] | 273 | debug_printf("%sorder at %s\n", preorder ? "pre" : "post", abac_role_string(start)); |
---|
[ebde9dd] | 274 | |
---|
[06293d1] | 275 | abac_vertex_t *start_vertex = _get_vertex(graph, start); |
---|
[2fd24c7] | 276 | abac_set_t *seen = abac_set_new(); |
---|
[cb0f2b2] | 277 | |
---|
[379a53f] | 278 | // create the return list |
---|
[6d5623e] | 279 | abac_list_t *stack = abac_list_new(); |
---|
[cb0f2b2] | 280 | |
---|
| 281 | _order_recurse(start_vertex, seen, preorder, stack); |
---|
| 282 | |
---|
[2fd24c7] | 283 | abac_set_free(seen); |
---|
[be963dc] | 284 | |
---|
[cb0f2b2] | 285 | return stack; |
---|
| 286 | } |
---|
| 287 | |
---|
[06293d1] | 288 | abac_list_t *abac_graph_postorder(abac_graph_t *graph, abac_role_t *start) { |
---|
[82aeefa] | 289 | return _order(graph, start, 0); |
---|
[cb0f2b2] | 290 | } |
---|
[97a5e13] | 291 | |
---|
[4e426c9] | 292 | /** |
---|
| 293 | * Postorder traverse the graph and return all the credentials within. |
---|
| 294 | */ |
---|
| 295 | abac_list_t *abac_graph_postorder_credentials(abac_graph_t *graph, char *start) { |
---|
| 296 | abac_vertex_t *vertex; |
---|
| 297 | abac_edge_t *incoming; |
---|
| 298 | |
---|
| 299 | // get the postorder of vertices |
---|
| 300 | abac_role_t *role = abac_role_from_string(start); |
---|
| 301 | abac_list_t *order = abac_graph_postorder(graph, role); |
---|
| 302 | |
---|
| 303 | // go through the list and dup all the credentials |
---|
| 304 | abac_list_t *credentials = abac_list_new(); |
---|
| 305 | abac_list_foreach(order, vertex, |
---|
| 306 | abac_list_foreach(vertex->edges, incoming, |
---|
| 307 | if (incoming->credential != NULL) |
---|
| 308 | abac_list_add(credentials, abac_credential_dup(incoming->credential)); |
---|
| 309 | ); |
---|
| 310 | ); |
---|
| 311 | |
---|
| 312 | abac_role_free(role); |
---|
| 313 | abac_list_free(order); |
---|
| 314 | |
---|
| 315 | return credentials; |
---|
| 316 | } |
---|
| 317 | |
---|
[401a054] | 318 | static void _query(abac_graph_t *graph, char *role_name, char *principal, abac_graph_t *return_graph) { |
---|
[06293d1] | 319 | abac_vertex_t *vertex; |
---|
| 320 | abac_edge_t *incoming; |
---|
[97a5e13] | 321 | |
---|
[401a054] | 322 | abac_role_t *role = abac_role_from_string(role_name); |
---|
[dcc1a8e] | 323 | abac_role_t *prin_role = abac_role_from_string(principal); |
---|
[97a5e13] | 324 | |
---|
[2fd24c7] | 325 | abac_set_t *on_path = abac_set_new(); |
---|
[dcc1a8e] | 326 | abac_set_add(on_path, abac_role_string(prin_role)); |
---|
[97a5e13] | 327 | |
---|
[401a054] | 328 | abac_list_t *traversal = abac_graph_postorder(graph, role); |
---|
[6d5623e] | 329 | abac_list_foreach(traversal, vertex, |
---|
[1743825] | 330 | abac_role_t *role = vertex->role; |
---|
[97a5e13] | 331 | |
---|
[6d5623e] | 332 | abac_list_foreach(vertex->edges, incoming, |
---|
[1743825] | 333 | abac_role_t *incoming_role = incoming->vertex->role; |
---|
[97a5e13] | 334 | |
---|
[dcc1a8e] | 335 | if (!abac_set_contains(on_path, abac_role_string(incoming_role))) |
---|
[ae72c5d] | 336 | continue; |
---|
[97a5e13] | 337 | |
---|
[dcc1a8e] | 338 | abac_set_add(on_path, abac_role_string(role)); |
---|
[97a5e13] | 339 | |
---|
| 340 | // add non-derived edges to the proof graph |
---|
[dcc1a8e] | 341 | if (!abac_role_is_linking(role)) |
---|
[401a054] | 342 | abac_graph_add_credential(return_graph, incoming->credential); |
---|
[97a5e13] | 343 | |
---|
| 344 | // recursively find linked roles |
---|
| 345 | else { |
---|
[401a054] | 346 | char *linked_role = abac_role_linked_role(role); |
---|
[dcc1a8e] | 347 | char *principal = abac_role_principal(incoming_role); |
---|
[97a5e13] | 348 | |
---|
[401a054] | 349 | _query(graph, linked_role, principal, return_graph); |
---|
[97a5e13] | 350 | } |
---|
| 351 | ); |
---|
| 352 | ); |
---|
| 353 | |
---|
[6d5623e] | 354 | abac_list_free(traversal); |
---|
[2fd24c7] | 355 | abac_set_free(on_path); |
---|
[401a054] | 356 | abac_role_free(role); |
---|
[dcc1a8e] | 357 | abac_role_free(prin_role); |
---|
[97a5e13] | 358 | } |
---|
| 359 | |
---|
[401a054] | 360 | abac_graph_t *abac_graph_query(abac_graph_t *graph, char *role, char *principal) { |
---|
[06293d1] | 361 | abac_graph_derive_links(graph); |
---|
[97a5e13] | 362 | |
---|
[06293d1] | 363 | abac_graph_t *return_graph = abac_graph_new(); |
---|
[401a054] | 364 | _query(graph, role, principal, return_graph); |
---|
[06293d1] | 365 | abac_graph_derive_links(return_graph); |
---|
[97a5e13] | 366 | return return_graph; |
---|
| 367 | } |
---|
[be963dc] | 368 | |
---|
[902d079] | 369 | /** |
---|
[401a054] | 370 | * Get all the credentials (attribute/issuer cert pairs) from the graph. |
---|
[902d079] | 371 | */ |
---|
[401a054] | 372 | abac_list_t *abac_graph_credentials(abac_graph_t *graph) { |
---|
| 373 | abac_list_t *credentials = abac_list_new(); |
---|
[902d079] | 374 | |
---|
[06293d1] | 375 | abac_vertex_t *vertex; |
---|
[902d079] | 376 | |
---|
| 377 | for (vertex = graph->vertices; vertex != NULL; vertex = vertex->hh.next) { |
---|
[06293d1] | 378 | abac_edge_t *edge; |
---|
[6d5623e] | 379 | abac_list_foreach(vertex->edges, edge, |
---|
[401a054] | 380 | if (edge->credential != NULL) |
---|
| 381 | abac_list_add(credentials, abac_credential_dup(edge->credential)); |
---|
[902d079] | 382 | ); |
---|
| 383 | } |
---|
| 384 | |
---|
[401a054] | 385 | return credentials; |
---|
[902d079] | 386 | } |
---|
| 387 | |
---|
[06293d1] | 388 | void abac_graph_free(abac_graph_t *graph) { |
---|
| 389 | abac_vertex_t *vertex; |
---|
| 390 | abac_edge_t *edge; |
---|
[be963dc] | 391 | |
---|
| 392 | // kill derived edges |
---|
| 393 | _clear_derived(graph); |
---|
| 394 | |
---|
| 395 | // delete vertices |
---|
| 396 | while ((vertex = graph->vertices) != NULL) { |
---|
| 397 | HASH_DEL(graph->vertices, vertex); |
---|
| 398 | |
---|
[dcc1a8e] | 399 | abac_role_free(vertex->role); |
---|
[186cb75] | 400 | |
---|
[6d5623e] | 401 | abac_list_foreach(vertex->edges, edge, |
---|
[401a054] | 402 | if (edge->credential != NULL) |
---|
| 403 | abac_credential_free(edge->credential); |
---|
[186cb75] | 404 | free(edge); |
---|
| 405 | ); |
---|
[6d5623e] | 406 | abac_list_free(vertex->edges); |
---|
[be963dc] | 407 | free(vertex); |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | free(graph); |
---|
| 411 | } |
---|
[f46412c] | 412 | |
---|
[06293d1] | 413 | abac_role_t *abac_vertex_role(abac_vertex_t *vertex) { |
---|
[f46412c] | 414 | return vertex->role; |
---|
| 415 | } |
---|