source: libabac/abac_term.c @ 6e8997e

mei_rt2mei_rt2_fix_1
Last change on this file since 6e8997e was 5110d42, checked in by Mei <mei@…>, 12 years ago

1) reorganized the test directory to include python tests
2) attribute via api and principal via api from python scripts is

working (although there is a annoying seg fault at the very end
that must be related to something not been dup()ed.. need to wait
for c example to debug it)

3) able to query via api
4) replicated access_rt2 example in python and the query result matches
5) expanded api to make it easier to generate rt2 structure

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