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