source: libabac/prover.c @ 8200a9c

Last change on this file since 8200a9c was 4571abc, checked in by Mei-Hui Su <mei@…>, 11 years ago

1) tweak for leak

  • Property mode set to 100644
File size: 2.3 KB
Line 
1#include <err.h>
2#include <stdio.h>
3#include <stdlib.h>
4
5#include <abac.h>
6#include "abac_list.h"
7#include "options.h"
8
9static void _dump_context(FILE *fp, abac_context_t *ctx)
10{
11    int i;
12    abac_credential_t **credentials = abac_context_credentials(ctx);
13    abac_credential_t *cred;
14    if (credentials != NULL)
15        for (i = 0; credentials[i] != NULL; ++i) {
16            cred = credentials[i];
17            fprintf(fp,"%s <- %s\n",
18                abac_role_string(abac_credential_head(cred)),
19                abac_role_string(abac_credential_tail(cred)));
20        }
21    abac_context_credentials_free(credentials);
22
23    abac_id_cert_t **ilist=abac_context_principals(ctx);
24    abac_id_cert_t *cert;
25    if (ilist != NULL)
26        for (i = 0; ilist[i] != NULL; ++i) {
27               cert = ilist[i];
28               fprintf(fp,"id[%d] %s (%s)\n",i, abac_id_cert_keyid(cert), abac_id_cert_cn(cert));
29        }
30    abac_context_id_credentials_free(ilist);
31}
32
33int main(int argc, char **argv) {
34    int i, success;
35    abac_credential_t *cred;
36
37    options_t opts = { 0, };
38    get_options(argc, argv, &opts);
39
40    abac_context_t *ctx = abac_context_new();
41    abac_context_load_directory(ctx, opts.keystore);
42
43    if(opts.rulefile) {
44        FILE *fp=fopen(opts.rulefile,"w+");
45        if(fp) { 
46            _dump_context(fp,ctx);       
47            fclose(fp);
48        }
49        if(opts.role == NULL) { /* just a pure dump call */
50            free_options(&opts);
51            abac_context_free(ctx);
52            return 0;
53        }
54    }
55
56    abac_credential_t **credentials = abac_context_query(ctx,
57        opts.role, opts.principal,
58        &success
59    );
60
61
62    if (success)
63        puts("success");
64    else
65        puts("fail, here's a partial proof");
66
67    if (credentials != NULL)
68        for (i = 0; credentials[i] != NULL; ++i) {
69            cred = credentials[i];
70            printf("credential %s <- %s\n",
71                    abac_role_string(abac_credential_head(cred)),
72                    abac_role_string(abac_credential_tail(cred))
73                  );
74        }
75
76    abac_context_credentials_free(credentials);
77    abac_context_free(ctx);
78    free_options(&opts);
79
80    if(success) {
81        fprintf(stderr,"returning success- 0\n");
82        return 0;
83    } else {
84        fprintf(stderr,"returning failure- 1\n");
85        return 1;
86    }
87}
Note: See TracBrowser for help on using the repository browser.