[718ad924] | 1 | |
---|
| 2 | /*********************************************************** |
---|
| 3 | abac_param.c |
---|
[da5afdf] | 4 | |
---|
| 5 | dterm=data term of rt1 |
---|
| 6 | param is a kind of dterm |
---|
[718ad924] | 7 | ***********************************************************/ |
---|
| 8 | #include <assert.h> |
---|
| 9 | #include <stdlib.h> |
---|
| 10 | #include <stdio.h> |
---|
| 11 | #include <string.h> |
---|
[da5afdf] | 12 | #include <ctype.h> |
---|
[718ad924] | 13 | |
---|
| 14 | #include "abac_list.h" |
---|
| 15 | #include "abac_util.h" |
---|
| 16 | |
---|
| 17 | #include "abac_pl.h" |
---|
| 18 | |
---|
[da5afdf] | 19 | typedef enum _termtype { |
---|
| 20 | e_TERM_INTEGER = 1, |
---|
| 21 | e_TERM_URN = 2, |
---|
| 22 | e_TERM_FLOAT = 3, |
---|
| 23 | e_TERM_BOOLEAN = 4, |
---|
| 24 | e_TERM_STRING = 5, |
---|
| 25 | e_TERM_TIME = 6, |
---|
| 26 | e_TERM_PRINCIPAL = 7, |
---|
| 27 | e_TERM_ANONYMOUS = 8 |
---|
| 28 | } termtype_t; |
---|
| 29 | |
---|
| 30 | /* termname[dtermtype] */ |
---|
| 31 | static char *const termname[] = |
---|
| 32 | { |
---|
| 33 | "badterm", |
---|
| 34 | "integer", |
---|
| 35 | "urn", |
---|
| 36 | "float", |
---|
| 37 | "boolean", |
---|
| 38 | "string", |
---|
| 39 | "time" , |
---|
| 40 | "principal", |
---|
| 41 | "anonymous", |
---|
| 42 | 0 |
---|
| 43 | }; |
---|
| 44 | static int termname_cnt=8; |
---|
| 45 | |
---|
| 46 | typedef enum _condtype_t { |
---|
| 47 | e_COND_ROLE = 1, |
---|
[d845403] | 48 | e_COND_OSET = 2, |
---|
| 49 | e_COND_RANGE = 3, |
---|
[da5afdf] | 50 | } condtype_t; |
---|
| 51 | |
---|
[0d0c3a9] | 52 | /* string will contain either |
---|
| 53 | the range literal (static constraint) |
---|
| 54 | or |
---|
| 55 | the oset/role prologized string (dynamic constraint) |
---|
| 56 | union ptr will only be set if it is a dynamic |
---|
| 57 | constraint */ |
---|
[718ad924] | 58 | struct _abac_condition_t { |
---|
[da5afdf] | 59 | int type; |
---|
[0d0c3a9] | 60 | char *string; |
---|
[da5afdf] | 61 | union { |
---|
| 62 | abac_role_t *of_role; |
---|
| 63 | abac_oset_t *of_oset; |
---|
| 64 | }; |
---|
[718ad924] | 65 | }; |
---|
| 66 | |
---|
[da5afdf] | 67 | struct _abac_term_t { |
---|
[718ad924] | 68 | int type; |
---|
| 69 | char *name; |
---|
| 70 | abac_condition_t *constraint; |
---|
| 71 | }; |
---|
| 72 | |
---|
| 73 | /* new, free, add one */ |
---|
| 74 | struct _abac_param_list_t { |
---|
| 75 | abac_list_t *list; |
---|
| 76 | }; |
---|
| 77 | |
---|
[da5afdf] | 78 | |
---|
[718ad924] | 79 | /******************************************************************/ |
---|
[b5a3da4] | 80 | static abac_condition_t *abac_condition_new(int type,char* condstr,void *cptr) |
---|
| 81 | { |
---|
| 82 | abac_condition_t *ptr= |
---|
| 83 | (abac_condition_t *)abac_xmalloc(sizeof(abac_condition_t)); |
---|
| 84 | ptr->type=type; |
---|
| 85 | ptr->string=strdup(condstr); |
---|
| 86 | ptr->of_role=cptr; |
---|
| 87 | return ptr; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | static void abac_condition_free(abac_condition_t *ptr) |
---|
| 91 | { |
---|
[d845403] | 92 | switch(ptr->type) { |
---|
| 93 | case e_COND_OSET: |
---|
| 94 | abac_oset_free(ptr->of_oset); |
---|
| 95 | break; |
---|
| 96 | case e_COND_ROLE: |
---|
| 97 | abac_role_free(ptr->of_role); |
---|
| 98 | break; |
---|
| 99 | case e_COND_RANGE: |
---|
| 100 | /* do nothing */ |
---|
| 101 | break; |
---|
| 102 | } |
---|
[b5a3da4] | 103 | if(ptr->string) free(ptr->string); |
---|
| 104 | free(ptr); |
---|
| 105 | } |
---|
| 106 | |
---|
[d845403] | 107 | /* made a copy */ |
---|
[b5a3da4] | 108 | char *abac_condition_string(abac_condition_t *ptr) |
---|
| 109 | { |
---|
| 110 | char *string=NULL; |
---|
[d845403] | 111 | switch(ptr->type) { |
---|
| 112 | case e_COND_OSET: |
---|
| 113 | string=abac_oset_string_with_condition(ptr->of_oset); |
---|
| 114 | break; |
---|
| 115 | case e_COND_ROLE: |
---|
| 116 | string=abac_role_string_with_condition(ptr->of_role); |
---|
| 117 | break; |
---|
| 118 | case e_COND_RANGE: |
---|
| 119 | string=strdup(ptr->string); |
---|
| 120 | break; |
---|
| 121 | } |
---|
[b5a3da4] | 122 | return string; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | /******************************************************************/ |
---|
| 127 | |
---|
[da5afdf] | 128 | char *abac_term_name(abac_term_t *term) |
---|
| 129 | { |
---|
| 130 | return term->name; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | abac_condition_t *abac_term_constraint(abac_term_t *term) |
---|
| 134 | { |
---|
| 135 | return term->constraint; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | char *abac_term_type(int i) |
---|
| 139 | { |
---|
| 140 | return termname[i]; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | int abac_term_is_time_type(int i) |
---|
| 144 | { |
---|
| 145 | if (i == e_TERM_TIME) |
---|
| 146 | return 1; |
---|
| 147 | return 0; |
---|
| 148 | } |
---|
[718ad924] | 149 | |
---|
[da5afdf] | 150 | int abac_term_is_string_type(int i) |
---|
[718ad924] | 151 | { |
---|
[da5afdf] | 152 | if (i == e_TERM_STRING) |
---|
| 153 | return 1; |
---|
| 154 | return 0; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | int abac_term_is_integer_type(int i) |
---|
| 158 | { |
---|
| 159 | if (i == e_TERM_INTEGER) |
---|
| 160 | return 1; |
---|
| 161 | return 0; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | int abac_term_is_urn_type(int i) |
---|
| 165 | { |
---|
| 166 | if (i == e_TERM_URN) |
---|
| 167 | return 1; |
---|
| 168 | return 0; |
---|
| 169 | } |
---|
| 170 | |
---|
[b5a3da4] | 171 | int abac_term_verify_term_type(char *type) { |
---|
[da5afdf] | 172 | int i; |
---|
| 173 | |
---|
| 174 | if (type == NULL) |
---|
| 175 | return 0; |
---|
| 176 | |
---|
| 177 | for (i = 1; i <= termname_cnt ; i++) |
---|
| 178 | if(strcmp(type,termname[i])==0) |
---|
| 179 | return i; |
---|
| 180 | return 0; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | /* always add p to the name except if its a variable and needed */ |
---|
| 184 | abac_term_t *abac_term_new(int type, char *name, char *cond, void *cptr) |
---|
[718ad924] | 185 | { |
---|
| 186 | abac_condition_t *constraint=NULL; |
---|
| 187 | if (cond) { |
---|
[d845403] | 188 | if(type==e_TERM_PRINCIPAL) { |
---|
[da5afdf] | 189 | constraint=abac_condition_new(e_COND_ROLE,cond,cptr); |
---|
[d845403] | 190 | } else { |
---|
| 191 | if(cptr == NULL) |
---|
| 192 | constraint=abac_condition_new(e_COND_RANGE,cond,NULL); |
---|
| 193 | else |
---|
| 194 | constraint=abac_condition_new(e_COND_OSET,cond,cptr); |
---|
| 195 | } |
---|
[718ad924] | 196 | } |
---|
[da5afdf] | 197 | abac_term_t *ptr=abac_xmalloc(sizeof(abac_term_t)); |
---|
[718ad924] | 198 | ptr->type=type; |
---|
[36b100a] | 199 | /* add p if it is a time type */ |
---|
| 200 | if((!constraint) && abac_term_is_time_type(type) && isdigit(name[0])) { |
---|
| 201 | char *tmp=NULL; |
---|
[da5afdf] | 202 | asprintf(&tmp,"p%s",name); |
---|
| 203 | ptr->name=tmp; |
---|
| 204 | } else { |
---|
| 205 | ptr->name=strdup(name); |
---|
| 206 | } |
---|
[718ad924] | 207 | ptr->constraint=constraint; |
---|
| 208 | return ptr; |
---|
| 209 | } |
---|
| 210 | |
---|
[da5afdf] | 211 | void abac_term_free(abac_term_t *ptr) |
---|
[718ad924] | 212 | { |
---|
| 213 | if(ptr->constraint) { |
---|
| 214 | abac_condition_free(ptr->constraint); |
---|
| 215 | } |
---|
| 216 | free(ptr->name); |
---|
| 217 | free(ptr); |
---|
| 218 | } |
---|
| 219 | |
---|
[da5afdf] | 220 | abac_term_t *abac_term_add_constraint(abac_term_t *ptr, abac_condition_t *cond) |
---|
[718ad924] | 221 | { |
---|
| 222 | if(ptr->constraint) { |
---|
| 223 | abac_condition_free(ptr->constraint); |
---|
| 224 | } |
---|
| 225 | ptr->constraint = cond; |
---|
| 226 | return ptr; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | /* name or name:cond */ |
---|
[da5afdf] | 230 | static char *abac_term_string_with_condition(abac_term_t *ptr) |
---|
[718ad924] | 231 | { |
---|
| 232 | char *tmp=NULL; |
---|
| 233 | char *cond=NULL; |
---|
| 234 | char *name=ptr->name; |
---|
[da5afdf] | 235 | int len=strlen(name)+1; |
---|
[718ad924] | 236 | if(ptr->constraint) { |
---|
[b5a3da4] | 237 | cond=abac_condition_string(ptr->constraint); |
---|
| 238 | len=len+strlen(cond)+1; |
---|
[718ad924] | 239 | } |
---|
| 240 | tmp = abac_xmalloc(len); |
---|
[d845403] | 241 | if(cond) { |
---|
[718ad924] | 242 | sprintf(tmp,"%s:%s", name, cond); |
---|
[d845403] | 243 | free(cond); |
---|
| 244 | } else |
---|
[718ad924] | 245 | sprintf(tmp,"%s", name); |
---|
| 246 | return tmp; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | /* len of name or name:cond */ |
---|
[da5afdf] | 250 | static int abac_term_len_with_condition(abac_term_t *ptr) |
---|
[718ad924] | 251 | { |
---|
| 252 | char *cond=NULL; |
---|
| 253 | char *name=ptr->name; |
---|
| 254 | int len=strlen(name); |
---|
| 255 | if(ptr->constraint) { |
---|
[b5a3da4] | 256 | cond=abac_condition_string(ptr->constraint); |
---|
| 257 | len=len+strlen(cond)+1; |
---|
| 258 | free(cond); |
---|
[718ad924] | 259 | } |
---|
| 260 | return len; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | /* just name */ |
---|
[da5afdf] | 264 | static char *abac_term_string(abac_term_t *ptr) |
---|
[718ad924] | 265 | { |
---|
[b5a3da4] | 266 | if(ptr->type == abac_term_verify_term_type("urn") || |
---|
| 267 | ptr->type == abac_term_verify_term_type("string") ) { |
---|
[da5afdf] | 268 | char *str; |
---|
| 269 | asprintf(&str, "%s", ptr->name); |
---|
| 270 | return str; |
---|
| 271 | } |
---|
[718ad924] | 272 | return strdup(ptr->name); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | /* just len of name */ |
---|
[da5afdf] | 276 | static int abac_term_len(abac_term_t *ptr) |
---|
[718ad924] | 277 | { |
---|
[da5afdf] | 278 | /* including the 2 ' */ |
---|
[b5a3da4] | 279 | if(ptr->type == abac_term_verify_term_type("urn") || |
---|
| 280 | ptr->type == abac_term_verify_term_type("string") ) { |
---|
[da5afdf] | 281 | return strlen(ptr->name)+2; |
---|
| 282 | } |
---|
[718ad924] | 283 | return strlen(ptr->name); |
---|
| 284 | } |
---|
| 285 | |
---|
[b5a3da4] | 286 | /********************************************************************/ |
---|
| 287 | abac_param_list_t *abac_param_list_new(abac_term_t *term) |
---|
| 288 | { |
---|
| 289 | abac_param_list_t *ptr=abac_xmalloc(sizeof(abac_param_list_t)); |
---|
| 290 | ptr->list=abac_list_new(); |
---|
| 291 | abac_list_add(ptr->list, term); |
---|
| 292 | return ptr; |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | abac_param_list_t *abac_param_list_free(abac_param_list_t *ptr) |
---|
| 296 | { |
---|
| 297 | abac_list_t *list=ptr->list; |
---|
| 298 | abac_term_t *cur; |
---|
| 299 | abac_list_foreach(list, cur, |
---|
| 300 | abac_term_free(cur); |
---|
| 301 | ); |
---|
| 302 | abac_list_free(list); |
---|
| 303 | free(ptr); |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | abac_param_list_t *abac_param_list_add_term(abac_param_list_t *ptr, abac_term_t *term) |
---|
| 307 | { |
---|
| 308 | abac_list_t *list=ptr->list; |
---|
| 309 | abac_list_add(list, term); |
---|
| 310 | return ptr; |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | |
---|
[718ad924] | 314 | /* param1:cond1,param2:cond2 */ |
---|
| 315 | char* abac_param_list_string_with_condition(abac_param_list_t *ptr) |
---|
| 316 | { |
---|
| 317 | if(ptr->list == NULL) |
---|
| 318 | return ""; |
---|
| 319 | |
---|
[da5afdf] | 320 | abac_term_t *cur; |
---|
[718ad924] | 321 | char *tmp; |
---|
| 322 | int len=0; |
---|
| 323 | /* collect up all the lens so can allocate once */ |
---|
| 324 | abac_list_foreach(ptr->list, cur, |
---|
[da5afdf] | 325 | int l=abac_term_len_with_condition(cur); |
---|
[718ad924] | 326 | len=len+l+1; |
---|
| 327 | ); |
---|
[da5afdf] | 328 | tmp=abac_xmalloc((len+1) * sizeof(char)); |
---|
[718ad924] | 329 | /* collect up all the string */ |
---|
| 330 | int first=1; |
---|
| 331 | abac_list_foreach(ptr->list, cur, |
---|
[da5afdf] | 332 | char *s=abac_term_string_with_condition(cur); |
---|
[718ad924] | 333 | if(first) { |
---|
| 334 | strcpy(tmp,s); |
---|
| 335 | first=0; |
---|
| 336 | } else { |
---|
| 337 | strcat(tmp,","); |
---|
| 338 | strcat(tmp,s); |
---|
| 339 | } |
---|
| 340 | free(s); |
---|
| 341 | ); |
---|
| 342 | return tmp; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | /* param1,param2 */ |
---|
| 346 | char* abac_param_list_string(abac_param_list_t *ptr) |
---|
| 347 | { |
---|
| 348 | abac_list_t *list=ptr->list; |
---|
| 349 | if(list == NULL) |
---|
| 350 | return ""; |
---|
[da5afdf] | 351 | abac_term_t *cur; |
---|
[718ad924] | 352 | char *tmp; |
---|
| 353 | int len=0; |
---|
| 354 | /* collect up all the lens so can allocate once */ |
---|
| 355 | abac_list_foreach(list, cur, |
---|
| 356 | // collect up the len |
---|
[da5afdf] | 357 | int l=abac_term_len(cur); |
---|
[718ad924] | 358 | len=len+l+1; |
---|
| 359 | ); |
---|
[da5afdf] | 360 | tmp=abac_xmalloc((len+1) * sizeof(char)); |
---|
[718ad924] | 361 | /* collect up all the string */ |
---|
| 362 | int first=1; |
---|
| 363 | abac_list_foreach(list, cur, |
---|
[da5afdf] | 364 | char *s=abac_term_string(cur); |
---|
[718ad924] | 365 | if(first) { |
---|
| 366 | strcpy(tmp,s); |
---|
| 367 | first=0; |
---|
| 368 | } else { |
---|
| 369 | strcat(tmp,","); |
---|
| 370 | strcat(tmp,s); |
---|
| 371 | } |
---|
| 372 | free(s); |
---|
| 373 | ); |
---|
| 374 | return tmp; |
---|
| 375 | } |
---|
[b5a3da4] | 376 | |
---|
| 377 | |
---|