source: libabac/abac_graph.c @ 9a411d7

abac0-leakabac0-meicompt_changesgec13mei-idmei-rt0-nmei_rt0mei_rt2mei_rt2_fix_1meiyap-rt1meiyap1rt2tvf-new-xml
Last change on this file since 9a411d7 was 9a411d7, checked in by Mike Ryan <mikeryan@…>, 14 years ago

fold intersection into ordinary role object

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