source: libabac/abac_param.c @ b5a3da4

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

1) add abac_oset.c
2) reorganized some yyparse related files

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