source: libabac/abac_param.c @ d845403

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

1) fix the way param's condition is string-up so the db dump

looks right

(db dump is at best if complete rule set is in or else it is
at best only

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