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