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> |
---|
26 | #include <stdlib.h> |
---|
27 | |
---|
28 | #include <abac.h> |
---|
29 | |
---|
30 | extern char *abac_id_keyid(abac_id_t *id); |
---|
31 | |
---|
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 | |
---|
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 | |
---|
54 | abac_chunk_t a_chunk=abac_id_cert_chunk(id); |
---|
55 | rc=abac_context_load_id_chunk(ctx, a_chunk); |
---|
56 | check_return(rc, 0, "load_id_chunk"); |
---|
57 | abac_chunk_free(&a_chunk); |
---|
58 | |
---|
59 | abac_id_t *chocolate_id = abac_id_from_file(argv[4]); |
---|
60 | rc=abac_context_load_id_id(ctx, chocolate_id); |
---|
61 | check_return(rc, 0, "load_id_id"); |
---|
62 | |
---|
63 | abac_attribute_t *attr; |
---|
64 | rc=abac_attribute_create(&attr, id, "delicious", 0); |
---|
65 | check_return(rc, 0, "attribute_create"); |
---|
66 | abac_attribute_principal(attr, abac_id_keyid(chocolate_id)); |
---|
67 | rc=abac_attribute_bake(attr); |
---|
68 | check_return(rc, 1, "attribute_bake"); |
---|
69 | |
---|
70 | char *string=abac_attribute_role_string(attr); |
---|
71 | printf(" attribute being made : %s\n",string); |
---|
72 | free(string); |
---|
73 | |
---|
74 | abac_attribute_write_file(attr,argv[3]); |
---|
75 | abac_chunk_t c_chunk=abac_attribute_cert_chunk(attr); |
---|
76 | rc = abac_context_load_attribute_chunk(ctx,c_chunk); |
---|
77 | check_return(rc, 0, "load_attribute_chunk"); |
---|
78 | abac_chunk_free(&c_chunk); |
---|
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 | ); |
---|
88 | |
---|
89 | |
---|
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 | } |
---|
107 | |
---|
108 | abac_context_credentials_free(credentials); |
---|
109 | |
---|
110 | free(tmp); |
---|
111 | abac_id_free(id); |
---|
112 | abac_attribute_free(attr); |
---|
113 | abac_context_free(ctx); |
---|
114 | |
---|
115 | return 0; |
---|
116 | } |
---|