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