source: examples/example_scripts/c/abac_attr.c @ bc0ce98

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

1) added namespace
2) tweak ?This,
3) allowing linking role/oset as constraining conditions
4) adding access_tests regression testing that uses GENI's access policy
5) added couple multi contexts regression tests
6) add compression/uncompression calls to abac_encode_string/abac_decode_string
(libstrongwan only allows 512 char for attribute rule storage)
7) add attribute_now option to creddy that takes a whole char string for attribute
rule

  • Property mode set to 100644
File size: 3.2 KB
Line 
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
28extern void abac_print_cred_info(abac_credential_t*, FILE*);
29extern char *abac_id_keyid(abac_id_t *id);
30extern abac_attribute_t *abac_attribute_add_tail(abac_attribute_t *ptr, abac_aspect_t *);
31
32int main(int argc, char **argv) {
33    int i, success=0;
34    abac_credential_t *cred=NULL;
35    abac_credential_t **credentials=NULL;
36
37    abac_context_t *ctx = abac_context_new();
38
39    if(argc != 5) return 1;
40
41    /* build up structure */
42    abac_id_t *id =NULL;
43    id = abac_id_from_file(argv[1]);
44    int rc=abac_id_load_privkey_file(id,argv[2]);
45
46    rc=abac_context_load_id_privkey_chunk(ctx, 
47                  abac_id_cert_chunk(id), 
48                  abac_id_privkey_chunk(id));
49
50    abac_id_t *chocolate_id = abac_id_from_file(argv[4]);
51/* XXX    rc=abac_context_load_id_id(ctx, chocolate_id); */ 
52    rc=abac_context_load_id_privkey_chunk(ctx, 
53                  abac_id_cert_chunk(chocolate_id), 
54                  abac_id_privkey_chunk(chocolate_id));
55
56    abac_aspect_t *head=abac_role_create(abac_id_keyid(id),"delicious");
57    abac_aspect_t *tail=abac_role_principal_create(abac_id_keyid(chocolate_id));
58    abac_attribute_t *attr;
59    rc=abac_attribute_create(&attr, head, NULL, 1800);
60    abac_attribute_add_tail(attr, tail);
61    rc=abac_attribute_bake(attr);
62
63    printf(" attribute being made : %s\n",abac_attribute_string(attr));
64
65    abac_attribute_write_cert_fname(attr,argv[3]);
66    abac_context_load_attribute_chunk(ctx,abac_attribute_cert_chunk(attr));
67
68    /* make a query */
69    abac_aspect_t *q=abac_role_create(abac_id_keyid(id),"delicious");
70    abac_aspect_t *w=abac_role_principal_create(abac_id_keyid(chocolate_id));
71    char *with=abac_aspect_typed_string(w);
72    char *query=abac_aspect_typed_string(q);
73
74printf("query %s\n", query);
75printf("with %s\n", with);
76
77    show_yap_db("dump prolog");
78
79    if(query !=NULL && with!=NULL) {
80        credentials = abac_context_query(ctx,
81                            query, with,
82                            &success);
83        if (success)
84            puts("prover success!!");
85            else puts("prover failed!!");
86
87        if (credentials != NULL && success) {
88            puts("credentials needed :");
89            for (i = 0; credentials[i] != NULL; ++i) {
90               cred = credentials[i];
91               abac_print_cred_info(cred,NULL);
92            }
93        }
94        if(credentials)
95            abac_free_credentials(credentials);
96    }
97
98    abac_context_free(ctx);
99    return 0;
100}
Note: See TracBrowser for help on using the repository browser.