source: libabac/abac_graph.c @ 15200be

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

move libabac into its own directory

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