[4721618] | 1 | /** |
---|
| 2 | abac_prover.c |
---|
| 3 | |
---|
| 4 | To demonstrate how to use ABAC's api in C to make a query |
---|
| 5 | |
---|
| 6 | call: abac_prover "keystorestring" "rolestring" "principalstring" |
---|
| 7 | |
---|
| 8 | pre-condition: run make attr_abac generate IceCream_ID.pem and IceCream_private.pem with |
---|
| 9 | |
---|
| 10 | This program will make a prover call using |
---|
| 11 | rolestring <- principalstring |
---|
| 12 | |
---|
| 13 | **/ |
---|
| 14 | |
---|
| 15 | #include <err.h> |
---|
| 16 | #include <stdio.h> |
---|
| 17 | #include <assert.h> |
---|
| 18 | #include <string.h> |
---|
[7764378] | 19 | #include <stdlib.h> |
---|
[4721618] | 20 | |
---|
| 21 | #include <abac.h> |
---|
| 22 | |
---|
| 23 | int main(int argc, char **argv) { |
---|
| 24 | int i, success=0; |
---|
| 25 | abac_credential_t *cred=NULL; |
---|
| 26 | abac_credential_t **credentials=NULL; |
---|
| 27 | |
---|
| 28 | abac_context_t *ctx = abac_context_new(); |
---|
| 29 | abac_context_load_directory(ctx, argv[1]); |
---|
| 30 | |
---|
| 31 | char *query=strdup(argv[2]); |
---|
| 32 | char *with=strdup(argv[3]); |
---|
| 33 | |
---|
| 34 | printf("query %s \n", query); |
---|
| 35 | printf("with %s\n", with); |
---|
| 36 | |
---|
[7764378] | 37 | int k=10; /* use to do repetitions */ |
---|
[4721618] | 38 | while(k) { |
---|
| 39 | credentials = abac_context_query(ctx, |
---|
| 40 | query, with, |
---|
| 41 | &success); |
---|
| 42 | if (success) |
---|
| 43 | puts("prover success!!"); |
---|
| 44 | else puts("prover failed!!"); |
---|
| 45 | |
---|
| 46 | if (credentials != NULL && success) { |
---|
| 47 | puts("credentials needed :"); |
---|
| 48 | for (i = 0; credentials[i] != NULL; ++i) { |
---|
| 49 | cred = credentials[i]; |
---|
| 50 | printf("credential %s <- %s\n", |
---|
| 51 | abac_role_string(abac_credential_head(cred)), |
---|
| 52 | abac_role_string(abac_credential_tail(cred))); |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | if(credentials) |
---|
| 56 | abac_context_credentials_free(credentials); |
---|
| 57 | k=k-1; |
---|
| 58 | } |
---|
[7764378] | 59 | free(query); |
---|
| 60 | free(with); |
---|
[4721618] | 61 | |
---|
| 62 | { /* dump credentials from context */ |
---|
| 63 | printf("\n\n"); |
---|
| 64 | credentials = abac_context_credentials(ctx); |
---|
| 65 | printf("Dump context credentials, original ctx \n"); |
---|
| 66 | if (credentials != NULL) { |
---|
| 67 | puts("context credentials :"); |
---|
| 68 | for (i = 0; credentials[i] != NULL; ++i) { |
---|
| 69 | cred = credentials[i]; |
---|
| 70 | printf("credential %s <- %s\n", |
---|
| 71 | abac_role_string(abac_credential_head(cred)), |
---|
| 72 | abac_role_string(abac_credential_tail(cred))); |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | if(credentials) |
---|
| 76 | abac_context_credentials_free(credentials); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | abac_context_t *ctx2 = abac_context_dup(ctx); |
---|
| 80 | { /* dump credentials from context */ |
---|
| 81 | printf("\n\n"); |
---|
| 82 | credentials = abac_context_credentials(ctx); |
---|
| 83 | printf("Dump context credentials, original ctx2 \n"); |
---|
| 84 | if (credentials != NULL) { |
---|
| 85 | puts("context credentials :"); |
---|
| 86 | for (i = 0; credentials[i] != NULL; ++i) { |
---|
| 87 | cred = credentials[i]; |
---|
| 88 | printf("credential %s <- %s\n", |
---|
| 89 | abac_role_string(abac_credential_head(cred)), |
---|
| 90 | abac_role_string(abac_credential_tail(cred))); |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | if(credentials) |
---|
| 94 | abac_context_credentials_free(credentials); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | abac_context_free(ctx); |
---|
| 98 | abac_context_free(ctx2); |
---|
| 99 | |
---|
| 100 | return 0; |
---|
| 101 | } |
---|