source: libabac/abac.c @ 92d6cca

abac0-leakabac0-mei
Last change on this file since 92d6cca was 4f79997, checked in by Mei <mei@…>, 11 years ago

1) add a new scaling test -haystack/ralphs
2) tweak some libabac code here and there

  • Property mode set to 100644
File size: 11.1 KB
RevLine 
[461541a]1/* abac.c */
2
[90d20f0]3#include <assert.h>
[0bf0e67]4#include <err.h>
[bec30b5]5#include <sys/types.h>
6#include <sys/stat.h>
7#include <unistd.h>
8#include <dirent.h>
[90d20f0]9
[9efbfbf]10#include "abac.h"
[4721618]11#include "abac_list.h"
[06293d1]12#include "abac_graph.h"
[3c251d0]13#include "abac_util.h"
[43e3b71]14#include "abac_verifier.h"
[90d20f0]15
[bec30b5]16int debug=0;
17abac_id_cert_t **abac_context_principals(abac_context_t *ctx);
18void abac_context_id_credentials_free(abac_id_cert_t **id_credentials);
19
[390f749]20struct _abac_context_t {
[bec30b5]21/* list of principal id credentials, abac_id_cert_t */
22    abac_list_t *id_certs;
[06293d1]23    abac_graph_t *graph;
[94605f2]24    abac_keyid_map_t *keymap;
[90d20f0]25};
26
27/**
28 * Init the library.
29 */
30void libabac_init(void) {
[55c272b]31    void libabac_deinit(void);
32    static int has_been_init = 0;
33
34    // called every time a context is created, so only do it once
35    if (!has_been_init) {
36        abac_verifier_init();
37        atexit(libabac_deinit);
38        has_been_init = 1;
39    }
[90d20f0]40}
41
42/**
43 * Deinit the library.
44 */
45void libabac_deinit(void) {
[43e3b71]46    abac_verifier_deinit();
[90d20f0]47}
48
49/**
50 * Create a new abac context.
51 */
[390f749]52abac_context_t *abac_context_new(void) {
[55c272b]53    libabac_init();
54
[390f749]55    abac_context_t *ctx = abac_xmalloc(sizeof(abac_context_t));
56    ctx->graph = abac_graph_new();
[bec30b5]57    ctx->id_certs=abac_list_new();
[94605f2]58    ctx->keymap = abac_keyid_map_new();
[390f749]59    return ctx;
[90d20f0]60}
61
62/**
63 * Deep copy an abac context.
64 */
[390f749]65abac_context_t *abac_context_dup(abac_context_t *ctx) {
66    assert(ctx != NULL);
[bec30b5]67   
[390f749]68    abac_context_t *dup = abac_xmalloc(sizeof(abac_context_t));
69    dup->graph = abac_graph_dup(ctx->graph);
[bec30b5]70    dup->id_certs=abac_list_new();
71
72    abac_id_cert_t *id_cert;
73    abac_list_foreach(ctx->id_certs, id_cert,
74        abac_list_add(dup->id_certs, abac_id_cert_dup(id_cert));
75    );
[90d20f0]76
[d2b198c]77    dup->keymap = abac_keyid_map_clone(ctx->keymap);
[90d20f0]78    return dup;
79}
80
81/**
82 * Free an abac context.
83 */
[390f749]84void abac_context_free(abac_context_t *ctx) {
85    assert(ctx != NULL);
[90d20f0]86
[390f749]87    abac_graph_free(ctx->graph);
[bec30b5]88
89    abac_id_cert_t *id_cert;
90    abac_list_foreach(ctx->id_certs, id_cert,
91        abac_id_cert_free(id_cert);
92    );
93    abac_list_free(ctx->id_certs);
[94605f2]94    abac_keyid_map_free(ctx->keymap);
[390f749]95    free(ctx);
[90d20f0]96}
97
98/**
99 * Load an ID cert from a file.
100 */
[390f749]101int abac_context_load_id_file(abac_context_t *ctx, char *filename) {
102    assert(ctx != NULL); assert(filename != NULL);
[94605f2]103    return abac_verifier_load_id_file(ctx->id_certs,filename, ctx->keymap);
[90d20f0]104}
105
106/**
107 * Load an ID cert from a chunk.
108 */
[461541a]109int abac_context_load_id_chunk(abac_context_t *ctx, abac_chunk_t cert_chunk) {
[390f749]110    assert(ctx != NULL);
[94605f2]111    return abac_verifier_load_id_chunk(ctx->id_certs,cert_chunk, ctx->keymap);
[90d20f0]112}
113
114/**
115 * Load an attribute cert from a file.
116 */
[390f749]117int abac_context_load_attribute_file(abac_context_t *ctx, char *filename) {
[0779c99]118    int ret, add_ret;
[461541a]119    abac_list_t *cred_list=abac_list_new(); // could be more than 1
[0779c99]120    abac_credential_t *cred;
[6dd2d1a]121
[390f749]122    assert(ctx != NULL); assert(filename != NULL);
[90d20f0]123
[d2b198c]124    ret = abac_verifier_load_attribute_cert_file(ctx->id_certs, filename, cred_list, ctx->keymap);
[461541a]125
[0779c99]126    if (ret == ABAC_CERT_SUCCESS) {
[461541a]127        int size = abac_list_size(cred_list);
128        if(size) {
129            abac_list_foreach(cred_list, cred,
130                add_ret = abac_graph_add_credential(ctx->graph, cred);
131                assert(add_ret != ABAC_GRAPH_CRED_INVALID);
132                abac_credential_free(cred);
133            );
134            abac_list_free(cred_list);
135        }
[6dd2d1a]136    }
137    return ret;
[90d20f0]138}
139
140/**
141 * Load an attribute cert from a chunk.
142 */
[461541a]143int abac_context_load_attribute_chunk(abac_context_t *ctx, abac_chunk_t cert_chunk) {
[0779c99]144    int ret, add_ret;
[4721618]145    abac_list_t  *cred_list=abac_list_new(); // could be more than 1
[0779c99]146    abac_credential_t *cred;
147
[390f749]148    assert(ctx != NULL);
[90d20f0]149
[d2b198c]150    ret = abac_verifier_load_attribute_cert_chunk(ctx->id_certs, cert_chunk, cred_list, ctx->keymap);
[0779c99]151    if (ret == ABAC_CERT_SUCCESS) {
[461541a]152        int size = abac_list_size(cred_list);
153        if(size) {
154            abac_list_foreach(cred_list, cred,
155                add_ret = abac_graph_add_credential(ctx->graph, cred);
156                assert(add_ret != ABAC_GRAPH_CRED_INVALID);
157                abac_credential_free(cred);
158            );
159            abac_list_free(cred_list);
160        }
[0779c99]161    }
162
163    return ret;
[90d20f0]164}
165
[50b9dc9]166#define ID_PAT "/*_ID.{der,pem}"
[461541a]167#define ATTR_PAT "/*_attr.xml"
[03b3293]168
[bec30b5]169static int is_regular_file(char *filename)
170{
171   struct stat sb;
172   if(stat(filename,&sb) == -1)
173       return 0;
174   if((sb.st_mode & S_IFMT) == S_IFREG)
175       return 1;
176   return 0;
177}
178
[03b3293]179/**
180 * Load a directory full of certs.
181 */
[390f749]182void abac_context_load_directory(abac_context_t *ctx, char *path) {
[bec30b5]183    DIR *dp;
184    struct dirent *ep;
185     
186    dp = opendir (path);
187    if (dp != NULL) {
188        while (ep = readdir (dp)) {
189            if(is_regular_file(ep->d_name)) {
190                int ret = abac_context_load_id_file(ctx, ep->d_name);
191                if (ret == ABAC_CERT_SUCCESS) {
192                    if(debug) fprintf(stderr,"abac_context_load_directory, found an id %s\n",ep->d_name);
193                    continue;
194                }
195                ret = abac_context_load_attribute_file(ctx, ep->d_name);
196                if (ret == ABAC_CERT_SUCCESS) {
197                    if(debug) fprintf(stderr,"abac_context_load_directory, found an attr %s\n",ep->d_name);
198                }
199            }
200        }
201        (void) closedir (dp);
202    } else fprintf(stderr, "abac_load_directory, Couldn't open the directory\n");
[03b3293]203}
204
[90d20f0]205/**
[dc62c68]206 * Run a query on the data in an abac context. Returns a NULL-terminated array
[38782df]207 * of abac_credential_t. Success/failure in *success.
[90d20f0]208 */
[4e426c9]209abac_credential_t **abac_context_query(abac_context_t *ctx, char *role, char *principal, int *success) {
[401a054]210    abac_credential_t **credentials = NULL, *cur;
[dc62c68]211    int i = 0;
212
[4f79997]213    assert(ctx != NULL);
214    assert(role != NULL);
215    assert(principal != NULL);
216    assert(success != NULL);
[90d20f0]217
[390f749]218    abac_graph_t *result_graph = abac_graph_query(ctx->graph, role, principal);
[401a054]219    abac_list_t *result = abac_graph_credentials(result_graph);
[90d20f0]220
[06293d1]221    abac_graph_free(result_graph);
[90d20f0]222
[6d5623e]223    int size = abac_list_size(result);
[4e426c9]224    if (size > 0)
225        *success = 1;
226
227    // if there is no actual path, return everything that can reach the role
228    else {
229        *success = 0;
230        abac_list_free(result);
[d4b3b52]231        result_graph = abac_graph_new();
232
233        // TODO: This can probably be better, but it now returns an
234        // approximation of a partial proof.  It returns all the attributes the
235        // principal can reach and all the attributes that will lead to a
236        // success.
237
238        /* Get all the attributes of the principal.  This calls sub-queries to
239         * flesh out the indirect proofs. */
240        result_graph = abac_graph_principal_creds(ctx->graph, principal);
241
242        /* This gets all the attributes linked to the target en route to the
243         * principal. */
244        result = abac_graph_postorder_credentials(ctx->graph, role);
245
246        /* Merge responses */
247        abac_list_foreach(result, cur,
248            abac_graph_add_credential(result_graph, cur);
249        );
250        abac_list_free(result);
251        abac_graph_derive_links(result_graph);
252
253        result = abac_graph_credentials(result_graph);
254        abac_graph_free(result_graph);
[4e426c9]255
256        size = abac_list_size(result);
257    }
258
[38782df]259    // make the array (leave space to NULL terminate it)
260    //      n.b., even if the list is empty, we still return an array that
261    //            only contains the NULL terminator
262    credentials = abac_xmalloc(sizeof(abac_credential_t *) * (size + 1));
263    abac_list_foreach(result, cur,
264        credentials[i++] = cur;
265    );
266    credentials[i] = NULL;
[dc62c68]267
[6d5623e]268    abac_list_free(result);
[dc62c68]269
[401a054]270    return credentials;
[90d20f0]271}
272
[bec30b5]273
[90d20f0]274/**
[3c4fd68]275 * A NULL-terminated array of all the credentials in the context.
[90d20f0]276 */
[3c4fd68]277abac_credential_t **abac_context_credentials(abac_context_t *ctx) {
278    abac_credential_t *cred;
279    int i = 0;
280
281    assert(ctx != NULL);
282
283    abac_list_t *cred_list = abac_graph_credentials(ctx->graph);
284    int size = abac_list_size(cred_list);
285
286    abac_credential_t **credentials = abac_xmalloc(sizeof(abac_credential_t *) * (size + 1));
287    abac_list_foreach(cred_list, cred,
288        credentials[i++] = cred;
289    );
290    credentials[i] = NULL;
291
292    abac_list_free(cred_list);
293
[bec30b5]294    if(debug) {
295        abac_id_cert_t **ilist=abac_context_principals(ctx);
296        abac_id_cert_t *cert;
297        if (ilist != NULL)
298            for (i = 0; ilist[i] != NULL; ++i) {
299                cert = ilist[i];
300                printf("id[%d] %s\n",i, abac_id_cert_keyid(cert));
301            }
302        abac_context_id_credentials_free(ilist);
303    }
304
[3c4fd68]305    return credentials;
306}
307
[94605f2]308/*
309 * Replace known keyids with their nicknames (mnemonic names).  If a non-NULL
310 * string is returned it needs to be freed by the caller.
311 */
312char *abac_context_expand_key(abac_context_t *ctxt, char *s ) {
313    if ( ctxt->keymap ) 
314        return abac_keyid_map_expand_key(ctxt->keymap, s);
315    else
316        return NULL;
317}
318
319/*
320 * Replace known nicknames(mnemonic names)  with their keyids.  If a non-NULL
321 * string is returned it needs to be freed by the caller.
322 */
323char *abac_context_expand_nickname(abac_context_t *ctxt, char *s ) {
324    if ( ctxt->keymap ) 
325        return abac_keyid_map_expand_nickname(ctxt->keymap, s);
326    else
327        return NULL;
328}
329
[d2b198c]330/*
331 * Add a nickname to the context.  The keyid must be known to the context.  If
332 * the nickname is in use, it is disambiguated.  Call abac_context_expand_key
333 * to see the assigned name if that is required.  Existing nickname for keyid
334 * is overwritten.  Returns true if the change was successful.
335 */
336int abac_context_set_nickname(abac_context_t *ctxt, char *key, char*nick) {
337    char *p = NULL;
338
339    if ( !ctxt->keymap) return 0;
340    /* Make sure we know the key.  Free the returned nickname */
341    if ( !(p = abac_keyid_map_key_to_nickname(ctxt->keymap, key))) return 0;
342    else free(p);
343
344    abac_keyid_map_remove_keyid(ctxt->keymap, key);
345    return abac_keyid_map_add_nickname(ctxt->keymap, key, nick);
346}
347
[afcafea]348/*
349 * Get direct access to the context's keyid mapping.  Used internally.  This
350 * does not make a reference to the map, use abac_keyid_map_dup if that is
351 * required.
352 */
353abac_keyid_map_t *abac_context_get_keyid_map(abac_context_t *ctxt) {
354    return ctxt->keymap;
355}
[d2b198c]356
357
[bec30b5]358/**
359 * A NULL-terminated array of all the principals in the context.
360 */
361abac_id_cert_t **abac_context_principals(abac_context_t *ctx)
362{
363    abac_id_cert_t **principals = NULL, *cur;
364    assert(ctx != NULL);
365
366    int size = abac_list_size(ctx->id_certs);
367
368    // make the array (leave space to NULL terminate it)
369    //      n.b., even if the list is empty, we still return an array that
370    //            only contains the NULL terminator
371    principals = abac_xmalloc(sizeof(abac_id_cert_t *) * (size + 1));
372    int i = 0;
373    abac_id_cert_t *id_cert;
374    abac_list_foreach(ctx->id_certs, id_cert,
375        principals[i]=abac_id_cert_dup(id_cert);
376        i++;
377    );
378    principals[i] = NULL;
379
380    return principals;
381}
382
383
[3c4fd68]384/**
385 * Frees a NULL-terminated list of credentials.
386 */
387void abac_context_credentials_free(abac_credential_t **credentials) {
[dc62c68]388    int i;
[90d20f0]389
[401a054]390    if (credentials == NULL)
[90d20f0]391        return;
392
[401a054]393    for (i = 0; credentials[i] != NULL; ++i)
394        abac_credential_free(credentials[i]);
395    free(credentials);
[90d20f0]396}
[bec30b5]397
398/**
399 * Frees a NULL-terminated list of id credentials
400 */
401void abac_context_id_credentials_free(abac_id_cert_t **id_credentials) {
402    int i;
403
404    if (id_credentials == NULL)
405        return;
406
407    for (i = 0; id_credentials[i] != NULL; ++i) {
408        abac_id_cert_free(id_credentials[i]);
409    }
410    free(id_credentials);
411}
412
413
Note: See TracBrowser for help on using the repository browser.