source: libabac/abac_pl_yap.c @ b5a3da4

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

1) add abac_oset.c
2) reorganized some yyparse related files

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