[4721618] | 1 | /** |
---|
| 2 | attr_abac.c |
---|
| 3 | |
---|
| 4 | To demonstrate how to use ABAC's api in C |
---|
| 5 | |
---|
| 6 | call: attr_abac IceCream_ID.pem IceCream_private.pem IceCream_attr.der |
---|
| 7 | |
---|
| 8 | pre-condition: generate IceCream_ID.pem and IceCream_private.pem with |
---|
| 9 | creddy --generate --cn IceCream |
---|
| 10 | generate Chocolate_ID.pem and Chocolate_private.pem with |
---|
| 11 | creddy --generate --cn IceCream |
---|
| 12 | |
---|
| 13 | This program will generate an attribute rule, write it out to an external |
---|
| 14 | file and also load it into the context (prolog db) |
---|
| 15 | [keyid:IceCream].delicious <- [Keyid:Chocolate] |
---|
| 16 | |
---|
| 17 | Then, a query is made against the context to see if it is populated correctly. |
---|
| 18 | |
---|
| 19 | ./abac_attr IceCream_ID.pem IceCream_private.pem IceCream_attr.der Chocolate_ID.pem |
---|
| 20 | **/ |
---|
| 21 | |
---|
| 22 | #define _GNU_SOURCE |
---|
| 23 | #include <err.h> |
---|
| 24 | #include <stdio.h> |
---|
| 25 | #include <assert.h> |
---|
[f2622ee] | 26 | #include <stdlib.h> |
---|
[4721618] | 27 | |
---|
| 28 | #include <abac.h> |
---|
| 29 | |
---|
| 30 | extern char *abac_id_keyid(abac_id_t *id); |
---|
| 31 | |
---|
[1e2a19c] | 32 | void check_return(int rc, int ok, char *msg) { |
---|
| 33 | if ( rc != ok) { |
---|
| 34 | fprintf(stderr, "%s failed: %d expected %d\n", |
---|
| 35 | (msg) ? msg : "No message", rc, ok); |
---|
| 36 | exit(20); |
---|
| 37 | } |
---|
| 38 | } |
---|
| 39 | |
---|
[4721618] | 40 | int main(int argc, char **argv) { |
---|
| 41 | int i, success=0; |
---|
| 42 | abac_credential_t *cred=NULL; |
---|
| 43 | abac_credential_t **credentials=NULL; |
---|
| 44 | |
---|
| 45 | abac_context_t *ctx = abac_context_new(); |
---|
| 46 | |
---|
| 47 | if(argc != 5) return 1; |
---|
| 48 | |
---|
| 49 | /* build up structure */ |
---|
| 50 | abac_id_t *id =NULL; |
---|
| 51 | id = abac_id_from_file(argv[1]); |
---|
| 52 | int rc=abac_id_privkey_from_file(id,argv[2]); |
---|
| 53 | |
---|
[f2622ee] | 54 | abac_chunk_t a_chunk=abac_id_cert_chunk(id); |
---|
| 55 | rc=abac_context_load_id_chunk(ctx, a_chunk); |
---|
[1e2a19c] | 56 | check_return(rc, 0, "load_id_chunk"); |
---|
[44c8505] | 57 | abac_chunk_free(&a_chunk); |
---|
[4721618] | 58 | |
---|
| 59 | abac_id_t *chocolate_id = abac_id_from_file(argv[4]); |
---|
[7764378] | 60 | rc=abac_context_load_id_id(ctx, chocolate_id); |
---|
[1e2a19c] | 61 | check_return(rc, 0, "load_id_id"); |
---|
[4721618] | 62 | |
---|
| 63 | abac_attribute_t *attr; |
---|
[1e2a19c] | 64 | rc=abac_attribute_create(&attr, id, "delicious", 0); |
---|
| 65 | check_return(rc, 0, "attribute_create"); |
---|
[4721618] | 66 | abac_attribute_principal(attr, abac_id_keyid(chocolate_id)); |
---|
| 67 | rc=abac_attribute_bake(attr); |
---|
[1e2a19c] | 68 | check_return(rc, 1, "attribute_bake"); |
---|
[4721618] | 69 | |
---|
[f2622ee] | 70 | char *string=abac_attribute_role_string(attr); |
---|
| 71 | printf(" attribute being made : %s\n",string); |
---|
| 72 | free(string); |
---|
[4721618] | 73 | |
---|
| 74 | abac_attribute_write_file(attr,argv[3]); |
---|
[f2622ee] | 75 | abac_chunk_t c_chunk=abac_attribute_cert_chunk(attr); |
---|
[1e2a19c] | 76 | rc = abac_context_load_attribute_chunk(ctx,c_chunk); |
---|
| 77 | check_return(rc, 0, "load_attribute_chunk"); |
---|
[44c8505] | 78 | abac_chunk_free(&c_chunk); |
---|
[4721618] | 79 | |
---|
| 80 | char *tmp=NULL; |
---|
| 81 | asprintf(&tmp,"%s.delicious",abac_id_keyid(id)); |
---|
| 82 | |
---|
| 83 | /* make a query */ |
---|
| 84 | credentials = abac_context_query(ctx, |
---|
| 85 | tmp, abac_id_keyid(chocolate_id), |
---|
| 86 | &success |
---|
| 87 | ); |
---|
[f2622ee] | 88 | |
---|
| 89 | |
---|
[4721618] | 90 | fprintf(stderr,"query with %s\n",tmp); |
---|
| 91 | fprintf(stderr," for %s\n",abac_id_keyid(chocolate_id)); |
---|
| 92 | int sz=abac_list_size(credentials); |
---|
| 93 | |
---|
| 94 | if (success) |
---|
| 95 | puts("attr success"); |
---|
| 96 | else |
---|
| 97 | puts("fail, here's a partial proof"); |
---|
| 98 | |
---|
| 99 | if (credentials != NULL) |
---|
| 100 | for (i = 0; credentials[i] != NULL; ++i) { |
---|
| 101 | cred = credentials[i]; |
---|
| 102 | printf("credential %s <- %s\n", |
---|
| 103 | abac_role_string(abac_credential_head(cred)), |
---|
| 104 | abac_role_string(abac_credential_tail(cred)) |
---|
| 105 | ); |
---|
| 106 | } |
---|
[f2622ee] | 107 | |
---|
[4721618] | 108 | abac_context_credentials_free(credentials); |
---|
[f2622ee] | 109 | |
---|
| 110 | free(tmp); |
---|
| 111 | abac_id_free(id); |
---|
| 112 | abac_attribute_free(attr); |
---|
[4721618] | 113 | abac_context_free(ctx); |
---|
[f2622ee] | 114 | |
---|
[4721618] | 115 | return 0; |
---|
| 116 | } |
---|