[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 | |
---|
[da5afdf] | 33 | // pl -- place holder for now |
---|
[e95d652] | 34 | struct _abac_pl_t { |
---|
[abf8d5d] | 35 | /* a_term <- prin */ |
---|
| 36 | char *aspect_term; |
---|
| 37 | char *prin_term; |
---|
| 38 | int query_made; /* 1+ yes, 0 no, rc is valid only if a query is made */ |
---|
| 39 | int query_rc; /* rc of query call */ |
---|
| 40 | int next_rc; /* rc of next-last query */ |
---|
| 41 | /* proof count = abac_list_size(proof_list) */ |
---|
| 42 | abac_list_t *proof_list; /* list of list of cred_list */ |
---|
| 43 | long safe_t; /* holding the YAP slot for the goal */ |
---|
[e95d652] | 44 | }; |
---|
| 45 | |
---|
[d5bbd3e] | 46 | /***********************************************************/ |
---|
| 47 | static int _get_next_constraint_label_idx() |
---|
| 48 | { |
---|
| 49 | constraint_label_count++; |
---|
| 50 | return constraint_label_count; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | static int _get_constraint_label_idx() |
---|
| 54 | { |
---|
| 55 | return constraint_label_count; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | /***********************************************************/ |
---|
| 59 | |
---|
[53e540d] | 60 | static int _insert_clause(char *str) |
---|
[e95d652] | 61 | { |
---|
[202a7f9] | 62 | YAP_Term *eterm; |
---|
[53e540d] | 63 | YAP_Term goalArgs=YAP_ReadBuffer(str, eterm); |
---|
| 64 | char *tmp=YAP_CompileClause(goalArgs); |
---|
| 65 | if (tmp!=NULL) { /* something is wrong */ |
---|
| 66 | printf("error: result of compile clause (%s)\n", tmp); |
---|
| 67 | printf("error: str used (%s)\n", str); |
---|
| 68 | return 1; |
---|
[e95d652] | 69 | } |
---|
[53e540d] | 70 | return 0; |
---|
[e95d652] | 71 | } |
---|
| 72 | |
---|
[53e540d] | 73 | static int _insert_cred_clause(char *cstr) |
---|
[e95d652] | 74 | { |
---|
| 75 | int ret=ABAC_CERT_SUCCESS; |
---|
[53e540d] | 76 | int rc=_insert_clause(cstr); |
---|
| 77 | if (rc) |
---|
| 78 | return ABAC_CERT_BAD_YAP; |
---|
| 79 | return ABAC_CERT_SUCCESS; |
---|
[e95d652] | 80 | } |
---|
| 81 | |
---|
[440ba20] | 82 | void show_yap_db(const char *msg) |
---|
[e95d652] | 83 | { |
---|
| 84 | char lstr[]="listing"; |
---|
[da5afdf] | 85 | YAP_Term *eterm; /* error term */ |
---|
[e95d652] | 86 | |
---|
[53e540d] | 87 | printf("\n\n========= yap db (%s)\n",msg); |
---|
[e95d652] | 88 | YAP_Term goal=YAP_ReadBuffer(lstr, eterm); |
---|
| 89 | int rc =YAP_RunGoal( goal ); |
---|
| 90 | if (rc) { |
---|
| 91 | printf("listing ok.. \n"); |
---|
| 92 | } else { |
---|
| 93 | printf("listing's rc is bad.. \n"); |
---|
| 94 | YAP_Exit(1); |
---|
| 95 | } |
---|
[53e540d] | 96 | printf("========= \n\n"); |
---|
[e95d652] | 97 | } |
---|
| 98 | |
---|
[53e540d] | 99 | /** |
---|
| 100 | * Include some utility routines |
---|
| 101 | */ |
---|
| 102 | abac_pl_t *abac_pl_utility(void) { |
---|
| 103 | /* |
---|
| 104 | append([],L,L). |
---|
| 105 | append([X|L1],L2,[X|L3]):-append(L1,L2,L3). |
---|
| 106 | |
---|
| 107 | appendL([],[]). |
---|
| 108 | appendL([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); |
---|
[e95d652] | 119 | } |
---|
| 120 | |
---|
[53e540d] | 121 | /** |
---|
| 122 | * Create a new yap structure. |
---|
| 123 | */ |
---|
| 124 | abac_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")))) { |
---|
[2485307] | 130 | if(debug) fprintf(stderr, "calling source..\n"); |
---|
[53e540d] | 131 | } else { |
---|
[2485307] | 132 | if(debug) fprintf(stderr,"calling source failed..\n"); |
---|
[202a7f9] | 133 | YAP_Exit(1); |
---|
[53e540d] | 134 | } |
---|
| 135 | |
---|
| 136 | abac_pl_utility(); |
---|
| 137 | |
---|
| 138 | abac_pl_t *pl = abac_xmalloc(sizeof(abac_pl_t)); |
---|
[abf8d5d] | 139 | pl->aspect_term=NULL; |
---|
| 140 | pl->prin_term=NULL; |
---|
| 141 | pl->query_made=0; |
---|
| 142 | pl->query_rc=0; |
---|
| 143 | pl->next_rc=0; |
---|
| 144 | /* proof count = abac_list_size(proof_list) */ |
---|
| 145 | pl->proof_list=NULL; |
---|
| 146 | pl->safe_t=0; |
---|
[53e540d] | 147 | return pl; |
---|
[e95d652] | 148 | } |
---|
| 149 | |
---|
[abf8d5d] | 150 | /* returns the number of solution so far */ |
---|
| 151 | int abac_pl_proof_cnt(abac_pl_t *pl) |
---|
| 152 | { |
---|
| 153 | if(pl->proof_list) |
---|
| 154 | return abac_list_size(pl->proof_list); |
---|
| 155 | return 0; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | /* is this the conclusion of the query ? */ |
---|
| 159 | /* 1 is yes, 0 N/A */ |
---|
| 160 | int abac_pl_done_yet(abac_pl_t *pl) |
---|
| 161 | { |
---|
| 162 | if(pl->query_made) { |
---|
| 163 | if(pl->query_rc) { |
---|
| 164 | if(!pl->next_rc) return 1; |
---|
| 165 | return 0; |
---|
| 166 | } else return 1; /* even init query failed */ |
---|
| 167 | } |
---|
| 168 | return 0; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | static void _free_p_list(abac_list_t *p) |
---|
| 172 | { |
---|
| 173 | if( p != NULL ) { |
---|
| 174 | if(abac_list_size(p) != 0) { |
---|
| 175 | abac_credential_t *cur=NULL; |
---|
| 176 | abac_list_foreach(p, cur, |
---|
| 177 | abac_credential_free(cur); |
---|
| 178 | ); |
---|
| 179 | } |
---|
| 180 | abac_list_free(p); |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | static void _free_pf_list(abac_list_t *p) |
---|
| 185 | { |
---|
| 186 | |
---|
| 187 | if(p != NULL) { |
---|
| 188 | if(abac_list_size(p) != 0) { |
---|
| 189 | abac_list_t *cur=NULL; |
---|
| 190 | abac_list_foreach(p, cur, |
---|
| 191 | _free_p_list(cur); |
---|
| 192 | ); |
---|
| 193 | } |
---|
| 194 | abac_list_free(p); |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | void abac_pl_free(abac_pl_t *pl) { |
---|
| 199 | if(pl->aspect_term) free(pl->aspect_term); |
---|
| 200 | if(pl->prin_term) free(pl->prin_term); |
---|
| 201 | _free_pf_list(pl->proof_list); |
---|
| 202 | free(pl); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | /* this is done whenever a new query starts in */ |
---|
| 206 | static void _reset_pl(abac_pl_t *pl, char *aspect_term, char *prin_term) |
---|
| 207 | { |
---|
| 208 | if(pl->safe_t) { |
---|
| 209 | YAP_RecoverSlots(1); |
---|
| 210 | YAP_Reset(); |
---|
| 211 | } |
---|
| 212 | /* free the old ones */ |
---|
| 213 | if(pl->aspect_term) free(pl->aspect_term); |
---|
| 214 | if(pl->prin_term) free(pl->prin_term); |
---|
| 215 | pl->aspect_term = abac_xstrdup(aspect_term); |
---|
| 216 | pl->prin_term = abac_xstrdup(prin_term); |
---|
| 217 | pl->query_made=0; |
---|
| 218 | pl->query_rc=0; |
---|
| 219 | pl->next_rc=0; |
---|
| 220 | _free_pf_list(pl->proof_list); |
---|
| 221 | pl->proof_list=abac_list_new(); |
---|
| 222 | pl->safe_t=0; |
---|
| 223 | |
---|
| 224 | } |
---|
| 225 | |
---|
[53e540d] | 226 | /** |
---|
[5730a10] | 227 | * Add a credential to the db, not duplicating a copy and so |
---|
| 228 | * don't try to free it after this call. |
---|
[53e540d] | 229 | */ |
---|
| 230 | int abac_pl_add_credential(abac_pl_t *pl, abac_credential_t *cred) |
---|
| 231 | { |
---|
| 232 | int rc=0; |
---|
[202a7f9] | 233 | abac_list_t *clauses=abac_credential_clauses(cred); |
---|
| 234 | if (clauses != NULL) { |
---|
| 235 | char *cur; |
---|
| 236 | abac_list_foreach(clauses, cur, |
---|
| 237 | if(cur) { |
---|
[2485307] | 238 | if(debug) fprintf(stderr,"inserting =>%s\n",cur); |
---|
[202a7f9] | 239 | rc=_insert_cred_clause(cur); |
---|
| 240 | } |
---|
| 241 | ); |
---|
[53e540d] | 242 | } |
---|
| 243 | return rc; |
---|
| 244 | } |
---|
[e95d652] | 245 | |
---|
[8bd77b5] | 246 | int abac_pl_add_type_credential(abac_pl_t *pl, abac_id_credential_t *id_cert) |
---|
[202a7f9] | 247 | { |
---|
| 248 | char *clause=abac_id_clause(id_cert); |
---|
| 249 | if (clause != NULL) { |
---|
| 250 | int rc=_insert_cred_clause(clause); |
---|
| 251 | return rc; |
---|
| 252 | } |
---|
| 253 | return 0; |
---|
| 254 | } |
---|
| 255 | |
---|
[d5bbd3e] | 256 | /* string1(S):- S="abc";S="efg";S="ijk" */ |
---|
[7b548fa] | 257 | char *abac_pl_add_range_constraint_clause(char *var, char *tmplist) |
---|
[d5bbd3e] | 258 | { |
---|
| 259 | int i=_get_next_constraint_label_idx(); |
---|
| 260 | char *tmp=NULL; |
---|
| 261 | asprintf(&tmp,"%s_%d(%s) :- %s", constraint_label, i, var, tmplist); |
---|
| 262 | int rc=_insert_cred_clause(tmp); |
---|
| 263 | free(tmp); |
---|
| 264 | if(rc) |
---|
[7b548fa] | 265 | panic("abac_pl_add_range_constraint_clause, failed to insert"); |
---|
[d5bbd3e] | 266 | asprintf(&tmp,"%s_%d(%s)", constraint_label, i, var); |
---|
| 267 | return tmp; |
---|
| 268 | } |
---|
| 269 | |
---|
[da5afdf] | 270 | /* cases, |
---|
[e95d652] | 271 | ['str'] |
---|
| 272 | ['str1','str2'] |
---|
| 273 | ([] is not possible, and don't care) |
---|
| 274 | */ |
---|
[abf8d5d] | 275 | static void _credentials_from_string(abac_list_t *credentials,char *slist) { |
---|
[e95d652] | 276 | char *cptr=slist; /* current ptr */ |
---|
| 277 | char *sptr; /* string ptr */ |
---|
| 278 | char *ptr; |
---|
[202a7f9] | 279 | int len=0; |
---|
[e95d652] | 280 | char *string; |
---|
| 281 | abac_credential_t *cred=NULL; |
---|
[202a7f9] | 282 | int cnt=0; |
---|
| 283 | |
---|
[e95d652] | 284 | /* find first [' */ |
---|
| 285 | ptr=strstr(cptr,"['"); |
---|
| 286 | if(ptr == NULL) |
---|
| 287 | return; |
---|
| 288 | cptr=ptr+2; |
---|
| 289 | sptr=cptr; |
---|
| 290 | while (1) { |
---|
| 291 | /* find next ',' or '] */ |
---|
| 292 | ptr=strstr(cptr,"','"); |
---|
| 293 | if(ptr!=NULL) { |
---|
| 294 | cptr=ptr+3; |
---|
[202a7f9] | 295 | len=(ptr-sptr); |
---|
[e95d652] | 296 | string=strndup(sptr,len); |
---|
| 297 | cred=abac_credential_lookup(string); |
---|
[202a7f9] | 298 | free(string); |
---|
[e95d652] | 299 | if(cred) { |
---|
[abf8d5d] | 300 | int i=abac_list_unique_add(credentials, cred); |
---|
[7b548fa] | 301 | if(i) cnt++; |
---|
[e95d652] | 302 | } else { |
---|
[9b43fc3] | 303 | printf("BAD BAD\n"); |
---|
[e95d652] | 304 | } |
---|
| 305 | sptr=cptr; |
---|
| 306 | } else { |
---|
| 307 | ptr=strstr(cptr,"']"); |
---|
| 308 | if(ptr!=NULL) { |
---|
[202a7f9] | 309 | len=(ptr-sptr); |
---|
[e95d652] | 310 | string=strndup(sptr,len); |
---|
| 311 | cred=abac_credential_lookup(string); |
---|
[202a7f9] | 312 | free(string); |
---|
[e95d652] | 313 | if(cred) { |
---|
[abf8d5d] | 314 | int i=abac_list_unique_add(credentials, cred); |
---|
[7b548fa] | 315 | if(i) cnt++; |
---|
[e95d652] | 316 | } else { |
---|
| 317 | printf("BAD BAD BAD\n"); |
---|
| 318 | } |
---|
| 319 | break; |
---|
| 320 | } |
---|
| 321 | } |
---|
| 322 | } |
---|
[202a7f9] | 323 | if(debug) |
---|
[2485307] | 324 | fprintf(stderr,"DEBUG:total %d credentials\n", cnt); |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | /* MAX 1024x1024, double as it goes */ |
---|
| 328 | static int try_again(int sz, char **tptr) |
---|
| 329 | { |
---|
| 330 | int blk=1024*2; /* 2048 */ |
---|
| 331 | int max=1024*1024; |
---|
| 332 | int size; |
---|
| 333 | char *tmp = NULL; |
---|
| 334 | |
---|
| 335 | if(sz==0) { |
---|
| 336 | size = (sizeof(char) * (blk+1)); |
---|
| 337 | } else { |
---|
| 338 | if (sz>=max) { |
---|
| 339 | size=0; |
---|
| 340 | *tptr=tmp; |
---|
| 341 | return 0; |
---|
| 342 | } |
---|
| 343 | size=sz*2+1; |
---|
| 344 | if(size > max) size=max; |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | tmp = (char *) YAP_AllocSpaceFromYap(size); |
---|
| 348 | if(tmp==NULL) { |
---|
| 349 | fprintf(stderr,"ERROR: malloc failed !!!\n"); |
---|
| 350 | YAP_Exit(1); |
---|
| 351 | } |
---|
| 352 | *tptr=tmp; |
---|
| 353 | return size; |
---|
[e95d652] | 354 | } |
---|
| 355 | |
---|
[abf8d5d] | 356 | /* make a query and extract just first set of result */ |
---|
| 357 | static abac_list_t *_make_yap_query(abac_pl_t *pl, char *prin, char *nm, char* estring) |
---|
[e95d652] | 358 | { |
---|
[36b100a] | 359 | YAP_Term *eterm0; |
---|
| 360 | YAP_Term *eterm1; |
---|
[e95d652] | 361 | YAP_Term arg[3]; |
---|
[2485307] | 362 | |
---|
[abf8d5d] | 363 | abac_list_t *cred_list = abac_list_new(); |
---|
[202a7f9] | 364 | |
---|
[abf8d5d] | 365 | if(debug) { |
---|
| 366 | fprintf(stderr," the principal part(%s)\n", prin); |
---|
| 367 | fprintf(stderr," the role/oset part(%s)\n", estring); |
---|
| 368 | fprintf(stderr," nm part(%s)\n", nm); |
---|
| 369 | } |
---|
[7727f26] | 370 | if(prin[0]=='\'' || prin[0]=='"') { |
---|
| 371 | arg[0]=YAP_ReadBuffer(prin,eterm0); |
---|
[36b100a] | 372 | } else { |
---|
[7727f26] | 373 | arg[0]=YAP_MkAtomTerm(YAP_LookupAtom(prin)); |
---|
[36b100a] | 374 | } |
---|
[2485307] | 375 | if(debug) fprintf(stderr," the role/oset part(%s)\n", estring); |
---|
[36b100a] | 376 | arg[1]=YAP_ReadBuffer(estring,eterm1); |
---|
| 377 | |
---|
| 378 | /* var for credential list */ |
---|
[da5afdf] | 379 | arg[2]=YAP_MkVarTerm(); |
---|
[36b100a] | 380 | |
---|
[da5afdf] | 381 | YAP_Atom f = YAP_LookupAtom("isMember"); |
---|
| 382 | YAP_Functor func = YAP_MkFunctor(f, 3); |
---|
| 383 | YAP_Term goal=YAP_MkApplTerm(func, 3, arg); |
---|
| 384 | |
---|
[abf8d5d] | 385 | pl->safe_t = YAP_InitSlot(goal); |
---|
| 386 | pl->query_rc =YAP_RunGoal(goal); |
---|
| 387 | pl->query_made=1; |
---|
| 388 | pl->next_rc=pl->query_rc; |
---|
| 389 | if (pl->query_rc) { |
---|
[da5afdf] | 390 | printf("YAP query succeed\n"); |
---|
[2485307] | 391 | char *tmp=NULL; |
---|
| 392 | int tmp_sz=try_again(0, &tmp); |
---|
[abf8d5d] | 393 | YAP_Term argterm=YAP_ArgOfTerm(3,YAP_GetFromSlot(pl->safe_t)); |
---|
[2485307] | 394 | while(1) { |
---|
[05f6d35] | 395 | /* |
---|
[6244e28] | 396 | int rc=YAP_WriteBuffer(argterm, tmp, tmp_sz,YAP_WRITE_HANDLE_VARS); |
---|
[05f6d35] | 397 | */ |
---|
| 398 | YAP_WriteBuffer(argterm, tmp, tmp_sz,YAP_WRITE_HANDLE_VARS); |
---|
| 399 | /* |
---|
| 400 | if(debug) fprintf(stderr,"call YAP_WriteBuffer: rc(%d) tmp_sz(%d)\n", rc, tmp_sz); |
---|
| 401 | */ |
---|
[6244e28] | 402 | if(strlen(tmp) > 5 && tmp[0]=='\[') { |
---|
| 403 | if(debug) |
---|
| 404 | fprintf(stderr,"what came back .. (%s)\n", tmp); |
---|
[2485307] | 405 | break; |
---|
[6244e28] | 406 | } |
---|
[2485307] | 407 | tmp_sz=try_again(tmp_sz,&tmp); |
---|
[6244e28] | 408 | if(debug) |
---|
| 409 | fprintf(stderr,"try_again: tmp_sz(%d)\n", tmp_sz); |
---|
[bf68132] | 410 | if(tmp_sz==0) { |
---|
| 411 | fprintf(stderr," PANIC, run out of heap space..\n"); |
---|
| 412 | YAP_Exit(1); |
---|
| 413 | } |
---|
[2485307] | 414 | } |
---|
[da5afdf] | 415 | /* this is returned as ['string1','string2'] */ |
---|
[2485307] | 416 | if(debug) fprintf(stderr," query answer : %s(%d)\n", tmp, strlen(tmp)); |
---|
[da5afdf] | 417 | _credentials_from_string(cred_list,tmp); |
---|
[abf8d5d] | 418 | if(abac_list_size(cred_list)==0) { |
---|
[2485307] | 419 | fprintf(stderr,"CAN NOT retrieve result properly from YAP!!!\n"); |
---|
| 420 | YAP_Exit(1); |
---|
| 421 | } |
---|
[abf8d5d] | 422 | abac_list_add(pl->proof_list, cred_list); |
---|
[b92a620] | 423 | YAP_FreeSpaceFromYap(tmp); |
---|
[da5afdf] | 424 | } else { |
---|
| 425 | printf("YAP query failed\n"); |
---|
| 426 | /* YAP_Exit(1); */ |
---|
| 427 | } |
---|
| 428 | return cred_list; |
---|
| 429 | } |
---|
| 430 | |
---|
[abf8d5d] | 431 | /* make a query for next possible solution proof */ |
---|
| 432 | static abac_list_t *_query_again(abac_pl_t *pl) |
---|
| 433 | { |
---|
| 434 | abac_list_t *cred_list=abac_list_new(); |
---|
| 435 | |
---|
| 436 | pl->next_rc=YAP_RestartGoal(); |
---|
| 437 | pl->query_made++; |
---|
| 438 | if(debug) fprintf(stderr,"query_again: another success\n"); |
---|
| 439 | |
---|
| 440 | char *tmp=NULL; |
---|
| 441 | int tmp_sz=try_again(0, &tmp); |
---|
| 442 | YAP_Term argterm=YAP_ArgOfTerm(3,YAP_GetFromSlot(pl->safe_t)); |
---|
| 443 | if(pl->next_rc) { |
---|
| 444 | while(1) { |
---|
| 445 | YAP_WriteBuffer(argterm, tmp, tmp_sz,YAP_WRITE_HANDLE_VARS); |
---|
| 446 | if(strlen(tmp) > 5 && tmp[0]=='\[') |
---|
| 447 | break; |
---|
| 448 | tmp_sz=try_again(tmp_sz,&tmp); |
---|
| 449 | if(tmp_sz==0) { |
---|
| 450 | fprintf(stderr," PANIC, run out of heap space..\n"); |
---|
| 451 | YAP_Exit(1); |
---|
| 452 | } |
---|
| 453 | } |
---|
| 454 | /* this is returned as ['string1','string2'] */ |
---|
| 455 | if(debug) fprintf(stderr," query answer : %s(%d)\n", tmp, strlen(tmp)); |
---|
| 456 | _credentials_from_string(cred_list,tmp); |
---|
| 457 | if(abac_list_size(cred_list)==0) { |
---|
| 458 | fprintf(stderr,"CAN NOT retrieve result properly from YAP!!!\n"); |
---|
| 459 | YAP_Exit(1); |
---|
| 460 | } |
---|
| 461 | abac_list_add(pl->proof_list, cred_list); |
---|
| 462 | YAP_FreeSpaceFromYap(tmp); |
---|
| 463 | } |
---|
| 464 | return cred_list; |
---|
| 465 | } |
---|
| 466 | |
---|
| 467 | abac_stack_t *_make_cred_stack(abac_list_t *p) |
---|
| 468 | { |
---|
| 469 | abac_stack_t *ret=abac_stack_new(); |
---|
| 470 | if(abac_list_size(p) != 0) { |
---|
| 471 | abac_credential_t *cur=NULL; |
---|
| 472 | abac_list_foreach(p, cur, |
---|
| 473 | abac_stack_push(ret, (void *)abac_credential_dup(cur)); |
---|
| 474 | ); |
---|
| 475 | } |
---|
| 476 | return ret; |
---|
| 477 | } |
---|
| 478 | |
---|
| 479 | /* force a backtrack to get next solution proof */ |
---|
| 480 | abac_stack_t *abac_pl_query_again(abac_pl_t *pl) |
---|
| 481 | { |
---|
| 482 | abac_list_t *rlist=_query_again(pl); |
---|
| 483 | abac_stack_t *ret=_make_cred_stack(rlist); |
---|
| 484 | return ret; |
---|
| 485 | } |
---|
| 486 | |
---|
[da5afdf] | 487 | /* 2 types |
---|
| 488 | acme.buys_rocket <- coyote (coyote=prin, acme.buys_rocket=role) |
---|
| 489 | ==> isMember(coyote,role(acme,buys_rocket), L) |
---|
[440ba20] | 490 | acme.buys_rocket <- acme.preferred_customer -- NOT valid |
---|
| 491 | */ |
---|
[8bd77b5] | 492 | static abac_stack_t *_query_with_aspect(abac_pl_t *pl, abac_aspect_t* head, abac_aspect_t* tail) |
---|
[da5afdf] | 493 | { |
---|
| 494 | abac_stack_t *ret=NULL; |
---|
[2485307] | 495 | char *tmp=NULL; |
---|
[da5afdf] | 496 | |
---|
[08b8da7] | 497 | if(0) |
---|
[440ba20] | 498 | show_yap_db("DEBUG:calling within _query_with_aspect"); |
---|
[da5afdf] | 499 | |
---|
[d037f54] | 500 | char *nm; |
---|
| 501 | PROLOG(nm=abac_aspect_principal_name(head);); |
---|
[8bd77b5] | 502 | |
---|
[da5afdf] | 503 | /* could be obj or principal */ |
---|
[7727f26] | 504 | char *prin_nm=NULL; |
---|
[8bd77b5] | 505 | if(abac_aspect_is_object(tail)) { |
---|
| 506 | prin_nm=abac_aspect_object_name(tail); |
---|
[da5afdf] | 507 | } else { |
---|
[d037f54] | 508 | PROLOG(prin_nm=abac_aspect_principal_name(tail);); |
---|
[da5afdf] | 509 | } |
---|
| 510 | |
---|
[8bd77b5] | 511 | if(abac_aspect_aspect_name(tail)!=NULL) { |
---|
| 512 | printf("fail, a.o <- b.o and a.r <- a.r query is not implemented yet !!!\n"); |
---|
[da5afdf] | 513 | YAP_Exit(1); |
---|
| 514 | } |
---|
| 515 | |
---|
[8bd77b5] | 516 | if (prin_nm == NULL || nm == NULL) { |
---|
| 517 | printf("fail, query's call got bad aspect names .. \n"); |
---|
[da5afdf] | 518 | YAP_Exit(1); |
---|
| 519 | } |
---|
| 520 | |
---|
[2485307] | 521 | if(debug) fprintf(stderr,"printing up the yap query ..\n"); |
---|
[d037f54] | 522 | |
---|
| 523 | char *pstring; |
---|
| 524 | PROLOG(pstring=abac_aspect_aspect_param_string(head);); |
---|
| 525 | |
---|
[8bd77b5] | 526 | char *stub=abac_aspect_type_string(head); |
---|
[da5afdf] | 527 | if(pstring) { |
---|
[2485307] | 528 | asprintf(&tmp,"%s(%s,%s,%s)", stub, nm, abac_aspect_aspect_name(head),pstring); |
---|
[da5afdf] | 529 | free(pstring); |
---|
| 530 | } else { |
---|
[2485307] | 531 | asprintf(&tmp,"%s(%s,%s)", stub, nm, abac_aspect_aspect_name(head)); |
---|
[da5afdf] | 532 | } |
---|
| 533 | |
---|
[abf8d5d] | 534 | _reset_pl(pl,tmp,prin_nm); |
---|
| 535 | abac_list_t *rlist=_make_yap_query(pl,prin_nm,nm,tmp); |
---|
| 536 | /* generate the resulting stack from a list */ |
---|
| 537 | ret=_make_cred_stack(rlist); |
---|
[da5afdf] | 538 | return ret; |
---|
| 539 | } |
---|
| 540 | |
---|
| 541 | /** |
---|
| 542 | * Get all the credentials (attribute/issuer cert pairs) from prolog |
---|
| 543 | * (which returns in string form) |
---|
| 544 | */ |
---|
| 545 | abac_stack_t *abac_pl_credentials(abac_pl_t *pl) |
---|
| 546 | { |
---|
[8bd77b5] | 547 | abac_stack_t *ret=abac_verifier_dump_creds(); |
---|
[da5afdf] | 548 | return ret; |
---|
| 549 | } |
---|
| 550 | |
---|
[5110d42] | 551 | abac_stack_t *abac_pl_principals(abac_pl_t *pl) |
---|
| 552 | { |
---|
| 553 | abac_stack_t *ret=abac_verifier_dump_principals(); |
---|
| 554 | return ret; |
---|
| 555 | } |
---|
| 556 | |
---|
[e95d652] | 557 | /** |
---|
| 558 | * Make a query into prolog db |
---|
| 559 | --role acme.preferred_customer --principal coyote |
---|
| 560 | --role acme.prefer_customer.buy_rockets --principlal coyote |
---|
[da5afdf] | 561 | --oset acme.rockets -- object mrx-21 |
---|
| 562 | --oset acme.villans -- principal coyote |
---|
[e95d652] | 563 | */ |
---|
[da5afdf] | 564 | abac_stack_t *abac_pl_query(abac_pl_t *pl, char *roleoset, char *prinobj) |
---|
[e95d652] | 565 | { |
---|
[202a7f9] | 566 | abac_stack_t *ret=NULL; |
---|
[da5afdf] | 567 | int len=strlen(roleoset)+strlen(prinobj)+5; |
---|
| 568 | char* attr_string=(char *) abac_xmalloc(sizeof(char)*len); |
---|
| 569 | sprintf(attr_string,"%s<-%s", roleoset, prinobj); |
---|
| 570 | |
---|
| 571 | if(debug) |
---|
[2485307] | 572 | fprintf(stderr,"abac_pl_query, query string is (%s)\n",attr_string); |
---|
[e95d652] | 573 | |
---|
[da5afdf] | 574 | /* call into yacc parser */ |
---|
| 575 | abac_reset_yyfptr(attr_string); |
---|
[b5a3da4] | 576 | abac_yy_init(); |
---|
[da5afdf] | 577 | int rc=yyparse(); |
---|
| 578 | if (rc) { |
---|
| 579 | free(attr_string); |
---|
[e95d652] | 580 | return NULL; |
---|
| 581 | } |
---|
| 582 | |
---|
[8bd77b5] | 583 | abac_aspect_t *head_aspect = abac_yy_get_rule_head_aspect(); |
---|
| 584 | abac_aspect_t *tail_aspect = abac_yy_get_rule_tail_aspect(); |
---|
[2efdff5] | 585 | |
---|
[8bd77b5] | 586 | ret=_query_with_aspect(pl,head_aspect,tail_aspect); |
---|
| 587 | |
---|
[e95d652] | 588 | return ret; |
---|
| 589 | } |
---|
| 590 | |
---|
[d9c3886] | 591 | abac_stack_t *abac_pl_query_with_structure(abac_pl_t *pl, abac_aspect_t *head_aspect, abac_aspect_t *tail_aspect) |
---|
| 592 | { |
---|
| 593 | abac_stack_t *ret=NULL; |
---|
| 594 | ret=_query_with_aspect(pl,head_aspect,tail_aspect); |
---|
| 595 | |
---|
| 596 | return ret; |
---|
| 597 | } |
---|