source: libabac/abac_term.c @ 5d06689

mei_rt2mei_rt2_fix_1
Last change on this file since 5d06689 was 5d06689, checked in by Mei <mei@…>, 12 years ago

1) modify abac.hh and added abac_c.c to unify the c and c++ api

interface (almost)

2) add new API
3) tweak the tests
4) filling missing code for abac_verifier_load_attribute_cert_attribute

  • 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(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(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(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(char *typename, char *name, abac_condition_t *cptr)
648{
649     int type=abac_verify_term_type(typename);
650     if(debug) {
651         printf("abac_term_create: \n");
652         printf("  type is %d(%s)\n", type, abac_termtype_string(type));
653         printf("  name is (%s)\n", name);
654         if(cptr)
655             printf("  cptr is (%s)\n", abac_condition_typed_string(cptr));
656             else printf("  there is no cptr\n");
657     }
658
659     abac_term_t *ptr=abac_xmalloc(sizeof(abac_term_t));
660     ptr->isnamed=0;
661     ptr->type=type;
662     ptr->p_name=NULL;
663     /* reformat the term name if it is a time type */
664     if((!cptr) && _abac_termtype_is_time_type(type) && isdigit(name[0])) {
665         char *tmp=abac_term_to_time(name);
666         ptr->name=tmp;
667         } else {
668             ptr->name=abac_xstrdup(name);
669     }
670     ptr->constraint=(cptr==NULL)?NULL:abac_condition_dup(cptr);
671     ptr->cn=NULL;
672     return ptr;
673}
674
675/* called from yy, named principal type */
676abac_term_t *abac_term_named_new(int idtype, char *name)
677{
678     if(debug) {
679         printf("abac_term_named_new: \n");
680         printf("  type is %d(%s)\n", idtype, abac_idtype_string(idtype));
681         printf("  name is (%s)\n", name);
682     }
683     abac_term_t *ptr=abac_xmalloc(sizeof(abac_term_t));
684     ptr->isnamed=1;
685     ptr->type=idtype;
686     ptr->name=abac_xstrdup(name);
687     ptr->p_name=NULL;
688     /* if ABAC_CN, no need to attach p */
689     if(!USE("ABAC_CN"))
690         asprintf(&ptr->p_name,"p%s",name);
691     ptr->constraint=NULL;
692     ptr->cn=NULL;
693     return ptr;
694}
695
696/* seems to be always idtype==e_TERM_PRINCIPAL */
697abac_term_t *abac_term_named_create(char *name)
698{
699     if(debug) {
700         printf("abac_term_named_create: \n");
701         printf("  name is (%s)\n", name);
702     }
703
704     abac_term_t *ptr=abac_xmalloc(sizeof(abac_term_t));
705     ptr->isnamed=1;
706     ptr->type=e_TERM_PRINCIPAL;
707     ptr->name=abac_xstrdup(name);
708     ptr->p_name=NULL;
709     /* if ABAC_CN, no need to attach p */
710     if(!USE("ABAC_CN"))
711         asprintf(&ptr->p_name,"p%s",name);
712     ptr->constraint=NULL;
713     ptr->cn=NULL;
714     return ptr;
715}
716
717abac_term_t *abac_term_dup(abac_term_t *ptr)
718{
719     assert(ptr);
720     abac_term_t *nptr=abac_xmalloc(sizeof(abac_term_t));
721     nptr->isnamed=ptr->isnamed;
722     nptr->type=ptr->type;
723     nptr->name=abac_xstrdup(ptr->name);
724     nptr->p_name=(ptr->p_name==NULL)?NULL:abac_xstrdup(ptr->p_name);
725     if(ptr->constraint!=NULL)
726        nptr->constraint=abac_condition_dup(ptr->constraint);
727        else nptr->constraint=NULL;
728     nptr->cn=(ptr->cn)==NULL?NULL:abac_xstrdup(ptr->cn);
729        nptr->cn=abac_xstrdup(ptr->cn);
730     return nptr;
731}
732
733void abac_term_free(abac_term_t *ptr)
734{
735    if(ptr->constraint) {
736         abac_condition_free(ptr->constraint);
737    }
738    free(ptr->name);
739    if(ptr->p_name) free(ptr->p_name);
740    if(ptr->cn) free(ptr->cn);
741    free(ptr);
742}
743
744abac_term_t *abac_term_add_constraint(abac_term_t *ptr, abac_condition_t *cond)
745{
746    /* make sure the types are matching */
747    if(abac_condition_vartype(cond) != abac_term_type(ptr)) {
748        panic("abac_term_add_constraint: mismatched constraint type with the dataterm\n");
749        return NULL;
750    }
751   
752    if(ptr->constraint) {
753        abac_condition_free(ptr->constraint);
754    }
755    ptr->constraint = abac_condition_dup(cond);
756    return ptr;
757}
758
759/* name or name:cond */
760static char *abac_term_string_with_condition(abac_term_t *ptr)
761{
762    char *tmp=NULL;
763    char *cond=NULL;
764    char *name=abac_term_name(ptr);
765    if(ptr->constraint) {
766       cond=abac_condition_string(ptr->constraint);
767    }
768    if(cond) {
769       asprintf(&tmp,"%s:%s", name, cond);
770       free(cond);
771    } else tmp=abac_xstrdup(name);
772    return tmp;
773}
774
775/* [type:name] or [typed:name:cond] */
776static char *abac_term_typed_string_with_condition(abac_term_t *ptr)
777{
778    char *tmp=NULL;
779    char *cond=NULL;
780    char *name=abac_term_name(ptr);
781    char *type=abac_term_type_name(ptr);
782    if(ptr->constraint) {
783       cond=abac_condition_typed_string(ptr->constraint);
784    }
785    if(cond) {
786       asprintf(&tmp,"[%s:?%s%s]", type, name, cond);
787       free(cond);
788       } else {
789           if(isupper(name[0])) 
790               asprintf(&tmp,"[%s:?%s]", type, name);
791           else asprintf(&tmp,"[%s:%s]", type, name);
792    }
793    if(debug) printf("abac_term_typed_string_with_condition: (%s)\n",tmp);
794    return tmp;
795}
796
797char *abac_term_string(abac_term_t *ptr)
798{
799    return abac_xstrdup(abac_term_name(ptr));
800}
801
802char *abac_term_typed_string(abac_term_t *ptr)
803{
804    return abac_term_typed_string_with_condition(ptr);
805}
806
807/********************************************************************/
808abac_list_t *abac_param_list(abac_param_list_t *ptr)
809{
810    assert(ptr);
811    return ptr->list;
812}
813
814void abac_param_dump(abac_param_list_t *ptr, char *offset)
815{
816    printf("===params===\n");
817    abac_term_t  *cur;
818    abac_list_foreach(ptr->list, cur,
819           abac_term_dump(cur, offset);
820    );
821    printf("============\n");
822}
823
824abac_param_list_t *abac_param_list_new(abac_term_t *term)
825{
826    abac_param_list_t *ptr=abac_xmalloc(sizeof(abac_param_list_t));
827    ptr->list=abac_list_new();
828    abac_list_add(ptr->list, abac_term_dup(term));
829    return ptr;
830}
831
832abac_param_list_t *abac_param_list_free(abac_param_list_t *ptr)
833{
834    abac_list_t *list=ptr->list;
835    abac_term_t  *cur;
836    abac_list_foreach(list, cur,
837           abac_term_free(cur);
838    );
839    abac_list_free(list);
840    free(ptr);
841}
842
843abac_param_list_t *abac_param_list_add_term(abac_param_list_t *ptr, abac_term_t *term)
844{
845    abac_list_t *list=ptr->list;
846    abac_list_add(list, abac_term_dup(term));
847    return ptr;
848}
849
850
851/* term1:cond1,term2:cond2 */
852char* abac_param_list_string_with_condition(abac_param_list_t *ptr)
853{
854    if(ptr->list == NULL)
855        return "";
856
857    abac_term_t  *cur;
858    char *tmp=NULL, *final=NULL;
859    abac_list_foreach(ptr->list, cur,
860           char *s=abac_term_string_with_condition(cur);
861           if(final==NULL) {
862               final=abac_xstrdup(s);
863               } else {
864                  tmp=final;
865                  final=NULL;
866                  asprintf(&final,"%s,%s",tmp,s);
867                  free(tmp);
868           }
869           free(s);
870    );
871    return final;
872}
873
874/* [type:term1:cond1],[type:term2:cond2] */
875char* abac_param_list_typed_string_with_condition(abac_param_list_t *ptr)
876{
877    if(ptr->list == NULL)
878        return "";
879
880    abac_term_t  *cur;
881    char *tmp=NULL, *final=NULL;
882    abac_list_foreach(ptr->list, cur,
883           char *s=abac_term_typed_string_with_condition(cur);
884           if(final==NULL) {
885               final=abac_xstrdup(s);
886               } else {
887                  tmp=final;
888                  final=NULL;
889                  asprintf(&final,"%s,%s",tmp,s);
890                  if(debug) printf("typed param so far, %s\n", final);
891                  free(tmp);
892           }
893           free(s);
894    );
895    if(debug) printf("typed param so far, %s\n", final);
896    return final;
897}
898
899/* term1,term2 */
900char* abac_param_list_string(abac_param_list_t *ptr)
901{
902    abac_list_t *list=ptr->list;
903    if(list == NULL)
904        return "";
905    abac_term_t  *cur;
906    char *final=NULL;
907    char *tmp=NULL;
908
909    /* collect up all the string */
910    abac_list_foreach(list, cur,
911           char *s=abac_term_string(cur);
912           if(final==NULL) {
913               final=abac_xstrdup(s);
914               } else {
915                  tmp=final;
916                  final=NULL;
917                  asprintf(&final,"%s,%s",tmp,s);
918                  if(debug) printf("param so far, %s\n", final);
919                  free(tmp);
920           }
921           free(s);
922    );
923    if(debug) printf("param so far, %s\n", final);
924    return final;
925}
926
927/* make a stack of it for the abac.hh */
928abac_term_t **abac_param_list_vectorize(abac_param_list_t *ptr)
929{
930    abac_term_t **terms=NULL;
931    abac_list_t *list=ptr->list;
932    int cnt=0;
933    if(list != NULL)
934        cnt=abac_list_size(list);
935   
936    // make the array (leave space to NULL terminate it)
937    //      n.b., even if the list is empty, we still return an array that
938    //            only contains the NULL terminator
939    terms = abac_xmalloc(sizeof(abac_term_t *) * (cnt + 1));
940
941    abac_term_t  *cur;
942    int i = 0;
943    if(i<cnt) {
944        abac_list_foreach(list, cur,
945            terms[i++] = abac_term_dup(cur);
946        );
947    }
948    terms[i] = NULL;
949    return terms;
950}
951
952
953void abac_terms_free(abac_term_t **terms)
954{
955    /* always null terminating */
956    assert(terms);
957    int i; 
958    for (i = 0; terms[i] != NULL; ++i) {
959        abac_term_free(terms[i]);
960    }
961    free(terms);
962}
963
964void abac_term_dump(abac_term_t *ptr, char *offset)
965{
966    printf("--- term---\n");
967    printf("%s type : %s\n",offset, abac_term_type_name(ptr));
968    printf("%s name : %s\n",offset, abac_term_name(ptr));
969    printf("%s isnamed: %d\n",offset,ptr->isnamed);
970    if(ptr->p_name)
971        printf("%s p_name: %s\n",offset,ptr->p_name);
972        else printf("%s p_name: --\n",offset);
973    if(ptr->cn)
974        printf("%s cn: %s\n",offset,ptr->cn);
975        else printf("%s cn: --\n",offset);
976    if(ptr->constraint) {
977        char *tmp=NULL;
978        asprintf(&tmp,"  %s", offset);
979        abac_condition_dump(ptr->constraint,offset);
980        free(tmp);
981        } else printf("%s constraint: --\n",offset);
982    printf("-----------\n");
983}
984
Note: See TracBrowser for help on using the repository browser.