update to target nrf52840
main.cpp@0:ecd06432fb4b, 2020-08-13 (annotated)
- Committer:
- geonnave
- Date:
- Thu Aug 13 12:23:41 2020 +0000
- Revision:
- 0:ecd06432fb4b
First test for pulga
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
geonnave | 0:ecd06432fb4b | 1 | #include <stdio.h> |
geonnave | 0:ecd06432fb4b | 2 | #include <stdlib.h> |
geonnave | 0:ecd06432fb4b | 3 | #include <string.h> |
geonnave | 0:ecd06432fb4b | 4 | |
geonnave | 0:ecd06432fb4b | 5 | #ifdef MBED_MAJOR_VERSION |
geonnave | 0:ecd06432fb4b | 6 | #include "mbed.h" |
geonnave | 0:ecd06432fb4b | 7 | Serial pc(P0_25, P0_8); |
geonnave | 0:ecd06432fb4b | 8 | #endif |
geonnave | 0:ecd06432fb4b | 9 | |
geonnave | 0:ecd06432fb4b | 10 | // definitions |
geonnave | 0:ecd06432fb4b | 11 | #include "abac_them.h" |
geonnave | 0:ecd06432fb4b | 12 | |
geonnave | 0:ecd06432fb4b | 13 | // policy constructors |
geonnave | 0:ecd06432fb4b | 14 | |
geonnave | 0:ecd06432fb4b | 15 | attr_v2 new_attr_integer(char *name, int value) |
geonnave | 0:ecd06432fb4b | 16 | { |
geonnave | 0:ecd06432fb4b | 17 | attr_v2 at; |
geonnave | 0:ecd06432fb4b | 18 | at.data_type = abac_integer; |
geonnave | 0:ecd06432fb4b | 19 | at.name = name; |
geonnave | 0:ecd06432fb4b | 20 | at.integer = value; |
geonnave | 0:ecd06432fb4b | 21 | return at; |
geonnave | 0:ecd06432fb4b | 22 | } |
geonnave | 0:ecd06432fb4b | 23 | |
geonnave | 0:ecd06432fb4b | 24 | attr_v2 new_attr_real(char *name, float value) |
geonnave | 0:ecd06432fb4b | 25 | { |
geonnave | 0:ecd06432fb4b | 26 | attr_v2 at; |
geonnave | 0:ecd06432fb4b | 27 | at.data_type = abac_real; |
geonnave | 0:ecd06432fb4b | 28 | at.name = name; |
geonnave | 0:ecd06432fb4b | 29 | at.real = value; |
geonnave | 0:ecd06432fb4b | 30 | return at; |
geonnave | 0:ecd06432fb4b | 31 | } |
geonnave | 0:ecd06432fb4b | 32 | |
geonnave | 0:ecd06432fb4b | 33 | attr_v2 new_attr_integer_range(char *name, int min, int max) |
geonnave | 0:ecd06432fb4b | 34 | { |
geonnave | 0:ecd06432fb4b | 35 | attr_v2 at; |
geonnave | 0:ecd06432fb4b | 36 | at.data_type = abac_integer_range; |
geonnave | 0:ecd06432fb4b | 37 | at.name = name; |
geonnave | 0:ecd06432fb4b | 38 | at.ran.integer_min = min; |
geonnave | 0:ecd06432fb4b | 39 | at.ran.integer_max = max; |
geonnave | 0:ecd06432fb4b | 40 | return at; |
geonnave | 0:ecd06432fb4b | 41 | } |
geonnave | 0:ecd06432fb4b | 42 | |
geonnave | 0:ecd06432fb4b | 43 | attr_v2 new_attr_real_range(char *name, float min, float max) |
geonnave | 0:ecd06432fb4b | 44 | { |
geonnave | 0:ecd06432fb4b | 45 | attr_v2 at; |
geonnave | 0:ecd06432fb4b | 46 | at.data_type = abac_real_range; |
geonnave | 0:ecd06432fb4b | 47 | at.name = name; |
geonnave | 0:ecd06432fb4b | 48 | at.ran.real_min = min; |
geonnave | 0:ecd06432fb4b | 49 | at.ran.real_max = max; |
geonnave | 0:ecd06432fb4b | 50 | return at; |
geonnave | 0:ecd06432fb4b | 51 | } |
geonnave | 0:ecd06432fb4b | 52 | |
geonnave | 0:ecd06432fb4b | 53 | attr_v2 new_attr_string(char *name, char *value) |
geonnave | 0:ecd06432fb4b | 54 | { |
geonnave | 0:ecd06432fb4b | 55 | attr_v2 at; |
geonnave | 0:ecd06432fb4b | 56 | at.data_type = abac_string; |
geonnave | 0:ecd06432fb4b | 57 | at.name = name; |
geonnave | 0:ecd06432fb4b | 58 | at.string = value; |
geonnave | 0:ecd06432fb4b | 59 | return at; |
geonnave | 0:ecd06432fb4b | 60 | } |
geonnave | 0:ecd06432fb4b | 61 | |
geonnave | 0:ecd06432fb4b | 62 | attr_v2 new_attr_string_list(char *name, size_t len) |
geonnave | 0:ecd06432fb4b | 63 | { |
geonnave | 0:ecd06432fb4b | 64 | attr_v2 at; |
geonnave | 0:ecd06432fb4b | 65 | at.data_type = abac_string_list; |
geonnave | 0:ecd06432fb4b | 66 | at.name = name; |
geonnave | 0:ecd06432fb4b | 67 | at.inner_list_len = len; |
geonnave | 0:ecd06432fb4b | 68 | at.string_list = (char **) malloc(sizeof(char *) * len); |
geonnave | 0:ecd06432fb4b | 69 | return at; |
geonnave | 0:ecd06432fb4b | 70 | } |
geonnave | 0:ecd06432fb4b | 71 | |
geonnave | 0:ecd06432fb4b | 72 | attr_v2 new_attr_dictionary(char *name, attr_v2 **value, size_t len) |
geonnave | 0:ecd06432fb4b | 73 | { |
geonnave | 0:ecd06432fb4b | 74 | attr_v2 at; |
geonnave | 0:ecd06432fb4b | 75 | at.data_type = abac_dictionary; |
geonnave | 0:ecd06432fb4b | 76 | at.name = name; |
geonnave | 0:ecd06432fb4b | 77 | at.inner_list_len = len; |
geonnave | 0:ecd06432fb4b | 78 | at.inner_attrs = value; |
geonnave | 0:ecd06432fb4b | 79 | return at; |
geonnave | 0:ecd06432fb4b | 80 | } |
geonnave | 0:ecd06432fb4b | 81 | |
geonnave | 0:ecd06432fb4b | 82 | attr_v2 **new_attr_list(size_t len) |
geonnave | 0:ecd06432fb4b | 83 | { |
geonnave | 0:ecd06432fb4b | 84 | attr_v2 **list = (attr_v2**) malloc(sizeof(attr_v2 *) * len); |
geonnave | 0:ecd06432fb4b | 85 | return list; |
geonnave | 0:ecd06432fb4b | 86 | } |
geonnave | 0:ecd06432fb4b | 87 | |
geonnave | 0:ecd06432fb4b | 88 | char **new_operations_list(size_t len) |
geonnave | 0:ecd06432fb4b | 89 | { |
geonnave | 0:ecd06432fb4b | 90 | char **list = (char**) malloc(sizeof(char *) * len); |
geonnave | 0:ecd06432fb4b | 91 | return list; |
geonnave | 0:ecd06432fb4b | 92 | } |
geonnave | 0:ecd06432fb4b | 93 | |
geonnave | 0:ecd06432fb4b | 94 | // graph constructors |
geonnave | 0:ecd06432fb4b | 95 | |
geonnave | 0:ecd06432fb4b | 96 | node new_graph_node(char *value) |
geonnave | 0:ecd06432fb4b | 97 | { |
geonnave | 0:ecd06432fb4b | 98 | node n; |
geonnave | 0:ecd06432fb4b | 99 | n.value = value; |
geonnave | 0:ecd06432fb4b | 100 | n.next = NULL; |
geonnave | 0:ecd06432fb4b | 101 | return n; |
geonnave | 0:ecd06432fb4b | 102 | } |
geonnave | 0:ecd06432fb4b | 103 | |
geonnave | 0:ecd06432fb4b | 104 | void create_directed_edge(node *a, node *b) |
geonnave | 0:ecd06432fb4b | 105 | { |
geonnave | 0:ecd06432fb4b | 106 | a->next = b; |
geonnave | 0:ecd06432fb4b | 107 | } |
geonnave | 0:ecd06432fb4b | 108 | |
geonnave | 0:ecd06432fb4b | 109 | graph new_graph(size_t len) |
geonnave | 0:ecd06432fb4b | 110 | { |
geonnave | 0:ecd06432fb4b | 111 | graph g; |
geonnave | 0:ecd06432fb4b | 112 | g.len = len; |
geonnave | 0:ecd06432fb4b | 113 | g.list = (node **) malloc(sizeof(node *) * len); |
geonnave | 0:ecd06432fb4b | 114 | return g; |
geonnave | 0:ecd06432fb4b | 115 | } |
geonnave | 0:ecd06432fb4b | 116 | |
geonnave | 0:ecd06432fb4b | 117 | // graph algorithms |
geonnave | 0:ecd06432fb4b | 118 | |
geonnave | 0:ecd06432fb4b | 119 | int is_node_in(node k, node *list, size_t v_len) |
geonnave | 0:ecd06432fb4b | 120 | { |
geonnave | 0:ecd06432fb4b | 121 | for (int i = 0; i < v_len; ++i) |
geonnave | 0:ecd06432fb4b | 122 | if (strcmp(k.value, list[i].value) == 0) |
geonnave | 0:ecd06432fb4b | 123 | return 1; |
geonnave | 0:ecd06432fb4b | 124 | return 0; |
geonnave | 0:ecd06432fb4b | 125 | } |
geonnave | 0:ecd06432fb4b | 126 | |
geonnave | 0:ecd06432fb4b | 127 | node *find_ancestors_dfs(graph g, node n, size_t *v_len) |
geonnave | 0:ecd06432fb4b | 128 | { |
geonnave | 0:ecd06432fb4b | 129 | node *visited = (node *) malloc(sizeof(node) * g.len); // allocate max number of nodes for simplicity |
geonnave | 0:ecd06432fb4b | 130 | node **stack = (node **) malloc(sizeof(node *) * g.len); |
geonnave | 0:ecd06432fb4b | 131 | node k; |
geonnave | 0:ecd06432fb4b | 132 | size_t s_head = 0, the_len = 0; |
geonnave | 0:ecd06432fb4b | 133 | stack[s_head++] = &n; |
geonnave | 0:ecd06432fb4b | 134 | while (s_head > 0) { |
geonnave | 0:ecd06432fb4b | 135 | k = *stack[--s_head]; |
geonnave | 0:ecd06432fb4b | 136 | if (!is_node_in(k, visited, the_len)) { |
geonnave | 0:ecd06432fb4b | 137 | visited[the_len++] = k; |
geonnave | 0:ecd06432fb4b | 138 | // walk over k.next and add to stack |
geonnave | 0:ecd06432fb4b | 139 | while (k.next) { |
geonnave | 0:ecd06432fb4b | 140 | stack[s_head++] = k.next; |
geonnave | 0:ecd06432fb4b | 141 | k = *k.next; |
geonnave | 0:ecd06432fb4b | 142 | } |
geonnave | 0:ecd06432fb4b | 143 | } |
geonnave | 0:ecd06432fb4b | 144 | } |
geonnave | 0:ecd06432fb4b | 145 | |
geonnave | 0:ecd06432fb4b | 146 | *v_len = the_len; |
geonnave | 0:ecd06432fb4b | 147 | return visited; |
geonnave | 0:ecd06432fb4b | 148 | } |
geonnave | 0:ecd06432fb4b | 149 | |
geonnave | 0:ecd06432fb4b | 150 | node *find_in_graph(attr_v2 *at, graph g) |
geonnave | 0:ecd06432fb4b | 151 | { |
geonnave | 0:ecd06432fb4b | 152 | for (int j = 0; j < g.len; ++j) |
geonnave | 0:ecd06432fb4b | 153 | if (strcmp(at->string, g.list[j]->value) == 0) |
geonnave | 0:ecd06432fb4b | 154 | return g.list[j]; |
geonnave | 0:ecd06432fb4b | 155 | return NULL; |
geonnave | 0:ecd06432fb4b | 156 | } |
geonnave | 0:ecd06432fb4b | 157 | |
geonnave | 0:ecd06432fb4b | 158 | void expand_attr(attr_v2 **at_orig, graph g) |
geonnave | 0:ecd06432fb4b | 159 | { |
geonnave | 0:ecd06432fb4b | 160 | if ((*at_orig)->data_type != abac_string) |
geonnave | 0:ecd06432fb4b | 161 | return; |
geonnave | 0:ecd06432fb4b | 162 | size_t v_len = 0; |
geonnave | 0:ecd06432fb4b | 163 | node *n; |
geonnave | 0:ecd06432fb4b | 164 | n = find_in_graph(*at_orig, g); |
geonnave | 0:ecd06432fb4b | 165 | if (n == NULL) |
geonnave | 0:ecd06432fb4b | 166 | return; |
geonnave | 0:ecd06432fb4b | 167 | node *visited = find_ancestors_dfs(g, *n, &v_len); |
geonnave | 0:ecd06432fb4b | 168 | attr_v2 *at = (attr_v2 *) malloc(sizeof(attr_v2)); |
geonnave | 0:ecd06432fb4b | 169 | *at = new_attr_string_list((*at_orig)->name, v_len); |
geonnave | 0:ecd06432fb4b | 170 | for (int j = 0; j < v_len; ++j) |
geonnave | 0:ecd06432fb4b | 171 | at->string_list[j] = visited[j].value; |
geonnave | 0:ecd06432fb4b | 172 | |
geonnave | 0:ecd06432fb4b | 173 | *at_orig = at; |
geonnave | 0:ecd06432fb4b | 174 | } |
geonnave | 0:ecd06432fb4b | 175 | |
geonnave | 0:ecd06432fb4b | 176 | void expand_attrs(rule *req, graph g) |
geonnave | 0:ecd06432fb4b | 177 | { |
geonnave | 0:ecd06432fb4b | 178 | for (int i = 0; i < req->users_len; ++i) |
geonnave | 0:ecd06432fb4b | 179 | expand_attr(&req->users[i], g); |
geonnave | 0:ecd06432fb4b | 180 | for (int i = 0; i < req->objects_len; ++i) |
geonnave | 0:ecd06432fb4b | 181 | expand_attr(&req->objects[i], g); |
geonnave | 0:ecd06432fb4b | 182 | for (int i = 0; i < req->contexts_len; ++i) |
geonnave | 0:ecd06432fb4b | 183 | expand_attr(&req->contexts[i], g); |
geonnave | 0:ecd06432fb4b | 184 | } |
geonnave | 0:ecd06432fb4b | 185 | |
geonnave | 0:ecd06432fb4b | 186 | // authorization |
geonnave | 0:ecd06432fb4b | 187 | |
geonnave | 0:ecd06432fb4b | 188 | int is_subset(char **ro, size_t ro_len, char **po, size_t po_len) |
geonnave | 0:ecd06432fb4b | 189 | { |
geonnave | 0:ecd06432fb4b | 190 | for (int i = 0; i < ro_len; i++) { |
geonnave | 0:ecd06432fb4b | 191 | int ok = 0; |
geonnave | 0:ecd06432fb4b | 192 | for (int j = 0; j < po_len; j++) |
geonnave | 0:ecd06432fb4b | 193 | if (strcmp(ro[i], po[j]) == 0) |
geonnave | 0:ecd06432fb4b | 194 | ok = 1; |
geonnave | 0:ecd06432fb4b | 195 | if (!ok) |
geonnave | 0:ecd06432fb4b | 196 | return 0; |
geonnave | 0:ecd06432fb4b | 197 | } |
geonnave | 0:ecd06432fb4b | 198 | return 1; |
geonnave | 0:ecd06432fb4b | 199 | } |
geonnave | 0:ecd06432fb4b | 200 | |
geonnave | 0:ecd06432fb4b | 201 | int is_string_in(char *a, char **b, size_t b_len) |
geonnave | 0:ecd06432fb4b | 202 | { |
geonnave | 0:ecd06432fb4b | 203 | for (int i = 0; i < b_len; ++i) |
geonnave | 0:ecd06432fb4b | 204 | if (strcmp(a, b[i]) == 0) |
geonnave | 0:ecd06432fb4b | 205 | return 1; |
geonnave | 0:ecd06432fb4b | 206 | return 0; |
geonnave | 0:ecd06432fb4b | 207 | } |
geonnave | 0:ecd06432fb4b | 208 | |
geonnave | 0:ecd06432fb4b | 209 | int match_attr_v2(attr_v2 ra, attr_v2 pa) |
geonnave | 0:ecd06432fb4b | 210 | { |
geonnave | 0:ecd06432fb4b | 211 | if (strcmp(ra.name, pa.name) != 0) |
geonnave | 0:ecd06432fb4b | 212 | return 0; |
geonnave | 0:ecd06432fb4b | 213 | |
geonnave | 0:ecd06432fb4b | 214 | switch(pa.data_type) { |
geonnave | 0:ecd06432fb4b | 215 | case abac_integer: |
geonnave | 0:ecd06432fb4b | 216 | if (ra.integer == pa.integer) |
geonnave | 0:ecd06432fb4b | 217 | return 1; |
geonnave | 0:ecd06432fb4b | 218 | break; |
geonnave | 0:ecd06432fb4b | 219 | case abac_real: |
geonnave | 0:ecd06432fb4b | 220 | if (ra.real == pa.real) |
geonnave | 0:ecd06432fb4b | 221 | return 1; |
geonnave | 0:ecd06432fb4b | 222 | break; |
geonnave | 0:ecd06432fb4b | 223 | case abac_integer_range: |
geonnave | 0:ecd06432fb4b | 224 | if (ra.integer >= pa.ran.integer_min && ra.integer <= pa.ran.integer_max) |
geonnave | 0:ecd06432fb4b | 225 | return 1; |
geonnave | 0:ecd06432fb4b | 226 | break; |
geonnave | 0:ecd06432fb4b | 227 | case abac_real_range: |
geonnave | 0:ecd06432fb4b | 228 | if (ra.real >= pa.ran.real_min && ra.real <= pa.ran.real_max) |
geonnave | 0:ecd06432fb4b | 229 | return 1; |
geonnave | 0:ecd06432fb4b | 230 | break; |
geonnave | 0:ecd06432fb4b | 231 | case abac_string: |
geonnave | 0:ecd06432fb4b | 232 | if (ra.data_type == abac_string && strcmp(ra.string, pa.string) == 0) |
geonnave | 0:ecd06432fb4b | 233 | return 1; |
geonnave | 0:ecd06432fb4b | 234 | else if (ra.data_type == abac_string_list && is_string_in(pa.string, ra.string_list, ra.inner_list_len)) |
geonnave | 0:ecd06432fb4b | 235 | return 1; |
geonnave | 0:ecd06432fb4b | 236 | break; |
geonnave | 0:ecd06432fb4b | 237 | case abac_dictionary: |
geonnave | 0:ecd06432fb4b | 238 | return match_attrs_v2(ra.inner_attrs, ra.inner_list_len, pa.inner_attrs, pa.inner_list_len); |
geonnave | 0:ecd06432fb4b | 239 | } |
geonnave | 0:ecd06432fb4b | 240 | return 0; |
geonnave | 0:ecd06432fb4b | 241 | } |
geonnave | 0:ecd06432fb4b | 242 | |
geonnave | 0:ecd06432fb4b | 243 | int match_attrs_v2(attr_v2 **ras, size_t ras_len, attr_v2 **pas, size_t pas_len) |
geonnave | 0:ecd06432fb4b | 244 | { |
geonnave | 0:ecd06432fb4b | 245 | int any_r; |
geonnave | 0:ecd06432fb4b | 246 | for (int i = 0; i < pas_len; i++) |
geonnave | 0:ecd06432fb4b | 247 | { |
geonnave | 0:ecd06432fb4b | 248 | any_r = 0; |
geonnave | 0:ecd06432fb4b | 249 | for (int j = 0; j < ras_len; j++) |
geonnave | 0:ecd06432fb4b | 250 | if (match_attr_v2(*(ras[i]), *(pas[i]))) |
geonnave | 0:ecd06432fb4b | 251 | any_r = 1; |
geonnave | 0:ecd06432fb4b | 252 | if (!any_r) |
geonnave | 0:ecd06432fb4b | 253 | return 0; |
geonnave | 0:ecd06432fb4b | 254 | } |
geonnave | 0:ecd06432fb4b | 255 | if (any_r) |
geonnave | 0:ecd06432fb4b | 256 | return 1; |
geonnave | 0:ecd06432fb4b | 257 | else |
geonnave | 0:ecd06432fb4b | 258 | return 0; |
geonnave | 0:ecd06432fb4b | 259 | } |
geonnave | 0:ecd06432fb4b | 260 | |
geonnave | 0:ecd06432fb4b | 261 | int match_permission(rule r, rule perm) |
geonnave | 0:ecd06432fb4b | 262 | { |
geonnave | 0:ecd06432fb4b | 263 | return |
geonnave | 0:ecd06432fb4b | 264 | is_subset(r.operations, r.operations_len, perm.operations, perm.operations_len) && |
geonnave | 0:ecd06432fb4b | 265 | match_attrs_v2(r.users, r.users_len, perm.users, perm.users_len) && |
geonnave | 0:ecd06432fb4b | 266 | match_attrs_v2(r.objects, r.objects_len, perm.objects, perm.objects_len) && |
geonnave | 0:ecd06432fb4b | 267 | match_attrs_v2(r.contexts, r.contexts_len, perm.contexts, perm.contexts_len); |
geonnave | 0:ecd06432fb4b | 268 | } |
geonnave | 0:ecd06432fb4b | 269 | |
geonnave | 0:ecd06432fb4b | 270 | int authorize_permissions(rule req, rule *perms, size_t p_len) |
geonnave | 0:ecd06432fb4b | 271 | { |
geonnave | 0:ecd06432fb4b | 272 | for (int i = 0; i < p_len; i++) |
geonnave | 0:ecd06432fb4b | 273 | if (match_permission(req, perms[i])) |
geonnave | 0:ecd06432fb4b | 274 | return 1; |
geonnave | 0:ecd06432fb4b | 275 | return 0; |
geonnave | 0:ecd06432fb4b | 276 | } |
geonnave | 0:ecd06432fb4b | 277 | |
geonnave | 0:ecd06432fb4b | 278 | int authorize_permissions_expand(rule req, rule *perms, size_t p_len, graph g) |
geonnave | 0:ecd06432fb4b | 279 | { |
geonnave | 0:ecd06432fb4b | 280 | expand_attrs(&req, g); |
geonnave | 0:ecd06432fb4b | 281 | for (int i = 0; i < p_len; i++) |
geonnave | 0:ecd06432fb4b | 282 | if (match_permission(req, perms[i])) |
geonnave | 0:ecd06432fb4b | 283 | return 1; |
geonnave | 0:ecd06432fb4b | 284 | return 0; |
geonnave | 0:ecd06432fb4b | 285 | } |
geonnave | 0:ecd06432fb4b | 286 | |
geonnave | 0:ecd06432fb4b | 287 | // debug |
geonnave | 0:ecd06432fb4b | 288 | |
geonnave | 0:ecd06432fb4b | 289 | void show_attr_v2(attr_v2 at) |
geonnave | 0:ecd06432fb4b | 290 | { |
geonnave | 0:ecd06432fb4b | 291 | switch(at.data_type) { |
geonnave | 0:ecd06432fb4b | 292 | case abac_integer: |
geonnave | 0:ecd06432fb4b | 293 | printf("%s: %d\n", at.name, at.integer); |
geonnave | 0:ecd06432fb4b | 294 | break; |
geonnave | 0:ecd06432fb4b | 295 | case abac_real: |
geonnave | 0:ecd06432fb4b | 296 | printf("%s: %.2f\n", at.name, at.real); |
geonnave | 0:ecd06432fb4b | 297 | break; |
geonnave | 0:ecd06432fb4b | 298 | case abac_integer_range: |
geonnave | 0:ecd06432fb4b | 299 | printf("%s: %d..%d\n", at.name, at.ran.integer_min, at.ran.integer_max); |
geonnave | 0:ecd06432fb4b | 300 | break; |
geonnave | 0:ecd06432fb4b | 301 | case abac_real_range: |
geonnave | 0:ecd06432fb4b | 302 | printf("%s: %.2f..%.2f\n", at.name, at.ran.real_min, at.ran.real_max); |
geonnave | 0:ecd06432fb4b | 303 | break; |
geonnave | 0:ecd06432fb4b | 304 | case abac_string: |
geonnave | 0:ecd06432fb4b | 305 | printf("%s: %s\n", at.name, at.string); |
geonnave | 0:ecd06432fb4b | 306 | break; |
geonnave | 0:ecd06432fb4b | 307 | case abac_string_list: |
geonnave | 0:ecd06432fb4b | 308 | printf("%s: ", at.name); |
geonnave | 0:ecd06432fb4b | 309 | for (int i = 0; i < at.inner_list_len; ++i) |
geonnave | 0:ecd06432fb4b | 310 | printf("%s ", at.string_list[i]); |
geonnave | 0:ecd06432fb4b | 311 | printf("\n"); |
geonnave | 0:ecd06432fb4b | 312 | break; |
geonnave | 0:ecd06432fb4b | 313 | case abac_dictionary: |
geonnave | 0:ecd06432fb4b | 314 | printf("[%s:\n", at.name); |
geonnave | 0:ecd06432fb4b | 315 | for (int i = 0; i < at.inner_list_len; i++) |
geonnave | 0:ecd06432fb4b | 316 | show_attr_v2(*(at.inner_attrs[i])); |
geonnave | 0:ecd06432fb4b | 317 | printf("]\n"); |
geonnave | 0:ecd06432fb4b | 318 | break; |
geonnave | 0:ecd06432fb4b | 319 | } |
geonnave | 0:ecd06432fb4b | 320 | } |
geonnave | 0:ecd06432fb4b | 321 | |
geonnave | 0:ecd06432fb4b | 322 | void show_operations(char **ops, size_t len) |
geonnave | 0:ecd06432fb4b | 323 | { |
geonnave | 0:ecd06432fb4b | 324 | for (int i = 0; i < len; i++) |
geonnave | 0:ecd06432fb4b | 325 | printf("%s ", ops[i]); |
geonnave | 0:ecd06432fb4b | 326 | printf("\n"); |
geonnave | 0:ecd06432fb4b | 327 | } |
geonnave | 0:ecd06432fb4b | 328 | |
geonnave | 0:ecd06432fb4b | 329 | void show_rule(rule r, char *desc) |
geonnave | 0:ecd06432fb4b | 330 | { |
geonnave | 0:ecd06432fb4b | 331 | printf("\n>%s\n", desc); |
geonnave | 0:ecd06432fb4b | 332 | printf("#users:\n"); |
geonnave | 0:ecd06432fb4b | 333 | for (int i = 0; i < r.users_len; i++) |
geonnave | 0:ecd06432fb4b | 334 | show_attr_v2(*(r.users[i])); |
geonnave | 0:ecd06432fb4b | 335 | printf("#objects:\n"); |
geonnave | 0:ecd06432fb4b | 336 | for (int i = 0; i < r.objects_len; i++) |
geonnave | 0:ecd06432fb4b | 337 | show_attr_v2(*(r.objects[i])); |
geonnave | 0:ecd06432fb4b | 338 | printf("#contexts:\n"); |
geonnave | 0:ecd06432fb4b | 339 | for (int i = 0; i < r.contexts_len; i++) |
geonnave | 0:ecd06432fb4b | 340 | show_attr_v2(*(r.contexts[i])); |
geonnave | 0:ecd06432fb4b | 341 | printf("#operations:\n"); |
geonnave | 0:ecd06432fb4b | 342 | show_operations(r.operations, r.operations_len); |
geonnave | 0:ecd06432fb4b | 343 | } |
geonnave | 0:ecd06432fb4b | 344 | |
geonnave | 0:ecd06432fb4b | 345 | void show_visited(node *visited, size_t v_len) |
geonnave | 0:ecd06432fb4b | 346 | { |
geonnave | 0:ecd06432fb4b | 347 | printf("visited: "); |
geonnave | 0:ecd06432fb4b | 348 | for (int i = 0; i < v_len; ++i) |
geonnave | 0:ecd06432fb4b | 349 | printf("%s ", visited[i].value); |
geonnave | 0:ecd06432fb4b | 350 | printf("\n"); |
geonnave | 0:ecd06432fb4b | 351 | } |
geonnave | 0:ecd06432fb4b | 352 | |
geonnave | 0:ecd06432fb4b | 353 | void show_node_list(node *list, size_t len, char *desc) |
geonnave | 0:ecd06432fb4b | 354 | { |
geonnave | 0:ecd06432fb4b | 355 | printf("%s: ", desc); |
geonnave | 0:ecd06432fb4b | 356 | for (int i = 0; i < len; ++i) |
geonnave | 0:ecd06432fb4b | 357 | printf("%s ", list[i].value); |
geonnave | 0:ecd06432fb4b | 358 | printf("\n"); |
geonnave | 0:ecd06432fb4b | 359 | } |
geonnave | 0:ecd06432fb4b | 360 | |
geonnave | 0:ecd06432fb4b | 361 | |
geonnave | 0:ecd06432fb4b | 362 | |
geonnave | 0:ecd06432fb4b | 363 | int main() { |
geonnave | 0:ecd06432fb4b | 364 | |
geonnave | 0:ecd06432fb4b | 365 | attr_v2 **at_list; |
geonnave | 0:ecd06432fb4b | 366 | |
geonnave | 0:ecd06432fb4b | 367 | attr_v2 id_alice = new_attr_string("id", "alice"); |
geonnave | 0:ecd06432fb4b | 368 | attr_v2 id_camera1 = new_attr_string("id", "camera1"); |
geonnave | 0:ecd06432fb4b | 369 | attr_v2 id_lamp1 = new_attr_string("id", "lamp1"); |
geonnave | 0:ecd06432fb4b | 370 | attr_v2 id_some_device_x = new_attr_string("id", "some-device-x"); |
geonnave | 0:ecd06432fb4b | 371 | attr_v2 owner_alice = new_attr_string("owner", "alice"); |
geonnave | 0:ecd06432fb4b | 372 | attr_v2 year_2020 = new_attr_integer("year", 2020); |
geonnave | 0:ecd06432fb4b | 373 | attr_v2 month_6 = new_attr_integer("month", 6); |
geonnave | 0:ecd06432fb4b | 374 | attr_v2 day_30 = new_attr_integer("day", 30); |
geonnave | 0:ecd06432fb4b | 375 | attr_v2 hour_17 = new_attr_integer("hour", 17); |
geonnave | 0:ecd06432fb4b | 376 | attr_v2 luminosity_25 = new_attr_integer("outdoorLuminosity", 25); |
geonnave | 0:ecd06432fb4b | 377 | attr_v2 age_min18 = new_attr_integer_range("age", 18, 120); |
geonnave | 0:ecd06432fb4b | 378 | attr_v2 minute_20_25 = new_attr_integer_range("minute", 20, 25); |
geonnave | 0:ecd06432fb4b | 379 | attr_v2 luminosity_max33 = new_attr_integer_range("outdoorLuminosity", 0, 33); |
geonnave | 0:ecd06432fb4b | 380 | attr_v2 reputation_min4 = new_attr_real_range("reputation", 4, 5); |
geonnave | 0:ecd06432fb4b | 381 | attr_v2 type_security = new_attr_string("type", "securityAppliance"); |
geonnave | 0:ecd06432fb4b | 382 | attr_v2 type_lighting = new_attr_string("type", "lightingAppliance"); |
geonnave | 0:ecd06432fb4b | 383 | attr_v2 household_role_child = new_attr_string("role", "child"); |
geonnave | 0:ecd06432fb4b | 384 | attr_v2 household_id_home1 = new_attr_string("id", "home-1"); |
geonnave | 0:ecd06432fb4b | 385 | attr_v2 type_camera = new_attr_string("type", "securityCamera"); |
geonnave | 0:ecd06432fb4b | 386 | attr_v2 location_outdoor = new_attr_string("location", "outdoor"); |
geonnave | 0:ecd06432fb4b | 387 | |
geonnave | 0:ecd06432fb4b | 388 | at_list = new_attr_list(1); |
geonnave | 0:ecd06432fb4b | 389 | at_list[0] = &household_id_home1; |
geonnave | 0:ecd06432fb4b | 390 | attr_v2 household_with_id = new_attr_dictionary("household", at_list, 1); |
geonnave | 0:ecd06432fb4b | 391 | |
geonnave | 0:ecd06432fb4b | 392 | at_list = new_attr_list(2); |
geonnave | 0:ecd06432fb4b | 393 | at_list[0] = &household_id_home1; |
geonnave | 0:ecd06432fb4b | 394 | at_list[1] = &household_role_child; |
geonnave | 0:ecd06432fb4b | 395 | attr_v2 household_with_id_role = new_attr_dictionary("household", at_list, 2); |
geonnave | 0:ecd06432fb4b | 396 | |
geonnave | 0:ecd06432fb4b | 397 | // p1 |
geonnave | 0:ecd06432fb4b | 398 | rule perm1; |
geonnave | 0:ecd06432fb4b | 399 | perm1.users = new_attr_list(1); |
geonnave | 0:ecd06432fb4b | 400 | perm1.users_len = 1; |
geonnave | 0:ecd06432fb4b | 401 | perm1.users[0] = &id_alice; |
geonnave | 0:ecd06432fb4b | 402 | |
geonnave | 0:ecd06432fb4b | 403 | perm1.objects = new_attr_list(1); |
geonnave | 0:ecd06432fb4b | 404 | perm1.objects_len = 1; |
geonnave | 0:ecd06432fb4b | 405 | perm1.objects[0] = &owner_alice; |
geonnave | 0:ecd06432fb4b | 406 | |
geonnave | 0:ecd06432fb4b | 407 | perm1.contexts_len = 0; |
geonnave | 0:ecd06432fb4b | 408 | |
geonnave | 0:ecd06432fb4b | 409 | perm1.operations = new_operations_list(4); |
geonnave | 0:ecd06432fb4b | 410 | perm1.operations_len = 4; |
geonnave | 0:ecd06432fb4b | 411 | perm1.operations[0] = "create"; |
geonnave | 0:ecd06432fb4b | 412 | perm1.operations[1] = "read"; |
geonnave | 0:ecd06432fb4b | 413 | perm1.operations[2] = "update"; |
geonnave | 0:ecd06432fb4b | 414 | perm1.operations[3] = "delete"; |
geonnave | 0:ecd06432fb4b | 415 | show_rule(perm1, "perm1\0"); |
geonnave | 0:ecd06432fb4b | 416 | |
geonnave | 0:ecd06432fb4b | 417 | // p2, authorizes req_e |
geonnave | 0:ecd06432fb4b | 418 | rule perm2; |
geonnave | 0:ecd06432fb4b | 419 | perm2.users = new_attr_list(2); |
geonnave | 0:ecd06432fb4b | 420 | perm2.users_len = 2; |
geonnave | 0:ecd06432fb4b | 421 | perm2.users[0] = &age_min18; |
geonnave | 0:ecd06432fb4b | 422 | perm2.users[1] = &household_with_id; |
geonnave | 0:ecd06432fb4b | 423 | |
geonnave | 0:ecd06432fb4b | 424 | perm2.objects = new_attr_list(2); |
geonnave | 0:ecd06432fb4b | 425 | perm2.objects_len = 2; |
geonnave | 0:ecd06432fb4b | 426 | perm2.objects[0] = &type_security; |
geonnave | 0:ecd06432fb4b | 427 | perm2.objects[1] = &household_with_id; |
geonnave | 0:ecd06432fb4b | 428 | |
geonnave | 0:ecd06432fb4b | 429 | perm2.contexts_len = 0; |
geonnave | 0:ecd06432fb4b | 430 | |
geonnave | 0:ecd06432fb4b | 431 | perm2.operations = new_operations_list(2); |
geonnave | 0:ecd06432fb4b | 432 | perm2.operations_len = 2; |
geonnave | 0:ecd06432fb4b | 433 | perm2.operations[0] = "read"; |
geonnave | 0:ecd06432fb4b | 434 | perm2.operations[1] = "update"; |
geonnave | 0:ecd06432fb4b | 435 | show_rule(perm2, "perm2\0"); |
geonnave | 0:ecd06432fb4b | 436 | |
geonnave | 0:ecd06432fb4b | 437 | // p3, authorizes req |
geonnave | 0:ecd06432fb4b | 438 | rule perm3; |
geonnave | 0:ecd06432fb4b | 439 | perm3.users = new_attr_list(1); |
geonnave | 0:ecd06432fb4b | 440 | perm3.users_len = 1; |
geonnave | 0:ecd06432fb4b | 441 | perm3.users[0] = &household_with_id_role; |
geonnave | 0:ecd06432fb4b | 442 | |
geonnave | 0:ecd06432fb4b | 443 | perm3.objects = new_attr_list(2); |
geonnave | 0:ecd06432fb4b | 444 | perm3.objects_len = 2; |
geonnave | 0:ecd06432fb4b | 445 | perm3.objects[0] = &type_lighting; |
geonnave | 0:ecd06432fb4b | 446 | perm3.objects[1] = &household_with_id; |
geonnave | 0:ecd06432fb4b | 447 | |
geonnave | 0:ecd06432fb4b | 448 | perm3.contexts = new_attr_list(1); |
geonnave | 0:ecd06432fb4b | 449 | perm3.contexts_len = 1; |
geonnave | 0:ecd06432fb4b | 450 | perm3.contexts[0] = &luminosity_max33; |
geonnave | 0:ecd06432fb4b | 451 | |
geonnave | 0:ecd06432fb4b | 452 | perm3.operations = new_operations_list(2); |
geonnave | 0:ecd06432fb4b | 453 | perm3.operations_len = 2; |
geonnave | 0:ecd06432fb4b | 454 | perm3.operations[0] = "read"; |
geonnave | 0:ecd06432fb4b | 455 | perm3.operations[1] = "update"; |
geonnave | 0:ecd06432fb4b | 456 | show_rule(perm3, "perm3\0"); |
geonnave | 0:ecd06432fb4b | 457 | |
geonnave | 0:ecd06432fb4b | 458 | // p4 |
geonnave | 0:ecd06432fb4b | 459 | rule perm4; |
geonnave | 0:ecd06432fb4b | 460 | perm4.users = new_attr_list(1); |
geonnave | 0:ecd06432fb4b | 461 | perm4.users_len = 1; |
geonnave | 0:ecd06432fb4b | 462 | perm4.users[0] = &id_camera1; |
geonnave | 0:ecd06432fb4b | 463 | |
geonnave | 0:ecd06432fb4b | 464 | perm4.objects = new_attr_list(1); |
geonnave | 0:ecd06432fb4b | 465 | perm4.objects_len = 1; |
geonnave | 0:ecd06432fb4b | 466 | perm4.objects[0] = &id_lamp1; |
geonnave | 0:ecd06432fb4b | 467 | |
geonnave | 0:ecd06432fb4b | 468 | perm4.contexts_len = 0; |
geonnave | 0:ecd06432fb4b | 469 | |
geonnave | 0:ecd06432fb4b | 470 | perm4.operations = new_operations_list(2); |
geonnave | 0:ecd06432fb4b | 471 | perm4.operations_len = 2; |
geonnave | 0:ecd06432fb4b | 472 | perm4.operations[0] = "read"; |
geonnave | 0:ecd06432fb4b | 473 | perm4.operations[1] = "update"; |
geonnave | 0:ecd06432fb4b | 474 | show_rule(perm4, "perm4\0"); |
geonnave | 0:ecd06432fb4b | 475 | |
geonnave | 0:ecd06432fb4b | 476 | // p5 |
geonnave | 0:ecd06432fb4b | 477 | rule perm5; |
geonnave | 0:ecd06432fb4b | 478 | perm5.users = new_attr_list(1); |
geonnave | 0:ecd06432fb4b | 479 | perm5.users_len = 1; |
geonnave | 0:ecd06432fb4b | 480 | perm5.users[0] = &reputation_min4; |
geonnave | 0:ecd06432fb4b | 481 | |
geonnave | 0:ecd06432fb4b | 482 | perm5.objects = new_attr_list(3); |
geonnave | 0:ecd06432fb4b | 483 | perm5.objects_len = 3; |
geonnave | 0:ecd06432fb4b | 484 | perm5.objects[0] = &type_camera; |
geonnave | 0:ecd06432fb4b | 485 | perm5.objects[1] = &household_with_id; |
geonnave | 0:ecd06432fb4b | 486 | perm5.objects[2] = &location_outdoor; |
geonnave | 0:ecd06432fb4b | 487 | |
geonnave | 0:ecd06432fb4b | 488 | perm5.contexts = new_attr_list(1); |
geonnave | 0:ecd06432fb4b | 489 | perm5.contexts_len = 1; |
geonnave | 0:ecd06432fb4b | 490 | perm5.contexts[0] = &luminosity_max33; |
geonnave | 0:ecd06432fb4b | 491 | |
geonnave | 0:ecd06432fb4b | 492 | perm5.operations = new_operations_list(1); |
geonnave | 0:ecd06432fb4b | 493 | perm5.operations_len = 1; |
geonnave | 0:ecd06432fb4b | 494 | perm5.operations[0] = "contract"; |
geonnave | 0:ecd06432fb4b | 495 | show_rule(perm5, "perm5\0"); |
geonnave | 0:ecd06432fb4b | 496 | |
geonnave | 0:ecd06432fb4b | 497 | // p6 |
geonnave | 0:ecd06432fb4b | 498 | rule perm6; |
geonnave | 0:ecd06432fb4b | 499 | perm6.users = new_attr_list(1); |
geonnave | 0:ecd06432fb4b | 500 | perm6.users_len = 1; |
geonnave | 0:ecd06432fb4b | 501 | perm6.users[0] = &id_some_device_x; |
geonnave | 0:ecd06432fb4b | 502 | |
geonnave | 0:ecd06432fb4b | 503 | perm6.objects = new_attr_list(5); |
geonnave | 0:ecd06432fb4b | 504 | perm6.objects_len = 5; |
geonnave | 0:ecd06432fb4b | 505 | perm6.objects[0] = &year_2020; |
geonnave | 0:ecd06432fb4b | 506 | perm6.objects[1] = &month_6; |
geonnave | 0:ecd06432fb4b | 507 | perm6.objects[2] = &day_30; |
geonnave | 0:ecd06432fb4b | 508 | perm6.objects[3] = &hour_17; |
geonnave | 0:ecd06432fb4b | 509 | perm6.objects[4] = &minute_20_25; |
geonnave | 0:ecd06432fb4b | 510 | |
geonnave | 0:ecd06432fb4b | 511 | perm6.contexts = new_attr_list(1); |
geonnave | 0:ecd06432fb4b | 512 | perm6.contexts_len = 1; |
geonnave | 0:ecd06432fb4b | 513 | perm6.contexts[0] = &luminosity_max33; |
geonnave | 0:ecd06432fb4b | 514 | |
geonnave | 0:ecd06432fb4b | 515 | perm6.operations = new_operations_list(1); |
geonnave | 0:ecd06432fb4b | 516 | perm6.operations_len = 1; |
geonnave | 0:ecd06432fb4b | 517 | perm6.operations[0] = "contract"; |
geonnave | 0:ecd06432fb4b | 518 | show_rule(perm6, "perm6\0"); |
geonnave | 0:ecd06432fb4b | 519 | |
geonnave | 0:ecd06432fb4b | 520 | // list of perms |
geonnave | 0:ecd06432fb4b | 521 | rule *perms = (rule *) malloc(sizeof(rule) * 6); |
geonnave | 0:ecd06432fb4b | 522 | perms[0] = perm1; |
geonnave | 0:ecd06432fb4b | 523 | perms[1] = perm2; |
geonnave | 0:ecd06432fb4b | 524 | perms[2] = perm3; |
geonnave | 0:ecd06432fb4b | 525 | perms[3] = perm4; |
geonnave | 0:ecd06432fb4b | 526 | perms[4] = perm5; |
geonnave | 0:ecd06432fb4b | 527 | perms[5] = perm6; |
geonnave | 0:ecd06432fb4b | 528 | |
geonnave | 0:ecd06432fb4b | 529 | // a request |
geonnave | 0:ecd06432fb4b | 530 | rule req; |
geonnave | 0:ecd06432fb4b | 531 | req.users = new_attr_list(1); |
geonnave | 0:ecd06432fb4b | 532 | req.users_len = 1; |
geonnave | 0:ecd06432fb4b | 533 | req.users[0] = &household_with_id_role; |
geonnave | 0:ecd06432fb4b | 534 | |
geonnave | 0:ecd06432fb4b | 535 | req.objects = new_attr_list(2); |
geonnave | 0:ecd06432fb4b | 536 | req.objects_len = 2; |
geonnave | 0:ecd06432fb4b | 537 | req.objects[0] = &type_lighting; |
geonnave | 0:ecd06432fb4b | 538 | req.objects[1] = &household_with_id; |
geonnave | 0:ecd06432fb4b | 539 | |
geonnave | 0:ecd06432fb4b | 540 | req.contexts = new_attr_list(1); |
geonnave | 0:ecd06432fb4b | 541 | req.contexts_len = 1; |
geonnave | 0:ecd06432fb4b | 542 | req.contexts[0] = &luminosity_25; |
geonnave | 0:ecd06432fb4b | 543 | |
geonnave | 0:ecd06432fb4b | 544 | req.operations = new_operations_list(1); |
geonnave | 0:ecd06432fb4b | 545 | req.operations_len = 1; |
geonnave | 0:ecd06432fb4b | 546 | req.operations[0] = "read"; |
geonnave | 0:ecd06432fb4b | 547 | show_rule(req, "request\0"); |
geonnave | 0:ecd06432fb4b | 548 | |
geonnave | 0:ecd06432fb4b | 549 | if (authorize_permissions(req, perms, 6)) |
geonnave | 0:ecd06432fb4b | 550 | printf("\nauthorized request for policy #3\n"); |
geonnave | 0:ecd06432fb4b | 551 | |
geonnave | 0:ecd06432fb4b | 552 | // creating a graph |
geonnave | 0:ecd06432fb4b | 553 | |
geonnave | 0:ecd06432fb4b | 554 | node n_child = new_graph_node("child"); |
geonnave | 0:ecd06432fb4b | 555 | node n_father = new_graph_node("father"); |
geonnave | 0:ecd06432fb4b | 556 | node n_mother = new_graph_node("mother"); |
geonnave | 0:ecd06432fb4b | 557 | node n_adultFamilyMember = new_graph_node("adultFamilyMember"); |
geonnave | 0:ecd06432fb4b | 558 | node n_family_member = new_graph_node("familyMember"); |
geonnave | 0:ecd06432fb4b | 559 | node n_person = new_graph_node("person"); |
geonnave | 0:ecd06432fb4b | 560 | create_directed_edge(&n_child, &n_family_member); |
geonnave | 0:ecd06432fb4b | 561 | create_directed_edge(&n_father, &n_adultFamilyMember); |
geonnave | 0:ecd06432fb4b | 562 | create_directed_edge(&n_mother, &n_adultFamilyMember); |
geonnave | 0:ecd06432fb4b | 563 | create_directed_edge(&n_adultFamilyMember, &n_family_member); |
geonnave | 0:ecd06432fb4b | 564 | create_directed_edge(&n_family_member, &n_person); |
geonnave | 0:ecd06432fb4b | 565 | |
geonnave | 0:ecd06432fb4b | 566 | node n_securityCamera = new_graph_node("securityCamera"); |
geonnave | 0:ecd06432fb4b | 567 | node n_intrusionAlarm = new_graph_node("intrusionAlarm"); |
geonnave | 0:ecd06432fb4b | 568 | node n_securityAppliance = new_graph_node("securityAppliance"); |
geonnave | 0:ecd06432fb4b | 569 | create_directed_edge(&n_securityCamera, &n_securityAppliance); |
geonnave | 0:ecd06432fb4b | 570 | create_directed_edge(&n_intrusionAlarm, &n_securityAppliance); |
geonnave | 0:ecd06432fb4b | 571 | |
geonnave | 0:ecd06432fb4b | 572 | graph g = new_graph(6+3); |
geonnave | 0:ecd06432fb4b | 573 | g.list[0] = &n_child; |
geonnave | 0:ecd06432fb4b | 574 | g.list[1] = &n_father; |
geonnave | 0:ecd06432fb4b | 575 | g.list[2] = &n_mother; |
geonnave | 0:ecd06432fb4b | 576 | g.list[3] = &n_adultFamilyMember; |
geonnave | 0:ecd06432fb4b | 577 | g.list[4] = &n_family_member; |
geonnave | 0:ecd06432fb4b | 578 | g.list[5] = &n_person; |
geonnave | 0:ecd06432fb4b | 579 | g.list[6] = &n_securityCamera; |
geonnave | 0:ecd06432fb4b | 580 | g.list[7] = &n_intrusionAlarm; |
geonnave | 0:ecd06432fb4b | 581 | g.list[8] = &n_securityAppliance; |
geonnave | 0:ecd06432fb4b | 582 | |
geonnave | 0:ecd06432fb4b | 583 | // a request to expand |
geonnave | 0:ecd06432fb4b | 584 | rule req_e; |
geonnave | 0:ecd06432fb4b | 585 | req_e.users = new_attr_list(2); |
geonnave | 0:ecd06432fb4b | 586 | req_e.users_len = 2; |
geonnave | 0:ecd06432fb4b | 587 | attr_v2 age_25 = new_attr_integer("age", 25); |
geonnave | 0:ecd06432fb4b | 588 | req_e.users[0] = &age_25; |
geonnave | 0:ecd06432fb4b | 589 | req_e.users[1] = &household_with_id; |
geonnave | 0:ecd06432fb4b | 590 | |
geonnave | 0:ecd06432fb4b | 591 | req_e.objects = new_attr_list(2); |
geonnave | 0:ecd06432fb4b | 592 | req_e.objects_len = 2; |
geonnave | 0:ecd06432fb4b | 593 | req_e.objects[0] = &type_camera; |
geonnave | 0:ecd06432fb4b | 594 | req_e.objects[1] = &household_with_id; |
geonnave | 0:ecd06432fb4b | 595 | |
geonnave | 0:ecd06432fb4b | 596 | req_e.contexts_len = 0; |
geonnave | 0:ecd06432fb4b | 597 | |
geonnave | 0:ecd06432fb4b | 598 | req_e.operations = new_operations_list(1); |
geonnave | 0:ecd06432fb4b | 599 | req_e.operations_len = 1; |
geonnave | 0:ecd06432fb4b | 600 | req_e.operations[0] = "read"; |
geonnave | 0:ecd06432fb4b | 601 | show_rule(req_e, "request that will be expanded\0"); |
geonnave | 0:ecd06432fb4b | 602 | |
geonnave | 0:ecd06432fb4b | 603 | if (!authorize_permissions(req_e, perms, 6)) |
geonnave | 0:ecd06432fb4b | 604 | printf("\ndenied non-expanded request for policy #2\n"); |
geonnave | 0:ecd06432fb4b | 605 | if (authorize_permissions_expand(req_e, perms, 6, g)) |
geonnave | 0:ecd06432fb4b | 606 | printf("\nauthorized expanded request for policy #2\n\n"); |
geonnave | 0:ecd06432fb4b | 607 | |
geonnave | 0:ecd06432fb4b | 608 | // many policies |
geonnave | 0:ecd06432fb4b | 609 | int n_perms = 3000, median; |
geonnave | 0:ecd06432fb4b | 610 | median = (int) (n_perms / 2); |
geonnave | 0:ecd06432fb4b | 611 | rule *many_perms = (rule *) malloc(sizeof(rule) * n_perms); |
geonnave | 0:ecd06432fb4b | 612 | for (int i = 0; i < n_perms; ++i) |
geonnave | 0:ecd06432fb4b | 613 | many_perms[i] = perm5; |
geonnave | 0:ecd06432fb4b | 614 | many_perms[median] = perm2; |
geonnave | 0:ecd06432fb4b | 615 | |
geonnave | 0:ecd06432fb4b | 616 | // benchmark |
geonnave | 0:ecd06432fb4b | 617 | |
geonnave | 0:ecd06432fb4b | 618 | int runs = 3000; |
geonnave | 0:ecd06432fb4b | 619 | #ifdef MBED_MAJOR_VERSION |
geonnave | 0:ecd06432fb4b | 620 | Timer t; |
geonnave | 0:ecd06432fb4b | 621 | t.start(); |
geonnave | 0:ecd06432fb4b | 622 | for (int i = 0; i < runs; i++) |
geonnave | 0:ecd06432fb4b | 623 | authorize_permissions_expand(req_e, perms, 6, g); |
geonnave | 0:ecd06432fb4b | 624 | t.stop(); |
geonnave | 0:ecd06432fb4b | 625 | pc.printf("The time taken to authorize 1 request against 6 policies, %d times, was %f ms\n", runs, t.read() * 1000); |
geonnave | 0:ecd06432fb4b | 626 | printf("> The time taken to authorize 1 request against 6 policies, %d times, was %f ms\n", runs, t.read() * 1000); |
geonnave | 0:ecd06432fb4b | 627 | |
geonnave | 0:ecd06432fb4b | 628 | t.start(); |
geonnave | 0:ecd06432fb4b | 629 | authorize_permissions_expand(req_e, many_perms, n_perms, g); |
geonnave | 0:ecd06432fb4b | 630 | t.stop(); |
geonnave | 0:ecd06432fb4b | 631 | pc.printf("The time taken to authorize 1 request against %d policies was %f ms\n", n_perms, t.read() * 1000); |
geonnave | 0:ecd06432fb4b | 632 | printf("> The time taken to authorize 1 request against %d policies was %f ms\n", n_perms, t.read() * 1000); |
geonnave | 0:ecd06432fb4b | 633 | #elif defined(ESP32) |
geonnave | 0:ecd06432fb4b | 634 | unsigned long startTime, endTime; |
geonnave | 0:ecd06432fb4b | 635 | startTime = millis(); |
geonnave | 0:ecd06432fb4b | 636 | for (int i = 0; i < runs; i++) |
geonnave | 0:ecd06432fb4b | 637 | authorize_permissions_expand(req_e, perms, 6, g); |
geonnave | 0:ecd06432fb4b | 638 | endTime = millis(); |
geonnave | 0:ecd06432fb4b | 639 | Serial.print("The time taken to authorize 1 request against 6 policies, "); |
geonnave | 0:ecd06432fb4b | 640 | Serial.print(runs); |
geonnave | 0:ecd06432fb4b | 641 | Serial.print(" times, was "); |
geonnave | 0:ecd06432fb4b | 642 | Serial.print(endTime - startTime); |
geonnave | 0:ecd06432fb4b | 643 | Serial.println(" ms"); |
geonnave | 0:ecd06432fb4b | 644 | |
geonnave | 0:ecd06432fb4b | 645 | startTime = millis(); |
geonnave | 0:ecd06432fb4b | 646 | authorize_permissions_expand(req_e, many_perms, n_perms, g); |
geonnave | 0:ecd06432fb4b | 647 | endTime = millis(); |
geonnave | 0:ecd06432fb4b | 648 | Serial.print("The time taken to authorize 1 request against "); |
geonnave | 0:ecd06432fb4b | 649 | Serial.print(n_perms); |
geonnave | 0:ecd06432fb4b | 650 | Serial.print(" policies was "); |
geonnave | 0:ecd06432fb4b | 651 | Serial.print(endTime - startTime); |
geonnave | 0:ecd06432fb4b | 652 | Serial.println(" ms"); |
geonnave | 0:ecd06432fb4b | 653 | #elif defined(__unix__) |
geonnave | 0:ecd06432fb4b | 654 | #include <time.h> |
geonnave | 0:ecd06432fb4b | 655 | clock_t t; |
geonnave | 0:ecd06432fb4b | 656 | t = clock(); |
geonnave | 0:ecd06432fb4b | 657 | double elapsed; |
geonnave | 0:ecd06432fb4b | 658 | for (int i = 0; i < runs; i++) |
geonnave | 0:ecd06432fb4b | 659 | authorize_permissions_expand(req_e, perms, 6, g); |
geonnave | 0:ecd06432fb4b | 660 | t = clock() - t; |
geonnave | 0:ecd06432fb4b | 661 | elapsed = ((double) t) / CLOCKS_PER_SEC; |
geonnave | 0:ecd06432fb4b | 662 | printf("The time taken to authorize 1 request against 6 policies, %d times, was %f ms\n", runs, elapsed * 1000); |
geonnave | 0:ecd06432fb4b | 663 | |
geonnave | 0:ecd06432fb4b | 664 | t = clock(); |
geonnave | 0:ecd06432fb4b | 665 | authorize_permissions_expand(req_e, many_perms, n_perms, g); |
geonnave | 0:ecd06432fb4b | 666 | t = clock() - t; |
geonnave | 0:ecd06432fb4b | 667 | elapsed = ((double) t) / CLOCKS_PER_SEC; |
geonnave | 0:ecd06432fb4b | 668 | printf("The time taken to authorize 1 request against %d policies was %f ms\n", n_perms, elapsed * 1000); |
geonnave | 0:ecd06432fb4b | 669 | #endif |
geonnave | 0:ecd06432fb4b | 670 | |
geonnave | 0:ecd06432fb4b | 671 | free(many_perms); |
geonnave | 0:ecd06432fb4b | 672 | } |