[2e9455f] | 1 | /** |
---|
| 2 | abac_prover.c |
---|
| 3 | |
---|
| 4 | To demonstrate how to use ABAC's api in C to load credentials |
---|
| 5 | |
---|
| 6 | call: abac_load |
---|
| 7 | |
---|
| 8 | **/ |
---|
| 9 | |
---|
| 10 | #include <err.h> |
---|
| 11 | #include <stdio.h> |
---|
| 12 | #include <assert.h> |
---|
| 13 | #include <string.h> |
---|
| 14 | |
---|
| 15 | #include <abac.h> |
---|
| 16 | #include "abac_c.h" |
---|
| 17 | |
---|
| 18 | extern void abac_print_cred_info(abac_credential_t*, FILE*); |
---|
| 19 | extern void abac_print_prin_info(abac_id_credential_t*, FILE*); |
---|
| 20 | |
---|
| 21 | int main(int argc, char **argv) { |
---|
| 22 | int i, success=0; |
---|
| 23 | abac_credential_t *cred=NULL; |
---|
| 24 | abac_credential_t **credentials=NULL; |
---|
| 25 | |
---|
| 26 | abac_context_t *ctx = abac_context_new(); |
---|
| 27 | int rc; |
---|
| 28 | |
---|
| 29 | fprintf(stderr, "\n --- load from directory\n"); |
---|
| 30 | abac_context_load_directory(ctx, argv[1]); |
---|
| 31 | |
---|
| 32 | fprintf(stderr, "\n --- load from chunk\n"); |
---|
| 33 | abac_id_t *tid; |
---|
| 34 | abac_id_generate(&tid,"Tim", 0); |
---|
| 35 | abac_chunk_t chunk=abac_id_cert_chunk(tid); |
---|
| 36 | tid=abac_id_from_chunk(chunk); |
---|
| 37 | rc=abac_context_load_id(ctx,tid); |
---|
| 38 | |
---|
| 39 | fprintf(stderr, "\n --- load from id_gen\n"); |
---|
| 40 | abac_id_t *id; |
---|
| 41 | abac_id_generate(&id,"Mary", 0); |
---|
| 42 | abac_id_write_cert_fname(id,"Mary_ID.pem"); |
---|
| 43 | abac_id_write_privkey_fname(id,"Mary_private.pem"); |
---|
| 44 | fprintf(stderr, "\n --- load from explicit call\n"); |
---|
| 45 | /* int rc=abac_context_load_id_id_key_files(ctx,"Mary_ID.pem","Mary_private.pem"); */ |
---|
| 46 | rc=abac_context_load_id(ctx,id); |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | fprintf(stderr, "\n --- load with idkey file\n"); |
---|
| 50 | abac_id_t *id2; |
---|
| 51 | abac_id_generate(&id2,"Tom", 0); |
---|
| 52 | abac_id_write_cert_fname(id2,"Tom_IDKEY.pem"); |
---|
| 53 | abac_id_write_privkey_fname(id2,"Tom_IDKEY.pem"); |
---|
| 54 | rc=abac_context_load_id_file(ctx,"Tom_IDKEY.pem"); |
---|
| 55 | |
---|
| 56 | { |
---|
| 57 | printf("\n\n"); |
---|
| 58 | credentials = abac_context_credentials(ctx); |
---|
| 59 | if (credentials != NULL) { |
---|
| 60 | puts("context credentials :"); |
---|
| 61 | for (i = 0; credentials[i] != NULL; ++i) { |
---|
| 62 | cred = credentials[i]; |
---|
| 63 | abac_print_typed_cred_info(cred,NULL); |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | if(credentials) |
---|
| 67 | abac_free_credentials(credentials); |
---|
| 68 | } |
---|
| 69 | { |
---|
| 70 | abac_id_credential_t *id_cred=NULL; |
---|
| 71 | abac_id_credential_t **id_credentials=NULL; |
---|
| 72 | printf("\n\n"); |
---|
| 73 | id_credentials = abac_context_principals(ctx); |
---|
| 74 | if (id_credentials != NULL) { |
---|
| 75 | puts("principal credentials :"); |
---|
| 76 | for (i = 0; id_credentials[i] != NULL; ++i) { |
---|
| 77 | id_cred = id_credentials[i]; |
---|
| 78 | abac_print_prin_info(id_cred,NULL); |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | if(id_credentials) |
---|
| 82 | abac_free_principals(id_credentials); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | show_yap_db("yap db"); |
---|
| 86 | |
---|
| 87 | fprintf(stderr,"\n --- id free from explicit call\n"); |
---|
| 88 | abac_id_free(id); |
---|
| 89 | abac_id_free(id2); |
---|
| 90 | |
---|
| 91 | fprintf(stderr,"\n --- explicitly free the context \n"); |
---|
| 92 | abac_context_free(ctx); |
---|
| 93 | |
---|
| 94 | fprintf(stderr,"\n --- that is it \n"); |
---|
| 95 | return 0; |
---|
| 96 | } |
---|