source: libabac/abac_param.c @ 9806e76

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

1) add time static constraint
2) add example balltime_rt2_typed
3) change the way that time is being kept

from 20120228T080000 to time(2012,2,28,8,0,0) and
the constraint check is via compare(op,time1,time2)

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