source: libabac/abac_term.c @ e97d2e2

mei_rt2_fix_1
Last change on this file since e97d2e2 was 646e57e, checked in by Mei <mei@…>, 12 years ago

1) add partial proof

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