[8bd77b5] | 1 | |
---|
| 2 | /*********************************************************** |
---|
| 3 | abac_term.c |
---|
| 4 | |
---|
| 5 | dterm=data term of rt1 |
---|
| 6 | term is a kind of dterm |
---|
| 7 | ***********************************************************/ |
---|
| 8 | #include <assert.h> |
---|
| 9 | #include <stdlib.h> |
---|
| 10 | #include <stdio.h> |
---|
| 11 | #include <string.h> |
---|
| 12 | #include <ctype.h> |
---|
| 13 | #include <time.h> |
---|
| 14 | |
---|
| 15 | #include "abac_internal.h" |
---|
| 16 | #include "abac_util.h" |
---|
| 17 | |
---|
| 18 | static int debug=0; |
---|
| 19 | |
---|
[440ba20] | 20 | /* itemtype is |
---|
| 21 | e_ITEM_MIN=1, |
---|
| 22 | e_ITEM_MAX=2, |
---|
| 23 | e_ITEM_TARGET=3, |
---|
| 24 | [a..b], [a..], [..b], [a],[a,b],[many a] |
---|
| 25 | */ |
---|
| 26 | struct _abac_item_t { |
---|
| 27 | int itemtype; |
---|
| 28 | char *val; |
---|
| 29 | }; |
---|
| 30 | |
---|
| 31 | /* condtype is |
---|
| 32 | e_COND_ROLE = 1, |
---|
| 33 | e_COND_OSET = 2, |
---|
| 34 | e_COND_RANGE = 3 |
---|
| 35 | vartype is one of termtype |
---|
| 36 | string will contain either |
---|
| 37 | the range literal (static constraint) |
---|
| 38 | or |
---|
| 39 | the oset/role prologized string (dynamic constraint) |
---|
| 40 | it is set if this condition has been pre-processed for codegen |
---|
| 41 | of_aspect ptr will only be set only if it is a dynamic |
---|
| 42 | constraint |
---|
| 43 | range_list is set only if needed |
---|
| 44 | */ |
---|
[8bd77b5] | 45 | struct _abac_condition_t { |
---|
[440ba20] | 46 | int condtype; |
---|
| 47 | int vartype; |
---|
[8bd77b5] | 48 | char *string; |
---|
[440ba20] | 49 | abac_list_t *range_list; |
---|
[8bd77b5] | 50 | abac_aspect_t *of_aspect; |
---|
| 51 | }; |
---|
| 52 | |
---|
[d037f54] | 53 | /* termtype_t/name pair for the data terms |
---|
| 54 | idtype_t/name/p_name pair for the named principal term |
---|
| 55 | */ |
---|
[8bd77b5] | 56 | struct _abac_term_t { |
---|
| 57 | int type; |
---|
| 58 | char *name; |
---|
[d037f54] | 59 | int isnamed; |
---|
| 60 | char *p_name; |
---|
| 61 | char *cn; //XXX ?? might not need this at all |
---|
[8bd77b5] | 62 | abac_condition_t *constraint; |
---|
| 63 | }; |
---|
| 64 | |
---|
| 65 | /* new, free, add one of terms */ |
---|
| 66 | struct _abac_param_list_t { |
---|
| 67 | abac_list_t *list; |
---|
| 68 | }; |
---|
| 69 | |
---|
| 70 | /******************************************************************/ |
---|
[440ba20] | 71 | char *abac_condtype_string(int i) |
---|
[8bd77b5] | 72 | { |
---|
| 73 | if(i>_condname_cnt) |
---|
[440ba20] | 74 | panic("abac_condtype_string: went out of range on condname"); |
---|
[d037f54] | 75 | return (char *) _condname[i]; |
---|
[8bd77b5] | 76 | } |
---|
[440ba20] | 77 | char *abac_termtype_string(int i) |
---|
| 78 | { |
---|
| 79 | if(i>_termname_cnt) |
---|
| 80 | panic("abac_termtype_string: went out of range on termname"); |
---|
| 81 | return (char *) _termname[i]; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | int abac_verify_term_type(char *type) { |
---|
| 85 | int i; |
---|
[8bd77b5] | 86 | |
---|
[440ba20] | 87 | if (type == NULL) |
---|
| 88 | panic("abac_verify_term_type: fail with NULL type\n"); |
---|
| 89 | |
---|
| 90 | for (i = 1; i <= _termname_cnt ; i++) |
---|
| 91 | if(strcmp(type,_termname[i])==0) |
---|
| 92 | return i; |
---|
| 93 | |
---|
| 94 | panic("abac_verify_term_type: fail with unfounded type\n"); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | int abac_verify_item_type(char *type) { |
---|
| 98 | int i; |
---|
| 99 | |
---|
| 100 | if (type == NULL) |
---|
| 101 | panic("abac_verify_item_type: fail with NULL type\n"); |
---|
| 102 | |
---|
| 103 | for (i = 1; i <= _itemname_cnt ; i++) |
---|
| 104 | if(strcmp(type,_itemname[i])==0) |
---|
| 105 | return i; |
---|
| 106 | panic("abac_verify_item_type: fail with unfounded type\n"); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | char *abac_range_string(abac_list_t *ptr) |
---|
| 110 | { |
---|
| 111 | assert(ptr); |
---|
| 112 | char *tmp=NULL; |
---|
| 113 | char *min=NULL; |
---|
| 114 | char *max=NULL; |
---|
| 115 | char *val=NULL; |
---|
| 116 | int type; |
---|
| 117 | |
---|
| 118 | abac_item_t *cur; |
---|
| 119 | assert(ptr); |
---|
| 120 | abac_list_foreach(ptr, cur, |
---|
| 121 | type=cur->itemtype; |
---|
| 122 | switch (type) { |
---|
| 123 | case e_ITEM_MIN: |
---|
| 124 | min=abac_xstrdup(cur->val); |
---|
| 125 | break; |
---|
| 126 | case e_ITEM_MAX: |
---|
| 127 | max=abac_xstrdup(cur->val); |
---|
| 128 | break; |
---|
| 129 | case e_ITEM_TARGET: |
---|
| 130 | if(val) |
---|
| 131 | asprintf(&val,"%s,%s",val,cur->val); |
---|
| 132 | else val=abac_xstrdup(cur->val); |
---|
| 133 | break; |
---|
| 134 | } |
---|
| 135 | ); |
---|
| 136 | if(max && min) { |
---|
| 137 | asprintf(&tmp,"[%s..%s]",min,max); |
---|
| 138 | free(max); |
---|
| 139 | free(min); |
---|
| 140 | return tmp; |
---|
| 141 | } |
---|
| 142 | if(max) { |
---|
| 143 | asprintf(&tmp,"[..%s]",max); |
---|
| 144 | free(max); |
---|
| 145 | return tmp; |
---|
| 146 | } |
---|
| 147 | if(min) { |
---|
| 148 | asprintf(&tmp,"[%s..]",min); |
---|
| 149 | free(min); |
---|
| 150 | return tmp; |
---|
| 151 | } |
---|
| 152 | if(val) { |
---|
| 153 | asprintf(&tmp,"[%s]",val); |
---|
| 154 | free(val); |
---|
| 155 | return tmp; |
---|
| 156 | } |
---|
| 157 | return NULL; |
---|
| 158 | } |
---|
| 159 | /******************************************************************/ |
---|
| 160 | abac_item_t *abac_item_new(char *itype, char *val) |
---|
| 161 | { |
---|
| 162 | abac_item_t *nptr= |
---|
| 163 | (abac_item_t *)abac_xmalloc(sizeof(abac_item_t)); |
---|
| 164 | nptr->itemtype=abac_verify_item_type(itype); |
---|
| 165 | nptr->val=abac_xstrdup(val); |
---|
| 166 | return nptr; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | void abac_item_free(abac_item_t *ptr) |
---|
| 170 | { |
---|
| 171 | assert(ptr); |
---|
| 172 | assert(ptr->val); |
---|
| 173 | free(ptr->val); |
---|
| 174 | free(ptr); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | abac_item_t *abac_item_dup(abac_item_t *ptr) |
---|
| 178 | { |
---|
| 179 | abac_item_t *nptr= |
---|
| 180 | (abac_item_t *)abac_xmalloc(sizeof(abac_item_t)); |
---|
| 181 | nptr->itemtype=ptr->itemtype; |
---|
| 182 | nptr->val=abac_xstrdup(ptr->val); |
---|
| 183 | return nptr; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | int abac_item_type(abac_item_t *ptr) |
---|
| 187 | { |
---|
| 188 | return ptr->itemtype; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | char *abac_item_val(abac_item_t *ptr) |
---|
| 192 | { |
---|
| 193 | return ptr->val; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | /******************************************************************/ |
---|
| 197 | static abac_condition_t *_abac_condition_new(int type, int vtype, |
---|
| 198 | char* condstr,void *cptr) |
---|
[8bd77b5] | 199 | { |
---|
| 200 | if(debug) { |
---|
| 201 | printf(" abac_condition_new: \n"); |
---|
[440ba20] | 202 | printf(" condtype is %d(%s)\n", type, abac_condtype_string(type)); |
---|
| 203 | printf(" vtype is %d(%s)\n", vtype, abac_termtype_string(type)); |
---|
[8bd77b5] | 204 | printf(" condstr is (%s)\n", condstr); |
---|
| 205 | if(cptr) printf(" yes on cptr\n"); |
---|
| 206 | else printf(" no on cptr\n"); |
---|
| 207 | } |
---|
| 208 | abac_condition_t *ptr= |
---|
| 209 | (abac_condition_t *)abac_xmalloc(sizeof(abac_condition_t)); |
---|
[440ba20] | 210 | ptr->condtype=type; |
---|
| 211 | ptr->vartype=vtype; |
---|
[8bd77b5] | 212 | ptr->string=abac_xstrdup(condstr); |
---|
[440ba20] | 213 | ptr->of_aspect=NULL; |
---|
| 214 | ptr->range_list=NULL; |
---|
| 215 | if(type==e_COND_RANGE) { |
---|
| 216 | ptr->range_list=(abac_list_t *)cptr; |
---|
| 217 | } else { |
---|
| 218 | if(cptr) ptr->of_aspect=abac_aspect_dup((abac_aspect_t *)cptr); |
---|
| 219 | } |
---|
[8bd77b5] | 220 | return ptr; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | void abac_condition_free(abac_condition_t *ptr) |
---|
| 224 | { |
---|
[440ba20] | 225 | switch(ptr->condtype) { |
---|
[8bd77b5] | 226 | case e_COND_OSET: |
---|
| 227 | case e_COND_ROLE: |
---|
| 228 | abac_aspect_free(ptr->of_aspect); |
---|
| 229 | break; |
---|
| 230 | case e_COND_RANGE: |
---|
| 231 | /* do nothing */ |
---|
| 232 | break; |
---|
| 233 | } |
---|
| 234 | if(ptr->string) free(ptr->string); |
---|
[440ba20] | 235 | if(ptr->range_list != NULL) { |
---|
| 236 | abac_item_t *cur; |
---|
| 237 | abac_list_foreach(ptr->range_list, cur, |
---|
| 238 | abac_item_free(cur); |
---|
| 239 | ); |
---|
| 240 | abac_list_free(ptr->range_list); |
---|
| 241 | } |
---|
[8bd77b5] | 242 | free(ptr); |
---|
| 243 | } |
---|
| 244 | |
---|
[440ba20] | 245 | abac_aspect_t *abac_condition_of_aspect(abac_condition_t *ptr) |
---|
| 246 | { |
---|
| 247 | return ptr->of_aspect; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | int abac_condition_is_range(abac_condition_t *ptr) |
---|
| 251 | { |
---|
| 252 | if(ptr->condtype==e_COND_RANGE) |
---|
| 253 | return 1; |
---|
| 254 | else return 0; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | /* called from backend, intended as stub for range constraint */ |
---|
| 258 | abac_condition_t *abac_condition_create(char *vtype) |
---|
[8bd77b5] | 259 | { |
---|
| 260 | abac_condition_t *nptr= |
---|
| 261 | (abac_condition_t *)abac_xmalloc(sizeof(abac_condition_t)); |
---|
[440ba20] | 262 | nptr->condtype=e_COND_RANGE; |
---|
| 263 | nptr->vartype=abac_verify_term_type(vtype); |
---|
| 264 | nptr->string=NULL; |
---|
| 265 | nptr->range_list=NULL; |
---|
[8bd77b5] | 266 | nptr->of_aspect=NULL; |
---|
| 267 | return nptr; |
---|
| 268 | } |
---|
| 269 | |
---|
[440ba20] | 270 | abac_condition_t *abac_condition_create_from_aspect(abac_aspect_t *ptr) |
---|
[8bd77b5] | 271 | { |
---|
| 272 | assert(ptr); |
---|
| 273 | abac_condition_t *nptr= |
---|
| 274 | (abac_condition_t *)abac_xmalloc(sizeof(abac_condition_t)); |
---|
| 275 | if(abac_aspect_is_role(ptr)) |
---|
[440ba20] | 276 | nptr->condtype=e_COND_ROLE; |
---|
| 277 | else nptr->condtype=e_COND_OSET; |
---|
| 278 | nptr->vartype=e_TERM_PRINCIPAL; |
---|
[8bd77b5] | 279 | nptr->string=NULL; |
---|
[440ba20] | 280 | nptr->range_list=NULL; |
---|
[8bd77b5] | 281 | nptr->of_aspect=abac_aspect_dup(ptr); |
---|
| 282 | return nptr; |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | /* made a copy */ |
---|
| 286 | abac_condition_t *abac_condition_dup(abac_condition_t *ptr) |
---|
| 287 | { |
---|
| 288 | abac_condition_t *nptr= |
---|
| 289 | (abac_condition_t *)abac_xmalloc(sizeof(abac_condition_t)); |
---|
[440ba20] | 290 | nptr->condtype=ptr->condtype; |
---|
| 291 | nptr->vartype=ptr->vartype; |
---|
[8bd77b5] | 292 | if(ptr->string) |
---|
| 293 | nptr->string=abac_xstrdup(ptr->string); |
---|
| 294 | else nptr->string=NULL; |
---|
| 295 | if(ptr->of_aspect) |
---|
| 296 | nptr->of_aspect=abac_aspect_dup(ptr->of_aspect); |
---|
| 297 | else nptr->of_aspect=NULL; |
---|
[440ba20] | 298 | |
---|
| 299 | if(ptr->range_list!=NULL) { |
---|
| 300 | nptr->range_list=abac_list_new(); |
---|
| 301 | abac_item_t *cur; |
---|
| 302 | abac_item_t *nitem; |
---|
| 303 | abac_list_foreach(ptr->range_list, cur, |
---|
| 304 | nitem=abac_item_dup(cur); |
---|
| 305 | abac_list_add(nptr->range_list,nitem); |
---|
| 306 | ); |
---|
| 307 | } else { |
---|
| 308 | nptr->range_list=NULL; |
---|
| 309 | } |
---|
[8bd77b5] | 310 | return nptr; |
---|
| 311 | } |
---|
| 312 | |
---|
[440ba20] | 313 | int abac_condition_vartype(abac_condition_t *ptr) |
---|
| 314 | { |
---|
| 315 | return ptr->vartype; |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | abac_list_t *abac_condition_range_list(abac_condition_t *ptr) |
---|
| 319 | { |
---|
| 320 | assert(ptr); |
---|
| 321 | return ptr->range_list; |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | |
---|
| 325 | int abac_condition_add_range_item(abac_condition_t *ptr, char *vtype, char* itype, char* val) |
---|
| 326 | { |
---|
| 327 | if(ptr->vartype != abac_verify_term_type(vtype)) |
---|
| 328 | panic("abac_condition_add_range_item: attempt to add range item that does not match with the constraint's type\n"); |
---|
| 329 | abac_item_t *nitem=abac_item_new(itype,val); |
---|
| 330 | if(ptr->range_list==NULL) |
---|
| 331 | ptr->range_list=abac_list_new(); |
---|
| 332 | abac_list_add(ptr->range_list,nitem); |
---|
| 333 | } |
---|
| 334 | |
---|
[8bd77b5] | 335 | char *abac_condition_string(abac_condition_t *ptr) |
---|
| 336 | { |
---|
| 337 | char *string=NULL; |
---|
[440ba20] | 338 | switch(ptr->condtype) { |
---|
[8bd77b5] | 339 | case e_COND_OSET: |
---|
| 340 | case e_COND_ROLE: |
---|
| 341 | string=abac_aspect_string_with_condition(ptr->of_aspect); |
---|
[440ba20] | 342 | return string; |
---|
[8bd77b5] | 343 | break; |
---|
| 344 | case e_COND_RANGE: |
---|
[440ba20] | 345 | if(ptr->string != NULL) { |
---|
| 346 | string=abac_xstrdup(ptr->string); |
---|
| 347 | return string; |
---|
| 348 | } |
---|
| 349 | if(ptr->range_list != NULL) { |
---|
| 350 | string=abac_range_string(ptr->range_list); |
---|
| 351 | return string; |
---|
| 352 | } |
---|
[8bd77b5] | 353 | break; |
---|
| 354 | } |
---|
| 355 | return string; |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | char *abac_condition_typed_string(abac_condition_t *ptr) |
---|
| 359 | { |
---|
| 360 | char *string=NULL; |
---|
[440ba20] | 361 | switch(ptr->condtype) { |
---|
[8bd77b5] | 362 | case e_COND_OSET: |
---|
| 363 | case e_COND_ROLE: |
---|
| 364 | string=abac_aspect_typed_string_with_condition(ptr->of_aspect); |
---|
[440ba20] | 365 | return string; |
---|
[8bd77b5] | 366 | break; |
---|
| 367 | case e_COND_RANGE: |
---|
[440ba20] | 368 | if(ptr->string != NULL) { |
---|
| 369 | string=abac_xstrdup(ptr->string); |
---|
| 370 | return string; |
---|
| 371 | } |
---|
| 372 | if(ptr->range_list != NULL) { |
---|
| 373 | string=abac_range_string(ptr->range_list); |
---|
| 374 | return string; |
---|
| 375 | } |
---|
[8bd77b5] | 376 | break; |
---|
| 377 | } |
---|
| 378 | return string; |
---|
| 379 | } |
---|
| 380 | |
---|
[440ba20] | 381 | int abac_condition_set_range_string(abac_condition_t *ptr) |
---|
| 382 | { |
---|
| 383 | char *string= abac_range_string(ptr->range_list); |
---|
| 384 | ptr->string=string; |
---|
| 385 | return 1; |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | int abac_condition_set_aspect_string(abac_condition_t *ptr, char *str) |
---|
| 389 | { |
---|
| 390 | ptr->string=abac_xstrdup(str); |
---|
| 391 | return 1; |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | int abac_condition_set_aspect_ptr(abac_condition_t *ptr, abac_aspect_t *aptr) |
---|
| 395 | { |
---|
| 396 | ptr->of_aspect=abac_aspect_dup(aptr); |
---|
| 397 | return 1; |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | void abac_condition_dump(abac_condition_t *ptr, char *offset) |
---|
| 401 | { |
---|
| 402 | printf("...constraint...\n"); |
---|
| 403 | printf("%s condtype: %d(%s)\n", |
---|
| 404 | offset, ptr->condtype,abac_condtype_string(ptr->condtype)); |
---|
| 405 | printf("%s vartype: %d(%s)\n", |
---|
| 406 | offset, ptr->vartype,abac_termtype_string(ptr->vartype)); |
---|
| 407 | if(ptr->string) |
---|
| 408 | printf("%s string: %s\n", offset, ptr->string); |
---|
| 409 | else printf("%s string: --\n", offset); |
---|
| 410 | if(ptr->range_list) |
---|
| 411 | printf("%s range_list: %s\n", |
---|
| 412 | offset, abac_range_string(ptr->range_list)); |
---|
| 413 | else printf("%s range_list: --\n", offset); |
---|
| 414 | if(ptr->of_aspect) { |
---|
| 415 | char *tmp=NULL; |
---|
| 416 | asprintf(&tmp," %s",offset); |
---|
| 417 | abac_aspect_dump(ptr->of_aspect,tmp); |
---|
| 418 | free(tmp); |
---|
| 419 | } else printf("%s of_aspect: --\n", offset); |
---|
| 420 | printf("................\n"); |
---|
| 421 | } |
---|
| 422 | |
---|
[8bd77b5] | 423 | /******************************************************************/ |
---|
[440ba20] | 424 | int abac_term_isvar(abac_term_t *term) |
---|
| 425 | { |
---|
| 426 | if(isupper(term->name[0])) return 1; |
---|
| 427 | else return 0; |
---|
| 428 | } |
---|
[8bd77b5] | 429 | |
---|
[d037f54] | 430 | int abac_term_isnamed(abac_term_t *term) |
---|
| 431 | { |
---|
| 432 | if(term->isnamed) return 1; |
---|
| 433 | else return 0; |
---|
| 434 | } |
---|
| 435 | |
---|
[8bd77b5] | 436 | char *abac_term_name(abac_term_t *term) |
---|
| 437 | { |
---|
[d037f54] | 438 | if(ABAC_IN_PROLOG && term->p_name) { |
---|
| 439 | return term->p_name; |
---|
| 440 | } |
---|
[8bd77b5] | 441 | return term->name; |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | char *abac_term_cn(abac_term_t *term) |
---|
| 445 | { |
---|
| 446 | return term->cn; |
---|
| 447 | } |
---|
| 448 | |
---|
| 449 | abac_condition_t *abac_term_constraint(abac_term_t *term) |
---|
| 450 | { |
---|
| 451 | return term->constraint; |
---|
| 452 | } |
---|
| 453 | |
---|
| 454 | char *abac_term_type_name(abac_term_t *term) |
---|
| 455 | { |
---|
[d037f54] | 456 | if(term->isnamed) |
---|
| 457 | return abac_idtype_string(term->type); |
---|
| 458 | return abac_termtype_string(term->type); |
---|
[8bd77b5] | 459 | } |
---|
| 460 | |
---|
| 461 | int abac_term_type(abac_term_t *term) |
---|
| 462 | { |
---|
| 463 | return term->type; |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | static bool _abac_termtype_is_time_type(int i) |
---|
| 467 | { |
---|
| 468 | if (i == e_TERM_TIME) |
---|
| 469 | return 1; |
---|
| 470 | return 0; |
---|
| 471 | } |
---|
| 472 | |
---|
| 473 | bool abac_term_is_time_type(abac_term_t *term) |
---|
| 474 | { |
---|
| 475 | return _abac_termtype_is_time_type(term->type); |
---|
| 476 | } |
---|
| 477 | |
---|
| 478 | static bool _abac_termtype_is_string_type(int i) |
---|
| 479 | { |
---|
| 480 | if (i == e_TERM_STRING) |
---|
| 481 | return 1; |
---|
| 482 | return 0; |
---|
| 483 | } |
---|
| 484 | |
---|
| 485 | bool abac_term_is_string_type(abac_term_t *term) |
---|
| 486 | { |
---|
| 487 | return _abac_termtype_is_string_type(term->type); |
---|
| 488 | } |
---|
| 489 | |
---|
| 490 | static bool _abac_termtype_is_integer_type(int i) |
---|
| 491 | { |
---|
| 492 | if (i == e_TERM_INTEGER) |
---|
| 493 | return 1; |
---|
| 494 | return 0; |
---|
| 495 | } |
---|
| 496 | |
---|
| 497 | bool abac_term_is_integer_type(abac_term_t *term) |
---|
| 498 | { |
---|
| 499 | return _abac_termtype_is_integer_type(term->type); |
---|
| 500 | } |
---|
| 501 | |
---|
| 502 | static bool _abac_termtype_is_urn_type(int i) |
---|
| 503 | { |
---|
| 504 | if (i == e_TERM_URN) |
---|
| 505 | return 1; |
---|
| 506 | return 0; |
---|
| 507 | } |
---|
| 508 | |
---|
| 509 | bool abac_term_is_urn_type(abac_term_t *term) |
---|
| 510 | { |
---|
| 511 | return _abac_termtype_is_urn_type(term->type); |
---|
| 512 | } |
---|
| 513 | |
---|
[440ba20] | 514 | bool abac_term_is_alpha(abac_term_t *term) |
---|
| 515 | { |
---|
| 516 | if(term->type == e_TERM_URN || |
---|
| 517 | term->type == e_TERM_STRING || |
---|
| 518 | term->type == e_TERM_BOOLEAN) |
---|
| 519 | return 1; |
---|
| 520 | return 0; |
---|
| 521 | } |
---|
[8bd77b5] | 522 | |
---|
[440ba20] | 523 | bool abac_term_is_numeric(abac_term_t *term) |
---|
| 524 | { |
---|
| 525 | if(term->type == e_TERM_INTEGER || |
---|
| 526 | term->type == e_TERM_FLOAT) |
---|
| 527 | return 1; |
---|
| 528 | return 0; |
---|
| 529 | } |
---|
[8bd77b5] | 530 | |
---|
[440ba20] | 531 | bool abac_term_is_time(abac_term_t *term) |
---|
| 532 | { |
---|
| 533 | if(term->type == e_TERM_TIME) |
---|
| 534 | return 1; |
---|
[8bd77b5] | 535 | return 0; |
---|
| 536 | } |
---|
| 537 | |
---|
| 538 | char *abac_term_to_time(char *string) |
---|
| 539 | { |
---|
| 540 | struct tm tm1; |
---|
| 541 | char buf[255]; |
---|
| 542 | |
---|
| 543 | strptime(string, "%Y%m%dT%H%M%S", &tm1); |
---|
| 544 | strftime(buf, sizeof(buf), "time(%Y,%m,%d,%H,%M,%S)", &tm1); |
---|
| 545 | return strdup(buf); |
---|
| 546 | } |
---|
| 547 | |
---|
[440ba20] | 548 | /* called from yy, cptr is either abac_aspect_t or abac_list_t */ |
---|
| 549 | abac_term_t *abac_term_new(int type, char *name, int isrange, char *cond, void *cptr) |
---|
[8bd77b5] | 550 | { |
---|
| 551 | if(debug) { |
---|
| 552 | printf("abac_term_new: \n"); |
---|
[d037f54] | 553 | printf(" type is %d(%s)\n", type, abac_termtype_string(type)); |
---|
[8bd77b5] | 554 | printf(" name is (%s)\n", name); |
---|
| 555 | if(cond) |
---|
| 556 | printf(" cond is %s\n", cond); |
---|
| 557 | else printf(" no cond \n"); |
---|
[440ba20] | 558 | printf(" isrange is %d\n", isrange); |
---|
| 559 | if(cptr) { |
---|
| 560 | if(isrange) printf(" cptr is (%s)\n", |
---|
| 561 | abac_range_string((abac_list_t *)cptr)); |
---|
| 562 | else |
---|
| 563 | printf(" cptr is (%s)\n", |
---|
| 564 | abac_aspect_string_with_condition((abac_aspect_t *)cptr)); |
---|
| 565 | } else printf(" there is no cptr\n"); |
---|
[8bd77b5] | 566 | } |
---|
| 567 | abac_condition_t *constraint=NULL; |
---|
[440ba20] | 568 | if (cptr) { |
---|
| 569 | if(isrange) { |
---|
| 570 | constraint=_abac_condition_new(e_COND_RANGE,type,cond,cptr); |
---|
| 571 | } else { |
---|
| 572 | if(type==e_TERM_PRINCIPAL) |
---|
| 573 | constraint=_abac_condition_new(e_COND_ROLE,type,cond,cptr); |
---|
| 574 | else |
---|
| 575 | constraint=_abac_condition_new(e_COND_OSET,type,cond,cptr); |
---|
[8bd77b5] | 576 | } |
---|
| 577 | } |
---|
| 578 | abac_term_t *ptr=abac_xmalloc(sizeof(abac_term_t)); |
---|
[d037f54] | 579 | ptr->isnamed=0; |
---|
[8bd77b5] | 580 | ptr->type=type; |
---|
[d037f54] | 581 | ptr->p_name=NULL; |
---|
[8bd77b5] | 582 | /* reformat the term name if it is a time type */ |
---|
| 583 | if((!constraint) && _abac_termtype_is_time_type(type) && isdigit(name[0])) { |
---|
| 584 | char *tmp=abac_term_to_time(name); |
---|
| 585 | ptr->name=tmp; |
---|
| 586 | } else { |
---|
| 587 | ptr->name=abac_xstrdup(name); |
---|
| 588 | } |
---|
| 589 | ptr->constraint=constraint; |
---|
[d037f54] | 590 | ptr->cn=NULL; |
---|
[8bd77b5] | 591 | return ptr; |
---|
| 592 | } |
---|
| 593 | |
---|
| 594 | abac_term_t *abac_term_create(int type, char *name, abac_condition_t *cptr) |
---|
| 595 | { |
---|
| 596 | if(debug) { |
---|
| 597 | printf("abac_term_create: \n"); |
---|
[d037f54] | 598 | printf(" type is %d(%s)\n", type, abac_termtype_string(type)); |
---|
[8bd77b5] | 599 | printf(" name is (%s)\n", name); |
---|
| 600 | if(cptr) |
---|
| 601 | printf(" cptr is (%s)\n", abac_condition_typed_string(cptr)); |
---|
| 602 | else printf(" there is no cptr\n"); |
---|
| 603 | } |
---|
| 604 | |
---|
| 605 | abac_term_t *ptr=abac_xmalloc(sizeof(abac_term_t)); |
---|
[d037f54] | 606 | ptr->isnamed=0; |
---|
[8bd77b5] | 607 | ptr->type=type; |
---|
[d037f54] | 608 | ptr->p_name=NULL; |
---|
[8bd77b5] | 609 | /* reformat the term name if it is a time type */ |
---|
| 610 | if((!cptr) && _abac_termtype_is_time_type(type) && isdigit(name[0])) { |
---|
| 611 | char *tmp=abac_term_to_time(name); |
---|
| 612 | ptr->name=tmp; |
---|
| 613 | } else { |
---|
| 614 | ptr->name=abac_xstrdup(name); |
---|
| 615 | } |
---|
| 616 | ptr->constraint=(cptr==NULL)?NULL:abac_condition_dup(cptr); |
---|
[d037f54] | 617 | ptr->cn=NULL; |
---|
| 618 | return ptr; |
---|
| 619 | } |
---|
| 620 | |
---|
| 621 | /* called from yy, named principal type */ |
---|
| 622 | abac_term_t *abac_term_named_new(int idtype, char *name) |
---|
| 623 | { |
---|
| 624 | if(debug) { |
---|
| 625 | printf("abac_term_named_new: \n"); |
---|
| 626 | printf(" type is %d(%s)\n", idtype, abac_idtype_string(idtype)); |
---|
| 627 | printf(" name is (%s)\n", name); |
---|
| 628 | } |
---|
| 629 | abac_term_t *ptr=abac_xmalloc(sizeof(abac_term_t)); |
---|
| 630 | ptr->isnamed=1; |
---|
| 631 | ptr->type=idtype; |
---|
| 632 | ptr->name=abac_xstrdup(name); |
---|
| 633 | ptr->p_name=NULL; |
---|
| 634 | /* if ABAC_CN, no need to attach p */ |
---|
| 635 | if(!USE("ABAC_CN")) |
---|
| 636 | asprintf(&ptr->p_name,"p%s",name); |
---|
| 637 | ptr->constraint=NULL; |
---|
| 638 | ptr->cn=NULL; |
---|
| 639 | return ptr; |
---|
| 640 | } |
---|
| 641 | |
---|
| 642 | abac_term_t *abac_term_named_create(int idtype, char *name) |
---|
| 643 | { |
---|
| 644 | if(debug) { |
---|
| 645 | printf("abac_term_named_create: \n"); |
---|
| 646 | printf(" type is %d(%s)\n", idtype, abac_idtype_string(idtype)); |
---|
| 647 | printf(" name is (%s)\n", name); |
---|
| 648 | } |
---|
| 649 | |
---|
| 650 | abac_term_t *ptr=abac_xmalloc(sizeof(abac_term_t)); |
---|
| 651 | ptr->isnamed=1; |
---|
| 652 | ptr->type=idtype; |
---|
| 653 | ptr->name=abac_xstrdup(name); |
---|
| 654 | ptr->p_name=NULL; |
---|
| 655 | /* if ABAC_CN, no need to attach p */ |
---|
| 656 | if(!USE("ABAC_CN")) |
---|
| 657 | asprintf(&ptr->p_name,"p%s",name); |
---|
| 658 | ptr->constraint=NULL; |
---|
| 659 | ptr->cn=NULL; |
---|
[8bd77b5] | 660 | return ptr; |
---|
| 661 | } |
---|
| 662 | |
---|
| 663 | abac_term_t *abac_term_dup(abac_term_t *ptr) |
---|
| 664 | { |
---|
| 665 | assert(ptr); |
---|
| 666 | abac_term_t *nptr=abac_xmalloc(sizeof(abac_term_t)); |
---|
[d037f54] | 667 | nptr->isnamed=ptr->isnamed; |
---|
[8bd77b5] | 668 | nptr->type=ptr->type; |
---|
| 669 | nptr->name=abac_xstrdup(ptr->name); |
---|
[d037f54] | 670 | nptr->p_name=(ptr->p_name==NULL)?NULL:abac_xstrdup(ptr->p_name); |
---|
[8bd77b5] | 671 | if(ptr->constraint!=NULL) |
---|
| 672 | nptr->constraint=abac_condition_dup(ptr->constraint); |
---|
| 673 | else nptr->constraint=NULL; |
---|
[d037f54] | 674 | nptr->cn=(ptr->cn)==NULL?NULL:abac_xstrdup(ptr->cn); |
---|
| 675 | nptr->cn=abac_xstrdup(ptr->cn); |
---|
[8bd77b5] | 676 | return nptr; |
---|
| 677 | } |
---|
| 678 | |
---|
| 679 | void abac_term_free(abac_term_t *ptr) |
---|
| 680 | { |
---|
| 681 | if(ptr->constraint) { |
---|
| 682 | abac_condition_free(ptr->constraint); |
---|
| 683 | } |
---|
| 684 | free(ptr->name); |
---|
[d037f54] | 685 | if(ptr->p_name) free(ptr->p_name); |
---|
| 686 | if(ptr->cn) free(ptr->cn); |
---|
[8bd77b5] | 687 | free(ptr); |
---|
| 688 | } |
---|
| 689 | |
---|
| 690 | abac_term_t *abac_term_add_constraint(abac_term_t *ptr, abac_condition_t *cond) |
---|
| 691 | { |
---|
[440ba20] | 692 | /* make sure the types are matching */ |
---|
| 693 | if(abac_condition_vartype(cond) != abac_term_type(ptr)) { |
---|
| 694 | panic("abac_term_add_constraint: mismatched constraint type with the dataterm\n"); |
---|
| 695 | return NULL; |
---|
| 696 | } |
---|
| 697 | |
---|
[8bd77b5] | 698 | if(ptr->constraint) { |
---|
| 699 | abac_condition_free(ptr->constraint); |
---|
| 700 | } |
---|
| 701 | ptr->constraint = abac_condition_dup(cond); |
---|
| 702 | return ptr; |
---|
| 703 | } |
---|
| 704 | |
---|
| 705 | /* name or name:cond */ |
---|
| 706 | static char *abac_term_string_with_condition(abac_term_t *ptr) |
---|
| 707 | { |
---|
| 708 | char *tmp=NULL; |
---|
| 709 | char *cond=NULL; |
---|
| 710 | char *name=abac_term_name(ptr); |
---|
| 711 | if(ptr->constraint) { |
---|
| 712 | cond=abac_condition_string(ptr->constraint); |
---|
| 713 | } |
---|
| 714 | if(cond) { |
---|
| 715 | asprintf(&tmp,"%s:%s", name, cond); |
---|
| 716 | free(cond); |
---|
| 717 | } else tmp=abac_xstrdup(name); |
---|
| 718 | return tmp; |
---|
| 719 | } |
---|
| 720 | |
---|
| 721 | /* [type:name] or [typed:name:cond] */ |
---|
| 722 | static char *abac_term_typed_string_with_condition(abac_term_t *ptr) |
---|
| 723 | { |
---|
| 724 | char *tmp=NULL; |
---|
| 725 | char *cond=NULL; |
---|
| 726 | char *name=abac_term_name(ptr); |
---|
| 727 | char *type=abac_term_type_name(ptr); |
---|
| 728 | if(ptr->constraint) { |
---|
| 729 | cond=abac_condition_typed_string(ptr->constraint); |
---|
| 730 | } |
---|
| 731 | if(cond) { |
---|
| 732 | asprintf(&tmp,"[%s:%s:%s]", type, name, cond); |
---|
| 733 | free(cond); |
---|
| 734 | } else |
---|
| 735 | asprintf(&tmp,"[%s:%s]", type, name); |
---|
| 736 | if(debug) printf("abac_term_typed_string_with_condition: (%s)\n",tmp); |
---|
| 737 | return tmp; |
---|
| 738 | } |
---|
| 739 | |
---|
| 740 | char *abac_term_string(abac_term_t *ptr) |
---|
| 741 | { |
---|
[d037f54] | 742 | return abac_xstrdup(abac_term_name(ptr)); |
---|
[8bd77b5] | 743 | } |
---|
| 744 | |
---|
| 745 | char *abac_term_typed_string(abac_term_t *ptr) |
---|
| 746 | { |
---|
| 747 | return abac_term_typed_string_with_condition(ptr); |
---|
| 748 | } |
---|
| 749 | |
---|
| 750 | /********************************************************************/ |
---|
[440ba20] | 751 | abac_list_t *abac_param_list(abac_param_list_t *ptr) |
---|
| 752 | { |
---|
| 753 | assert(ptr); |
---|
| 754 | return ptr->list; |
---|
| 755 | } |
---|
| 756 | |
---|
| 757 | void abac_param_dump(abac_param_list_t *ptr, char *offset) |
---|
| 758 | { |
---|
| 759 | printf("===params===\n"); |
---|
| 760 | abac_term_t *cur; |
---|
| 761 | abac_list_foreach(ptr->list, cur, |
---|
| 762 | abac_term_dump(cur, offset); |
---|
| 763 | ); |
---|
| 764 | printf("============\n"); |
---|
| 765 | } |
---|
| 766 | |
---|
[8bd77b5] | 767 | abac_param_list_t *abac_param_list_new(abac_term_t *term) |
---|
| 768 | { |
---|
| 769 | abac_param_list_t *ptr=abac_xmalloc(sizeof(abac_param_list_t)); |
---|
| 770 | ptr->list=abac_list_new(); |
---|
| 771 | abac_list_add(ptr->list, abac_term_dup(term)); |
---|
| 772 | return ptr; |
---|
| 773 | } |
---|
| 774 | |
---|
| 775 | abac_param_list_t *abac_param_list_free(abac_param_list_t *ptr) |
---|
| 776 | { |
---|
| 777 | abac_list_t *list=ptr->list; |
---|
| 778 | abac_term_t *cur; |
---|
| 779 | abac_list_foreach(list, cur, |
---|
| 780 | abac_term_free(cur); |
---|
| 781 | ); |
---|
| 782 | abac_list_free(list); |
---|
| 783 | free(ptr); |
---|
| 784 | } |
---|
| 785 | |
---|
| 786 | abac_param_list_t *abac_param_list_add_term(abac_param_list_t *ptr, abac_term_t *term) |
---|
| 787 | { |
---|
| 788 | abac_list_t *list=ptr->list; |
---|
| 789 | abac_list_add(list, abac_term_dup(term)); |
---|
| 790 | return ptr; |
---|
| 791 | } |
---|
| 792 | |
---|
| 793 | |
---|
| 794 | /* term1:cond1,term2:cond2 */ |
---|
| 795 | char* abac_param_list_string_with_condition(abac_param_list_t *ptr) |
---|
| 796 | { |
---|
| 797 | if(ptr->list == NULL) |
---|
| 798 | return ""; |
---|
| 799 | |
---|
| 800 | abac_term_t *cur; |
---|
| 801 | char *tmp=NULL, *final=NULL; |
---|
| 802 | abac_list_foreach(ptr->list, cur, |
---|
| 803 | char *s=abac_term_string_with_condition(cur); |
---|
| 804 | if(final==NULL) { |
---|
| 805 | final=abac_xstrdup(s); |
---|
| 806 | } else { |
---|
| 807 | tmp=final; |
---|
| 808 | final=NULL; |
---|
| 809 | asprintf(&final,"%s,%s",tmp,s); |
---|
| 810 | free(tmp); |
---|
| 811 | } |
---|
| 812 | free(s); |
---|
| 813 | ); |
---|
| 814 | return final; |
---|
| 815 | } |
---|
| 816 | |
---|
| 817 | /* [type:term1:cond1],[type:term2:cond2] */ |
---|
| 818 | char* abac_param_list_typed_string_with_condition(abac_param_list_t *ptr) |
---|
| 819 | { |
---|
| 820 | if(ptr->list == NULL) |
---|
| 821 | return ""; |
---|
| 822 | |
---|
| 823 | abac_term_t *cur; |
---|
| 824 | char *tmp=NULL, *final=NULL; |
---|
| 825 | abac_list_foreach(ptr->list, cur, |
---|
| 826 | char *s=abac_term_typed_string_with_condition(cur); |
---|
| 827 | if(final==NULL) { |
---|
| 828 | final=abac_xstrdup(s); |
---|
| 829 | } else { |
---|
| 830 | tmp=final; |
---|
| 831 | final=NULL; |
---|
| 832 | asprintf(&final,"%s,%s",tmp,s); |
---|
| 833 | if(debug) printf("typed param so far, %s\n", final); |
---|
| 834 | free(tmp); |
---|
| 835 | } |
---|
| 836 | free(s); |
---|
| 837 | ); |
---|
| 838 | if(debug) printf("typed param so far, %s\n", final); |
---|
| 839 | return final; |
---|
| 840 | } |
---|
| 841 | |
---|
| 842 | /* term1,term2 */ |
---|
| 843 | char* abac_param_list_string(abac_param_list_t *ptr) |
---|
| 844 | { |
---|
| 845 | abac_list_t *list=ptr->list; |
---|
| 846 | if(list == NULL) |
---|
| 847 | return ""; |
---|
| 848 | abac_term_t *cur; |
---|
| 849 | char *final=NULL; |
---|
| 850 | char *tmp=NULL; |
---|
| 851 | |
---|
| 852 | /* collect up all the string */ |
---|
| 853 | abac_list_foreach(list, cur, |
---|
| 854 | char *s=abac_term_string(cur); |
---|
| 855 | if(final==NULL) { |
---|
| 856 | final=abac_xstrdup(s); |
---|
| 857 | } else { |
---|
| 858 | tmp=final; |
---|
| 859 | final=NULL; |
---|
| 860 | asprintf(&final,"%s,%s",tmp,s); |
---|
| 861 | if(debug) printf("param so far, %s\n", final); |
---|
| 862 | free(tmp); |
---|
| 863 | } |
---|
| 864 | free(s); |
---|
| 865 | ); |
---|
| 866 | if(debug) printf("param so far, %s\n", final); |
---|
| 867 | return final; |
---|
| 868 | } |
---|
| 869 | |
---|
| 870 | /* make a stack of it for the abac.hh */ |
---|
| 871 | abac_term_t **abac_param_list_vectorize(abac_param_list_t *ptr) |
---|
| 872 | { |
---|
| 873 | abac_term_t **terms=NULL; |
---|
| 874 | abac_list_t *list=ptr->list; |
---|
| 875 | int cnt=0; |
---|
| 876 | if(list != NULL) |
---|
| 877 | cnt=abac_list_size(list); |
---|
| 878 | |
---|
| 879 | // make the array (leave space to NULL terminate it) |
---|
| 880 | // n.b., even if the list is empty, we still return an array that |
---|
| 881 | // only contains the NULL terminator |
---|
| 882 | terms = abac_xmalloc(sizeof(abac_term_t *) * (cnt + 1)); |
---|
| 883 | |
---|
| 884 | abac_term_t *cur; |
---|
| 885 | int i = 0; |
---|
| 886 | if(i<cnt) { |
---|
| 887 | abac_list_foreach(list, cur, |
---|
| 888 | terms[i++] = abac_term_dup(cur); |
---|
| 889 | ); |
---|
| 890 | } |
---|
| 891 | terms[i] = NULL; |
---|
| 892 | return terms; |
---|
| 893 | } |
---|
| 894 | |
---|
[440ba20] | 895 | |
---|
[8bd77b5] | 896 | void abac_terms_free(abac_term_t **terms) |
---|
| 897 | { |
---|
| 898 | /* always null terminating */ |
---|
| 899 | assert(terms); |
---|
| 900 | int i; |
---|
| 901 | for (i = 0; terms[i] != NULL; ++i) { |
---|
| 902 | abac_term_free(terms[i]); |
---|
| 903 | } |
---|
| 904 | free(terms); |
---|
| 905 | } |
---|
[440ba20] | 906 | |
---|
| 907 | void abac_term_dump(abac_term_t *ptr, char *offset) |
---|
| 908 | { |
---|
| 909 | printf("--- term---\n"); |
---|
| 910 | printf("%s type : %s\n",offset, abac_term_type_name(ptr)); |
---|
| 911 | printf("%s name : %s\n",offset, abac_term_name(ptr)); |
---|
| 912 | printf("%s isnamed: %d\n",offset,ptr->isnamed); |
---|
| 913 | if(ptr->p_name) |
---|
| 914 | printf("%s p_name: %s\n",offset,ptr->p_name); |
---|
| 915 | else printf("%s p_name: --\n",offset); |
---|
| 916 | if(ptr->cn) |
---|
| 917 | printf("%s cn: %s\n",offset,ptr->cn); |
---|
| 918 | else printf("%s cn: --\n",offset); |
---|
| 919 | if(ptr->constraint) { |
---|
| 920 | char *tmp=NULL; |
---|
| 921 | asprintf(&tmp," %s", offset); |
---|
| 922 | abac_condition_dump(ptr->constraint,offset); |
---|
| 923 | free(tmp); |
---|
| 924 | } else printf("%s constraint: --\n",offset); |
---|
| 925 | printf("-----------\n"); |
---|
| 926 | } |
---|
| 927 | |
---|