source: libabac/abac.c @ c0fe894

abac0-leak
Last change on this file since c0fe894 was 3613ab8, checked in by Ted Faber <faber@…>, 11 years ago

Back out the badly executed memory leak changes

  • 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            );
[3613ab8]134            abac_list_free(cred_list);
[461541a]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
[3613ab8]213    assert(ctx != NULL); assert(role != NULL); assert(principal != NULL); assert(success != NULL);
[90d20f0]214
[390f749]215    abac_graph_t *result_graph = abac_graph_query(ctx->graph, role, principal);
[401a054]216    abac_list_t *result = abac_graph_credentials(result_graph);
[90d20f0]217
[06293d1]218    abac_graph_free(result_graph);
[90d20f0]219
[6d5623e]220    int size = abac_list_size(result);
[4e426c9]221    if (size > 0)
222        *success = 1;
223
224    // if there is no actual path, return everything that can reach the role
225    else {
226        *success = 0;
227        abac_list_free(result);
[d4b3b52]228        result_graph = abac_graph_new();
229
230        // TODO: This can probably be better, but it now returns an
231        // approximation of a partial proof.  It returns all the attributes the
232        // principal can reach and all the attributes that will lead to a
233        // success.
234
235        /* Get all the attributes of the principal.  This calls sub-queries to
236         * flesh out the indirect proofs. */
237        result_graph = abac_graph_principal_creds(ctx->graph, principal);
238
239        /* This gets all the attributes linked to the target en route to the
240         * principal. */
241        result = abac_graph_postorder_credentials(ctx->graph, role);
242
243        /* Merge responses */
244        abac_list_foreach(result, cur,
245            abac_graph_add_credential(result_graph, cur);
246        );
247        abac_list_free(result);
248        abac_graph_derive_links(result_graph);
249
250        result = abac_graph_credentials(result_graph);
251        abac_graph_free(result_graph);
[4e426c9]252
253        size = abac_list_size(result);
254    }
255
[38782df]256    // make the array (leave space to NULL terminate it)
257    //      n.b., even if the list is empty, we still return an array that
258    //            only contains the NULL terminator
259    credentials = abac_xmalloc(sizeof(abac_credential_t *) * (size + 1));
260    abac_list_foreach(result, cur,
261        credentials[i++] = cur;
262    );
263    credentials[i] = NULL;
[dc62c68]264
[6d5623e]265    abac_list_free(result);
[dc62c68]266
[401a054]267    return credentials;
[90d20f0]268}
269
[bec30b5]270
[90d20f0]271/**
[3c4fd68]272 * A NULL-terminated array of all the credentials in the context.
[90d20f0]273 */
[3c4fd68]274abac_credential_t **abac_context_credentials(abac_context_t *ctx) {
275    abac_credential_t *cred;
276    int i = 0;
277
278    assert(ctx != NULL);
279
280    abac_list_t *cred_list = abac_graph_credentials(ctx->graph);
281    int size = abac_list_size(cred_list);
282
283    abac_credential_t **credentials = abac_xmalloc(sizeof(abac_credential_t *) * (size + 1));
284    abac_list_foreach(cred_list, cred,
285        credentials[i++] = cred;
286    );
287    credentials[i] = NULL;
288
289    abac_list_free(cred_list);
290
[bec30b5]291    if(debug) {
292        abac_id_cert_t **ilist=abac_context_principals(ctx);
293        abac_id_cert_t *cert;
294        if (ilist != NULL)
295            for (i = 0; ilist[i] != NULL; ++i) {
296                cert = ilist[i];
297                printf("id[%d] %s\n",i, abac_id_cert_keyid(cert));
298            }
299        abac_context_id_credentials_free(ilist);
300    }
301
[3c4fd68]302    return credentials;
303}
304
[94605f2]305/*
306 * Replace known keyids with their nicknames (mnemonic names).  If a non-NULL
307 * string is returned it needs to be freed by the caller.
308 */
309char *abac_context_expand_key(abac_context_t *ctxt, char *s ) {
310    if ( ctxt->keymap ) 
311        return abac_keyid_map_expand_key(ctxt->keymap, s);
312    else
313        return NULL;
314}
315
316/*
317 * Replace known nicknames(mnemonic names)  with their keyids.  If a non-NULL
318 * string is returned it needs to be freed by the caller.
319 */
320char *abac_context_expand_nickname(abac_context_t *ctxt, char *s ) {
321    if ( ctxt->keymap ) 
322        return abac_keyid_map_expand_nickname(ctxt->keymap, s);
323    else
324        return NULL;
325}
326
[d2b198c]327/*
328 * Add a nickname to the context.  The keyid must be known to the context.  If
329 * the nickname is in use, it is disambiguated.  Call abac_context_expand_key
330 * to see the assigned name if that is required.  Existing nickname for keyid
331 * is overwritten.  Returns true if the change was successful.
332 */
333int abac_context_set_nickname(abac_context_t *ctxt, char *key, char*nick) {
334    char *p = NULL;
335
336    if ( !ctxt->keymap) return 0;
337    /* Make sure we know the key.  Free the returned nickname */
338    if ( !(p = abac_keyid_map_key_to_nickname(ctxt->keymap, key))) return 0;
339    else free(p);
340
341    abac_keyid_map_remove_keyid(ctxt->keymap, key);
342    return abac_keyid_map_add_nickname(ctxt->keymap, key, nick);
343}
344
[afcafea]345/*
346 * Get direct access to the context's keyid mapping.  Used internally.  This
347 * does not make a reference to the map, use abac_keyid_map_dup if that is
348 * required.
349 */
350abac_keyid_map_t *abac_context_get_keyid_map(abac_context_t *ctxt) {
351    return ctxt->keymap;
352}
[d2b198c]353
354
[bec30b5]355/**
356 * A NULL-terminated array of all the principals in the context.
357 */
358abac_id_cert_t **abac_context_principals(abac_context_t *ctx)
359{
360    abac_id_cert_t **principals = NULL, *cur;
361    assert(ctx != NULL);
362
363    int size = abac_list_size(ctx->id_certs);
364
365    // make the array (leave space to NULL terminate it)
366    //      n.b., even if the list is empty, we still return an array that
367    //            only contains the NULL terminator
368    principals = abac_xmalloc(sizeof(abac_id_cert_t *) * (size + 1));
369    int i = 0;
370    abac_id_cert_t *id_cert;
371    abac_list_foreach(ctx->id_certs, id_cert,
372        principals[i]=abac_id_cert_dup(id_cert);
373        i++;
374    );
375    principals[i] = NULL;
376
377    return principals;
378}
379
380
[3c4fd68]381/**
382 * Frees a NULL-terminated list of credentials.
383 */
384void abac_context_credentials_free(abac_credential_t **credentials) {
[dc62c68]385    int i;
[90d20f0]386
[401a054]387    if (credentials == NULL)
[90d20f0]388        return;
389
[401a054]390    for (i = 0; credentials[i] != NULL; ++i)
391        abac_credential_free(credentials[i]);
392    free(credentials);
[90d20f0]393}
[bec30b5]394
395/**
396 * Frees a NULL-terminated list of id credentials
397 */
398void abac_context_id_credentials_free(abac_id_cert_t **id_credentials) {
399    int i;
400
401    if (id_credentials == NULL)
402        return;
403
404    for (i = 0; id_credentials[i] != NULL; ++i) {
405        abac_id_cert_free(id_credentials[i]);
406    }
407    free(id_credentials);
408}
409
410
Note: See TracBrowser for help on using the repository browser.