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 | #include <err.h> |
---|
23 | #include <stdio.h> |
---|
24 | #include <assert.h> |
---|
25 | |
---|
26 | #include <abac.h> |
---|
27 | |
---|
28 | extern void abac_print_cred_info(abac_credential_t*, FILE*); |
---|
29 | extern char *abac_id_keyid(abac_id_t *id); |
---|
30 | extern abac_attribute_t *abac_attribute_add_tail(abac_attribute_t *ptr, abac_aspect_t *); |
---|
31 | |
---|
32 | int main(int argc, char **argv) { |
---|
33 | int i, success=0; |
---|
34 | abac_credential_t *cred=NULL; |
---|
35 | abac_credential_t **credentials=NULL; |
---|
36 | int rc; |
---|
37 | |
---|
38 | abac_context_t *ctx = abac_context_new(); |
---|
39 | |
---|
40 | /* build up structure */ |
---|
41 | abac_id_t *superK =NULL; |
---|
42 | abac_id_t *jack =NULL; |
---|
43 | superK = abac_id_from_file("SuperK_ID.pem"); |
---|
44 | rc=abac_id_load_privkey_file(superK,"SuperK_private.pem"); |
---|
45 | |
---|
46 | jack = abac_id_from_file("Jack_ID.pem"); |
---|
47 | rc=abac_id_load_privkey_file(jack,"Jack_private.pem"); |
---|
48 | |
---|
49 | abac_context_load_id(ctx,superK); |
---|
50 | abac_context_load_id(ctx,jack); |
---|
51 | |
---|
52 | abac_aspect_t *head=abac_role_create(abac_id_keyid(superK),"employee"); |
---|
53 | abac_aspect_t *tail=abac_role_principal_create(abac_id_keyid(jack)); |
---|
54 | abac_attribute_t *attr; |
---|
55 | rc=abac_attribute_create(&attr, head, NULL, 1800); |
---|
56 | abac_attribute_add_tail(attr, tail); |
---|
57 | rc=abac_attribute_bake(attr); |
---|
58 | |
---|
59 | abac_chunk_t chunk=abac_attribute_cert_chunk(attr); |
---|
60 | abac_attribute_t *nattr=abac_attribute_from_chunk(chunk); |
---|
61 | abac_context_load_attribute(ctx,nattr); |
---|
62 | /* abac_context_load_attribute(ctx,attr); */ |
---|
63 | |
---|
64 | show_yap_db("dump prolog"); |
---|
65 | |
---|
66 | abac_id_free(jack); |
---|
67 | abac_id_free(superK); |
---|
68 | abac_attribute_free(attr); |
---|
69 | abac_context_free(ctx); |
---|
70 | |
---|
71 | return 0; |
---|
72 | } |
---|