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