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

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

1) less bsd compiler complaints

  • Property mode set to 100644
File size: 3.4 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 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/* XXX    rc=abac_context_load_id_id(ctx, chocolate_id); */ 
53    rc=abac_context_load_id_privkey_chunk(ctx, 
54                  abac_id_cert_chunk(chocolate_id), 
55                  abac_id_privkey_chunk(chocolate_id));
56
57    abac_aspect_t *head=abac_role_create(abac_id_keyid(id),"delicious");
58    abac_aspect_t *tail=abac_role_principal_create(abac_id_keyid(chocolate_id));
59    abac_attribute_t *attr;
60    rc=abac_attribute_create(&attr, head, NULL, 1800);
61    abac_attribute_add_tail(attr, tail);
62    rc=abac_attribute_bake(attr);
63
64    printf(" attribute being made : %s\n",abac_attribute_string(attr));
65
66    abac_attribute_write_cert_fname(attr,argv[3]);
67    abac_context_load_attribute_chunk(ctx,abac_attribute_cert_chunk(attr));
68
69    /* make a query */
70    abac_aspect_t *q=abac_role_create(abac_id_keyid(id),"delicious");
71    abac_aspect_t *w=abac_role_principal_create(abac_id_keyid(chocolate_id));
72    char *with=abac_aspect_typed_string(w);
73    fprintf(stderr,"with is(%s) at(%ld)\n", with, (long)w);
74    char *query=abac_aspect_typed_string(q);
75    fprintf(stderr,"query is(%s) at(%ld)\n", with, (long)w);
76
77printf("query %s\n", query);
78printf("with %s\n", with);
79
80    show_yap_db("dump prolog");
81
82    if(query !=NULL && with!=NULL) {
83        credentials = abac_context_query(ctx,
84                            query, with,
85                            &success);
86        if (success)
87            puts("prover success!!");
88            else puts("prover failed!!");
89
90        if (credentials != NULL && success) {
91            puts("credentials needed :");
92            for (i = 0; credentials[i] != NULL; ++i) {
93               cred = credentials[i];
94               abac_print_cred_info(cred,NULL);
95            }
96        }
97        if(credentials)
98            abac_free_credentials(credentials);
99    }
100
101    abac_context_free(ctx);
102    return 0;
103}
Note: See TracBrowser for help on using the repository browser.