source: tests/example_scripts/c/abac_attr.c @ f2622ee

abac0-leak
Last change on this file since f2622ee was f2622ee, checked in by Mei-Hui Su <mei@…>, 11 years ago

1) ran with valgrind and did some leak patching

  • 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
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#define _GNU_SOURCE
23#include <err.h>
24#include <stdio.h>
25#include <assert.h>
26#include <stdlib.h>
27
28#include <abac.h>
29
30extern char *abac_id_keyid(abac_id_t *id);
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_privkey_from_file(id,argv[2]);
45
46    abac_chunk_t a_chunk=abac_id_cert_chunk(id);
47    rc=abac_context_load_id_chunk(ctx, a_chunk);
48    abac_chunk_free(a_chunk);
49
50    abac_id_t *chocolate_id = abac_id_from_file(argv[4]);
51    abac_chunk_t b_chunk=abac_id_cert_chunk(chocolate_id);
52    rc=abac_context_load_id_chunk(ctx, b_chunk);
53    abac_chunk_free(b_chunk);
54
55    abac_attribute_t *attr;
56    rc=abac_attribute_create(&attr, id, "delicious", 1800);
57    abac_attribute_principal(attr, abac_id_keyid(chocolate_id));
58    rc=abac_attribute_bake(attr);
59
60    char *string=abac_attribute_role_string(attr);
61    printf(" attribute being made : %s\n",string);
62    free(string);
63
64    abac_attribute_write_file(attr,argv[3]);
65    abac_chunk_t c_chunk=abac_attribute_cert_chunk(attr);
66    abac_context_load_attribute_chunk(ctx,c_chunk);
67    abac_chunk_free(c_chunk);
68
69    char *tmp=NULL;
70    asprintf(&tmp,"%s.delicious",abac_id_keyid(id));
71
72    /* make a query */
73    credentials = abac_context_query(ctx,
74        tmp, abac_id_keyid(chocolate_id),
75        &success
76    );
77
78
79    fprintf(stderr,"query with %s\n",tmp);
80    fprintf(stderr,"      for %s\n",abac_id_keyid(chocolate_id));
81    int sz=abac_list_size(credentials);
82
83    if (success)
84        puts("attr success");
85    else
86        puts("fail, here's a partial proof");
87
88    if (credentials != NULL)
89        for (i = 0; credentials[i] != NULL; ++i) {
90            cred = credentials[i];
91            printf("credential %s <- %s\n",
92                    abac_role_string(abac_credential_head(cred)),
93                    abac_role_string(abac_credential_tail(cred))
94                  );
95        }
96
97    abac_context_credentials_free(credentials);
98
99    free(tmp);
100    abac_id_free(id);
101    abac_id_free(chocolate_id);
102    abac_attribute_free(attr);
103    abac_context_free(ctx);
104
105    return 0;
106}
Note: See TracBrowser for help on using the repository browser.