source: creddy/display.c @ 6d3fc40

abac0-leakabac0-meitvf-new-xml
Last change on this file since 6d3fc40 was 6d3fc40, checked in by Mei <mei@…>, 11 years ago

1) fixed doc on linked role
2) add a dummy list to creddy's display so attribute's issuer can hang

somewhere.

  • Property mode set to 100644
File size: 4.0 KB
Line 
1
2/* display.c */
3
4#include <time.h>
5#include <string.h>
6#include <err.h>
7
8#include "libabac_common.h"
9#include "creddy_common.h"
10
11// helper
12void _print_validity(struct tm not_before, struct tm not_after);
13void _print_info(abac_attribute_t *attr, int show_issuer, int show_subject, int show_validity, int show_roles, char*);
14
15void display_main(options_t *opts) {
16    if (opts->cert == NULL)
17        usage(opts);
18
19    char *show = opts->show;
20    if (show == NULL)
21        usage(opts);
22
23    int show_issuer = 0;
24    int show_subject = 0;
25    int show_validity = 0;
26    int show_roles = 0;
27    char *opt;
28
29    while ((opt = strsep(&show, ",")) != NULL) {
30        if (strcmp(opt, "issuer") == 0)
31            show_issuer = 1;
32        else if (strcmp(opt, "subject") == 0)
33            show_subject = 1;
34        else if (strcmp(opt, "validity") == 0)
35            show_validity = 1;
36        else if (strcmp(opt, "roles") == 0)
37            show_roles = 1;
38        else if (strcmp(opt, "all") == 0) {
39            show_issuer = 1;
40            show_subject = 1;
41            show_validity = 1;
42            show_roles = 1;
43        }
44        else {
45            printf("Error: Unknown option to --show: %s\n", opt);
46            usage(opts);
47        }
48    }
49
50    // first try ID cert
51    abac_id_t *id = abac_id_from_file(opts->cert);
52    if (id != NULL) {
53        if (show_issuer) {
54            char *issuer = abac_id_issuer(id);
55            printf("Issuer: %s\n", issuer);
56            free(issuer);
57        }
58
59        if (show_subject) {
60            char *subject = abac_id_subject(id);
61            printf("Subject: %s\n", subject);
62            free(subject);
63        }
64
65        if (show_validity) {
66            struct tm not_before, not_after;
67            abac_id_validity(id, &not_before, &not_after);
68            _print_validity(not_before, not_after);
69        }
70
71        abac_id_free(id);
72        return;
73    }
74
75    // then try attribute cert
76    abac_attribute_t *attr;
77    abac_list_t *dummy_cert_list=abac_list_new();
78    abac_list_t *attr_list = abac_attribute_certs_from_file(dummy_cert_list,opts->cert);
79    int sz=abac_list_size(attr_list);
80    abac_list_foreach(attr_list, attr,
81        _print_info(attr,show_issuer,show_subject,show_validity,show_roles, opts->cert);
82        abac_attribute_free(attr);
83    );
84    abac_list_free(attr_list);
85    abac_id_cert_t *cert;
86    abac_list_foreach(dummy_cert_list, cert,
87        abac_id_cert_free(cert);
88    );
89    abac_list_free(dummy_cert_list);
90
91    // give up if neither works
92    if(sz==0)
93        errx(1, "Couldn't load %s as an ID or attribute", opts->cert);
94}
95
96void _print_info(abac_attribute_t *attr, int show_issuer, int show_subject, int show_validity, int show_roles, char *fname)
97{
98    if (attr != NULL) {
99        abac_id_t *issuer_id=abac_attribute_issuer_id(attr);
100        if (show_issuer && issuer_id) {
101            char *issuer = abac_id_issuer(issuer_id);
102            printf("Issuer: %s\n", issuer);
103            free(issuer);
104        }
105        if (show_subject && issuer_id) {
106            char *subject = abac_id_subject(issuer_id);
107            printf("Subject: %s\n", subject);
108            free(subject);
109        }
110        if (show_validity) {
111            struct tm not_before, not_after;
112            abac_attribute_validity(attr, &not_before, &not_after);
113            _print_validity(not_before, not_after);
114        }
115        if (show_roles) {
116            char *role_string = abac_attribute_role_string(attr);
117            if (role_string == NULL) errx(1, "Couldn't get attributes from %s", fname );
118            printf("Roles: %s\n", role_string);
119            free(role_string);
120        }
121    }
122}
123
124// display the validity period of a cert
125void _print_validity(struct tm not_before, struct tm not_after) {
126    char buf[256];
127    printf("Validity:\n");
128
129    strftime(buf, sizeof(buf), "%F %T %Z", &not_before);
130    printf("    Not before: %s [%lld]\n", buf, (long long) mktime(&not_before));
131
132    strftime(buf, sizeof(buf), "%F %T %Z", &not_after);
133    printf("    Not after:  %s [%lld]\n", buf, (long long) mktime(&not_after));
134}
Note: See TracBrowser for help on using the repository browser.