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