1 | |
---|
2 | /* copied from abac_graph.c |
---|
3 | implementation of the low level using |
---|
4 | yap prolog |
---|
5 | */ |
---|
6 | |
---|
7 | #include <err.h> |
---|
8 | #include <stdio.h> |
---|
9 | #include <assert.h> |
---|
10 | #include <stdlib.h> |
---|
11 | #include <string.h> |
---|
12 | #include <Yap/YapInterface.h> |
---|
13 | |
---|
14 | #include "abac_internal.h" |
---|
15 | |
---|
16 | #include "abac_pl_yap.h" |
---|
17 | #include "abac_util.h" |
---|
18 | |
---|
19 | #include "uthash.h" |
---|
20 | |
---|
21 | extern abac_list_t *abac_credential_clauses(abac_credential_t *); |
---|
22 | extern char *abac_id_clause(abac_id_credential_t *); |
---|
23 | extern abac_aspect_t *abac_yy_get_rule_tail_aspect(); |
---|
24 | extern abac_aspect_t *abac_yy_get_rule_head_aspect(); |
---|
25 | |
---|
26 | static int debug=0; |
---|
27 | |
---|
28 | /* track constraint's external clause's -unique name id */ |
---|
29 | |
---|
30 | static int constraint_label_count=0; |
---|
31 | static char *constraint_label="isABAC_constraint"; |
---|
32 | |
---|
33 | // pl -- place holder for now |
---|
34 | struct _abac_pl_t { |
---|
35 | FILE *fptr; |
---|
36 | char *fname; |
---|
37 | char *yap_certs; |
---|
38 | }; |
---|
39 | |
---|
40 | /***********************************************************/ |
---|
41 | static int _get_next_constraint_label_idx() |
---|
42 | { |
---|
43 | constraint_label_count++; |
---|
44 | return constraint_label_count; |
---|
45 | } |
---|
46 | |
---|
47 | static int _get_constraint_label_idx() |
---|
48 | { |
---|
49 | return constraint_label_count; |
---|
50 | } |
---|
51 | |
---|
52 | /***********************************************************/ |
---|
53 | |
---|
54 | static int _insert_clause(char *str) |
---|
55 | { |
---|
56 | YAP_Term *eterm; |
---|
57 | YAP_Term goalArgs=YAP_ReadBuffer(str, eterm); |
---|
58 | char *tmp=YAP_CompileClause(goalArgs); |
---|
59 | if (tmp!=NULL) { /* something is wrong */ |
---|
60 | printf("error: result of compile clause (%s)\n", tmp); |
---|
61 | printf("error: str used (%s)\n", str); |
---|
62 | return 1; |
---|
63 | } |
---|
64 | return 0; |
---|
65 | } |
---|
66 | |
---|
67 | static int _insert_cred_clause(char *cstr) |
---|
68 | { |
---|
69 | int ret=ABAC_CERT_SUCCESS; |
---|
70 | int rc=_insert_clause(cstr); |
---|
71 | if (rc) |
---|
72 | return ABAC_CERT_BAD_YAP; |
---|
73 | return ABAC_CERT_SUCCESS; |
---|
74 | } |
---|
75 | |
---|
76 | void show_yap_db(const char *msg) |
---|
77 | { |
---|
78 | char lstr[]="listing"; |
---|
79 | YAP_Term *eterm; /* error term */ |
---|
80 | |
---|
81 | printf("\n\n========= yap db (%s)\n",msg); |
---|
82 | YAP_Term goal=YAP_ReadBuffer(lstr, eterm); |
---|
83 | int rc =YAP_RunGoal( goal ); |
---|
84 | if (rc) { |
---|
85 | printf("listing ok.. \n"); |
---|
86 | } else { |
---|
87 | printf("listing's rc is bad.. \n"); |
---|
88 | YAP_Exit(1); |
---|
89 | } |
---|
90 | printf("========= \n\n"); |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Include some utility routines |
---|
95 | */ |
---|
96 | abac_pl_t *abac_pl_utility(void) { |
---|
97 | /* |
---|
98 | append([],L,L). |
---|
99 | append([X|L1],L2,[X|L3]):-append(L1,L2,L3). |
---|
100 | |
---|
101 | appendL([],[]). |
---|
102 | appendL([H|T], L) :- |
---|
103 | appendL(T,L2), append(H,L2,L). |
---|
104 | */ |
---|
105 | if(_insert_clause("append([],L,L)")) |
---|
106 | YAP_Exit(1); |
---|
107 | if(_insert_clause("append([X|L1],L2,[X|L3]):-append(L1,L2,L3)")) |
---|
108 | YAP_Exit(1); |
---|
109 | if(_insert_clause("appendL([],[])")) |
---|
110 | YAP_Exit(1); |
---|
111 | if(_insert_clause("appendL([H|T], L) :- appendL(T,L2), append(H,L2,L)")) |
---|
112 | YAP_Exit(1); |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * Create a new yap structure. |
---|
117 | */ |
---|
118 | abac_pl_t *abac_pl_new(void) { |
---|
119 | |
---|
120 | if (YAP_FastInit(NULL) == YAP_BOOT_ERROR) |
---|
121 | YAP_Exit(1); |
---|
122 | |
---|
123 | if (YAP_RunGoal(YAP_MkAtomTerm(YAP_LookupAtom("source")))) { |
---|
124 | if(debug) fprintf(stderr, "calling source..\n"); |
---|
125 | } else { |
---|
126 | if(debug) fprintf(stderr,"calling source failed..\n"); |
---|
127 | YAP_Exit(1); |
---|
128 | } |
---|
129 | |
---|
130 | abac_pl_utility(); |
---|
131 | |
---|
132 | abac_pl_t *pl = abac_xmalloc(sizeof(abac_pl_t)); |
---|
133 | pl->fptr=NULL; |
---|
134 | pl->fname=NULL; |
---|
135 | pl->yap_certs=NULL; |
---|
136 | return pl; |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | * Add a credential to the db, not duplicating a copy and so |
---|
141 | * don't try to free it after this call. |
---|
142 | */ |
---|
143 | int abac_pl_add_credential(abac_pl_t *pl, abac_credential_t *cred) |
---|
144 | { |
---|
145 | int rc=0; |
---|
146 | abac_list_t *clauses=abac_credential_clauses(cred); |
---|
147 | if (clauses != NULL) { |
---|
148 | char *cur; |
---|
149 | abac_list_foreach(clauses, cur, |
---|
150 | if(cur) { |
---|
151 | if(debug) fprintf(stderr,"inserting =>%s\n",cur); |
---|
152 | rc=_insert_cred_clause(cur); |
---|
153 | } |
---|
154 | ); |
---|
155 | } |
---|
156 | return rc; |
---|
157 | } |
---|
158 | |
---|
159 | int abac_pl_add_type_credential(abac_pl_t *pl, abac_id_credential_t *id_cert) |
---|
160 | { |
---|
161 | char *clause=abac_id_clause(id_cert); |
---|
162 | if (clause != NULL) { |
---|
163 | int rc=_insert_cred_clause(clause); |
---|
164 | return rc; |
---|
165 | } |
---|
166 | return 0; |
---|
167 | } |
---|
168 | |
---|
169 | /* string1(S):- S="abc";S="efg";S="ijk" */ |
---|
170 | char *abac_pl_add_range_constraint_clause(char *var, char *tmplist) |
---|
171 | { |
---|
172 | int i=_get_next_constraint_label_idx(); |
---|
173 | char *tmp=NULL; |
---|
174 | asprintf(&tmp,"%s_%d(%s) :- %s", constraint_label, i, var, tmplist); |
---|
175 | int rc=_insert_cred_clause(tmp); |
---|
176 | free(tmp); |
---|
177 | if(rc) |
---|
178 | panic("abac_pl_add_range_constraint_clause, failed to insert"); |
---|
179 | asprintf(&tmp,"%s_%d(%s)", constraint_label, i, var); |
---|
180 | return tmp; |
---|
181 | } |
---|
182 | |
---|
183 | /* cases, |
---|
184 | ['str'] |
---|
185 | ['str1','str2'] |
---|
186 | ([] is not possible, and don't care) |
---|
187 | */ |
---|
188 | static void _credentials_from_string(abac_stack_t *credentials,char *slist) { |
---|
189 | char *cptr=slist; /* current ptr */ |
---|
190 | char *sptr; /* string ptr */ |
---|
191 | char *ptr; |
---|
192 | int len=0; |
---|
193 | char *string; |
---|
194 | abac_credential_t *cred=NULL; |
---|
195 | int cnt=0; |
---|
196 | |
---|
197 | /* find first [' */ |
---|
198 | ptr=strstr(cptr,"['"); |
---|
199 | if(ptr == NULL) |
---|
200 | return; |
---|
201 | cptr=ptr+2; |
---|
202 | sptr=cptr; |
---|
203 | while (1) { |
---|
204 | /* find next ',' or '] */ |
---|
205 | ptr=strstr(cptr,"','"); |
---|
206 | if(ptr!=NULL) { |
---|
207 | cptr=ptr+3; |
---|
208 | len=(ptr-sptr); |
---|
209 | string=strndup(sptr,len); |
---|
210 | cred=abac_credential_lookup(string); |
---|
211 | free(string); |
---|
212 | if(cred) { |
---|
213 | int i=abac_stack_unique_push(credentials, cred); |
---|
214 | if(i) cnt++; |
---|
215 | } else { |
---|
216 | printf("BAD BAD\n"); |
---|
217 | } |
---|
218 | sptr=cptr; |
---|
219 | } else { |
---|
220 | ptr=strstr(cptr,"']"); |
---|
221 | if(ptr!=NULL) { |
---|
222 | len=(ptr-sptr); |
---|
223 | string=strndup(sptr,len); |
---|
224 | cred=abac_credential_lookup(string); |
---|
225 | free(string); |
---|
226 | if(cred) { |
---|
227 | int i=abac_stack_unique_push(credentials, cred); |
---|
228 | if(i) cnt++; |
---|
229 | } else { |
---|
230 | printf("BAD BAD BAD\n"); |
---|
231 | } |
---|
232 | break; |
---|
233 | } |
---|
234 | } |
---|
235 | } |
---|
236 | if(debug) |
---|
237 | fprintf(stderr,"DEBUG:total %d credentials\n", cnt); |
---|
238 | } |
---|
239 | |
---|
240 | /* MAX 1024x1024, double as it goes */ |
---|
241 | static int try_again(int sz, char **tptr) |
---|
242 | { |
---|
243 | int blk=1024*2; /* 2048 */ |
---|
244 | int max=1024*1024; |
---|
245 | int size; |
---|
246 | char *tmp = NULL; |
---|
247 | |
---|
248 | if(sz==0) { |
---|
249 | size = (sizeof(char) * (blk+1)); |
---|
250 | } else { |
---|
251 | if (sz>=max) { |
---|
252 | size=0; |
---|
253 | *tptr=tmp; |
---|
254 | return 0; |
---|
255 | } |
---|
256 | size=sz*2+1; |
---|
257 | if(size > max) size=max; |
---|
258 | } |
---|
259 | |
---|
260 | tmp = (char *) YAP_AllocSpaceFromYap(size); |
---|
261 | if(tmp==NULL) { |
---|
262 | fprintf(stderr,"ERROR: malloc failed !!!\n"); |
---|
263 | YAP_Exit(1); |
---|
264 | } |
---|
265 | *tptr=tmp; |
---|
266 | return size; |
---|
267 | } |
---|
268 | |
---|
269 | /* make a query and extract just 1 set of result */ |
---|
270 | static abac_stack_t *_make_yap_query(char *prin, char *rule, char* estring) |
---|
271 | { |
---|
272 | YAP_Term *eterm0; |
---|
273 | YAP_Term *eterm1; |
---|
274 | YAP_Term arg[3]; |
---|
275 | |
---|
276 | abac_stack_t *cred_list = abac_stack_new(); |
---|
277 | |
---|
278 | if(debug) fprintf(stderr," the principal part(%s)\n", prin); |
---|
279 | if(prin[0]=='\'' || prin[0]=='"') { |
---|
280 | arg[0]=YAP_ReadBuffer(prin,eterm0); |
---|
281 | } else { |
---|
282 | arg[0]=YAP_MkAtomTerm(YAP_LookupAtom(prin)); |
---|
283 | } |
---|
284 | if(debug) fprintf(stderr," the role/oset part(%s)\n", estring); |
---|
285 | arg[1]=YAP_ReadBuffer(estring,eterm1); |
---|
286 | |
---|
287 | /* var for credential list */ |
---|
288 | arg[2]=YAP_MkVarTerm(); |
---|
289 | |
---|
290 | YAP_Atom f = YAP_LookupAtom("isMember"); |
---|
291 | YAP_Functor func = YAP_MkFunctor(f, 3); |
---|
292 | YAP_Term goal=YAP_MkApplTerm(func, 3, arg); |
---|
293 | |
---|
294 | int rc =YAP_RunGoal( goal ); |
---|
295 | if (rc) { |
---|
296 | printf("YAP query succeed\n"); |
---|
297 | char *tmp=NULL; |
---|
298 | int tmp_sz=try_again(0, &tmp); |
---|
299 | while(1) { |
---|
300 | YAP_WriteBuffer(arg[2], tmp, tmp_sz,YAP_WRITE_HANDLE_VARS); |
---|
301 | if(strlen(tmp) > 5 ) |
---|
302 | break; |
---|
303 | tmp_sz=try_again(tmp_sz,&tmp); |
---|
304 | if(tmp_sz==0) break; |
---|
305 | } |
---|
306 | /* this is returned as ['string1','string2'] */ |
---|
307 | if(debug) fprintf(stderr," query answer : %s(%d)\n", tmp, strlen(tmp)); |
---|
308 | _credentials_from_string(cred_list,tmp); |
---|
309 | if(abac_stack_size(cred_list)==0) { |
---|
310 | fprintf(stderr,"CAN NOT retrieve result properly from YAP!!!\n"); |
---|
311 | YAP_Exit(1); |
---|
312 | } |
---|
313 | YAP_FreeSpaceFromYap(tmp); |
---|
314 | /**** XXX |
---|
315 | while (YAP_RestartGoal()) { |
---|
316 | if(debug) fprintf(stderr,"another success\n"); |
---|
317 | YAP_WriteBuffer(arg[2], tmp, 5000,YAP_WRITE_HANDLE_VARS); |
---|
318 | if(debug) fprintf(stderr," restart query answer : %s\n", tmp); |
---|
319 | _credentials_from_string(cred_list,tmp); |
---|
320 | } |
---|
321 | ***/ |
---|
322 | } else { |
---|
323 | printf("YAP query failed\n"); |
---|
324 | /* YAP_Exit(1); */ |
---|
325 | } |
---|
326 | return cred_list; |
---|
327 | } |
---|
328 | |
---|
329 | /* 2 types |
---|
330 | acme.buys_rocket <- coyote (coyote=prin, acme.buys_rocket=role) |
---|
331 | ==> isMember(coyote,role(acme,buys_rocket), L) |
---|
332 | acme.buys_rocket <- acme.preferred_customer -- NOT valid |
---|
333 | */ |
---|
334 | static abac_stack_t *_query_with_aspect(abac_pl_t *pl, abac_aspect_t* head, abac_aspect_t* tail) |
---|
335 | { |
---|
336 | abac_stack_t *ret=NULL; |
---|
337 | char *tmp=NULL; |
---|
338 | |
---|
339 | if(debug) |
---|
340 | show_yap_db("DEBUG:calling within _query_with_aspect"); |
---|
341 | |
---|
342 | char *nm; |
---|
343 | PROLOG(nm=abac_aspect_principal_name(head);); |
---|
344 | |
---|
345 | /* could be obj or principal */ |
---|
346 | char *prin_nm=NULL; |
---|
347 | if(abac_aspect_is_object(tail)) { |
---|
348 | prin_nm=abac_aspect_object_name(tail); |
---|
349 | } else { |
---|
350 | PROLOG(prin_nm=abac_aspect_principal_name(tail);); |
---|
351 | } |
---|
352 | |
---|
353 | if(abac_aspect_aspect_name(tail)!=NULL) { |
---|
354 | printf("fail, a.o <- b.o and a.r <- a.r query is not implemented yet !!!\n"); |
---|
355 | YAP_Exit(1); |
---|
356 | } |
---|
357 | |
---|
358 | if (prin_nm == NULL || nm == NULL) { |
---|
359 | printf("fail, query's call got bad aspect names .. \n"); |
---|
360 | YAP_Exit(1); |
---|
361 | } |
---|
362 | |
---|
363 | if(debug) fprintf(stderr,"printing up the yap query ..\n"); |
---|
364 | |
---|
365 | char *pstring; |
---|
366 | PROLOG(pstring=abac_aspect_aspect_param_string(head);); |
---|
367 | |
---|
368 | char *stub=abac_aspect_type_string(head); |
---|
369 | if(pstring) { |
---|
370 | asprintf(&tmp,"%s(%s,%s,%s)", stub, nm, abac_aspect_aspect_name(head),pstring); |
---|
371 | free(pstring); |
---|
372 | } else { |
---|
373 | asprintf(&tmp,"%s(%s,%s)", stub, nm, abac_aspect_aspect_name(head)); |
---|
374 | } |
---|
375 | |
---|
376 | ret=_make_yap_query(prin_nm,nm,tmp); |
---|
377 | return ret; |
---|
378 | } |
---|
379 | |
---|
380 | /** |
---|
381 | * Get all the credentials (attribute/issuer cert pairs) from prolog |
---|
382 | * (which returns in string form) |
---|
383 | */ |
---|
384 | abac_stack_t *abac_pl_credentials(abac_pl_t *pl) |
---|
385 | { |
---|
386 | abac_stack_t *ret=abac_verifier_dump_creds(); |
---|
387 | return ret; |
---|
388 | } |
---|
389 | |
---|
390 | abac_stack_t *abac_pl_principals(abac_pl_t *pl) |
---|
391 | { |
---|
392 | abac_stack_t *ret=abac_verifier_dump_principals(); |
---|
393 | return ret; |
---|
394 | } |
---|
395 | |
---|
396 | /** |
---|
397 | * Make a query into prolog db |
---|
398 | --role acme.preferred_customer --principal coyote |
---|
399 | --role acme.prefer_customer.buy_rockets --principlal coyote |
---|
400 | --oset acme.rockets -- object mrx-21 |
---|
401 | --oset acme.villans -- principal coyote |
---|
402 | */ |
---|
403 | abac_stack_t *abac_pl_query(abac_pl_t *pl, char *roleoset, char *prinobj) |
---|
404 | { |
---|
405 | abac_stack_t *ret=NULL; |
---|
406 | int len=strlen(roleoset)+strlen(prinobj)+5; |
---|
407 | char* attr_string=(char *) abac_xmalloc(sizeof(char)*len); |
---|
408 | sprintf(attr_string,"%s<-%s", roleoset, prinobj); |
---|
409 | |
---|
410 | if(debug) |
---|
411 | fprintf(stderr,"abac_pl_query, query string is (%s)\n",attr_string); |
---|
412 | |
---|
413 | /* call into yacc parser */ |
---|
414 | abac_reset_yyfptr(attr_string); |
---|
415 | abac_yy_init(); |
---|
416 | int rc=yyparse(); |
---|
417 | if (rc) { |
---|
418 | free(attr_string); |
---|
419 | return NULL; |
---|
420 | } |
---|
421 | |
---|
422 | abac_aspect_t *head_aspect = abac_yy_get_rule_head_aspect(); |
---|
423 | abac_aspect_t *tail_aspect = abac_yy_get_rule_tail_aspect(); |
---|
424 | |
---|
425 | ret=_query_with_aspect(pl,head_aspect,tail_aspect); |
---|
426 | |
---|
427 | return ret; |
---|
428 | } |
---|
429 | |
---|
430 | abac_stack_t *abac_pl_query_with_structure(abac_pl_t *pl, abac_aspect_t *head_aspect, abac_aspect_t *tail_aspect) |
---|
431 | { |
---|
432 | abac_stack_t *ret=NULL; |
---|
433 | ret=_query_with_aspect(pl,head_aspect,tail_aspect); |
---|
434 | |
---|
435 | return ret; |
---|
436 | } |
---|
437 | |
---|
438 | void abac_pl_free(abac_pl_t *pl) { |
---|
439 | if(pl->fptr) { |
---|
440 | fflush(pl->fptr); |
---|
441 | free(pl->fptr); |
---|
442 | } |
---|
443 | if(pl->fname) { |
---|
444 | unlink(pl->fname); |
---|
445 | free(pl->fname); |
---|
446 | } |
---|
447 | free(pl); |
---|
448 | } |
---|
449 | |
---|