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