[e95d652] | 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 | |
---|
[8bd77b5] | 14 | #include "abac_internal.h" |
---|
[e95d652] | 15 | |
---|
[8bd77b5] | 16 | #include "abac_pl_yap.h" |
---|
[e95d652] | 17 | #include "abac_util.h" |
---|
| 18 | |
---|
| 19 | #include "uthash.h" |
---|
| 20 | |
---|
[da5afdf] | 21 | extern abac_list_t *abac_credential_clauses(abac_credential_t *); |
---|
[8bd77b5] | 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(); |
---|
[0d0c3a9] | 25 | |
---|
[202a7f9] | 26 | static int debug=0; |
---|
[8bd77b5] | 27 | |
---|
[d5bbd3e] | 28 | /* track constraint's external clause's -unique name id */ |
---|
[8bd77b5] | 29 | |
---|
[d5bbd3e] | 30 | static int constraint_label_count=0; |
---|
| 31 | static char *constraint_label="isABAC_constraint"; |
---|
[e95d652] | 32 | |
---|
[646e57e] | 33 | /* |
---|
| 34 | x can pLadybug buy_rockets from pAcme ? (_arg_for_fact) |
---|
| 35 | x can pLadybug What from pAcme ? (_arg_for_what) |
---|
| 36 | x Who can buy_rockets from pAcme ? (_arg_for_who) |
---|
| 37 | x can pLadybug buy_rockets from Whom ? (_arg_for_whom) |
---|
| 38 | x can pLadybug What from Whom ? (_arg_for_what_whom) |
---|
| 39 | the type ordering also dictates the order of partial proof query |
---|
| 40 | sequence |
---|
| 41 | */ |
---|
| 42 | enum _query_type_t { |
---|
| 43 | e_PL_FACT=0, |
---|
| 44 | e_PL_WHAT=1, |
---|
| 45 | e_PL_WHAT_DFLT=2, |
---|
| 46 | e_PL_WHO=3, |
---|
| 47 | e_PL_WHOM=4, |
---|
| 48 | e_PL_WHAT_WHOM=5, |
---|
| 49 | e_PL_NOMORE=6 |
---|
| 50 | }; |
---|
| 51 | |
---|
[a59bc06] | 52 | static const char *const _queryname[] = |
---|
| 53 | { |
---|
| 54 | "fact", |
---|
| 55 | "what", |
---|
| 56 | "what_default", |
---|
| 57 | "who", |
---|
| 58 | "whom", |
---|
| 59 | "what_whom", |
---|
| 60 | "nomore" |
---|
| 61 | }; |
---|
| 62 | static int _queryname_cnt=6; |
---|
| 63 | |
---|
| 64 | |
---|
[da5afdf] | 65 | // pl -- place holder for now |
---|
[e95d652] | 66 | struct _abac_pl_t { |
---|
[abf8d5d] | 67 | /* a_term <- prin */ |
---|
[646e57e] | 68 | char *whom; |
---|
| 69 | char *who; |
---|
| 70 | char *aspect; |
---|
| 71 | char *what; |
---|
| 72 | char *whatwith; |
---|
| 73 | int pcnt; /* pcnt=number of params in whatwith */ |
---|
| 74 | |
---|
[abf8d5d] | 75 | int query_made; /* 1+ yes, 0 no, rc is valid only if a query is made */ |
---|
| 76 | int query_rc; /* rc of query call */ |
---|
| 77 | int next_rc; /* rc of next-last query */ |
---|
| 78 | /* proof count = abac_list_size(proof_list) */ |
---|
[646e57e] | 79 | int is_partial; |
---|
| 80 | int want_partial; |
---|
| 81 | int query_type; |
---|
[abf8d5d] | 82 | abac_list_t *proof_list; /* list of list of cred_list */ |
---|
| 83 | long safe_t; /* holding the YAP slot for the goal */ |
---|
[e95d652] | 84 | }; |
---|
| 85 | |
---|
[d5bbd3e] | 86 | /***********************************************************/ |
---|
| 87 | static int _get_next_constraint_label_idx() |
---|
| 88 | { |
---|
| 89 | constraint_label_count++; |
---|
| 90 | return constraint_label_count; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | static int _get_constraint_label_idx() |
---|
| 94 | { |
---|
| 95 | return constraint_label_count; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /***********************************************************/ |
---|
| 99 | |
---|
[53e540d] | 100 | static int _insert_clause(char *str) |
---|
[e95d652] | 101 | { |
---|
[dfe6b61] | 102 | YAP_Term eterm; |
---|
| 103 | YAP_Term goalArgs=YAP_ReadBuffer(str, &eterm); |
---|
[53e540d] | 104 | char *tmp=YAP_CompileClause(goalArgs); |
---|
| 105 | if (tmp!=NULL) { /* something is wrong */ |
---|
| 106 | printf("error: result of compile clause (%s)\n", tmp); |
---|
| 107 | printf("error: str used (%s)\n", str); |
---|
| 108 | return 1; |
---|
[e95d652] | 109 | } |
---|
[53e540d] | 110 | return 0; |
---|
[e95d652] | 111 | } |
---|
| 112 | |
---|
[53e540d] | 113 | static int _insert_cred_clause(char *cstr) |
---|
[e95d652] | 114 | { |
---|
| 115 | int ret=ABAC_CERT_SUCCESS; |
---|
[53e540d] | 116 | int rc=_insert_clause(cstr); |
---|
| 117 | if (rc) |
---|
| 118 | return ABAC_CERT_BAD_YAP; |
---|
| 119 | return ABAC_CERT_SUCCESS; |
---|
[e95d652] | 120 | } |
---|
| 121 | |
---|
[440ba20] | 122 | void show_yap_db(const char *msg) |
---|
[e95d652] | 123 | { |
---|
| 124 | char lstr[]="listing"; |
---|
[dfe6b61] | 125 | YAP_Term eterm; /* error term */ |
---|
[e95d652] | 126 | |
---|
[a7f03f3] | 127 | printf("\n\n/* ========= yap db (%s) */\n",msg); |
---|
[dfe6b61] | 128 | YAP_Term goal=YAP_ReadBuffer(lstr, &eterm); |
---|
[e95d652] | 129 | int rc =YAP_RunGoal( goal ); |
---|
| 130 | if (rc) { |
---|
[5d1b6dc] | 131 | printf("/* listing ok.. */\n"); |
---|
[e95d652] | 132 | } else { |
---|
[5d1b6dc] | 133 | printf("/* listing's rc is bad.. */ \n"); |
---|
[e95d652] | 134 | YAP_Exit(1); |
---|
| 135 | } |
---|
[a7f03f3] | 136 | printf("/* ========= */\n\n"); |
---|
[e95d652] | 137 | } |
---|
| 138 | |
---|
[53e540d] | 139 | /** |
---|
| 140 | * Include some utility routines |
---|
| 141 | */ |
---|
| 142 | abac_pl_t *abac_pl_utility(void) { |
---|
| 143 | /* |
---|
| 144 | append([],L,L). |
---|
| 145 | append([X|L1],L2,[X|L3]):-append(L1,L2,L3). |
---|
| 146 | |
---|
| 147 | appendL([],[]). |
---|
| 148 | appendL([H|T], L) :- |
---|
| 149 | appendL(T,L2), append(H,L2,L). |
---|
| 150 | */ |
---|
| 151 | if(_insert_clause("append([],L,L)")) |
---|
| 152 | YAP_Exit(1); |
---|
| 153 | if(_insert_clause("append([X|L1],L2,[X|L3]):-append(L1,L2,L3)")) |
---|
| 154 | YAP_Exit(1); |
---|
| 155 | if(_insert_clause("appendL([],[])")) |
---|
| 156 | YAP_Exit(1); |
---|
| 157 | if(_insert_clause("appendL([H|T], L) :- appendL(T,L2), append(H,L2,L)")) |
---|
| 158 | YAP_Exit(1); |
---|
[e95d652] | 159 | } |
---|
| 160 | |
---|
[53e540d] | 161 | /** |
---|
| 162 | * Create a new yap structure. |
---|
| 163 | */ |
---|
| 164 | abac_pl_t *abac_pl_new(void) { |
---|
| 165 | |
---|
| 166 | if (YAP_FastInit(NULL) == YAP_BOOT_ERROR) |
---|
| 167 | YAP_Exit(1); |
---|
| 168 | |
---|
| 169 | if (YAP_RunGoal(YAP_MkAtomTerm(YAP_LookupAtom("source")))) { |
---|
[2485307] | 170 | if(debug) fprintf(stderr, "calling source..\n"); |
---|
[53e540d] | 171 | } else { |
---|
[2485307] | 172 | if(debug) fprintf(stderr,"calling source failed..\n"); |
---|
[202a7f9] | 173 | YAP_Exit(1); |
---|
[53e540d] | 174 | } |
---|
| 175 | |
---|
| 176 | abac_pl_utility(); |
---|
| 177 | |
---|
| 178 | abac_pl_t *pl = abac_xmalloc(sizeof(abac_pl_t)); |
---|
[646e57e] | 179 | |
---|
| 180 | pl->who=NULL; |
---|
| 181 | pl->whom = NULL; |
---|
| 182 | pl->what = NULL; |
---|
| 183 | pl->whatwith = NULL; |
---|
| 184 | pl->aspect = NULL; |
---|
| 185 | pl->pcnt=0; |
---|
[abf8d5d] | 186 | pl->query_made=0; |
---|
| 187 | pl->query_rc=0; |
---|
[646e57e] | 188 | pl->is_partial=0; |
---|
| 189 | pl->want_partial=1; |
---|
| 190 | pl->query_type=e_PL_FACT; |
---|
[abf8d5d] | 191 | pl->next_rc=0; |
---|
| 192 | pl->proof_list=NULL; |
---|
| 193 | pl->safe_t=0; |
---|
[53e540d] | 194 | return pl; |
---|
[e95d652] | 195 | } |
---|
| 196 | |
---|
[646e57e] | 197 | int abac_pl_returning_partial(abac_pl_t *pl) |
---|
| 198 | { |
---|
| 199 | return pl->is_partial; |
---|
| 200 | } |
---|
| 201 | |
---|
[abf8d5d] | 202 | /* returns the number of solution so far */ |
---|
| 203 | int abac_pl_proof_cnt(abac_pl_t *pl) |
---|
| 204 | { |
---|
| 205 | if(pl->proof_list) |
---|
| 206 | return abac_list_size(pl->proof_list); |
---|
| 207 | return 0; |
---|
| 208 | } |
---|
| 209 | |
---|
[646e57e] | 210 | int abac_pl_is_partial(abac_pl_t *pl) |
---|
| 211 | { |
---|
| 212 | return pl->is_partial; |
---|
| 213 | } |
---|
| 214 | |
---|
[abf8d5d] | 215 | /* is this the conclusion of the query ? */ |
---|
| 216 | /* 1 is yes, 0 N/A */ |
---|
| 217 | int abac_pl_done_yet(abac_pl_t *pl) |
---|
| 218 | { |
---|
| 219 | if(pl->query_made) { |
---|
| 220 | if(pl->query_rc) { |
---|
| 221 | if(!pl->next_rc) return 1; |
---|
| 222 | return 0; |
---|
| 223 | } else return 1; /* even init query failed */ |
---|
| 224 | } |
---|
| 225 | return 0; |
---|
| 226 | } |
---|
| 227 | |
---|
[646e57e] | 228 | void abac_pl_set_no_partial(abac_pl_t *pl) |
---|
| 229 | { |
---|
| 230 | pl->want_partial=0; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | void abac_pl_set_want_partial(abac_pl_t *pl) |
---|
| 234 | { |
---|
| 235 | pl->want_partial=1; |
---|
| 236 | } |
---|
| 237 | |
---|
[abf8d5d] | 238 | static void _free_p_list(abac_list_t *p) |
---|
| 239 | { |
---|
| 240 | if( p != NULL ) { |
---|
| 241 | if(abac_list_size(p) != 0) { |
---|
| 242 | abac_credential_t *cur=NULL; |
---|
| 243 | abac_list_foreach(p, cur, |
---|
| 244 | abac_credential_free(cur); |
---|
| 245 | ); |
---|
| 246 | } |
---|
| 247 | abac_list_free(p); |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | static void _free_pf_list(abac_list_t *p) |
---|
| 252 | { |
---|
| 253 | |
---|
| 254 | if(p != NULL) { |
---|
| 255 | if(abac_list_size(p) != 0) { |
---|
| 256 | abac_list_t *cur=NULL; |
---|
| 257 | abac_list_foreach(p, cur, |
---|
| 258 | _free_p_list(cur); |
---|
| 259 | ); |
---|
| 260 | } |
---|
| 261 | abac_list_free(p); |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | void abac_pl_free(abac_pl_t *pl) { |
---|
[646e57e] | 266 | if(pl->who) free(pl->who); |
---|
| 267 | if(pl->whom) free(pl->whom); |
---|
| 268 | if(pl->what) free(pl->what); |
---|
| 269 | if(pl->whatwith) free(pl->whatwith); |
---|
| 270 | if(pl->aspect) free(pl->aspect); |
---|
[abf8d5d] | 271 | _free_pf_list(pl->proof_list); |
---|
| 272 | free(pl); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | /* this is done whenever a new query starts in */ |
---|
[646e57e] | 276 | static void _reset_pl(abac_pl_t *pl, char *who, |
---|
| 277 | char *what, char *whom, char *aspect, char *whatwith, int pcnt) |
---|
[abf8d5d] | 278 | { |
---|
| 279 | if(pl->safe_t) { |
---|
| 280 | YAP_RecoverSlots(1); |
---|
| 281 | YAP_Reset(); |
---|
| 282 | } |
---|
| 283 | /* free the old ones */ |
---|
[646e57e] | 284 | if(pl->who) free(pl->who); |
---|
| 285 | if(pl->whom) free(pl->whom); |
---|
| 286 | if(pl->what) free(pl->what); |
---|
| 287 | if(pl->whatwith) free(pl->whatwith); |
---|
| 288 | if(pl->aspect) free(pl->aspect); |
---|
| 289 | |
---|
| 290 | pl->who = (who !=NULL)?abac_xstrdup(who):NULL; |
---|
| 291 | pl->whom = (whom !=NULL)?abac_xstrdup(whom):NULL; |
---|
| 292 | pl->what = (what!=NULL)?abac_xstrdup(what):NULL; |
---|
| 293 | pl->whatwith = (whatwith!=NULL)?abac_xstrdup(whatwith):NULL; |
---|
| 294 | pl->aspect = (aspect!=NULL)?abac_xstrdup(aspect):NULL; |
---|
| 295 | |
---|
| 296 | pl->pcnt=pcnt; |
---|
| 297 | |
---|
[abf8d5d] | 298 | pl->query_made=0; |
---|
| 299 | pl->query_rc=0; |
---|
| 300 | pl->next_rc=0; |
---|
[646e57e] | 301 | pl->is_partial=0; |
---|
| 302 | pl->query_type=e_PL_FACT; |
---|
[abf8d5d] | 303 | _free_pf_list(pl->proof_list); |
---|
| 304 | pl->proof_list=abac_list_new(); |
---|
| 305 | pl->safe_t=0; |
---|
| 306 | } |
---|
| 307 | |
---|
[53e540d] | 308 | /** |
---|
[5730a10] | 309 | * Add a credential to the db, not duplicating a copy and so |
---|
| 310 | * don't try to free it after this call. |
---|
[53e540d] | 311 | */ |
---|
| 312 | int abac_pl_add_credential(abac_pl_t *pl, abac_credential_t *cred) |
---|
| 313 | { |
---|
| 314 | int rc=0; |
---|
[202a7f9] | 315 | abac_list_t *clauses=abac_credential_clauses(cred); |
---|
| 316 | if (clauses != NULL) { |
---|
| 317 | char *cur; |
---|
| 318 | abac_list_foreach(clauses, cur, |
---|
| 319 | if(cur) { |
---|
[2485307] | 320 | if(debug) fprintf(stderr,"inserting =>%s\n",cur); |
---|
[202a7f9] | 321 | rc=_insert_cred_clause(cur); |
---|
| 322 | } |
---|
| 323 | ); |
---|
[53e540d] | 324 | } |
---|
| 325 | return rc; |
---|
| 326 | } |
---|
[e95d652] | 327 | |
---|
[8bd77b5] | 328 | int abac_pl_add_type_credential(abac_pl_t *pl, abac_id_credential_t *id_cert) |
---|
[202a7f9] | 329 | { |
---|
| 330 | char *clause=abac_id_clause(id_cert); |
---|
| 331 | if (clause != NULL) { |
---|
| 332 | int rc=_insert_cred_clause(clause); |
---|
| 333 | return rc; |
---|
| 334 | } |
---|
| 335 | return 0; |
---|
| 336 | } |
---|
| 337 | |
---|
[d5bbd3e] | 338 | /* string1(S):- S="abc";S="efg";S="ijk" */ |
---|
[7b548fa] | 339 | char *abac_pl_add_range_constraint_clause(char *var, char *tmplist) |
---|
[d5bbd3e] | 340 | { |
---|
| 341 | int i=_get_next_constraint_label_idx(); |
---|
| 342 | char *tmp=NULL; |
---|
| 343 | asprintf(&tmp,"%s_%d(%s) :- %s", constraint_label, i, var, tmplist); |
---|
| 344 | int rc=_insert_cred_clause(tmp); |
---|
| 345 | free(tmp); |
---|
| 346 | if(rc) |
---|
[7b548fa] | 347 | panic("abac_pl_add_range_constraint_clause, failed to insert"); |
---|
[d5bbd3e] | 348 | asprintf(&tmp,"%s_%d(%s)", constraint_label, i, var); |
---|
| 349 | return tmp; |
---|
| 350 | } |
---|
| 351 | |
---|
[da5afdf] | 352 | /* cases, |
---|
[e95d652] | 353 | ['str'] |
---|
| 354 | ['str1','str2'] |
---|
| 355 | ([] is not possible, and don't care) |
---|
| 356 | */ |
---|
[abf8d5d] | 357 | static void _credentials_from_string(abac_list_t *credentials,char *slist) { |
---|
[e95d652] | 358 | char *cptr=slist; /* current ptr */ |
---|
| 359 | char *sptr; /* string ptr */ |
---|
| 360 | char *ptr; |
---|
[202a7f9] | 361 | int len=0; |
---|
[e95d652] | 362 | char *string; |
---|
| 363 | abac_credential_t *cred=NULL; |
---|
[202a7f9] | 364 | int cnt=0; |
---|
| 365 | |
---|
[e95d652] | 366 | /* find first [' */ |
---|
| 367 | ptr=strstr(cptr,"['"); |
---|
| 368 | if(ptr == NULL) |
---|
| 369 | return; |
---|
| 370 | cptr=ptr+2; |
---|
| 371 | sptr=cptr; |
---|
| 372 | while (1) { |
---|
| 373 | /* find next ',' or '] */ |
---|
| 374 | ptr=strstr(cptr,"','"); |
---|
| 375 | if(ptr!=NULL) { |
---|
| 376 | cptr=ptr+3; |
---|
[202a7f9] | 377 | len=(ptr-sptr); |
---|
[e95d652] | 378 | string=strndup(sptr,len); |
---|
| 379 | cred=abac_credential_lookup(string); |
---|
[202a7f9] | 380 | free(string); |
---|
[e95d652] | 381 | if(cred) { |
---|
[abf8d5d] | 382 | int i=abac_list_unique_add(credentials, cred); |
---|
[7b548fa] | 383 | if(i) cnt++; |
---|
[e95d652] | 384 | } else { |
---|
[9b43fc3] | 385 | printf("BAD BAD\n"); |
---|
[e95d652] | 386 | } |
---|
| 387 | sptr=cptr; |
---|
| 388 | } else { |
---|
| 389 | ptr=strstr(cptr,"']"); |
---|
| 390 | if(ptr!=NULL) { |
---|
[202a7f9] | 391 | len=(ptr-sptr); |
---|
[e95d652] | 392 | string=strndup(sptr,len); |
---|
| 393 | cred=abac_credential_lookup(string); |
---|
[202a7f9] | 394 | free(string); |
---|
[e95d652] | 395 | if(cred) { |
---|
[abf8d5d] | 396 | int i=abac_list_unique_add(credentials, cred); |
---|
[7b548fa] | 397 | if(i) cnt++; |
---|
[e95d652] | 398 | } else { |
---|
| 399 | printf("BAD BAD BAD\n"); |
---|
| 400 | } |
---|
| 401 | break; |
---|
| 402 | } |
---|
| 403 | } |
---|
| 404 | } |
---|
[202a7f9] | 405 | if(debug) |
---|
[2485307] | 406 | fprintf(stderr,"DEBUG:total %d credentials\n", cnt); |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | /* MAX 1024x1024, double as it goes */ |
---|
| 410 | static int try_again(int sz, char **tptr) |
---|
| 411 | { |
---|
| 412 | int blk=1024*2; /* 2048 */ |
---|
| 413 | int max=1024*1024; |
---|
| 414 | int size; |
---|
| 415 | char *tmp = NULL; |
---|
| 416 | |
---|
| 417 | if(sz==0) { |
---|
| 418 | size = (sizeof(char) * (blk+1)); |
---|
| 419 | } else { |
---|
| 420 | if (sz>=max) { |
---|
| 421 | size=0; |
---|
| 422 | *tptr=tmp; |
---|
| 423 | return 0; |
---|
| 424 | } |
---|
| 425 | size=sz*2+1; |
---|
| 426 | if(size > max) size=max; |
---|
| 427 | } |
---|
| 428 | |
---|
| 429 | tmp = (char *) YAP_AllocSpaceFromYap(size); |
---|
| 430 | if(tmp==NULL) { |
---|
| 431 | fprintf(stderr,"ERROR: malloc failed !!!\n"); |
---|
| 432 | YAP_Exit(1); |
---|
| 433 | } |
---|
| 434 | *tptr=tmp; |
---|
| 435 | return size; |
---|
[e95d652] | 436 | } |
---|
| 437 | |
---|
[646e57e] | 438 | abac_list_t *run_goal(abac_pl_t *pl, YAP_Term *arg) |
---|
[e95d652] | 439 | { |
---|
[646e57e] | 440 | abac_list_t *cred_list=abac_list_new(); |
---|
[da5afdf] | 441 | YAP_Atom f = YAP_LookupAtom("isMember"); |
---|
| 442 | YAP_Functor func = YAP_MkFunctor(f, 3); |
---|
| 443 | YAP_Term goal=YAP_MkApplTerm(func, 3, arg); |
---|
| 444 | |
---|
[abf8d5d] | 445 | pl->safe_t = YAP_InitSlot(goal); |
---|
| 446 | pl->query_rc =YAP_RunGoal(goal); |
---|
| 447 | pl->query_made=1; |
---|
| 448 | pl->next_rc=pl->query_rc; |
---|
| 449 | if (pl->query_rc) { |
---|
[646e57e] | 450 | if(pl->is_partial) |
---|
[a59bc06] | 451 | printf("A partial proof(type:%s)\n",_queryname[pl->query_type]); |
---|
[646e57e] | 452 | else printf("YAP query succeed\n"); |
---|
[2485307] | 453 | char *tmp=NULL; |
---|
| 454 | int tmp_sz=try_again(0, &tmp); |
---|
[abf8d5d] | 455 | YAP_Term argterm=YAP_ArgOfTerm(3,YAP_GetFromSlot(pl->safe_t)); |
---|
[2485307] | 456 | while(1) { |
---|
[fc943ee] | 457 | /* not in 6.2.2 |
---|
[6244e28] | 458 | int rc=YAP_WriteBuffer(argterm, tmp, tmp_sz,YAP_WRITE_HANDLE_VARS); |
---|
[05f6d35] | 459 | */ |
---|
| 460 | YAP_WriteBuffer(argterm, tmp, tmp_sz,YAP_WRITE_HANDLE_VARS); |
---|
| 461 | /* |
---|
| 462 | if(debug) fprintf(stderr,"call YAP_WriteBuffer: rc(%d) tmp_sz(%d)\n", rc, tmp_sz); |
---|
| 463 | */ |
---|
[6244e28] | 464 | if(strlen(tmp) > 5 && tmp[0]=='\[') { |
---|
[fc943ee] | 465 | if(debug) fprintf(stderr,"what came back .. (%s)\n", tmp); |
---|
[2485307] | 466 | break; |
---|
[6244e28] | 467 | } |
---|
[2485307] | 468 | tmp_sz=try_again(tmp_sz,&tmp); |
---|
[fc943ee] | 469 | if(debug) fprintf(stderr,"try_again: tmp_sz(%d)\n", tmp_sz); |
---|
[bf68132] | 470 | if(tmp_sz==0) { |
---|
| 471 | fprintf(stderr," PANIC, run out of heap space..\n"); |
---|
| 472 | YAP_Exit(1); |
---|
| 473 | } |
---|
[2485307] | 474 | } |
---|
[da5afdf] | 475 | /* this is returned as ['string1','string2'] */ |
---|
[2485307] | 476 | if(debug) fprintf(stderr," query answer : %s(%d)\n", tmp, strlen(tmp)); |
---|
[da5afdf] | 477 | _credentials_from_string(cred_list,tmp); |
---|
[abf8d5d] | 478 | if(abac_list_size(cred_list)==0) { |
---|
[2485307] | 479 | fprintf(stderr,"CAN NOT retrieve result properly from YAP!!!\n"); |
---|
| 480 | YAP_Exit(1); |
---|
| 481 | } |
---|
[abf8d5d] | 482 | abac_list_add(pl->proof_list, cred_list); |
---|
[b92a620] | 483 | YAP_FreeSpaceFromYap(tmp); |
---|
[da5afdf] | 484 | } else { |
---|
[646e57e] | 485 | /* don't print anything if it is running a parital proof goal */ |
---|
| 486 | if(!pl->is_partial) |
---|
| 487 | printf("YAP query failed\n"); |
---|
| 488 | |
---|
[da5afdf] | 489 | } |
---|
| 490 | return cred_list; |
---|
| 491 | } |
---|
| 492 | |
---|
[646e57e] | 493 | /*************************************************************************/ |
---|
| 494 | YAP_Term _make_term(char *term) { |
---|
| 495 | YAP_Term eterm; |
---|
| 496 | YAP_Term ret; |
---|
| 497 | if(term[0]=='\'' || term[0]=='"') { |
---|
| 498 | ret =YAP_ReadBuffer(term,&eterm); |
---|
| 499 | } else { |
---|
| 500 | ret = YAP_MkAtomTerm(YAP_LookupAtom(term)); |
---|
| 501 | } |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | // isMember(who,aspect(whom,what,whatwith)) |
---|
| 505 | /* aspect(whom, what, whatwith) */ |
---|
| 506 | YAP_Term _aspect_base(char *whom,char *aspect, char *what, char *whatwith) |
---|
| 507 | { |
---|
| 508 | YAP_Term eterm; |
---|
| 509 | YAP_Term ret; |
---|
| 510 | |
---|
| 511 | char *estring=NULL; |
---|
| 512 | if(whatwith) |
---|
| 513 | asprintf(&estring, "%s(%s,%s,%s)",aspect,whom,what,whatwith); |
---|
| 514 | else asprintf(&estring, "%s(%s,%s)",aspect,whom,what); |
---|
| 515 | if(debug) fprintf(stderr,"_aspect_base, doing %s\n",estring); |
---|
| 516 | ret=YAP_ReadBuffer(estring,&eterm); |
---|
| 517 | free(estring); |
---|
| 518 | return ret; |
---|
| 519 | } |
---|
| 520 | |
---|
| 521 | /* generate a string of whatwith that are of ?variable */ |
---|
| 522 | char *_make_whatwith_var(char *whatwith, int pcnt) |
---|
| 523 | { |
---|
| 524 | /* this is a dumb count since it is in porting directory */ |
---|
| 525 | if(whatwith==NULL || pcnt==0) return NULL; |
---|
| 526 | char *tmp=NULL; |
---|
| 527 | char *ptr=NULL; |
---|
| 528 | int i; |
---|
| 529 | for(i=0;i<pcnt;i++) { |
---|
| 530 | ptr=tmp; |
---|
| 531 | tmp=NULL; |
---|
| 532 | if(i == 0) |
---|
| 533 | asprintf(&tmp,"Whatwith%d",i); |
---|
| 534 | else asprintf(&tmp,"%s,Whatwith%d",ptr,i); |
---|
| 535 | } |
---|
| 536 | return tmp; |
---|
| 537 | } |
---|
| 538 | |
---|
| 539 | /* aspect(X, Y, Whatwith) */ |
---|
| 540 | YAP_Term _aspect_what_whom(char *whom,char *aspect, char *what, char *whatwith) |
---|
| 541 | { |
---|
| 542 | YAP_Term ret=_aspect_base("Whom",aspect,"What",whatwith); |
---|
| 543 | return ret; |
---|
| 544 | } |
---|
| 545 | |
---|
| 546 | /* aspect(whom,Y,Whatwith) */ |
---|
| 547 | /* call with char *tmp=_make_whatwith_var(whatwith); */ |
---|
| 548 | YAP_Term _aspect_what(char *whom,char *aspect, char *what, char *whatwith) |
---|
| 549 | { |
---|
| 550 | YAP_Term ret=_aspect_base(whom,aspect,"What",whatwith); |
---|
| 551 | return ret; |
---|
| 552 | } |
---|
| 553 | |
---|
| 554 | /* aspect(X,what,whatwith) */ |
---|
| 555 | YAP_Term _aspect_whom(char *whom,char *aspect, char *what, char *whatwith) |
---|
| 556 | { |
---|
| 557 | YAP_Term ret=_aspect_base("Whom",aspect,what,whatwith); |
---|
| 558 | return ret; |
---|
| 559 | } |
---|
| 560 | |
---|
| 561 | /* aspect(whom,what,whatwith) */ |
---|
| 562 | YAP_Term _aspect_fact(char *whom,char *aspect, char *what, char* whatwith) |
---|
| 563 | { |
---|
| 564 | YAP_Term ret=_aspect_base(whom,aspect,what,whatwith); |
---|
| 565 | return ret; |
---|
| 566 | } |
---|
| 567 | |
---|
| 568 | // Whom.What <- ladybug |
---|
| 569 | // isMember(ladybug, role(W1, W2), R) |
---|
| 570 | static void _arg_for_what_whom(YAP_Term *arg,char *who, char *whom, |
---|
| 571 | char *aspect, char*what, char *whatwith, int pcnt) |
---|
| 572 | { |
---|
| 573 | arg[0] = _make_term(who); |
---|
| 574 | char *tmp=_make_whatwith_var(whatwith,pcnt); |
---|
| 575 | arg[1] = _aspect_what_whom(who,aspect,what,tmp); |
---|
| 576 | if(tmp) free(tmp); |
---|
| 577 | arg[2] = YAP_MkVarTerm(); |
---|
| 578 | } |
---|
| 579 | |
---|
| 580 | // pAcme.What <- ladybug |
---|
| 581 | // isMember(ladybug, role(pAcme, W), R) |
---|
| 582 | static void _arg_for_what(YAP_Term *arg,char *who, char* whom, |
---|
| 583 | char *aspect, char*what, char*whatwith, int pcnt) |
---|
| 584 | { |
---|
| 585 | arg[0] = _make_term(who); |
---|
| 586 | char *tmp=_make_whatwith_var(whatwith,pcnt); |
---|
| 587 | arg[1] = _aspect_what(whom,aspect,what,tmp); |
---|
| 588 | if(tmp) free(tmp); |
---|
| 589 | arg[2] = YAP_MkVarTerm(); |
---|
| 590 | } |
---|
| 591 | |
---|
| 592 | // W.rocket <- ladybug |
---|
| 593 | // isMember(ladybug, role(W, buy_rockets), R) |
---|
| 594 | static void _arg_for_whom(YAP_Term *arg, char *who, char*whom, |
---|
| 595 | char *aspect, char *what, char*whatwith) |
---|
| 596 | { |
---|
| 597 | arg[0] = _make_term(who); |
---|
| 598 | arg[1] = _aspect_whom(whom,aspect,what,whatwith); |
---|
| 599 | arg[2] = YAP_MkVarTerm(); |
---|
| 600 | } |
---|
| 601 | |
---|
| 602 | // Acme.rocket <- V |
---|
| 603 | // isMember(V, role(pAcme, buy_rockets), R) |
---|
| 604 | static int _arg_for_who(YAP_Term *arg, char *who,char *whom, |
---|
| 605 | char *aspect, char* what, char*whatwith) |
---|
| 606 | { |
---|
| 607 | arg[0] = YAP_MkVarTerm(); |
---|
| 608 | arg[1] = _aspect_fact(whom, aspect, what, whatwith); |
---|
| 609 | arg[2] = YAP_MkVarTerm(); |
---|
| 610 | } |
---|
| 611 | |
---|
| 612 | // Acme.rocket <- x |
---|
| 613 | // isMember(x, role(pAcme, buy_rockets), R) |
---|
| 614 | static void _arg_for_fact(YAP_Term *arg,char *who,char *whom, |
---|
| 615 | char* aspect, char *what, char*whatwith) |
---|
| 616 | { |
---|
| 617 | arg[0] = _make_term(who); |
---|
| 618 | arg[1] = _aspect_fact(whom, aspect, what, whatwith); |
---|
| 619 | arg[2] = YAP_MkVarTerm(); |
---|
| 620 | } |
---|
| 621 | |
---|
| 622 | |
---|
| 623 | static abac_list_t *_make_partial_query(abac_pl_t *pl) |
---|
| 624 | { |
---|
| 625 | abac_list_t *ret=abac_list_new(); |
---|
| 626 | pl->is_partial=1; |
---|
| 627 | char *who=pl->who; |
---|
| 628 | char *whom=pl->whom; |
---|
| 629 | char *aspect=pl->aspect; |
---|
| 630 | char *what=pl->what; |
---|
| 631 | char *whatwith=pl->whatwith; |
---|
| 632 | int pcnt=pl->pcnt; |
---|
| 633 | if(debug) fprintf(stderr,"making make_partial_query..\n"); |
---|
| 634 | |
---|
| 635 | switch (pl->query_type) { |
---|
| 636 | case e_PL_FACT: /* try what */ |
---|
| 637 | { |
---|
| 638 | YAP_Term arg[3]; |
---|
| 639 | if(debug) fprintf(stderr,"... 1st partial attempt\n"); |
---|
| 640 | pl->query_type = e_PL_WHAT; |
---|
| 641 | _arg_for_what(arg,who,whom, aspect, what, whatwith, pcnt); |
---|
| 642 | ret=run_goal(pl,arg); |
---|
| 643 | if(pcnt==0) pl->query_type= e_PL_WHAT_DFLT; |
---|
| 644 | if(pl->query_rc) return ret; |
---|
| 645 | break; |
---|
| 646 | } |
---|
| 647 | case e_PL_WHAT: /* try what with no params if need to */ |
---|
| 648 | { |
---|
| 649 | YAP_Term arg[3]; |
---|
| 650 | if(debug) fprintf(stderr,"... 2nd partial attempt\n"); |
---|
| 651 | pl->query_type = e_PL_WHAT_DFLT; |
---|
| 652 | _arg_for_what(arg,who,whom, aspect, what, whatwith, 0); |
---|
| 653 | ret=run_goal(pl,arg); |
---|
| 654 | if(pl->query_rc) return ret; |
---|
| 655 | break; |
---|
| 656 | } |
---|
| 657 | case e_PL_WHAT_DFLT: /* try who */ |
---|
| 658 | { |
---|
| 659 | YAP_Term arg[3]; |
---|
| 660 | if(debug) fprintf(stderr,"... 2nd half partial attempt\n"); |
---|
| 661 | pl->query_type = e_PL_WHO; |
---|
| 662 | _arg_for_who(arg,who,whom, aspect, what, whatwith); |
---|
| 663 | ret=run_goal(pl,arg); |
---|
| 664 | if(pl->query_rc) return ret; |
---|
| 665 | break; |
---|
| 666 | } |
---|
| 667 | case e_PL_WHO: /* try whom */ |
---|
| 668 | { |
---|
| 669 | YAP_Term arg[3]; |
---|
| 670 | if(debug) fprintf(stderr,"... 3rd partial attempt\n"); |
---|
| 671 | pl->query_type = e_PL_WHOM; |
---|
| 672 | _arg_for_whom(arg,who,whom, aspect, what, whatwith); |
---|
| 673 | ret=run_goal(pl,arg); |
---|
| 674 | if(pl->query_rc) return ret; |
---|
| 675 | break; |
---|
| 676 | } |
---|
| 677 | case e_PL_WHOM: /* try what-whom */ |
---|
| 678 | { |
---|
| 679 | YAP_Term arg[3]; |
---|
| 680 | if(debug) fprintf(stderr,"... 4th partial attempt\n"); |
---|
| 681 | pl->query_type = e_PL_WHAT_WHOM; |
---|
| 682 | _arg_for_what_whom(arg,who,whom, aspect, what, whatwith,pcnt); |
---|
| 683 | ret=run_goal(pl,arg); |
---|
| 684 | if(pl->query_rc) return ret; |
---|
| 685 | break; |
---|
| 686 | } |
---|
| 687 | default : |
---|
| 688 | { |
---|
| 689 | if(debug) fprintf(stderr,"... no more partial query to try\n"); |
---|
| 690 | pl->query_type = e_PL_NOMORE; |
---|
| 691 | } |
---|
| 692 | } |
---|
| 693 | return ret; |
---|
| 694 | } |
---|
| 695 | |
---|
| 696 | /* make a query and extract just first set of result, |
---|
| 697 | simple = rt0 |
---|
| 698 | aspect => role/oset |
---|
| 699 | aspect_string => rest of params |
---|
| 700 | prin => principal |
---|
| 701 | issuer => issuer |
---|
| 702 | estring => aspect(issuer, aspect_type_string) |
---|
| 703 | */ |
---|
| 704 | static abac_list_t *_make_yap_query(abac_pl_t *pl, char *who, |
---|
| 705 | char *whom, char *aspect, char *what, char *whatwith, int pcnt) |
---|
| 706 | { |
---|
| 707 | if(debug) { |
---|
| 708 | fprintf(stderr,"_make_yap_query: the who part(%s)\n", who); |
---|
| 709 | fprintf(stderr,"_make_yap_query: whom part(%s)\n", whom); |
---|
| 710 | fprintf(stderr,"_make_yap_query: the role/oset (%s)\n", aspect); |
---|
| 711 | fprintf(stderr,"_make_yap_query: aspect what (%s)\n", what); |
---|
| 712 | fprintf(stderr,"_make_yap_query: pcnt (%d)\n", pcnt); |
---|
| 713 | if(whatwith) |
---|
| 714 | fprintf(stderr,"_make_yap_query: aspect what part(%s)\n", whatwith); |
---|
| 715 | } |
---|
| 716 | if(debug) fprintf(stderr,"making a yap query..\n"); |
---|
| 717 | |
---|
| 718 | /* run query on fact goal */ |
---|
| 719 | YAP_Term arg[3]; |
---|
| 720 | _arg_for_fact(arg, who,whom, aspect, what, whatwith); |
---|
| 721 | abac_list_t *ret=run_goal(pl,arg); |
---|
| 722 | |
---|
| 723 | /* fact query failed and so run partial query */ |
---|
| 724 | if(!pl->query_rc && pl->want_partial) { |
---|
| 725 | while(pl->query_type != e_PL_NOMORE) { |
---|
| 726 | abac_list_free(ret); |
---|
| 727 | ret=_make_partial_query(pl); |
---|
| 728 | if( pl->query_rc ) break; |
---|
| 729 | } |
---|
| 730 | } |
---|
| 731 | return ret; |
---|
| 732 | } |
---|
| 733 | |
---|
[abf8d5d] | 734 | /* make a query for next possible solution proof */ |
---|
| 735 | static abac_list_t *_query_again(abac_pl_t *pl) |
---|
| 736 | { |
---|
| 737 | abac_list_t *cred_list=abac_list_new(); |
---|
| 738 | |
---|
[646e57e] | 739 | if(debug) fprintf(stderr,"making query_again call..\n"); |
---|
[abf8d5d] | 740 | pl->next_rc=YAP_RestartGoal(); |
---|
| 741 | pl->query_made++; |
---|
| 742 | |
---|
| 743 | char *tmp=NULL; |
---|
| 744 | int tmp_sz=try_again(0, &tmp); |
---|
| 745 | YAP_Term argterm=YAP_ArgOfTerm(3,YAP_GetFromSlot(pl->safe_t)); |
---|
| 746 | if(pl->next_rc) { |
---|
[646e57e] | 747 | if(debug) fprintf(stderr,"query_again: another success\n"); |
---|
| 748 | if(pl->is_partial) |
---|
[a59bc06] | 749 | printf("A partial proof(type:%s)\n",_queryname[pl->query_type]); |
---|
[abf8d5d] | 750 | while(1) { |
---|
| 751 | YAP_WriteBuffer(argterm, tmp, tmp_sz,YAP_WRITE_HANDLE_VARS); |
---|
| 752 | if(strlen(tmp) > 5 && tmp[0]=='\[') |
---|
| 753 | break; |
---|
| 754 | tmp_sz=try_again(tmp_sz,&tmp); |
---|
| 755 | if(tmp_sz==0) { |
---|
| 756 | fprintf(stderr," PANIC, run out of heap space..\n"); |
---|
| 757 | YAP_Exit(1); |
---|
| 758 | } |
---|
| 759 | } |
---|
| 760 | /* this is returned as ['string1','string2'] */ |
---|
| 761 | if(debug) fprintf(stderr," query answer : %s(%d)\n", tmp, strlen(tmp)); |
---|
| 762 | _credentials_from_string(cred_list,tmp); |
---|
| 763 | if(abac_list_size(cred_list)==0) { |
---|
| 764 | fprintf(stderr,"CAN NOT retrieve result properly from YAP!!!\n"); |
---|
| 765 | YAP_Exit(1); |
---|
| 766 | } |
---|
| 767 | abac_list_add(pl->proof_list, cred_list); |
---|
| 768 | YAP_FreeSpaceFromYap(tmp); |
---|
[646e57e] | 769 | } else { |
---|
| 770 | if(debug) fprintf(stderr,"query_again: fail finding next\n"); |
---|
| 771 | if(pl->is_partial) { /* if there is more partial query to make */ |
---|
| 772 | while(pl->query_type != e_PL_NOMORE) { |
---|
| 773 | abac_list_free(cred_list); |
---|
| 774 | cred_list=_make_partial_query(pl); |
---|
| 775 | if( pl->query_rc ) break; |
---|
| 776 | } |
---|
| 777 | } |
---|
[abf8d5d] | 778 | } |
---|
| 779 | return cred_list; |
---|
| 780 | } |
---|
| 781 | |
---|
| 782 | abac_stack_t *_make_cred_stack(abac_list_t *p) |
---|
| 783 | { |
---|
| 784 | abac_stack_t *ret=abac_stack_new(); |
---|
| 785 | if(abac_list_size(p) != 0) { |
---|
| 786 | abac_credential_t *cur=NULL; |
---|
| 787 | abac_list_foreach(p, cur, |
---|
| 788 | abac_stack_push(ret, (void *)abac_credential_dup(cur)); |
---|
| 789 | ); |
---|
| 790 | } |
---|
| 791 | return ret; |
---|
| 792 | } |
---|
| 793 | |
---|
| 794 | /* force a backtrack to get next solution proof */ |
---|
| 795 | abac_stack_t *abac_pl_query_again(abac_pl_t *pl) |
---|
| 796 | { |
---|
| 797 | abac_list_t *rlist=_query_again(pl); |
---|
| 798 | abac_stack_t *ret=_make_cred_stack(rlist); |
---|
| 799 | return ret; |
---|
| 800 | } |
---|
| 801 | |
---|
[da5afdf] | 802 | /* 2 types |
---|
| 803 | acme.buys_rocket <- coyote (coyote=prin, acme.buys_rocket=role) |
---|
| 804 | ==> isMember(coyote,role(acme,buys_rocket), L) |
---|
[440ba20] | 805 | acme.buys_rocket <- acme.preferred_customer -- NOT valid |
---|
| 806 | */ |
---|
[8bd77b5] | 807 | static abac_stack_t *_query_with_aspect(abac_pl_t *pl, abac_aspect_t* head, abac_aspect_t* tail) |
---|
[da5afdf] | 808 | { |
---|
| 809 | abac_stack_t *ret=NULL; |
---|
[2485307] | 810 | char *tmp=NULL; |
---|
[da5afdf] | 811 | |
---|
[08b8da7] | 812 | if(0) |
---|
[440ba20] | 813 | show_yap_db("DEBUG:calling within _query_with_aspect"); |
---|
[da5afdf] | 814 | |
---|
[646e57e] | 815 | char *whom=NULL; |
---|
| 816 | PROLOG(whom=abac_aspect_principal_name(head);); |
---|
[8bd77b5] | 817 | |
---|
[da5afdf] | 818 | /* could be obj or principal */ |
---|
[646e57e] | 819 | char *who=NULL; |
---|
[8bd77b5] | 820 | if(abac_aspect_is_object(tail)) { |
---|
[646e57e] | 821 | who=abac_aspect_object_name(tail); |
---|
[da5afdf] | 822 | } else { |
---|
[646e57e] | 823 | PROLOG(who=abac_aspect_principal_name(tail);); |
---|
[da5afdf] | 824 | } |
---|
| 825 | |
---|
[8bd77b5] | 826 | if(abac_aspect_aspect_name(tail)!=NULL) { |
---|
| 827 | printf("fail, a.o <- b.o and a.r <- a.r query is not implemented yet !!!\n"); |
---|
[da5afdf] | 828 | YAP_Exit(1); |
---|
| 829 | } |
---|
| 830 | |
---|
[646e57e] | 831 | if (who == NULL || whom == NULL) { |
---|
[8bd77b5] | 832 | printf("fail, query's call got bad aspect names .. \n"); |
---|
[da5afdf] | 833 | YAP_Exit(1); |
---|
| 834 | } |
---|
| 835 | |
---|
[2485307] | 836 | if(debug) fprintf(stderr,"printing up the yap query ..\n"); |
---|
[d037f54] | 837 | |
---|
| 838 | char *pstring; |
---|
| 839 | PROLOG(pstring=abac_aspect_aspect_param_string(head);); |
---|
| 840 | |
---|
[646e57e] | 841 | char *aspect=abac_aspect_type_string(head); |
---|
| 842 | int pcnt=abac_aspect_aspect_param_cnt(head); |
---|
| 843 | char *what=abac_aspect_aspect_name(head); |
---|
| 844 | |
---|
| 845 | _reset_pl(pl, who, what, whom, aspect, pstring, pcnt); |
---|
| 846 | |
---|
| 847 | abac_list_t *rlist=NULL; |
---|
| 848 | if(pcnt) |
---|
| 849 | rlist=_make_yap_query(pl,who,whom,aspect,abac_aspect_aspect_name(head),pstring,pcnt); |
---|
| 850 | else |
---|
| 851 | rlist=_make_yap_query(pl,who,whom,aspect,abac_aspect_aspect_name(head),NULL,0); |
---|
[da5afdf] | 852 | |
---|
[abf8d5d] | 853 | /* generate the resulting stack from a list */ |
---|
| 854 | ret=_make_cred_stack(rlist); |
---|
[646e57e] | 855 | if(tmp) free(tmp); |
---|
| 856 | if(pstring) free(pstring); |
---|
[da5afdf] | 857 | return ret; |
---|
| 858 | } |
---|
| 859 | |
---|
| 860 | /** |
---|
| 861 | * Get all the credentials (attribute/issuer cert pairs) from prolog |
---|
| 862 | * (which returns in string form) |
---|
| 863 | */ |
---|
| 864 | abac_stack_t *abac_pl_credentials(abac_pl_t *pl) |
---|
| 865 | { |
---|
[8bd77b5] | 866 | abac_stack_t *ret=abac_verifier_dump_creds(); |
---|
[da5afdf] | 867 | return ret; |
---|
| 868 | } |
---|
| 869 | |
---|
[5110d42] | 870 | abac_stack_t *abac_pl_principals(abac_pl_t *pl) |
---|
| 871 | { |
---|
| 872 | abac_stack_t *ret=abac_verifier_dump_principals(); |
---|
| 873 | return ret; |
---|
| 874 | } |
---|
| 875 | |
---|
[e95d652] | 876 | /** |
---|
| 877 | * Make a query into prolog db |
---|
| 878 | --role acme.preferred_customer --principal coyote |
---|
| 879 | --role acme.prefer_customer.buy_rockets --principlal coyote |
---|
[da5afdf] | 880 | --oset acme.rockets -- object mrx-21 |
---|
| 881 | --oset acme.villans -- principal coyote |
---|
[e95d652] | 882 | */ |
---|
[da5afdf] | 883 | abac_stack_t *abac_pl_query(abac_pl_t *pl, char *roleoset, char *prinobj) |
---|
[e95d652] | 884 | { |
---|
[202a7f9] | 885 | abac_stack_t *ret=NULL; |
---|
[da5afdf] | 886 | int len=strlen(roleoset)+strlen(prinobj)+5; |
---|
| 887 | char* attr_string=(char *) abac_xmalloc(sizeof(char)*len); |
---|
| 888 | sprintf(attr_string,"%s<-%s", roleoset, prinobj); |
---|
| 889 | |
---|
| 890 | if(debug) |
---|
[2485307] | 891 | fprintf(stderr,"abac_pl_query, query string is (%s)\n",attr_string); |
---|
[e95d652] | 892 | |
---|
[da5afdf] | 893 | /* call into yacc parser */ |
---|
| 894 | abac_reset_yyfptr(attr_string); |
---|
[b5a3da4] | 895 | abac_yy_init(); |
---|
[da5afdf] | 896 | int rc=yyparse(); |
---|
| 897 | if (rc) { |
---|
| 898 | free(attr_string); |
---|
[e95d652] | 899 | return NULL; |
---|
| 900 | } |
---|
| 901 | |
---|
[8bd77b5] | 902 | abac_aspect_t *head_aspect = abac_yy_get_rule_head_aspect(); |
---|
| 903 | abac_aspect_t *tail_aspect = abac_yy_get_rule_tail_aspect(); |
---|
[2efdff5] | 904 | |
---|
[8bd77b5] | 905 | ret=_query_with_aspect(pl,head_aspect,tail_aspect); |
---|
| 906 | |
---|
[e95d652] | 907 | return ret; |
---|
| 908 | } |
---|
| 909 | |
---|
[d9c3886] | 910 | abac_stack_t *abac_pl_query_with_structure(abac_pl_t *pl, abac_aspect_t *head_aspect, abac_aspect_t *tail_aspect) |
---|
| 911 | { |
---|
| 912 | abac_stack_t *ret=NULL; |
---|
| 913 | ret=_query_with_aspect(pl,head_aspect,tail_aspect); |
---|
| 914 | |
---|
| 915 | return ret; |
---|
| 916 | } |
---|