[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 | |
---|
| 20 | /* string will contain either |
---|
| 21 | the range literal (static constraint) |
---|
| 22 | or |
---|
| 23 | the oset/role prologized string (dynamic constraint) |
---|
| 24 | union ptr will only be set if it is a dynamic |
---|
| 25 | constraint */ |
---|
| 26 | struct _abac_condition_t { |
---|
| 27 | int type; |
---|
| 28 | char *string; |
---|
| 29 | abac_aspect_t *of_aspect; |
---|
| 30 | }; |
---|
| 31 | |
---|
[d037f54] | 32 | /* termtype_t/name pair for the data terms |
---|
| 33 | idtype_t/name/p_name pair for the named principal term |
---|
| 34 | */ |
---|
| 35 | |
---|
[8bd77b5] | 36 | struct _abac_term_t { |
---|
| 37 | int type; |
---|
| 38 | char *name; |
---|
[d037f54] | 39 | // e_TERM_PRINCIPAL isnamed |
---|
| 40 | int isnamed; |
---|
| 41 | char *p_name; |
---|
| 42 | char *cn; //XXX ?? might not need this at all |
---|
[8bd77b5] | 43 | abac_condition_t *constraint; |
---|
| 44 | }; |
---|
| 45 | |
---|
| 46 | /* new, free, add one of terms */ |
---|
| 47 | struct _abac_param_list_t { |
---|
| 48 | abac_list_t *list; |
---|
| 49 | }; |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | /******************************************************************/ |
---|
| 53 | char *abac_condtype_name(int i) |
---|
| 54 | { |
---|
| 55 | if(i>_condname_cnt) |
---|
| 56 | panic("abac_condtype_name: went out of range on condname"); |
---|
[d037f54] | 57 | return (char *) _condname[i]; |
---|
[8bd77b5] | 58 | } |
---|
| 59 | |
---|
| 60 | abac_condition_t *abac_condition_new(int type,char* condstr,abac_aspect_t *cptr) |
---|
| 61 | { |
---|
| 62 | if(debug) { |
---|
| 63 | printf(" abac_condition_new: \n"); |
---|
| 64 | printf(" type is %d(%s)\n", type, abac_condtype_name(type)); |
---|
| 65 | printf(" condstr is (%s)\n", condstr); |
---|
| 66 | if(cptr) printf(" yes on cptr\n"); |
---|
| 67 | else printf(" no on cptr\n"); |
---|
| 68 | } |
---|
| 69 | abac_condition_t *ptr= |
---|
| 70 | (abac_condition_t *)abac_xmalloc(sizeof(abac_condition_t)); |
---|
| 71 | ptr->type=type; |
---|
| 72 | ptr->string=abac_xstrdup(condstr); |
---|
| 73 | if(cptr) ptr->of_aspect=abac_aspect_dup(cptr); |
---|
| 74 | else ptr->of_aspect=NULL; |
---|
| 75 | return ptr; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | void abac_condition_free(abac_condition_t *ptr) |
---|
| 79 | { |
---|
| 80 | switch(ptr->type) { |
---|
| 81 | case e_COND_OSET: |
---|
| 82 | case e_COND_ROLE: |
---|
| 83 | abac_aspect_free(ptr->of_aspect); |
---|
| 84 | break; |
---|
| 85 | case e_COND_RANGE: |
---|
| 86 | /* do nothing */ |
---|
| 87 | break; |
---|
| 88 | } |
---|
| 89 | if(ptr->string) free(ptr->string); |
---|
| 90 | free(ptr); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | abac_condition_t *abac_condition_from_string(char *string) |
---|
| 94 | { |
---|
| 95 | assert(string); |
---|
| 96 | abac_condition_t *nptr= |
---|
| 97 | (abac_condition_t *)abac_xmalloc(sizeof(abac_condition_t)); |
---|
| 98 | nptr->type=e_COND_RANGE; |
---|
| 99 | nptr->string=abac_xstrdup(string); |
---|
| 100 | nptr->of_aspect=NULL; |
---|
| 101 | return nptr; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | abac_condition_t *abac_condition_from_aspect(abac_aspect_t *ptr) |
---|
| 105 | { |
---|
| 106 | assert(ptr); |
---|
| 107 | abac_condition_t *nptr= |
---|
| 108 | (abac_condition_t *)abac_xmalloc(sizeof(abac_condition_t)); |
---|
| 109 | if(abac_aspect_is_role(ptr)) |
---|
| 110 | nptr->type=e_COND_ROLE; |
---|
| 111 | else nptr->type=e_COND_OSET; |
---|
| 112 | nptr->string=NULL; |
---|
| 113 | nptr->of_aspect=abac_aspect_dup(ptr); |
---|
| 114 | return nptr; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /* made a copy */ |
---|
| 118 | abac_condition_t *abac_condition_dup(abac_condition_t *ptr) |
---|
| 119 | { |
---|
| 120 | abac_condition_t *nptr= |
---|
| 121 | (abac_condition_t *)abac_xmalloc(sizeof(abac_condition_t)); |
---|
| 122 | nptr->type=ptr->type; |
---|
| 123 | if(ptr->string) |
---|
| 124 | nptr->string=abac_xstrdup(ptr->string); |
---|
| 125 | else nptr->string=NULL; |
---|
| 126 | if(ptr->of_aspect) |
---|
| 127 | nptr->of_aspect=abac_aspect_dup(ptr->of_aspect); |
---|
| 128 | else nptr->of_aspect=NULL; |
---|
| 129 | return nptr; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | char *abac_condition_string(abac_condition_t *ptr) |
---|
| 133 | { |
---|
| 134 | char *string=NULL; |
---|
| 135 | switch(ptr->type) { |
---|
| 136 | case e_COND_OSET: |
---|
| 137 | case e_COND_ROLE: |
---|
| 138 | string=abac_aspect_string_with_condition(ptr->of_aspect); |
---|
| 139 | break; |
---|
| 140 | case e_COND_RANGE: |
---|
| 141 | string=abac_xstrdup(ptr->string); |
---|
| 142 | break; |
---|
| 143 | } |
---|
| 144 | return string; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | char *abac_condition_typed_string(abac_condition_t *ptr) |
---|
| 148 | { |
---|
| 149 | char *string=NULL; |
---|
| 150 | switch(ptr->type) { |
---|
| 151 | case e_COND_OSET: |
---|
| 152 | case e_COND_ROLE: |
---|
| 153 | string=abac_aspect_typed_string_with_condition(ptr->of_aspect); |
---|
| 154 | break; |
---|
| 155 | case e_COND_RANGE: |
---|
| 156 | string=abac_xstrdup(ptr->string); |
---|
| 157 | break; |
---|
| 158 | } |
---|
| 159 | return string; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | /******************************************************************/ |
---|
| 163 | |
---|
[d037f54] | 164 | int abac_term_isnamed(abac_term_t *term) |
---|
| 165 | { |
---|
| 166 | if(term->isnamed) return 1; |
---|
| 167 | else return 0; |
---|
| 168 | } |
---|
| 169 | |
---|
[8bd77b5] | 170 | char *abac_term_name(abac_term_t *term) |
---|
| 171 | { |
---|
[d037f54] | 172 | if(ABAC_IN_PROLOG && term->p_name) { |
---|
| 173 | return term->p_name; |
---|
| 174 | } |
---|
[8bd77b5] | 175 | return term->name; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | char *abac_term_cn(abac_term_t *term) |
---|
| 179 | { |
---|
| 180 | return term->cn; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | abac_condition_t *abac_term_constraint(abac_term_t *term) |
---|
| 184 | { |
---|
| 185 | return term->constraint; |
---|
| 186 | } |
---|
| 187 | |
---|
[d037f54] | 188 | char *abac_termtype_string(int i) |
---|
[8bd77b5] | 189 | { |
---|
| 190 | if(i>_termname_cnt) |
---|
[d037f54] | 191 | panic("abac_termtype_string: went out of range on termname"); |
---|
| 192 | return (char *) _termname[i]; |
---|
[8bd77b5] | 193 | } |
---|
| 194 | |
---|
| 195 | char *abac_term_type_name(abac_term_t *term) |
---|
| 196 | { |
---|
[d037f54] | 197 | if(term->isnamed) |
---|
| 198 | return abac_idtype_string(term->type); |
---|
| 199 | return abac_termtype_string(term->type); |
---|
[8bd77b5] | 200 | } |
---|
| 201 | |
---|
| 202 | int abac_term_type(abac_term_t *term) |
---|
| 203 | { |
---|
| 204 | return term->type; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | static bool _abac_termtype_is_time_type(int i) |
---|
| 208 | { |
---|
| 209 | if (i == e_TERM_TIME) |
---|
| 210 | return 1; |
---|
| 211 | return 0; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | bool abac_term_is_time_type(abac_term_t *term) |
---|
| 215 | { |
---|
| 216 | return _abac_termtype_is_time_type(term->type); |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | static bool _abac_termtype_is_string_type(int i) |
---|
| 220 | { |
---|
| 221 | if (i == e_TERM_STRING) |
---|
| 222 | return 1; |
---|
| 223 | return 0; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | bool abac_term_is_string_type(abac_term_t *term) |
---|
| 227 | { |
---|
| 228 | return _abac_termtype_is_string_type(term->type); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | static bool _abac_termtype_is_integer_type(int i) |
---|
| 232 | { |
---|
| 233 | if (i == e_TERM_INTEGER) |
---|
| 234 | return 1; |
---|
| 235 | return 0; |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | bool abac_term_is_integer_type(abac_term_t *term) |
---|
| 239 | { |
---|
| 240 | return _abac_termtype_is_integer_type(term->type); |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | static bool _abac_termtype_is_urn_type(int i) |
---|
| 244 | { |
---|
| 245 | if (i == e_TERM_URN) |
---|
| 246 | return 1; |
---|
| 247 | return 0; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | bool abac_term_is_urn_type(abac_term_t *term) |
---|
| 251 | { |
---|
| 252 | return _abac_termtype_is_urn_type(term->type); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | int abac_term_verify_term_type(char *type) { |
---|
| 256 | int i; |
---|
| 257 | |
---|
| 258 | if (type == NULL) |
---|
| 259 | return 0; |
---|
| 260 | |
---|
| 261 | for (i = 1; i <= _termname_cnt ; i++) |
---|
| 262 | if(strcmp(type,_termname[i])==0) |
---|
| 263 | return i; |
---|
| 264 | return 0; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | char *abac_term_to_time(char *string) |
---|
| 268 | { |
---|
| 269 | struct tm tm1; |
---|
| 270 | char buf[255]; |
---|
| 271 | |
---|
| 272 | strptime(string, "%Y%m%dT%H%M%S", &tm1); |
---|
| 273 | strftime(buf, sizeof(buf), "time(%Y,%m,%d,%H,%M,%S)", &tm1); |
---|
| 274 | return strdup(buf); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | /* called from yy */ |
---|
| 278 | abac_term_t *abac_term_new(int type, char *name, char *cond, abac_aspect_t *cptr) |
---|
| 279 | { |
---|
| 280 | if(debug) { |
---|
| 281 | printf("abac_term_new: \n"); |
---|
[d037f54] | 282 | printf(" type is %d(%s)\n", type, abac_termtype_string(type)); |
---|
[8bd77b5] | 283 | printf(" name is (%s)\n", name); |
---|
| 284 | if(cond) |
---|
| 285 | printf(" cond is %s\n", cond); |
---|
| 286 | else printf(" no cond \n"); |
---|
| 287 | if(cptr) |
---|
| 288 | printf(" cptr is (%s)\n", abac_aspect_string_with_condition(cptr)); |
---|
| 289 | else printf(" there is no cptr\n"); |
---|
| 290 | } |
---|
| 291 | abac_condition_t *constraint=NULL; |
---|
| 292 | if (cond) { |
---|
| 293 | if(type==e_TERM_PRINCIPAL) { |
---|
| 294 | constraint=abac_condition_new(e_COND_ROLE,cond,cptr); |
---|
| 295 | } else { |
---|
| 296 | if(cptr == NULL) |
---|
| 297 | constraint=abac_condition_new(e_COND_RANGE,cond,NULL); |
---|
| 298 | else |
---|
| 299 | constraint=abac_condition_new(e_COND_OSET,cond,cptr); |
---|
| 300 | } |
---|
| 301 | } |
---|
| 302 | abac_term_t *ptr=abac_xmalloc(sizeof(abac_term_t)); |
---|
[d037f54] | 303 | ptr->isnamed=0; |
---|
[8bd77b5] | 304 | ptr->type=type; |
---|
[d037f54] | 305 | ptr->p_name=NULL; |
---|
[8bd77b5] | 306 | /* reformat the term name if it is a time type */ |
---|
| 307 | if((!constraint) && _abac_termtype_is_time_type(type) && isdigit(name[0])) { |
---|
| 308 | char *tmp=abac_term_to_time(name); |
---|
| 309 | ptr->name=tmp; |
---|
| 310 | } else { |
---|
| 311 | ptr->name=abac_xstrdup(name); |
---|
| 312 | } |
---|
| 313 | ptr->constraint=constraint; |
---|
[d037f54] | 314 | ptr->cn=NULL; |
---|
[8bd77b5] | 315 | return ptr; |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | abac_term_t *abac_term_create(int type, char *name, abac_condition_t *cptr) |
---|
| 319 | { |
---|
| 320 | if(debug) { |
---|
| 321 | printf("abac_term_create: \n"); |
---|
[d037f54] | 322 | printf(" type is %d(%s)\n", type, abac_termtype_string(type)); |
---|
[8bd77b5] | 323 | printf(" name is (%s)\n", name); |
---|
| 324 | if(cptr) |
---|
| 325 | printf(" cptr is (%s)\n", abac_condition_typed_string(cptr)); |
---|
| 326 | else printf(" there is no cptr\n"); |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | abac_term_t *ptr=abac_xmalloc(sizeof(abac_term_t)); |
---|
[d037f54] | 330 | ptr->isnamed=0; |
---|
[8bd77b5] | 331 | ptr->type=type; |
---|
[d037f54] | 332 | ptr->p_name=NULL; |
---|
[8bd77b5] | 333 | /* reformat the term name if it is a time type */ |
---|
| 334 | if((!cptr) && _abac_termtype_is_time_type(type) && isdigit(name[0])) { |
---|
| 335 | char *tmp=abac_term_to_time(name); |
---|
| 336 | ptr->name=tmp; |
---|
| 337 | } else { |
---|
| 338 | ptr->name=abac_xstrdup(name); |
---|
| 339 | } |
---|
| 340 | ptr->constraint=(cptr==NULL)?NULL:abac_condition_dup(cptr); |
---|
[d037f54] | 341 | ptr->cn=NULL; |
---|
| 342 | return ptr; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | /* called from yy, named principal type */ |
---|
| 346 | abac_term_t *abac_term_named_new(int idtype, char *name) |
---|
| 347 | { |
---|
| 348 | if(debug) { |
---|
| 349 | printf("abac_term_named_new: \n"); |
---|
| 350 | printf(" type is %d(%s)\n", idtype, abac_idtype_string(idtype)); |
---|
| 351 | printf(" name is (%s)\n", name); |
---|
| 352 | } |
---|
| 353 | abac_term_t *ptr=abac_xmalloc(sizeof(abac_term_t)); |
---|
| 354 | ptr->isnamed=1; |
---|
| 355 | ptr->type=idtype; |
---|
| 356 | ptr->name=abac_xstrdup(name); |
---|
| 357 | ptr->p_name=NULL; |
---|
| 358 | /* if ABAC_CN, no need to attach p */ |
---|
| 359 | if(!USE("ABAC_CN")) |
---|
| 360 | asprintf(&ptr->p_name,"p%s",name); |
---|
| 361 | ptr->constraint=NULL; |
---|
| 362 | ptr->cn=NULL; |
---|
| 363 | return ptr; |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | abac_term_t *abac_term_named_create(int idtype, char *name) |
---|
| 367 | { |
---|
| 368 | if(debug) { |
---|
| 369 | printf("abac_term_named_create: \n"); |
---|
| 370 | printf(" type is %d(%s)\n", idtype, abac_idtype_string(idtype)); |
---|
| 371 | printf(" name is (%s)\n", name); |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | abac_term_t *ptr=abac_xmalloc(sizeof(abac_term_t)); |
---|
| 375 | ptr->isnamed=1; |
---|
| 376 | ptr->type=idtype; |
---|
| 377 | ptr->name=abac_xstrdup(name); |
---|
| 378 | ptr->p_name=NULL; |
---|
| 379 | /* if ABAC_CN, no need to attach p */ |
---|
| 380 | if(!USE("ABAC_CN")) |
---|
| 381 | asprintf(&ptr->p_name,"p%s",name); |
---|
| 382 | ptr->constraint=NULL; |
---|
| 383 | ptr->cn=NULL; |
---|
[8bd77b5] | 384 | return ptr; |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | abac_term_t *abac_term_dup(abac_term_t *ptr) |
---|
| 388 | { |
---|
| 389 | assert(ptr); |
---|
| 390 | abac_term_t *nptr=abac_xmalloc(sizeof(abac_term_t)); |
---|
[d037f54] | 391 | nptr->isnamed=ptr->isnamed; |
---|
[8bd77b5] | 392 | nptr->type=ptr->type; |
---|
| 393 | nptr->name=abac_xstrdup(ptr->name); |
---|
[d037f54] | 394 | nptr->p_name=(ptr->p_name==NULL)?NULL:abac_xstrdup(ptr->p_name); |
---|
[8bd77b5] | 395 | if(ptr->constraint!=NULL) |
---|
| 396 | nptr->constraint=abac_condition_dup(ptr->constraint); |
---|
| 397 | else nptr->constraint=NULL; |
---|
[d037f54] | 398 | nptr->cn=(ptr->cn)==NULL?NULL:abac_xstrdup(ptr->cn); |
---|
| 399 | nptr->cn=abac_xstrdup(ptr->cn); |
---|
[8bd77b5] | 400 | return nptr; |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | void abac_term_free(abac_term_t *ptr) |
---|
| 404 | { |
---|
| 405 | if(ptr->constraint) { |
---|
| 406 | abac_condition_free(ptr->constraint); |
---|
| 407 | } |
---|
| 408 | free(ptr->name); |
---|
[d037f54] | 409 | if(ptr->p_name) free(ptr->p_name); |
---|
| 410 | if(ptr->cn) free(ptr->cn); |
---|
[8bd77b5] | 411 | free(ptr); |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | abac_term_t *abac_term_add_constraint(abac_term_t *ptr, abac_condition_t *cond) |
---|
| 415 | { |
---|
| 416 | if(ptr->constraint) { |
---|
| 417 | abac_condition_free(ptr->constraint); |
---|
| 418 | } |
---|
| 419 | ptr->constraint = abac_condition_dup(cond); |
---|
| 420 | return ptr; |
---|
| 421 | } |
---|
| 422 | |
---|
| 423 | /* name or name:cond */ |
---|
| 424 | static char *abac_term_string_with_condition(abac_term_t *ptr) |
---|
| 425 | { |
---|
| 426 | char *tmp=NULL; |
---|
| 427 | char *cond=NULL; |
---|
| 428 | char *name=abac_term_name(ptr); |
---|
| 429 | if(ptr->constraint) { |
---|
| 430 | cond=abac_condition_string(ptr->constraint); |
---|
| 431 | } |
---|
| 432 | if(cond) { |
---|
| 433 | asprintf(&tmp,"%s:%s", name, cond); |
---|
| 434 | free(cond); |
---|
| 435 | } else tmp=abac_xstrdup(name); |
---|
| 436 | return tmp; |
---|
| 437 | } |
---|
| 438 | |
---|
| 439 | /* [type:name] or [typed:name:cond] */ |
---|
| 440 | static char *abac_term_typed_string_with_condition(abac_term_t *ptr) |
---|
| 441 | { |
---|
| 442 | char *tmp=NULL; |
---|
| 443 | char *cond=NULL; |
---|
| 444 | char *name=abac_term_name(ptr); |
---|
| 445 | char *type=abac_term_type_name(ptr); |
---|
| 446 | if(ptr->constraint) { |
---|
| 447 | cond=abac_condition_typed_string(ptr->constraint); |
---|
| 448 | } |
---|
| 449 | if(cond) { |
---|
| 450 | asprintf(&tmp,"[%s:%s:%s]", type, name, cond); |
---|
| 451 | free(cond); |
---|
| 452 | } else |
---|
| 453 | asprintf(&tmp,"[%s:%s]", type, name); |
---|
| 454 | if(debug) printf("abac_term_typed_string_with_condition: (%s)\n",tmp); |
---|
| 455 | return tmp; |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | char *abac_term_string(abac_term_t *ptr) |
---|
| 459 | { |
---|
[d037f54] | 460 | return abac_xstrdup(abac_term_name(ptr)); |
---|
[8bd77b5] | 461 | } |
---|
| 462 | |
---|
| 463 | char *abac_term_typed_string(abac_term_t *ptr) |
---|
| 464 | { |
---|
| 465 | return abac_term_typed_string_with_condition(ptr); |
---|
| 466 | } |
---|
| 467 | |
---|
| 468 | /********************************************************************/ |
---|
| 469 | abac_param_list_t *abac_param_list_new(abac_term_t *term) |
---|
| 470 | { |
---|
| 471 | abac_param_list_t *ptr=abac_xmalloc(sizeof(abac_param_list_t)); |
---|
| 472 | ptr->list=abac_list_new(); |
---|
| 473 | abac_list_add(ptr->list, abac_term_dup(term)); |
---|
| 474 | return ptr; |
---|
| 475 | } |
---|
| 476 | |
---|
| 477 | abac_param_list_t *abac_param_list_free(abac_param_list_t *ptr) |
---|
| 478 | { |
---|
| 479 | abac_list_t *list=ptr->list; |
---|
| 480 | abac_term_t *cur; |
---|
| 481 | abac_list_foreach(list, cur, |
---|
| 482 | abac_term_free(cur); |
---|
| 483 | ); |
---|
| 484 | abac_list_free(list); |
---|
| 485 | free(ptr); |
---|
| 486 | } |
---|
| 487 | |
---|
| 488 | abac_param_list_t *abac_param_list_add_term(abac_param_list_t *ptr, abac_term_t *term) |
---|
| 489 | { |
---|
| 490 | abac_list_t *list=ptr->list; |
---|
| 491 | abac_list_add(list, abac_term_dup(term)); |
---|
| 492 | return ptr; |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | |
---|
| 496 | /* term1:cond1,term2:cond2 */ |
---|
| 497 | char* abac_param_list_string_with_condition(abac_param_list_t *ptr) |
---|
| 498 | { |
---|
| 499 | if(ptr->list == NULL) |
---|
| 500 | return ""; |
---|
| 501 | |
---|
| 502 | abac_term_t *cur; |
---|
| 503 | char *tmp=NULL, *final=NULL; |
---|
| 504 | abac_list_foreach(ptr->list, cur, |
---|
| 505 | char *s=abac_term_string_with_condition(cur); |
---|
| 506 | if(final==NULL) { |
---|
| 507 | final=abac_xstrdup(s); |
---|
| 508 | } else { |
---|
| 509 | tmp=final; |
---|
| 510 | final=NULL; |
---|
| 511 | asprintf(&final,"%s,%s",tmp,s); |
---|
| 512 | free(tmp); |
---|
| 513 | } |
---|
| 514 | free(s); |
---|
| 515 | ); |
---|
| 516 | return final; |
---|
| 517 | } |
---|
| 518 | |
---|
| 519 | /* [type:term1:cond1],[type:term2:cond2] */ |
---|
| 520 | char* abac_param_list_typed_string_with_condition(abac_param_list_t *ptr) |
---|
| 521 | { |
---|
| 522 | if(ptr->list == NULL) |
---|
| 523 | return ""; |
---|
| 524 | |
---|
| 525 | abac_term_t *cur; |
---|
| 526 | char *tmp=NULL, *final=NULL; |
---|
| 527 | abac_list_foreach(ptr->list, cur, |
---|
| 528 | char *s=abac_term_typed_string_with_condition(cur); |
---|
| 529 | if(final==NULL) { |
---|
| 530 | final=abac_xstrdup(s); |
---|
| 531 | } else { |
---|
| 532 | tmp=final; |
---|
| 533 | final=NULL; |
---|
| 534 | asprintf(&final,"%s,%s",tmp,s); |
---|
| 535 | if(debug) printf("typed param so far, %s\n", final); |
---|
| 536 | free(tmp); |
---|
| 537 | } |
---|
| 538 | free(s); |
---|
| 539 | ); |
---|
| 540 | if(debug) printf("typed param so far, %s\n", final); |
---|
| 541 | return final; |
---|
| 542 | } |
---|
| 543 | |
---|
| 544 | /* term1,term2 */ |
---|
| 545 | char* abac_param_list_string(abac_param_list_t *ptr) |
---|
| 546 | { |
---|
| 547 | abac_list_t *list=ptr->list; |
---|
| 548 | if(list == NULL) |
---|
| 549 | return ""; |
---|
| 550 | abac_term_t *cur; |
---|
| 551 | char *final=NULL; |
---|
| 552 | char *tmp=NULL; |
---|
| 553 | |
---|
| 554 | /* collect up all the string */ |
---|
| 555 | abac_list_foreach(list, cur, |
---|
| 556 | char *s=abac_term_string(cur); |
---|
| 557 | if(final==NULL) { |
---|
| 558 | final=abac_xstrdup(s); |
---|
| 559 | } else { |
---|
| 560 | tmp=final; |
---|
| 561 | final=NULL; |
---|
| 562 | asprintf(&final,"%s,%s",tmp,s); |
---|
| 563 | if(debug) printf("param so far, %s\n", final); |
---|
| 564 | free(tmp); |
---|
| 565 | } |
---|
| 566 | free(s); |
---|
| 567 | ); |
---|
| 568 | if(debug) printf("param so far, %s\n", final); |
---|
| 569 | return final; |
---|
| 570 | } |
---|
| 571 | |
---|
| 572 | /* make a stack of it for the abac.hh */ |
---|
| 573 | abac_term_t **abac_param_list_vectorize(abac_param_list_t *ptr) |
---|
| 574 | { |
---|
| 575 | abac_term_t **terms=NULL; |
---|
| 576 | abac_list_t *list=ptr->list; |
---|
| 577 | int cnt=0; |
---|
| 578 | if(list != NULL) |
---|
| 579 | cnt=abac_list_size(list); |
---|
| 580 | |
---|
| 581 | // make the array (leave space to NULL terminate it) |
---|
| 582 | // n.b., even if the list is empty, we still return an array that |
---|
| 583 | // only contains the NULL terminator |
---|
| 584 | terms = abac_xmalloc(sizeof(abac_term_t *) * (cnt + 1)); |
---|
| 585 | |
---|
| 586 | abac_term_t *cur; |
---|
| 587 | int i = 0; |
---|
| 588 | if(i<cnt) { |
---|
| 589 | abac_list_foreach(list, cur, |
---|
| 590 | terms[i++] = abac_term_dup(cur); |
---|
| 591 | ); |
---|
| 592 | } |
---|
| 593 | terms[i] = NULL; |
---|
| 594 | return terms; |
---|
| 595 | } |
---|
| 596 | |
---|
| 597 | void abac_terms_free(abac_term_t **terms) |
---|
| 598 | { |
---|
| 599 | /* always null terminating */ |
---|
| 600 | assert(terms); |
---|
| 601 | int i; |
---|
| 602 | for (i = 0; terms[i] != NULL; ++i) { |
---|
| 603 | abac_term_free(terms[i]); |
---|
| 604 | } |
---|
| 605 | free(terms); |
---|
| 606 | } |
---|