source: examples/example_scripts/c/abac_attr_partial.c @ accd63d

mei_rt2
Last change on this file since accd63d was d0efdec, checked in by Mei <mei@…>, 11 years ago

1) less bsd compiler complaints

  • Property mode set to 100644
File size: 3.0 KB
Line 
1/**
2   attr_abac.c
3
4   To demonstrate how to use ABAC's api in C with partial proof result
5
6   call:   attr_abac_partial 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 invalid query is made against the context to see if partial proof is
18working in c
19
20**/
21
22#include <err.h>
23#include <stdio.h>
24#include <assert.h>
25
26#include <abac.h>
27
28extern char *abac_aspect_typed_string(abac_aspect_t *ptr); 
29extern void abac_print_cred_info(abac_credential_t*, FILE*);
30extern char *abac_id_keyid(abac_id_t *id);
31extern abac_attribute_t *abac_attribute_add_tail(abac_attribute_t *ptr, abac_aspect_t *);
32
33int main(int argc, char **argv) {
34    int i, success=0;
35    abac_credential_t *cred=NULL;
36    abac_credential_t **credentials=NULL;
37
38    abac_context_t *ctx = abac_context_new();
39
40    if(argc != 5) return 1;
41
42    /* build up structure */
43    abac_id_t *id =NULL;
44    id = abac_id_from_file(argv[1]);
45    int rc=abac_id_load_privkey_file(id,argv[2]);
46
47    rc=abac_context_load_id_privkey_chunk(ctx, 
48                  abac_id_cert_chunk(id), 
49                  abac_id_privkey_chunk(id));
50
51    abac_id_t *chocolate_id = abac_id_from_file(argv[4]);
52    rc=abac_context_load_id_id(ctx, chocolate_id); 
53
54    abac_aspect_t *head=abac_role_create(abac_id_keyid(id),"delicious");
55    abac_aspect_t *tail=abac_role_principal_create(abac_id_keyid(chocolate_id));
56    abac_attribute_t *attr;
57    rc=abac_attribute_create(&attr, head, NULL, 1800);
58    abac_attribute_add_tail(attr, tail);
59    rc=abac_attribute_bake(attr);
60
61    printf(" attribute being made : %s\n",abac_attribute_string(attr));
62
63    abac_attribute_write_cert_fname(attr,argv[3]);
64    abac_context_load_attribute_chunk(ctx,abac_attribute_cert_chunk(attr));
65
66   
67    /* make a query */
68    abac_aspect_t *q=abac_role_create(abac_id_keyid(id),"yummy");
69    abac_aspect_t *w=abac_role_principal_create(abac_id_keyid(chocolate_id));
70    char *with=abac_aspect_typed_string(w);
71    char *query=abac_aspect_typed_string(q);
72
73printf("query %s\n", query);
74printf("with %s\n", with);
75
76    if(query !=NULL && with!=NULL) {
77        credentials = abac_context_query(ctx,
78                            query, with,
79                            &success);
80        if (success)
81            puts("prover success!!");
82            else puts("prover failed!!");
83
84        if (credentials != NULL && success) {
85            puts("credentials needed :");
86            for (i = 0; credentials[i] != NULL; ++i) {
87               cred = credentials[i];
88               abac_print_cred_info(cred,NULL);
89            }
90        }
91        if(credentials)
92            abac_free_credentials(credentials);
93    }
94
95    abac_context_free(ctx);
96    return 0;
97}
Note: See TracBrowser for help on using the repository browser.