source: libabac/abac_pl_yap.c @ 36b100a

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

1) fix up fruits_rt2_typed example to test different types of strings

as oset term

2) fix the abac_pl_yap.c's _abac_yap_query so the first term if given

a literal string will use ReadBuffer? instead of MkAtomTerm?

  • Property mode set to 100644
File size: 13.5 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
234static abac_stack_t *_make_yap_query_simple(char *cn_prin, char *cn_rule, char* estring)
235{
236    YAP_Term *eterm;
237    abac_stack_t *cred_list = abac_stack_new();
238    char *lstr=NULL;
239
240    asprintf(&lstr,"isMember(%s,%s,_)",cn_prin, estring);
241    if(debug)
242        printf("make_yap_query_simple --(%s)\n",lstr);
243
244    YAP_Term goal=YAP_ReadBuffer(lstr, eterm);
245    int rc =YAP_RunGoal( goal );
246    if (rc) {
247        printf("simple call ok.. \n");
248    } else {
249        printf("simple query call not good.. \n");
250        YAP_Exit(1);
251    }
252    printf("========= \n\n");
253    return cred_list;
254}
255
256static abac_stack_t *_make_yap_query(char *cn_prin, char *cn_rule, char* estring)
257{
258    YAP_Term *eterm0;
259    YAP_Term *eterm1;
260    YAP_Term arg[3];
261    char tmp[5000];
262    abac_stack_t *cred_list = abac_stack_new();
263
264    if(debug) printf(" the principal part(%s)\n", cn_prin);
265    if(cn_prin[0]=='\'' || cn_prin[0]=='"') {
266        arg[0]=YAP_ReadBuffer(cn_prin,eterm0);
267        } else {
268            arg[0]=YAP_MkAtomTerm(YAP_LookupAtom(cn_prin)); 
269    }
270    if(debug) printf(" the role/oset part(%s)\n", estring);
271    arg[1]=YAP_ReadBuffer(estring,eterm1);
272
273    /* var for credential list */
274    arg[2]=YAP_MkVarTerm();
275
276    YAP_Atom f = YAP_LookupAtom("isMember");
277    YAP_Functor func = YAP_MkFunctor(f, 3);
278    YAP_Term goal=YAP_MkApplTerm(func, 3, arg);
279
280    int rc =YAP_RunGoal( goal );
281    if (rc) {
282        printf("YAP query succeed\n");
283        YAP_WriteBuffer(arg[2], tmp, 5000,YAP_WRITE_HANDLE_VARS);
284        if(debug) printf("    query answer : %s\n", tmp);
285        /* this is returned as ['string1','string2'] */
286        _credentials_from_string(cred_list,tmp); 
287/**** XXX somehow this got stuck in yap ???
288        while (YAP_RestartGoal()) {
289            if(debug) printf("another success\n");
290            YAP_WriteBuffer(arg[2], tmp, 5000,YAP_WRITE_HANDLE_VARS);
291            if(debug) printf("    query answer : %s\n", tmp);
292            _credentials_from_string(cred_list,tmp);
293        }
294***/
295    } else {
296        printf("YAP query failed\n");
297        /* YAP_Exit(1); */
298    }
299    return cred_list;
300}
301
302/* 2 types
303      acme.buys_rocket <- coyote (coyote=prin, acme.buys_rocket=role)
304        ==> isMember(coyote,role(acme,buys_rocket), L)
305
306      acme.buys_rocket <- acme.preferred_customer  -- NOT DONE YET */
307static abac_stack_t *_query_with_role(abac_pl_t *pl, abac_role_t* role, abac_role_t* role_prin)
308{
309    char tmp[5000];
310    abac_stack_t *ret=NULL;
311
312    if(debug)
313        _show_yap_db("DEBUG:calling within _query_with_role");
314
315    char *cn_prin=abac_cn_with_role(role_prin);
316    char *cn_role=abac_cn_with_role(role);
317
318    if(abac_role_role_name(role_prin)!=NULL) {
319        printf("fail, a.r <- b.r query is not implemented yet !!!\n");
320        YAP_Exit(1);
321    }
322
323    if (cn_prin == NULL || cn_role == NULL) {
324        printf("fail, query's call got bad roles.. \n");
325        YAP_Exit(1);
326    }
327
328    if(debug) printf("printing up the yap query ..\n");
329    char *pstring=abac_role_role_param_string(role);
330    if(pstring) {
331        sprintf(tmp,"role(%s,%s,%s)", cn_role, abac_role_role_name(role),pstring);
332        free(pstring);
333        } else {
334            sprintf(tmp,"role(%s,%s)", cn_role, abac_role_role_name(role));
335    }
336    ret=_make_yap_query(cn_prin,cn_role,tmp);
337    return ret;
338}
339
340static abac_stack_t *_query_with_oset(abac_pl_t *pl, abac_oset_t* oset, abac_oset_t* oset_obj)
341{
342    char tmp[5000];
343    abac_stack_t *ret=NULL;
344
345    if(debug)
346        _show_yap_db("DEBUG:calling within _query_with_oset");
347
348    char *cn_oset=abac_cn_with_oset(oset);
349    /* could be obj or principal */
350    char *cn_prin=NULL;
351    if(abac_oset_is_object(oset_obj)) {
352        cn_prin=abac_oset_object_cn(oset_obj);
353        } else {
354            cn_prin=abac_oset_principal_cn(oset_obj);
355    }
356
357    if(abac_oset_oset_name(oset_obj)!=NULL) {
358        printf("fail, a.o <- b.o query is not implemented yet !!!\n");
359        YAP_Exit(1);
360    }
361
362    if (cn_prin == NULL || cn_oset == NULL) {
363        printf("fail, query's call got bad oset.. \n");
364        YAP_Exit(1);
365    }
366
367    if(debug) printf("printing up the yap query ..\n");
368    char *pstring=abac_oset_oset_param_string(oset);
369    if(pstring) {
370        sprintf(tmp,"oset(%s,%s,%s)", cn_oset, abac_oset_oset_name(oset),pstring);
371        free(pstring);
372        } else {
373            sprintf(tmp,"oset(%s,%s)", cn_oset, abac_oset_oset_name(oset));
374    }
375
376    ret=_make_yap_query(cn_prin,cn_oset,tmp);
377    return ret;
378}
379
380static abac_stack_t *_dump(abac_pl_t *pl)
381{
382    YAP_Term *eterm;
383    YAP_Term arg[3];
384    char tmp[5000];
385    abac_stack_t *cred_list = abac_stack_new();
386
387    if(debug)
388        _show_yap_db("DEBUG:calling within _dump");
389
390    arg[0]=YAP_MkVarTerm();
391    arg[1]=YAP_MkVarTerm();
392    arg[2]=YAP_MkVarTerm();
393    YAP_Atom f = YAP_LookupAtom("isMember");
394    YAP_Functor func = YAP_MkFunctor(f, 3);
395    YAP_Term goal=YAP_MkApplTerm(func, 3, arg);
396
397    int rc =YAP_RunGoal( goal );
398    if (rc) {
399        printf("YAP dump succeed\n");
400        YAP_WriteBuffer(arg[2], tmp, 5000,YAP_WRITE_HANDLE_VARS);
401        if(debug) printf("    dump answer : %s\n", tmp);
402        /* this is returned as ['string1','string2'] */
403        _credentials_from_string(cred_list,tmp); 
404        while (YAP_RestartGoal()) {
405            if(debug) printf("another dump success\n");
406            YAP_WriteBuffer(arg[2], tmp, 5000,YAP_WRITE_HANDLE_VARS);
407            if(debug) printf("    dump restart answer : %s\n", tmp);
408            _credentials_from_string(cred_list,tmp); 
409        }
410    } else {
411        printf("YAP _dump query failed\n");
412        /* YAP_Exit(1); */
413    }
414    return cred_list;
415}
416
417/* findall(X,isMember(_,_,X),L) */
418static abac_stack_t *_dump2(abac_pl_t *pl)
419{
420    YAP_Term *eterm;
421    YAP_Term arg[3];
422    char tmp[5000];
423    abac_stack_t *cred_list = abac_stack_new();
424
425    if(debug)
426        _show_yap_db("DEBUG:calling within _dump");
427
428    char *estring="isMember(_,_,X)";
429    arg[0]=YAP_MkAtomTerm(YAP_LookupAtom("X"));
430    arg[1]=YAP_ReadBuffer(estring,eterm);
431    arg[2]=YAP_MkVarTerm();
432    YAP_Atom f = YAP_LookupAtom("findall");
433    YAP_Functor func = YAP_MkFunctor(f, 3);
434    YAP_Term goal=YAP_MkApplTerm(func, 3, arg);
435
436    int rc =YAP_RunGoal( goal );
437    if (rc) {
438        printf("YAP dump succeed\n");
439        YAP_WriteBuffer(arg[2], tmp, 5000,YAP_WRITE_HANDLE_VARS);
440        if(debug) printf("    dump answer : %s\n", tmp);
441        /* this is returned as ['string1','string2'] */
442        _credentials_from_string(cred_list,tmp); 
443        } else {
444            printf("YAP _dump query failed\n");
445            /* YAP_Exit(1); */
446    }
447    return cred_list;
448}
449
450/**
451 * Get all the credentials (attribute/issuer cert pairs) from prolog
452 * (which returns in string form)
453 */
454abac_stack_t *abac_pl_credentials(abac_pl_t *pl)
455{
456    abac_stack_t *ret=_dump(pl);
457    return ret;
458}
459
460/**
461 * Make a query into prolog db
462--role acme.preferred_customer --principal coyote
463--role acme.prefer_customer.buy_rockets --principlal coyote
464--oset acme.rockets -- object mrx-21
465--oset acme.villans -- principal coyote
466 */
467abac_stack_t *abac_pl_query(abac_pl_t *pl, char *roleoset, char *prinobj)
468{
469    abac_stack_t *ret=NULL;
470    int len=strlen(roleoset)+strlen(prinobj)+5;
471    char* attr_string=(char *) abac_xmalloc(sizeof(char)*len);
472    sprintf(attr_string,"%s<-%s", roleoset, prinobj);
473
474    if(debug)
475        printf("abac_pl_query, query string is (%s)\n",attr_string);
476
477    /* call into yacc parser */
478    abac_reset_yyfptr(attr_string);
479    abac_yy_init();
480    int rc=yyparse();
481    if (rc) {
482        free(attr_string);
483        return NULL;
484    }
485
486    if(abac_yy_get_rule_is_oset()) {
487        abac_oset_t *head_oset = abac_yy_get_rule_head_oset();
488        abac_oset_t *tail_oset = abac_yy_get_rule_tail_oset();
489        ret=_query_with_oset(pl,head_oset,tail_oset);
490        } else {
491            abac_role_t *head_role = abac_yy_get_rule_head_role();
492            abac_role_t *tail_role = abac_yy_get_rule_tail_role();
493            ret=_query_with_role(pl,head_role,tail_role);
494    }
495    return ret;
496}
497
498void abac_pl_free(abac_pl_t *pl) {
499    if(pl->fptr) {
500        fflush(pl->fptr);
501        free(pl->fptr);
502    }
503    if(pl->fname) {
504        unlink(pl->fname);
505        free(pl->fname);
506    }
507    free(pl);
508}
509
Note: See TracBrowser for help on using the repository browser.