source: libabac/abac_param.c @ d5bbd3e

mei_rt2mei_rt2_fix_1meiyap-rt1rt2
Last change on this file since d5bbd3e was 36b100a, checked in by Mei <mei@…>, 12 years ago

1) fix up fruits_rt2_typed example to test different types of strings

as oset term

2) fix the abac_pl_yap.c's _abac_yap_query so the first term if given

a literal string will use ReadBuffer? instead of MkAtomTerm?

  • Property mode set to 100644
File size: 8.3 KB
Line 
1
2/***********************************************************
3   abac_param.c
4
5 dterm=data term of rt1
6 param 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
14#include "abac_list.h"
15#include "abac_util.h"
16
17#include "abac_pl.h"
18
19typedef enum _termtype {
20    e_TERM_INTEGER = 1,
21    e_TERM_URN = 2,
22    e_TERM_FLOAT = 3,
23    e_TERM_BOOLEAN = 4,
24    e_TERM_STRING = 5,
25    e_TERM_TIME = 6,
26    e_TERM_PRINCIPAL = 7,
27    e_TERM_ANONYMOUS = 8
28} termtype_t;
29
30/* termname[dtermtype] */
31static char *const termname[] =
32{
33  "badterm",
34  "integer",
35  "urn",
36  "float",
37  "boolean",
38  "string",
39  "time" ,
40  "principal",
41  "anonymous",
42  0
43};
44static int termname_cnt=8;
45
46typedef enum _condtype_t {
47    e_COND_ROLE = 1,
48    e_COND_OSET = 2,
49    e_COND_RANGE = 3,
50} condtype_t;
51
52/* string will contain either
53     the range literal (static constraint)
54       or
55     the oset/role prologized string (dynamic constraint)
56   union ptr will only be set if it is a dynamic
57   constraint */
58struct _abac_condition_t {
59    int type;
60    char *string; 
61    union {
62       abac_role_t *of_role;
63       abac_oset_t *of_oset;
64    };
65};
66
67struct _abac_term_t {
68    int type;
69    char *name;
70    abac_condition_t *constraint;
71}; 
72
73/* new, free, add one */
74struct _abac_param_list_t {
75    abac_list_t *list;
76};
77
78
79/******************************************************************/
80static abac_condition_t *abac_condition_new(int type,char* condstr,void *cptr)
81{
82     abac_condition_t *ptr=
83             (abac_condition_t *)abac_xmalloc(sizeof(abac_condition_t));
84     ptr->type=type;
85     ptr->string=strdup(condstr);
86     ptr->of_role=cptr;
87     return ptr;
88}
89
90static void abac_condition_free(abac_condition_t *ptr)
91{
92     switch(ptr->type) {
93        case e_COND_OSET:
94            abac_oset_free(ptr->of_oset);
95            break;
96        case e_COND_ROLE:
97            abac_role_free(ptr->of_role);
98            break;
99        case e_COND_RANGE:
100            /* do nothing */
101            break;
102     }
103     if(ptr->string) free(ptr->string);
104     free(ptr);
105}
106
107/* made a copy */
108char *abac_condition_string(abac_condition_t *ptr)
109{
110     char *string=NULL;
111     switch(ptr->type) {
112        case e_COND_OSET:
113            string=abac_oset_string_with_condition(ptr->of_oset);
114            break;
115        case e_COND_ROLE:
116            string=abac_role_string_with_condition(ptr->of_role);
117            break;
118        case e_COND_RANGE:
119            string=strdup(ptr->string);
120            break;
121     }
122     return string;
123}
124
125
126/******************************************************************/
127
128char *abac_term_name(abac_term_t *term)
129{
130    return term->name;
131}
132
133abac_condition_t *abac_term_constraint(abac_term_t *term)
134{
135    return term->constraint;
136}
137
138char *abac_term_type(int i)
139{
140    return termname[i];
141}
142
143int abac_term_is_time_type(int i)
144{
145    if (i == e_TERM_TIME) 
146       return 1;
147    return 0;
148}
149
150int abac_term_is_string_type(int i)
151{
152    if (i == e_TERM_STRING) 
153       return 1;
154    return 0;
155}
156
157int abac_term_is_integer_type(int i)
158{
159    if (i == e_TERM_INTEGER) 
160       return 1;
161    return 0;
162}
163
164int abac_term_is_urn_type(int i)
165{
166    if (i == e_TERM_URN) 
167       return 1;
168    return 0;
169}
170
171int abac_term_verify_term_type(char *type) {
172    int i;
173
174    if (type == NULL)
175        return 0;
176
177    for (i = 1; i <= termname_cnt ; i++)
178        if(strcmp(type,termname[i])==0)
179            return i;
180    return 0;
181}
182
183/* always add p to the name except if its a variable and needed */
184abac_term_t *abac_term_new(int type, char *name, char *cond, void *cptr)
185{
186     abac_condition_t *constraint=NULL;
187     if (cond) {
188         if(type==e_TERM_PRINCIPAL) {
189             constraint=abac_condition_new(e_COND_ROLE,cond,cptr);
190             } else {
191                 if(cptr == NULL)
192                    constraint=abac_condition_new(e_COND_RANGE,cond,NULL);
193                    else
194                        constraint=abac_condition_new(e_COND_OSET,cond,cptr);
195         }
196     }
197     abac_term_t *ptr=abac_xmalloc(sizeof(abac_term_t));
198     ptr->type=type;
199     /* add p if it is a time type */
200     if((!constraint) && abac_term_is_time_type(type) && isdigit(name[0])) {
201         char *tmp=NULL;
202         asprintf(&tmp,"p%s",name);
203         ptr->name=tmp;
204         } else {
205             ptr->name=strdup(name);
206     }
207     ptr->constraint=constraint;
208     return ptr;
209}
210
211void abac_term_free(abac_term_t *ptr)
212{
213    if(ptr->constraint) {
214         abac_condition_free(ptr->constraint);
215    }
216    free(ptr->name);
217    free(ptr);
218}
219
220abac_term_t *abac_term_add_constraint(abac_term_t *ptr, abac_condition_t *cond)
221{
222    if(ptr->constraint) {
223        abac_condition_free(ptr->constraint);
224    }
225    ptr->constraint = cond;
226    return ptr;
227}
228
229/* name or name:cond */
230static char *abac_term_string_with_condition(abac_term_t *ptr)
231{
232    char *tmp=NULL;
233    char *cond=NULL;
234    char *name=ptr->name;
235    int len=strlen(name)+1;
236    if(ptr->constraint) {
237       cond=abac_condition_string(ptr->constraint);
238       len=len+strlen(cond)+1;
239    }
240    tmp = abac_xmalloc(len);
241    if(cond) {
242       sprintf(tmp,"%s:%s", name, cond);
243       free(cond);
244    } else
245           sprintf(tmp,"%s", name);
246    return tmp;
247}
248
249/* len of name or name:cond */
250static int abac_term_len_with_condition(abac_term_t *ptr)
251{
252    char *cond=NULL;
253    char *name=ptr->name;
254    int len=strlen(name);
255    if(ptr->constraint) {
256       cond=abac_condition_string(ptr->constraint);
257       len=len+strlen(cond)+1;
258       free(cond); 
259    }
260    return len;
261}
262
263/* just name */
264static char *abac_term_string(abac_term_t *ptr)
265{
266    if(ptr->type == abac_term_verify_term_type("urn") ||
267              ptr->type == abac_term_verify_term_type("string") ) {
268        char *str;
269        asprintf(&str, "%s", ptr->name);
270        return str;
271    }
272    return strdup(ptr->name);
273}
274
275/* just len of name */
276static int abac_term_len(abac_term_t *ptr)
277{
278    /* including the 2 ' */
279    if(ptr->type == abac_term_verify_term_type("urn") ||
280              ptr->type == abac_term_verify_term_type("string") ) {
281        return strlen(ptr->name)+2; 
282    }
283    return strlen(ptr->name);
284}
285
286/********************************************************************/
287abac_param_list_t *abac_param_list_new(abac_term_t *term)
288{
289    abac_param_list_t *ptr=abac_xmalloc(sizeof(abac_param_list_t));
290    ptr->list=abac_list_new();
291    abac_list_add(ptr->list, term);
292    return ptr;
293}
294
295abac_param_list_t *abac_param_list_free(abac_param_list_t *ptr)
296{
297    abac_list_t *list=ptr->list;
298    abac_term_t  *cur;
299    abac_list_foreach(list, cur,
300           abac_term_free(cur);
301    );
302    abac_list_free(list);
303    free(ptr);
304}
305
306abac_param_list_t *abac_param_list_add_term(abac_param_list_t *ptr, abac_term_t *term)
307{
308    abac_list_t *list=ptr->list;
309    abac_list_add(list, term);
310    return ptr;
311}
312
313
314/* param1:cond1,param2:cond2 */
315char* abac_param_list_string_with_condition(abac_param_list_t *ptr)
316{
317    if(ptr->list == NULL)
318        return "";
319
320    abac_term_t  *cur;
321    char *tmp;
322    int len=0;
323    /* collect up all the lens so can allocate once */
324    abac_list_foreach(ptr->list, cur,
325           int l=abac_term_len_with_condition(cur);
326           len=len+l+1;
327    );
328    tmp=abac_xmalloc((len+1) * sizeof(char));
329    /* collect up all the string */
330    int first=1;
331    abac_list_foreach(ptr->list, cur,
332           char *s=abac_term_string_with_condition(cur);
333           if(first) {
334               strcpy(tmp,s);
335               first=0;
336               } else {
337                  strcat(tmp,",");
338                  strcat(tmp,s);
339           }
340           free(s);
341    );
342    return tmp;
343}
344
345/* param1,param2 */
346char* abac_param_list_string(abac_param_list_t *ptr)
347{
348    abac_list_t *list=ptr->list;
349    if(list == NULL)
350        return "";
351    abac_term_t  *cur;
352    char *tmp;
353    int len=0;
354    /* collect up all the lens so can allocate once */
355    abac_list_foreach(list, cur,
356           // collect up the len
357           int l=abac_term_len(cur);
358           len=len+l+1;
359    );
360    tmp=abac_xmalloc((len+1) * sizeof(char));
361    /* collect up all the string */
362    int first=1;
363    abac_list_foreach(list, cur,
364           char *s=abac_term_string(cur);
365           if(first) {
366               strcpy(tmp,s);
367               first=0;
368               } else {
369                  strcat(tmp,",");
370                  strcat(tmp,s);
371           }
372           free(s);
373    );
374    return tmp;
375}
376
377
Note: See TracBrowser for help on using the repository browser.