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
Line 
1/* abac.c */
2
3#include <assert.h>
4#include <err.h>
5#include <sys/types.h>
6#include <sys/stat.h>
7#include <unistd.h>
8#include <dirent.h>
9
10#include "abac.h"
11#include "abac_list.h"
12#include "abac_graph.h"
13#include "abac_util.h"
14#include "abac_verifier.h"
15
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
20struct _abac_context_t {
21/* list of principal id credentials, abac_id_cert_t */
22    abac_list_t *id_certs;
23    abac_graph_t *graph;
24    abac_keyid_map_t *keymap;
25};
26
27/**
28 * Init the library.
29 */
30void libabac_init(void) {
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    }
40}
41
42/**
43 * Deinit the library.
44 */
45void libabac_deinit(void) {
46    abac_verifier_deinit();
47}
48
49/**
50 * Create a new abac context.
51 */
52abac_context_t *abac_context_new(void) {
53    libabac_init();
54
55    abac_context_t *ctx = abac_xmalloc(sizeof(abac_context_t));
56    ctx->graph = abac_graph_new();
57    ctx->id_certs=abac_list_new();
58    ctx->keymap = abac_keyid_map_new();
59    return ctx;
60}
61
62/**
63 * Deep copy an abac context.
64 */
65abac_context_t *abac_context_dup(abac_context_t *ctx) {
66    assert(ctx != NULL);
67   
68    abac_context_t *dup = abac_xmalloc(sizeof(abac_context_t));
69    dup->graph = abac_graph_dup(ctx->graph);
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    );
76
77    dup->keymap = abac_keyid_map_clone(ctx->keymap);
78    return dup;
79}
80
81/**
82 * Free an abac context.
83 */
84void abac_context_free(abac_context_t *ctx) {
85    assert(ctx != NULL);
86
87    abac_graph_free(ctx->graph);
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);
94    abac_keyid_map_free(ctx->keymap);
95    free(ctx);
96}
97
98/**
99 * Load an ID cert from a file.
100 */
101int abac_context_load_id_file(abac_context_t *ctx, char *filename) {
102    assert(ctx != NULL); assert(filename != NULL);
103    return abac_verifier_load_id_file(ctx->id_certs,filename, ctx->keymap);
104}
105
106/**
107 * Load an ID cert from a chunk.
108 */
109int abac_context_load_id_chunk(abac_context_t *ctx, abac_chunk_t cert_chunk) {
110    assert(ctx != NULL);
111    return abac_verifier_load_id_chunk(ctx->id_certs,cert_chunk, ctx->keymap);
112}
113
114/**
115 * Load an attribute cert from a file.
116 */
117int abac_context_load_attribute_file(abac_context_t *ctx, char *filename) {
118    int ret, add_ret;
119    abac_list_t *cred_list=abac_list_new(); // could be more than 1
120    abac_credential_t *cred;
121
122    assert(ctx != NULL); assert(filename != NULL);
123
124    ret = abac_verifier_load_attribute_cert_file(ctx->id_certs, filename, cred_list, ctx->keymap);
125
126    if (ret == ABAC_CERT_SUCCESS) {
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        }
136    }
137    return ret;
138}
139
140/**
141 * Load an attribute cert from a chunk.
142 */
143int abac_context_load_attribute_chunk(abac_context_t *ctx, abac_chunk_t cert_chunk) {
144    int ret, add_ret;
145    abac_list_t  *cred_list=abac_list_new(); // could be more than 1
146    abac_credential_t *cred;
147
148    assert(ctx != NULL);
149
150    ret = abac_verifier_load_attribute_cert_chunk(ctx->id_certs, cert_chunk, cred_list, ctx->keymap);
151    if (ret == ABAC_CERT_SUCCESS) {
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        }
161    }
162
163    return ret;
164}
165
166#define ID_PAT "/*_ID.{der,pem}"
167#define ATTR_PAT "/*_attr.xml"
168
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
179/**
180 * Load a directory full of certs.
181 */
182void abac_context_load_directory(abac_context_t *ctx, char *path) {
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");
203}
204
205/**
206 * Run a query on the data in an abac context. Returns a NULL-terminated array
207 * of abac_credential_t. Success/failure in *success.
208 */
209abac_credential_t **abac_context_query(abac_context_t *ctx, char *role, char *principal, int *success) {
210    abac_credential_t **credentials = NULL, *cur;
211    int i = 0;
212
213    assert(ctx != NULL); assert(role != NULL); assert(principal != NULL); assert(success != NULL);
214
215    abac_graph_t *result_graph = abac_graph_query(ctx->graph, role, principal);
216    abac_list_t *result = abac_graph_credentials(result_graph);
217
218    abac_graph_free(result_graph);
219
220    int size = abac_list_size(result);
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);
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);
252
253        size = abac_list_size(result);
254    }
255
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;
264
265    abac_list_free(result);
266
267    return credentials;
268}
269
270
271/**
272 * A NULL-terminated array of all the credentials in the context.
273 */
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
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
302    return credentials;
303}
304
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
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
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}
353
354
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
381/**
382 * Frees a NULL-terminated list of credentials.
383 */
384void abac_context_credentials_free(abac_credential_t **credentials) {
385    int i;
386
387    if (credentials == NULL)
388        return;
389
390    for (i = 0; credentials[i] != NULL; ++i)
391        abac_credential_free(credentials[i]);
392    free(credentials);
393}
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.