source: libabac/abac_param.c @ 9937351

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

1) add environment variables DUMP_DB, ABAC_CN.

ABAC_CN will switch to using CNs for keyid insead of SHAs

2) add/modified couple of doc files.

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