source: libabac/abac_pl_yap.c @ 21aa410

mei_rt2mei_rt2_fix_1meiyap-rt1rt2
Last change on this file since 21aa410 was c586a3c, checked in by Mei <mei@…>, 13 years ago

1) add support for float static range constraint
2) add a testcase for testing float static range constraint
3) add runall-fast (skipping the creddy part)

  • Property mode set to 100644
File size: 12.9 KB
Line 
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_pl.h"
15#include "abac_pl_yap.h"
16
17#include "abac_list.h"
18#include "abac_util.h"
19
20#include "uthash.h"
21
22extern char *abac_cn_with_role(abac_role_t *);
23extern char *abac_cn_with_oset(abac_oset_t *);
24extern abac_list_t *abac_credential_clauses(abac_credential_t *);
25extern char *abac_id_clause(abac_id_cert_t *);
26extern abac_oset_t *abac_yy_get_rule_tail_oset();
27extern abac_oset_t *abac_yy_get_rule_head_oset();
28extern abac_role_t *abac_yy_get_rule_tail_role();
29extern abac_role_t *abac_yy_get_rule_head_role();
30
31
32static int debug=0;
33
34// pl -- place holder for now
35struct _abac_pl_t {
36    FILE *fptr;
37    char *fname;
38    char *yap_certs;
39};
40
41static int _insert_clause(char *str)
42{
43    YAP_Term *eterm;
44    YAP_Term goalArgs=YAP_ReadBuffer(str, eterm);
45    char *tmp=YAP_CompileClause(goalArgs);
46    if (tmp!=NULL) { /* something is wrong */
47        printf("error: result of compile clause (%s)\n", tmp);
48        printf("error: str used (%s)\n", str);
49        return 1;
50    }
51    return 0;
52}
53
54static int _insert_cred_clause(char *cstr)
55{
56    int ret=ABAC_CERT_SUCCESS;
57    int rc=_insert_clause(cstr);
58    if (rc)
59        return ABAC_CERT_BAD_YAP;
60    return ABAC_CERT_SUCCESS;
61}
62
63static void _show_yap_db(char *msg)
64{
65    char lstr[]="listing";
66    YAP_Term *eterm; /* error term */
67
68    printf("\n\n========= yap db (%s)\n",msg);
69    YAP_Term goal=YAP_ReadBuffer(lstr, eterm);
70    int rc =YAP_RunGoal( goal );
71    if (rc) {
72        printf("listing ok.. \n");
73    } else {
74        printf("listing's rc is bad.. \n");
75        YAP_Exit(1);
76    }
77    printf("========= \n\n");
78}
79
80static void _set_dbg(abac_pl_t *pl)
81{
82    char *fname=strdup( "/tmp/abac_yap-XXXXXX.pl" );
83    int fd = mkstemps( pl->fname, 3 );
84    if ( fd == -1) {
85        printf("Couldn't get a valid temp name %s\n", fname);
86        free((void*)fname);
87        YAP_Exit(1);
88    }
89    FILE *fptr = fdopen(fd, "w");
90    if (fptr == NULL) {
91        printf("Couldn't open %s for writing\n", fname);
92        free((void*)fname);
93        YAP_Exit(1);
94    }
95    pl->fptr=fptr;
96    pl->fname=fname;
97}
98
99/**
100 * Include some utility routines
101 */
102abac_pl_t *abac_pl_utility(void) {
103/*
104append([],L,L).
105append([X|L1],L2,[X|L3]):-append(L1,L2,L3).
106
107appendL([],[]).
108appendL([H|T], L) :-
109   appendL(T,L2), append(H,L2,L).
110*/
111    if(_insert_clause("append([],L,L)"))
112        YAP_Exit(1);
113    if(_insert_clause("append([X|L1],L2,[X|L3]):-append(L1,L2,L3)"))
114        YAP_Exit(1);
115    if(_insert_clause("appendL([],[])"))
116        YAP_Exit(1);
117    if(_insert_clause("appendL([H|T], L) :- appendL(T,L2), append(H,L2,L)"))
118        YAP_Exit(1);
119}
120
121/**
122 * Create a new yap structure.
123 */
124abac_pl_t *abac_pl_new(void) {
125
126    if (YAP_FastInit(NULL) == YAP_BOOT_ERROR)
127        YAP_Exit(1);
128
129    if (YAP_RunGoal(YAP_MkAtomTerm(YAP_LookupAtom("source")))) {
130        if(debug) printf("calling source..\n");
131        } else {
132            if(debug) printf("calling source failed..\n");
133            YAP_Exit(1);
134    }
135
136    abac_pl_utility();
137 
138    abac_pl_t *pl = abac_xmalloc(sizeof(abac_pl_t));
139    pl->fptr=NULL;
140    pl->fname=NULL;
141    pl->yap_certs=NULL;
142    return pl;
143}
144
145/**
146 * Add a credential to the db
147 */
148int abac_pl_add_credential(abac_pl_t *pl, abac_credential_t *cred)
149{
150    int rc=0;
151    abac_list_t *clauses=abac_credential_clauses(cred);
152    if (clauses != NULL) {
153        char *cur;
154        abac_list_foreach(clauses, cur,
155            if(cur) {
156                if(debug) printf("inserting =>%s\n",cur);
157                rc=_insert_cred_clause(cur);
158            }
159        );
160    }
161    return rc;
162}
163
164int abac_pl_add_type_credential(abac_pl_t *pl, abac_id_cert_t *id_cert)
165{
166    char *clause=abac_id_clause(id_cert);
167    if (clause != NULL) {
168        int rc=_insert_cred_clause(clause);
169        return rc;
170    }
171    return 0;
172}
173
174/* cases,
175     ['str']
176     ['str1','str2']
177     ([] is not possible, and don't care)
178*/
179static void _credentials_from_string(abac_stack_t *credentials,char *slist) {
180    char *cptr=slist; /* current ptr */
181    char *sptr; /* string ptr */
182    char *ptr;
183    int len=0;
184    char *string;
185    abac_credential_t *cred=NULL;
186    int cnt=0;
187
188    if(debug)
189        printf("DEBUG:responds from yap(%s)\n",slist);
190
191    /* find first [' */ 
192    ptr=strstr(cptr,"['");
193    if(ptr == NULL) 
194         return;
195    cptr=ptr+2;
196    sptr=cptr;
197    while (1) {
198        /* find next ',' or '] */
199        ptr=strstr(cptr,"','");
200        if(ptr!=NULL) {
201             cptr=ptr+3;
202             len=(ptr-sptr);
203             string=strndup(sptr,len);
204             cred=abac_credential_lookup(string);
205             free(string); 
206             if(cred) {
207                 abac_stack_unique_push(credentials, cred);
208                 cnt++;
209                 } else {
210                 printf("BAD BAD BAD\n");
211             }
212             sptr=cptr;
213             } else {
214             ptr=strstr(cptr,"']");
215             if(ptr!=NULL) {
216                 len=(ptr-sptr);
217                 string=strndup(sptr,len);
218                 cred=abac_credential_lookup(string);
219                 free(string);
220                 if(cred) {
221                     abac_stack_unique_push(credentials, cred);
222                     cnt++;
223                     } else {
224                     printf("BAD BAD BAD\n");
225                 }
226                 break;
227             }
228        }
229    }
230    if(debug)
231        printf("DEBUG:total %d credentials\n", cnt);
232}
233
234
235static abac_stack_t *_make_yap_query(char *cn_prin, char *cn_rule, char* estring)
236{
237    YAP_Term *eterm;
238    YAP_Term arg[3];
239    char tmp[5000];
240    abac_stack_t *cred_list = abac_stack_new();
241
242    if(debug) printf(" the role part(%s)\n", estring);
243    /* it is possible that cn_prin might have quotes around it */
244    cn_prin=abac_trim_quotes(cn_prin);
245    arg[0]=YAP_MkAtomTerm(YAP_LookupAtom(cn_prin));
246    if(debug) printf(" the principal part(%s)\n", cn_prin);
247    free(cn_prin);
248    arg[1]=YAP_ReadBuffer(estring,eterm);
249    arg[2]=YAP_MkVarTerm();
250    YAP_Atom f = YAP_LookupAtom("isMember");
251    YAP_Functor func = YAP_MkFunctor(f, 3);
252    YAP_Term goal=YAP_MkApplTerm(func, 3, arg);
253
254    int rc =YAP_RunGoal( goal );
255    if (rc) {
256        printf("YAP query succeed\n");
257        YAP_WriteBuffer(arg[2], tmp, 5000,YAP_WRITE_HANDLE_VARS);
258        if(debug) printf("    query answer : %s\n", tmp);
259        /* this is returned as ['string1','string2'] */
260        _credentials_from_string(cred_list,tmp); 
261/**** XXX somehow this got stuck in yap ???
262        while (YAP_RestartGoal()) {
263            if(debug) printf("another success\n");
264            YAP_WriteBuffer(arg[2], tmp, 5000,YAP_WRITE_HANDLE_VARS);
265            if(debug) printf("    query answer : %s\n", tmp);
266            _credentials_from_string(cred_list,tmp);
267        }
268***/
269    } else {
270        printf("YAP query failed\n");
271        /* YAP_Exit(1); */
272    }
273    return cred_list;
274}
275
276/* 2 types
277      acme.buys_rocket <- coyote (coyote=prin, acme.buys_rocket=role)
278        ==> isMember(coyote,role(acme,buys_rocket), L)
279
280      acme.buys_rocket <- acme.preferred_customer  -- NOT DONE YET */
281static abac_stack_t *_query_with_role(abac_pl_t *pl, abac_role_t* role, abac_role_t* role_prin)
282{
283    char tmp[5000];
284    abac_stack_t *ret=NULL;
285
286    if(debug)
287        _show_yap_db("DEBUG:calling within _query_with_role");
288
289    char *cn_prin=abac_cn_with_role(role_prin);
290    char *cn_role=abac_cn_with_role(role);
291
292    if(abac_role_role_name(role_prin)!=NULL) {
293        printf("fail, a.r <- b.r query is not implemented yet !!!\n");
294        YAP_Exit(1);
295    }
296
297    if (cn_prin == NULL || cn_role == NULL) {
298        printf("fail, query's call got bad roles.. \n");
299        YAP_Exit(1);
300    }
301
302    if(debug) printf("printing up the yap query ..\n");
303    char *pstring=abac_role_role_param_string(role);
304    if(pstring) {
305        sprintf(tmp,"role(%s,%s,%s)", cn_role, abac_role_role_name(role),pstring);
306        free(pstring);
307        } else {
308            sprintf(tmp,"role(%s,%s)", cn_role, abac_role_role_name(role));
309    }
310    ret=_make_yap_query(cn_prin,cn_role,tmp);
311    return ret;
312}
313
314static abac_stack_t *_query_with_oset(abac_pl_t *pl, abac_oset_t* oset, abac_oset_t* oset_obj)
315{
316    char tmp[5000];
317    abac_stack_t *ret=NULL;
318
319    if(debug)
320        _show_yap_db("DEBUG:calling within _query_with_oset");
321
322    char *cn_oset=abac_cn_with_oset(oset);
323    /* could be obj or principal */
324    char *cn_prin=NULL;
325    if(abac_oset_is_object(oset_obj)) {
326        cn_prin=abac_oset_object_cn(oset_obj);
327        } else {
328            cn_prin=abac_oset_principal_cn(oset_obj);
329    }
330
331    if(abac_oset_oset_name(oset_obj)!=NULL) {
332        printf("fail, a.o <- b.o query is not implemented yet !!!\n");
333        YAP_Exit(1);
334    }
335
336    if (cn_prin == NULL || cn_oset == NULL) {
337        printf("fail, query's call got bad oset.. \n");
338        YAP_Exit(1);
339    }
340
341    if(debug) printf("printing up the yap query ..\n");
342    char *pstring=abac_oset_oset_param_string(oset);
343    if(pstring) {
344        sprintf(tmp,"oset(%s,%s,%s)", cn_oset, abac_oset_oset_name(oset),pstring);
345        free(pstring);
346        } else {
347            sprintf(tmp,"oset(%s,%s)", cn_oset, abac_oset_oset_name(oset));
348    }
349
350    ret=_make_yap_query(cn_prin,cn_oset,tmp);
351    return ret;
352}
353
354static abac_stack_t *_dump(abac_pl_t *pl)
355{
356    YAP_Term *eterm;
357    YAP_Term arg[3];
358    char tmp[5000];
359    abac_stack_t *cred_list = abac_stack_new();
360
361    if(debug)
362        _show_yap_db("DEBUG:calling within _dump");
363
364    arg[0]=YAP_MkVarTerm();
365    arg[1]=YAP_MkVarTerm();
366    arg[2]=YAP_MkVarTerm();
367    YAP_Atom f = YAP_LookupAtom("isMember");
368    YAP_Functor func = YAP_MkFunctor(f, 3);
369    YAP_Term goal=YAP_MkApplTerm(func, 3, arg);
370
371    int rc =YAP_RunGoal( goal );
372    if (rc) {
373        printf("YAP dump succeed\n");
374        YAP_WriteBuffer(arg[2], tmp, 5000,YAP_WRITE_HANDLE_VARS);
375        if(debug) printf("    dump answer : %s\n", tmp);
376        /* this is returned as ['string1','string2'] */
377        _credentials_from_string(cred_list,tmp); 
378        while (YAP_RestartGoal()) {
379            if(debug) printf("another dump success\n");
380            YAP_WriteBuffer(arg[2], tmp, 5000,YAP_WRITE_HANDLE_VARS);
381            if(debug) printf("    dump restart answer : %s\n", tmp);
382            _credentials_from_string(cred_list,tmp); 
383        }
384    } else {
385        printf("YAP _dump query failed\n");
386        /* YAP_Exit(1); */
387    }
388    return cred_list;
389}
390
391/* findall(X,isMember(_,_,X),L) */
392static abac_stack_t *_dump2(abac_pl_t *pl)
393{
394    YAP_Term *eterm;
395    YAP_Term arg[3];
396    char tmp[5000];
397    abac_stack_t *cred_list = abac_stack_new();
398
399    if(debug)
400        _show_yap_db("DEBUG:calling within _dump");
401
402    char *estring="isMember(_,_,X)";
403    arg[0]=YAP_MkAtomTerm(YAP_LookupAtom("X"));
404    arg[1]=YAP_ReadBuffer(estring,eterm);
405    arg[2]=YAP_MkVarTerm();
406    YAP_Atom f = YAP_LookupAtom("findall");
407    YAP_Functor func = YAP_MkFunctor(f, 3);
408    YAP_Term goal=YAP_MkApplTerm(func, 3, arg);
409
410    int rc =YAP_RunGoal( goal );
411    if (rc) {
412        printf("YAP dump succeed\n");
413        YAP_WriteBuffer(arg[2], tmp, 5000,YAP_WRITE_HANDLE_VARS);
414        if(debug) printf("    dump answer : %s\n", tmp);
415        /* this is returned as ['string1','string2'] */
416        _credentials_from_string(cred_list,tmp); 
417        } else {
418            printf("YAP _dump query failed\n");
419            /* YAP_Exit(1); */
420    }
421    return cred_list;
422}
423
424/**
425 * Get all the credentials (attribute/issuer cert pairs) from prolog
426 * (which returns in string form)
427 */
428abac_stack_t *abac_pl_credentials(abac_pl_t *pl)
429{
430    abac_stack_t *ret=_dump(pl);
431    return ret;
432}
433
434/**
435 * Make a query into prolog db
436--role acme.preferred_customer --principal coyote
437--role acme.prefer_customer.buy_rockets --principlal coyote
438--oset acme.rockets -- object mrx-21
439--oset acme.villans -- principal coyote
440 */
441abac_stack_t *abac_pl_query(abac_pl_t *pl, char *roleoset, char *prinobj)
442{
443    abac_stack_t *ret=NULL;
444    int len=strlen(roleoset)+strlen(prinobj)+5;
445    char* attr_string=(char *) abac_xmalloc(sizeof(char)*len);
446    sprintf(attr_string,"%s<-%s", roleoset, prinobj);
447
448    if(debug)
449        printf("abac_pl_query, query string is (%s)\n",attr_string);
450
451    /* call into yacc parser */
452    abac_reset_yyfptr(attr_string);
453    abac_yy_init();
454    int rc=yyparse();
455    if (rc) {
456        free(attr_string);
457        return NULL;
458    }
459
460    if(abac_yy_get_rule_is_oset()) {
461        abac_oset_t *head_oset = abac_yy_get_rule_head_oset();
462        abac_oset_t *tail_oset = abac_yy_get_rule_tail_oset();
463        ret=_query_with_oset(pl,head_oset,tail_oset);
464        } else {
465            abac_role_t *head_role = abac_yy_get_rule_head_role();
466            abac_role_t *tail_role = abac_yy_get_rule_tail_role();
467            ret=_query_with_role(pl,head_role,tail_role);
468    }
469    return ret;
470}
471
472void abac_pl_free(abac_pl_t *pl) {
473    if(pl->fptr) {
474        fflush(pl->fptr);
475        free(pl->fptr);
476    }
477    if(pl->fname) {
478        unlink(pl->fname);
479        free(pl->fname);
480    }
481    free(pl);
482}
483
Note: See TracBrowser for help on using the repository browser.