source: tests/example_scripts/c/abac_attr.c @ 80f0770

abac0-leak
Last change on this file since 80f0770 was 7764378, checked in by Mei <mei@…>, 11 years ago

1) tweak according valgrind's leak report

  • Property mode set to 100644
File size: 2.9 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    rc=abac_context_load_id_id(ctx, chocolate_id);
52
53    abac_attribute_t *attr;
54    rc=abac_attribute_create(&attr, id, "delicious", 1800);
55    abac_attribute_principal(attr, abac_id_keyid(chocolate_id));
56    rc=abac_attribute_bake(attr);
57
58    char *string=abac_attribute_role_string(attr);
59    printf(" attribute being made : %s\n",string);
60    free(string);
61
62    abac_attribute_write_file(attr,argv[3]);
63    abac_chunk_t c_chunk=abac_attribute_cert_chunk(attr);
64    abac_context_load_attribute_chunk(ctx,c_chunk);
65    abac_chunk_free(c_chunk);
66
67    char *tmp=NULL;
68    asprintf(&tmp,"%s.delicious",abac_id_keyid(id));
69
70    /* make a query */
71    credentials = abac_context_query(ctx,
72        tmp, abac_id_keyid(chocolate_id),
73        &success
74    );
75
76
77    fprintf(stderr,"query with %s\n",tmp);
78    fprintf(stderr,"      for %s\n",abac_id_keyid(chocolate_id));
79    int sz=abac_list_size(credentials);
80
81    if (success)
82        puts("attr success");
83    else
84        puts("fail, here's a partial proof");
85
86    if (credentials != NULL)
87        for (i = 0; credentials[i] != NULL; ++i) {
88            cred = credentials[i];
89            printf("credential %s <- %s\n",
90                    abac_role_string(abac_credential_head(cred)),
91                    abac_role_string(abac_credential_tail(cred))
92                  );
93        }
94
95    abac_context_credentials_free(credentials);
96
97    free(tmp);
98    abac_id_free(id);
99    abac_attribute_free(attr);
100    abac_context_free(ctx);
101
102    return 0;
103}
Note: See TracBrowser for help on using the repository browser.