[7f25a67f] | 1 | #include <assert.h> |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | #include <stdio.h> |
---|
| 4 | #include <string.h> |
---|
| 5 | |
---|
[0bf0e67] | 6 | #include "abac.h" |
---|
[9a411d7] | 7 | #include "abac_list.h" |
---|
[3c251d0] | 8 | #include "abac_util.h" |
---|
[7f25a67f] | 9 | |
---|
| 10 | // typedef'd in role.h |
---|
[1743825] | 11 | struct _abac_role_t { |
---|
[7f25a67f] | 12 | char *principal; |
---|
| 13 | char *linked_role; |
---|
[9a411d7] | 14 | char *role_name; |
---|
[7e05a2f] | 15 | |
---|
| 16 | char *string; |
---|
[d4cbf71] | 17 | char *linked; |
---|
[fbb591e] | 18 | |
---|
[9a411d7] | 19 | abac_list_t *prereqs; |
---|
| 20 | |
---|
[fbb591e] | 21 | int refcount; |
---|
[7f25a67f] | 22 | }; |
---|
| 23 | |
---|
| 24 | /** |
---|
| 25 | * Create a new principal and initialize it. |
---|
| 26 | */ |
---|
[dcc1a8e] | 27 | abac_role_t *abac_role_principal_new(char *principal) { |
---|
[7f25a67f] | 28 | assert(principal != NULL); |
---|
| 29 | |
---|
[1743825] | 30 | abac_role_t *role; |
---|
[7f25a67f] | 31 | |
---|
| 32 | if (strlen(principal) == 0) |
---|
| 33 | return NULL; |
---|
| 34 | |
---|
[3c251d0] | 35 | role = abac_xmalloc(sizeof(abac_role_t)); |
---|
[7f25a67f] | 36 | |
---|
[3c251d0] | 37 | role->principal = abac_xstrdup(principal); |
---|
[9a411d7] | 38 | role->role_name = NULL; |
---|
[7f25a67f] | 39 | role->linked_role = NULL; |
---|
| 40 | |
---|
[3c251d0] | 41 | role->string = abac_xstrdup(principal); |
---|
[d4cbf71] | 42 | role->linked = NULL; |
---|
[9a411d7] | 43 | role->prereqs = NULL; |
---|
[7e05a2f] | 44 | |
---|
[fbb591e] | 45 | role->refcount = 1; |
---|
| 46 | |
---|
[7f25a67f] | 47 | return role; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * Create a new role and initialize it. |
---|
| 52 | */ |
---|
[9a411d7] | 53 | abac_role_t *abac_role_role_new(char *principal, char *role_name) { |
---|
[7f25a67f] | 54 | assert(principal != NULL); |
---|
[9a411d7] | 55 | assert(role_name != NULL); |
---|
[7f25a67f] | 56 | |
---|
[1743825] | 57 | abac_role_t *role; |
---|
[7f25a67f] | 58 | |
---|
[9a411d7] | 59 | if (strlen(principal) == 0 || strlen(role_name) == 0) |
---|
[7f25a67f] | 60 | return NULL; |
---|
| 61 | |
---|
[3c251d0] | 62 | role = abac_xmalloc(sizeof(abac_role_t)); |
---|
[7f25a67f] | 63 | |
---|
[3c251d0] | 64 | role->principal = abac_xstrdup(principal); |
---|
[9a411d7] | 65 | role->role_name = abac_xstrdup(role_name); |
---|
[7f25a67f] | 66 | role->linked_role = NULL; |
---|
| 67 | |
---|
[7e05a2f] | 68 | int prin_len = strlen(principal); |
---|
[9a411d7] | 69 | int role_len = strlen(role_name); |
---|
[7e05a2f] | 70 | |
---|
[9a411d7] | 71 | role->string = abac_xmalloc(prin_len + 1 + role_len + 1); |
---|
[7e05a2f] | 72 | memcpy(role->string, principal, prin_len); |
---|
| 73 | role->string[prin_len] = '.'; |
---|
[9a411d7] | 74 | memcpy(role->string + prin_len + 1, role_name, role_len); |
---|
| 75 | role->string[prin_len + 1 + role_len] = 0; |
---|
[7e05a2f] | 76 | |
---|
[d4cbf71] | 77 | role->linked = NULL; |
---|
[9a411d7] | 78 | role->prereqs = NULL; |
---|
[d4cbf71] | 79 | |
---|
[fbb591e] | 80 | role->refcount = 1; |
---|
| 81 | |
---|
[7f25a67f] | 82 | return role; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | * Created a new linking role and initialize it. |
---|
| 87 | */ |
---|
[9a411d7] | 88 | abac_role_t *abac_role_linking_new(char *principal, char *linked, char *role_name) { |
---|
[7f25a67f] | 89 | assert(principal != NULL); |
---|
| 90 | assert(linked != NULL); |
---|
[9a411d7] | 91 | assert(role_name != NULL); |
---|
[7f25a67f] | 92 | |
---|
[1743825] | 93 | abac_role_t *role; |
---|
[7f25a67f] | 94 | |
---|
[9a411d7] | 95 | if (strlen(principal) == 0 || strlen(linked) == 0 || strlen(role_name) == 0) |
---|
[7f25a67f] | 96 | return NULL; |
---|
| 97 | |
---|
[3c251d0] | 98 | role = abac_xmalloc(sizeof(abac_role_t)); |
---|
[7f25a67f] | 99 | |
---|
[3c251d0] | 100 | role->principal = abac_xstrdup(principal); |
---|
| 101 | role->linked_role = abac_xstrdup(linked); |
---|
[9a411d7] | 102 | role->role_name = abac_xstrdup(role_name); |
---|
[7f25a67f] | 103 | |
---|
[7e05a2f] | 104 | int prin_len = strlen(principal); |
---|
| 105 | int link_len = strlen(linked); |
---|
[9a411d7] | 106 | int role_len = strlen(role_name); |
---|
[7e05a2f] | 107 | |
---|
[9a411d7] | 108 | role->string = abac_xmalloc(prin_len + 1 + link_len + 1 + role_len + 1); |
---|
[7e05a2f] | 109 | |
---|
| 110 | memcpy(role->string, principal, prin_len); |
---|
| 111 | role->string[prin_len] = '.'; |
---|
| 112 | memcpy(role->string + prin_len + 1, linked, link_len); |
---|
[d4cbf71] | 113 | role->string[prin_len + 1 + link_len] = 0; |
---|
| 114 | |
---|
| 115 | // hack: linked role is first two parts of full string |
---|
[3c251d0] | 116 | role->linked = abac_xstrdup(role->string); |
---|
[d4cbf71] | 117 | |
---|
[7e05a2f] | 118 | role->string[prin_len + 1 + link_len] = '.'; |
---|
[9a411d7] | 119 | memcpy(role->string + prin_len + 1 + link_len + 1, role_name, role_len); |
---|
| 120 | role->string[prin_len + 1 + link_len + 1 + role_len] = 0; |
---|
| 121 | |
---|
| 122 | role->prereqs = NULL; |
---|
| 123 | |
---|
| 124 | role->refcount = 1; |
---|
| 125 | |
---|
| 126 | return role; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | /** |
---|
| 130 | * Create an intersection role. |
---|
| 131 | */ |
---|
| 132 | abac_role_t *abac_role_intersection_new(char *name, abac_list_t *prereqs) { |
---|
| 133 | abac_role_t *role = abac_xmalloc(sizeof(abac_role_t)); |
---|
| 134 | |
---|
| 135 | role->principal = role->linked_role = role->role_name = NULL; |
---|
| 136 | role->linked = NULL; |
---|
| 137 | |
---|
| 138 | role->string = abac_xstrdup(name); |
---|
| 139 | role->prereqs = prereqs; |
---|
[7e05a2f] | 140 | |
---|
[fbb591e] | 141 | role->refcount = 1; |
---|
| 142 | |
---|
[7f25a67f] | 143 | return role; |
---|
| 144 | } |
---|
| 145 | |
---|
[c66e07c] | 146 | /** |
---|
[fbb591e] | 147 | * Decrease a role's reference count, freeing it when it reaches 0. |
---|
[c66e07c] | 148 | */ |
---|
[dcc1a8e] | 149 | void abac_role_free(abac_role_t *role) { |
---|
[c66e07c] | 150 | if (role == NULL) |
---|
| 151 | return; |
---|
| 152 | |
---|
[fbb591e] | 153 | --role->refcount; |
---|
| 154 | if (role->refcount > 0) |
---|
| 155 | return; |
---|
| 156 | |
---|
[c66e07c] | 157 | free(role->principal); |
---|
| 158 | free(role->linked_role); |
---|
[9a411d7] | 159 | free(role->role_name); |
---|
[c66e07c] | 160 | |
---|
[7e05a2f] | 161 | free(role->string); |
---|
[be963dc] | 162 | free(role->linked); |
---|
[7e05a2f] | 163 | |
---|
[9a411d7] | 164 | if (role->prereqs != NULL) { |
---|
| 165 | abac_role_t *cur; |
---|
| 166 | abac_list_foreach(role->prereqs, cur, |
---|
| 167 | abac_role_free(cur); |
---|
| 168 | ); |
---|
| 169 | abac_list_free(role->prereqs); |
---|
| 170 | } |
---|
| 171 | |
---|
[c66e07c] | 172 | free(role); |
---|
| 173 | } |
---|
| 174 | |
---|
[7f25a67f] | 175 | /** |
---|
[9a411d7] | 176 | * Create a non-intersecting role from a string. Handles principals, roles, |
---|
| 177 | * and linking roles. |
---|
[7f25a67f] | 178 | */ |
---|
[9a411d7] | 179 | static abac_role_t *_abac_single_role_from_string(char *string) { |
---|
[7f25a67f] | 180 | int num_dots = 0; |
---|
| 181 | char *dot = string; |
---|
[9a411d7] | 182 | abac_role_t *ret = NULL; |
---|
[7f25a67f] | 183 | |
---|
| 184 | // count the dots |
---|
| 185 | while ((dot = strchr(dot, '.')) != NULL) { |
---|
| 186 | ++num_dots; |
---|
| 187 | ++dot; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | // no dots: easy case, principal |
---|
| 191 | if (num_dots == 0) |
---|
[9a411d7] | 192 | ret = abac_role_principal_new(string); |
---|
[7f25a67f] | 193 | |
---|
| 194 | // a role has exactly 1 dot |
---|
[9a411d7] | 195 | else if (num_dots == 1) { |
---|
[7f25a67f] | 196 | char *principal = string; |
---|
| 197 | |
---|
| 198 | // terminate the principal part |
---|
| 199 | dot = strchr(principal, '.'); |
---|
| 200 | *dot = 0; |
---|
| 201 | |
---|
| 202 | // role name comes after the dot |
---|
[9a411d7] | 203 | char *role_name = dot + 1; |
---|
[7f25a67f] | 204 | |
---|
| 205 | // create the role (if possible) |
---|
[9a411d7] | 206 | ret = abac_role_role_new(string, role_name); |
---|
[7f25a67f] | 207 | } |
---|
| 208 | |
---|
| 209 | // a linked role has 2 dots |
---|
| 210 | else if (num_dots == 2) { |
---|
| 211 | char *principal = string; |
---|
| 212 | |
---|
| 213 | // terminate the principal part |
---|
| 214 | dot = strchr(principal, '.'); |
---|
| 215 | *dot = 0; |
---|
| 216 | |
---|
| 217 | // linked name is next, terminate it |
---|
| 218 | char *linked = dot + 1; |
---|
| 219 | dot = strchr(linked, '.'); |
---|
| 220 | *dot = 0; |
---|
| 221 | |
---|
| 222 | // role name is last, already terminated |
---|
[9a411d7] | 223 | char *role_name = dot + 1; |
---|
[7f25a67f] | 224 | |
---|
[9a411d7] | 225 | ret = abac_role_linking_new(principal, linked, role_name); |
---|
[7f25a67f] | 226 | } |
---|
| 227 | |
---|
| 228 | // more than two dots: return NULL |
---|
| 229 | |
---|
[9a411d7] | 230 | return ret; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | /** |
---|
| 234 | * Create a role from a string. Handles intersecting and normal roles. |
---|
| 235 | */ |
---|
| 236 | abac_role_t *abac_role_from_string(char *string) { |
---|
| 237 | abac_list_t *prereqs = NULL; |
---|
| 238 | abac_role_t *ret = NULL, *role; |
---|
| 239 | char *roles[256]; |
---|
| 240 | int num_roles, i; |
---|
| 241 | |
---|
| 242 | char *original = string; |
---|
| 243 | |
---|
| 244 | // make a copy so we can mess with it |
---|
| 245 | string = abac_xstrdup(string); |
---|
| 246 | |
---|
| 247 | // split the string (in case of an intersection num_roles > 1) |
---|
| 248 | abac_split(string, " & ", roles, &num_roles); |
---|
| 249 | |
---|
| 250 | // normal role: |
---|
| 251 | if (num_roles == 1) |
---|
| 252 | ret = _abac_single_role_from_string(string); |
---|
[7f25a67f] | 253 | |
---|
[9a411d7] | 254 | else { |
---|
| 255 | prereqs = abac_list_new(); |
---|
| 256 | |
---|
| 257 | for (i = 0; i < num_roles; ++i) { |
---|
| 258 | // make sure the tail role is valid |
---|
| 259 | role = abac_role_from_string(roles[i]); |
---|
| 260 | if (role == NULL) |
---|
| 261 | goto error; |
---|
| 262 | |
---|
| 263 | abac_list_add(prereqs, role); |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | ret = abac_role_intersection_new(original, prereqs); |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | free(string); |
---|
[7f25a67f] | 270 | return ret; |
---|
[9a411d7] | 271 | |
---|
| 272 | error: |
---|
| 273 | if (prereqs != NULL) { |
---|
| 274 | abac_list_foreach(prereqs, role, |
---|
| 275 | abac_role_free(role); |
---|
| 276 | ); |
---|
| 277 | abac_list_free(prereqs); |
---|
| 278 | } |
---|
| 279 | free(string); |
---|
| 280 | |
---|
| 281 | return NULL; |
---|
[7f25a67f] | 282 | } |
---|
| 283 | |
---|
[9a411d7] | 284 | |
---|
[ea401bc] | 285 | /** |
---|
[fbb591e] | 286 | * Increase a role's reference count. |
---|
[ea401bc] | 287 | */ |
---|
[dcc1a8e] | 288 | abac_role_t *abac_role_dup(abac_role_t *role) { |
---|
[fbb591e] | 289 | assert(role != NULL); |
---|
[ea401bc] | 290 | |
---|
[fbb591e] | 291 | ++role->refcount; |
---|
| 292 | return role; |
---|
[ea401bc] | 293 | } |
---|
| 294 | |
---|
[7f25a67f] | 295 | /** |
---|
| 296 | * True if a role is a principal. |
---|
| 297 | */ |
---|
[dcc1a8e] | 298 | int abac_role_is_principal(abac_role_t *role) { |
---|
[7f25a67f] | 299 | assert(role != NULL); |
---|
[9a411d7] | 300 | return role->role_name == NULL && role->linked_role == NULL && role->prereqs == NULL; |
---|
[7f25a67f] | 301 | } |
---|
| 302 | |
---|
| 303 | /** |
---|
| 304 | * True if a role is a role. |
---|
| 305 | */ |
---|
[dcc1a8e] | 306 | int abac_role_is_role(abac_role_t *role) { |
---|
[7f25a67f] | 307 | assert(role != NULL); |
---|
[9a411d7] | 308 | return role->role_name != NULL && role->linked_role == NULL && role->prereqs == NULL; |
---|
[7f25a67f] | 309 | } |
---|
| 310 | |
---|
| 311 | /** |
---|
| 312 | * True if a role is a linked role. |
---|
| 313 | */ |
---|
[dcc1a8e] | 314 | int abac_role_is_linking(abac_role_t *role) { |
---|
[7f25a67f] | 315 | assert(role != NULL); |
---|
| 316 | return role->linked_role != NULL; |
---|
| 317 | } |
---|
| 318 | |
---|
[9a411d7] | 319 | /** |
---|
| 320 | * True if a role is an intersection. |
---|
| 321 | */ |
---|
| 322 | int abac_role_is_intersection(abac_role_t *role) { |
---|
| 323 | assert(role != NULL); |
---|
| 324 | return role->prereqs != NULL; |
---|
| 325 | } |
---|
| 326 | |
---|
[7e05a2f] | 327 | /** |
---|
| 328 | * Returns the string representation of the role. |
---|
| 329 | */ |
---|
[dcc1a8e] | 330 | char *abac_role_string(abac_role_t *role) { |
---|
[7e05a2f] | 331 | assert(role != NULL); |
---|
| 332 | return role->string; |
---|
[7f25a67f] | 333 | } |
---|
[d4cbf71] | 334 | |
---|
[ebde9dd] | 335 | /** |
---|
| 336 | * Returns the name of a role. If the role is A.r1 then return r1. If the role |
---|
| 337 | * is A.r1.r2 then return r2. |
---|
| 338 | */ |
---|
[dcc1a8e] | 339 | char *abac_role_role_name(abac_role_t *role) { |
---|
[ebde9dd] | 340 | assert(role != NULL); |
---|
[9a411d7] | 341 | return role->role_name; |
---|
[ebde9dd] | 342 | } |
---|
| 343 | |
---|
[d4cbf71] | 344 | /** |
---|
| 345 | * Returns the linked part of a linking role. For instance, if the role is |
---|
| 346 | * A.r1.r2, this returns A.r1. |
---|
| 347 | */ |
---|
[dcc1a8e] | 348 | char *abac_role_linked_role(abac_role_t *role) { |
---|
[d4cbf71] | 349 | assert(role != NULL); |
---|
| 350 | return role->linked; |
---|
| 351 | } |
---|
[c03fe9b] | 352 | |
---|
| 353 | /** |
---|
| 354 | * Returns the principal part of a role. The stuff before the first dot. |
---|
| 355 | */ |
---|
[dcc1a8e] | 356 | char *abac_role_principal(abac_role_t *role) { |
---|
[c03fe9b] | 357 | assert(role != NULL); |
---|
| 358 | return role->principal; |
---|
| 359 | } |
---|
[85f33fd] | 360 | |
---|
[9a411d7] | 361 | /** |
---|
| 362 | * Returns the prereqs of an intersection. |
---|
| 363 | */ |
---|
| 364 | abac_list_t *abac_role_prereqs(abac_role_t *role) { |
---|
| 365 | assert(role != NULL); |
---|
| 366 | return role->prereqs; |
---|
| 367 | } |
---|
| 368 | |
---|
[85f33fd] | 369 | /** |
---|
| 370 | * Build an attribute key from head and tail roles. Static. |
---|
| 371 | */ |
---|
| 372 | #define ROLE_SEPARATOR " <- " |
---|
[dcc1a8e] | 373 | char *abac_role_attr_key(abac_role_t *head_role, abac_role_t *tail_role) { |
---|
| 374 | char *head = abac_role_string(head_role); |
---|
[85f33fd] | 375 | int head_len = strlen(head); |
---|
| 376 | |
---|
[dcc1a8e] | 377 | char *tail = abac_role_string(tail_role); |
---|
[85f33fd] | 378 | int tail_len = strlen(tail); |
---|
| 379 | |
---|
| 380 | int sep_len = sizeof(ROLE_SEPARATOR) - 1; |
---|
| 381 | |
---|
| 382 | // "head <- tail" |
---|
[3c251d0] | 383 | char *ret = abac_xmalloc(head_len + tail_len + sep_len + 1); |
---|
[85f33fd] | 384 | memcpy(ret, head, head_len); |
---|
| 385 | memcpy(ret + head_len, ROLE_SEPARATOR, sep_len); |
---|
| 386 | memcpy(ret + head_len + sep_len, tail, tail_len); |
---|
| 387 | ret[head_len + sep_len + tail_len] = 0; |
---|
| 388 | |
---|
| 389 | return ret; |
---|
| 390 | } |
---|