source: swig/c/abac_attr.c @ f89b991

mei_rt2
Last change on this file since f89b991 was 5730a10, checked in by Mei <mei@…>, 12 years ago

1) fix up the sample scripts under swig and added some docs for them

  • Property mode set to 100644
File size: 2.7 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
11   This program will generate an attribute rule, write it out to an external
12           file and also load it into the context (prolog db)
13           [keyid:IceCream].delicious <- [Keyid:IceCream]
14
15   Then, a query is made against the context to see if it is populated correctly.
16
17**/
18
19#include <err.h>
20#include <stdio.h>
21#include <assert.h>
22
23#include <abac.h>
24
25extern void abac_print_cred_info(abac_credential_t*, FILE*);
26extern char *abac_id_keyid(abac_id_t *id);
27extern abac_attribute_t *abac_attribute_add_tail(abac_attribute_t *ptr, abac_aspect_t *);
28
29int main(int argc, char **argv) {
30    int i, success=0;
31    abac_credential_t *cred=NULL;
32    abac_credential_t **credentials=NULL;
33
34    abac_context_t *ctx = abac_context_new();
35
36    if(argc != 4) return 1;
37
38    /* build up structure */
39    abac_id_t *id =NULL;
40    id = abac_id_from_file(argv[1]);
41    int rc=abac_id_load_privkey_file(id,argv[2]);
42
43    rc=abac_context_load_id_privkey_chunk(ctx, 
44                  abac_id_cert_chunk(id), 
45                  abac_id_privkey_chunk(id));
46
47    abac_aspect_t *head=abac_role_create(abac_id_keyid(id),"delicious");
48    abac_aspect_t *tail=abac_role_principal_create(abac_id_keyid(id));
49    abac_attribute_t *attr;
50    rc=abac_attribute_create(&attr, head, NULL, 1800);
51    abac_attribute_add_tail(attr, tail);
52    rc=abac_attribute_bake(attr);
53
54    printf(" attribute being made : %s\n",abac_attribute_string(attr));
55
56    abac_attribute_write_cert_fname(attr,argv[3]);
57    abac_context_load_attribute_chunk(ctx,abac_attribute_cert_chunk(attr));
58
59   
60    /* make a query */
61    abac_aspect_t *q=abac_role_create(abac_id_keyid(id),"delicious");
62    abac_aspect_t *w=abac_role_principal_create(abac_id_keyid(id));
63    char *with=abac_aspect_typed_string(w);
64    char *query=abac_aspect_typed_string(q);
65
66    if(query !=NULL && with!=NULL) {
67        credentials = abac_context_query(ctx,
68                            query, with,
69                            &success);
70        if (success)
71            puts("prover success!!");
72            else puts("prover failed!!");
73
74        if (credentials != NULL && success) {
75            puts("credentials needed :");
76            for (i = 0; credentials[i] != NULL; ++i) {
77               cred = credentials[i];
78               abac_print_cred_info(cred,NULL);
79            }
80        }
81        if(credentials)
82            abac_context_credentials_free(credentials);
83    }
84
85    abac_context_free(ctx);
86    return 0;
87}
Note: See TracBrowser for help on using the repository browser.