source: creddy/display.c @ bec30b5

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

1) change abac_context_load_directory to check on every regular files

and try to extract id id/privkey and then attribute in turn.

2) move id_certs to be context based instead of shared globally

  • Property mode set to 100644
File size: 3.8 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 *attr_list = abac_attribute_certs_from_file(NULL,opts->cert);
78    int sz=abac_list_size(attr_list);
79    abac_list_foreach(attr_list, attr,
80        _print_info(attr,show_issuer,show_subject,show_validity,show_roles, opts->cert);
81        abac_attribute_free(attr);
82    );
83    abac_list_free(attr_list);
84
85    // give up if neither works
86    if(sz==0)
87        errx(1, "Couldn't load %s as an ID or attribute, ", opts->cert);
88}
89
90void _print_info(abac_attribute_t *attr, int show_issuer, int show_subject, int show_validity, int show_roles, char *fname)
91{
92    if (attr != NULL) {
93        abac_id_t *issuer_id=abac_attribute_issuer_id(attr);
94        if (show_issuer && issuer_id) {
95            char *issuer = abac_id_issuer(issuer_id);
96            printf("Issuer: %s\n", issuer);
97            free(issuer);
98        }
99        if (show_subject && issuer_id) {
100            char *subject = abac_id_subject(issuer_id);
101            printf("Subject: %s\n", subject);
102            free(subject);
103        }
104        if (show_validity) {
105            struct tm not_before, not_after;
106            abac_attribute_validity(attr, &not_before, &not_after);
107            _print_validity(not_before, not_after);
108        }
109        if (show_roles) {
110            char *role_string = abac_attribute_role_string(attr);
111            if (role_string == NULL) errx(1, "Couldn't get attributes from %s", fname );
112            printf("Roles: %s\n", role_string);
113            free(role_string);
114        }
115    }
116}
117
118// display the validity period of a cert
119void _print_validity(struct tm not_before, struct tm not_after) {
120    char buf[256];
121    printf("Validity:\n");
122
123    strftime(buf, sizeof(buf), "%F %T %Z", &not_before);
124    printf("    Not before: %s [%lld]\n", buf, (long long) mktime(&not_before));
125
126    strftime(buf, sizeof(buf), "%F %T %Z", &not_after);
127    printf("    Not after:  %s [%lld]\n", buf, (long long) mktime(&not_after));
128}
Note: See TracBrowser for help on using the repository browser.