[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 | |
---|
| 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 | |
---|
| 22 | extern char *abac_cn_with_role(abac_role_t *); |
---|
[202a7f9] | 23 | extern abac_list_t *abac_credential_clauses(abac_credential_t *cred); |
---|
| 24 | static int debug=0; |
---|
[e95d652] | 25 | |
---|
| 26 | // pl |
---|
| 27 | struct _abac_pl_t { |
---|
| 28 | FILE *fptr; |
---|
| 29 | char *fname; |
---|
| 30 | char *yap_certs; |
---|
| 31 | }; |
---|
| 32 | |
---|
[53e540d] | 33 | static int _insert_clause(char *str) |
---|
[e95d652] | 34 | { |
---|
[202a7f9] | 35 | YAP_Term *eterm; |
---|
[53e540d] | 36 | YAP_Term goalArgs=YAP_ReadBuffer(str, eterm); |
---|
| 37 | char *tmp=YAP_CompileClause(goalArgs); |
---|
| 38 | if (tmp!=NULL) { /* something is wrong */ |
---|
| 39 | printf("error: result of compile clause (%s)\n", tmp); |
---|
| 40 | printf("error: str used (%s)\n", str); |
---|
| 41 | return 1; |
---|
[e95d652] | 42 | } |
---|
[53e540d] | 43 | return 0; |
---|
[e95d652] | 44 | } |
---|
| 45 | |
---|
[53e540d] | 46 | static int _insert_cred_clause(char *cstr) |
---|
[e95d652] | 47 | { |
---|
| 48 | int ret=ABAC_CERT_SUCCESS; |
---|
[53e540d] | 49 | int rc=_insert_clause(cstr); |
---|
| 50 | if (rc) |
---|
| 51 | return ABAC_CERT_BAD_YAP; |
---|
| 52 | return ABAC_CERT_SUCCESS; |
---|
[e95d652] | 53 | } |
---|
| 54 | |
---|
| 55 | static void _show_yap_db(char *msg) |
---|
| 56 | { |
---|
| 57 | char lstr[]="listing"; |
---|
| 58 | YAP_Term *eterm; |
---|
| 59 | |
---|
[53e540d] | 60 | printf("\n\n========= yap db (%s)\n",msg); |
---|
[e95d652] | 61 | YAP_Term goal=YAP_ReadBuffer(lstr, eterm); |
---|
| 62 | int rc =YAP_RunGoal( goal ); |
---|
| 63 | if (rc) { |
---|
| 64 | printf("listing ok.. \n"); |
---|
| 65 | } else { |
---|
| 66 | printf("listing's rc is bad.. \n"); |
---|
| 67 | YAP_Exit(1); |
---|
| 68 | } |
---|
[53e540d] | 69 | printf("========= \n\n"); |
---|
[e95d652] | 70 | } |
---|
| 71 | |
---|
| 72 | static void _set_dbg(abac_pl_t *pl) |
---|
| 73 | { |
---|
| 74 | char *fname=strdup( "/tmp/abac_yap-XXXXXX.pl" ); |
---|
| 75 | int fd = mkstemps( pl->fname, 3 ); |
---|
| 76 | if ( fd == -1) { |
---|
| 77 | printf("Couldn't get a valid temp name %s\n", fname); |
---|
| 78 | free((void*)fname); |
---|
| 79 | YAP_Exit(1); |
---|
| 80 | } |
---|
| 81 | FILE *fptr = fdopen(fd, "w"); |
---|
| 82 | if (fptr == NULL) { |
---|
| 83 | printf("Couldn't open %s for writing\n", fname); |
---|
| 84 | free((void*)fname); |
---|
| 85 | YAP_Exit(1); |
---|
| 86 | } |
---|
| 87 | pl->fptr=fptr; |
---|
| 88 | pl->fname=fname; |
---|
| 89 | } |
---|
| 90 | |
---|
[53e540d] | 91 | /** |
---|
| 92 | * Include some utility routines |
---|
| 93 | */ |
---|
| 94 | abac_pl_t *abac_pl_utility(void) { |
---|
| 95 | /* |
---|
| 96 | append([],L,L). |
---|
| 97 | append([X|L1],L2,[X|L3]):-append(L1,L2,L3). |
---|
| 98 | |
---|
| 99 | appendL([],[]). |
---|
| 100 | appendL([H|T], L) :- |
---|
| 101 | appendL(T,L2), append(H,L2,L). |
---|
| 102 | */ |
---|
| 103 | if(_insert_clause("append([],L,L)")) |
---|
| 104 | YAP_Exit(1); |
---|
| 105 | if(_insert_clause("append([X|L1],L2,[X|L3]):-append(L1,L2,L3)")) |
---|
| 106 | YAP_Exit(1); |
---|
| 107 | if(_insert_clause("appendL([],[])")) |
---|
| 108 | YAP_Exit(1); |
---|
| 109 | if(_insert_clause("appendL([H|T], L) :- appendL(T,L2), append(H,L2,L)")) |
---|
| 110 | YAP_Exit(1); |
---|
[e95d652] | 111 | } |
---|
| 112 | |
---|
[53e540d] | 113 | /** |
---|
| 114 | * Create a new yap structure. |
---|
| 115 | */ |
---|
| 116 | abac_pl_t *abac_pl_new(void) { |
---|
| 117 | |
---|
| 118 | if (YAP_FastInit(NULL) == YAP_BOOT_ERROR) |
---|
| 119 | YAP_Exit(1); |
---|
| 120 | |
---|
| 121 | if (YAP_RunGoal(YAP_MkAtomTerm(YAP_LookupAtom("source")))) { |
---|
[202a7f9] | 122 | if(debug) printf("calling source..\n"); |
---|
[53e540d] | 123 | } else { |
---|
[202a7f9] | 124 | if(debug) printf("calling source failed..\n"); |
---|
| 125 | YAP_Exit(1); |
---|
[53e540d] | 126 | } |
---|
| 127 | |
---|
| 128 | abac_pl_utility(); |
---|
| 129 | |
---|
| 130 | abac_pl_t *pl = abac_xmalloc(sizeof(abac_pl_t)); |
---|
| 131 | pl->fptr=NULL; |
---|
| 132 | pl->fname=NULL; |
---|
| 133 | pl->yap_certs=NULL; |
---|
| 134 | return pl; |
---|
[e95d652] | 135 | } |
---|
| 136 | |
---|
[53e540d] | 137 | /** |
---|
[202a7f9] | 138 | * Add a credential to the db |
---|
[53e540d] | 139 | */ |
---|
| 140 | int abac_pl_add_credential(abac_pl_t *pl, abac_credential_t *cred) |
---|
| 141 | { |
---|
| 142 | int rc=0; |
---|
[202a7f9] | 143 | abac_list_t *clauses=abac_credential_clauses(cred); |
---|
| 144 | if (clauses != NULL) { |
---|
| 145 | char *cur; |
---|
| 146 | abac_list_foreach(clauses, cur, |
---|
| 147 | if(cur) { |
---|
| 148 | if(debug) printf("inserting =>%s\n",cur); |
---|
| 149 | rc=_insert_cred_clause(cur); |
---|
| 150 | } |
---|
| 151 | ); |
---|
[53e540d] | 152 | } |
---|
| 153 | return rc; |
---|
| 154 | } |
---|
[e95d652] | 155 | |
---|
[202a7f9] | 156 | int abac_pl_add_type_credential(abac_pl_t *pl, abac_id_cert_t *id_cert) |
---|
| 157 | { |
---|
| 158 | char *clause=abac_id_clause(id_cert); |
---|
| 159 | if (clause != NULL) { |
---|
| 160 | int rc=_insert_cred_clause(clause); |
---|
| 161 | return rc; |
---|
| 162 | } |
---|
| 163 | return 0; |
---|
| 164 | } |
---|
| 165 | |
---|
[e95d652] | 166 | /* cases, |
---|
| 167 | ['str'] |
---|
| 168 | ['str1','str2'] |
---|
| 169 | ([] is not possible, and don't care) |
---|
| 170 | */ |
---|
[202a7f9] | 171 | static void _credentials_from_string(abac_stack_t *credentials,char *slist) { |
---|
[e95d652] | 172 | char *cptr=slist; /* current ptr */ |
---|
| 173 | char *sptr; /* string ptr */ |
---|
| 174 | char *ptr; |
---|
[202a7f9] | 175 | int len=0; |
---|
[e95d652] | 176 | char *string; |
---|
| 177 | abac_credential_t *cred=NULL; |
---|
[202a7f9] | 178 | int cnt=0; |
---|
| 179 | |
---|
| 180 | if(debug) |
---|
| 181 | printf("DEBUG:responds from yap(%s)\n",slist); |
---|
[e95d652] | 182 | |
---|
| 183 | /* find first [' */ |
---|
| 184 | ptr=strstr(cptr,"['"); |
---|
| 185 | if(ptr == NULL) |
---|
| 186 | return; |
---|
| 187 | cptr=ptr+2; |
---|
| 188 | sptr=cptr; |
---|
| 189 | while (1) { |
---|
| 190 | /* find next ',' or '] */ |
---|
| 191 | ptr=strstr(cptr,"','"); |
---|
| 192 | if(ptr!=NULL) { |
---|
| 193 | cptr=ptr+3; |
---|
[202a7f9] | 194 | len=(ptr-sptr); |
---|
[e95d652] | 195 | string=strndup(sptr,len); |
---|
| 196 | cred=abac_credential_lookup(string); |
---|
[202a7f9] | 197 | free(string); |
---|
[e95d652] | 198 | if(cred) { |
---|
[202a7f9] | 199 | abac_stack_push(credentials, cred); |
---|
| 200 | cnt++; |
---|
[e95d652] | 201 | } else { |
---|
| 202 | printf("BAD BAD BAD\n"); |
---|
| 203 | } |
---|
| 204 | sptr=cptr; |
---|
| 205 | } else { |
---|
| 206 | ptr=strstr(cptr,"']"); |
---|
| 207 | if(ptr!=NULL) { |
---|
[202a7f9] | 208 | len=(ptr-sptr); |
---|
[e95d652] | 209 | string=strndup(sptr,len); |
---|
| 210 | cred=abac_credential_lookup(string); |
---|
[202a7f9] | 211 | free(string); |
---|
[e95d652] | 212 | if(cred) { |
---|
[202a7f9] | 213 | abac_stack_push(credentials, cred); |
---|
| 214 | cnt++; |
---|
[e95d652] | 215 | } else { |
---|
| 216 | printf("BAD BAD BAD\n"); |
---|
| 217 | } |
---|
| 218 | break; |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | } |
---|
[202a7f9] | 222 | if(debug) |
---|
| 223 | printf("DEBUG:total %d credentials\n", cnt); |
---|
[e95d652] | 224 | } |
---|
| 225 | |
---|
| 226 | /* 2 types |
---|
[202a7f9] | 227 | acme.buys_rocket <- coyote (coyote=prin, acme.buys_rocket=role) |
---|
[e95d652] | 228 | acme.buys_rocket <- acme.preferred_customer -- NOT DONE YET */ |
---|
[202a7f9] | 229 | static abac_stack_t *_query(abac_pl_t *pl, abac_role_t* role, abac_role_t* role_prin ) |
---|
[e95d652] | 230 | { |
---|
| 231 | YAP_Term *eterm; |
---|
| 232 | YAP_Term arg[3]; |
---|
[202a7f9] | 233 | char tmp[2000]; |
---|
| 234 | abac_stack_t *cred_list = abac_stack_new(); |
---|
| 235 | |
---|
| 236 | if(debug) |
---|
| 237 | _show_yap_db("DEBUG:calling within _query"); |
---|
[e95d652] | 238 | |
---|
| 239 | char *cn_prin=abac_cn_with_role(role_prin); |
---|
| 240 | char *cn_role=abac_cn_with_role(role); |
---|
| 241 | |
---|
| 242 | if(abac_role_role_name(role_prin)!=NULL) { |
---|
[202a7f9] | 243 | printf("fail, a.r <- b.r query is not implemented yet !!!\n"); |
---|
[e95d652] | 244 | YAP_Exit(1); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | if (cn_prin == NULL || cn_role == NULL) { |
---|
| 248 | printf("fail, query's call got bad roles.. \n"); |
---|
| 249 | YAP_Exit(1); |
---|
| 250 | } |
---|
| 251 | sprintf(tmp,"role(%s,%s)", cn_role, abac_role_role_name(role)); |
---|
| 252 | arg[0]=YAP_MkAtomTerm(YAP_LookupAtom(cn_prin)); |
---|
| 253 | arg[1]=YAP_ReadBuffer(tmp,eterm); |
---|
| 254 | arg[2]=YAP_MkVarTerm(); |
---|
| 255 | YAP_Atom f = YAP_LookupAtom("isMember"); |
---|
| 256 | YAP_Functor func = YAP_MkFunctor(f, 3); |
---|
| 257 | YAP_Term goal=YAP_MkApplTerm(func, 3, arg); |
---|
| 258 | int rc =YAP_RunGoal( goal ); |
---|
| 259 | if (rc) { |
---|
| 260 | printf("success, query call ok.. \n"); |
---|
[202a7f9] | 261 | YAP_WriteBuffer(arg[2], tmp, 2000,YAP_WRITE_HANDLE_VARS); |
---|
[53e540d] | 262 | /* printf(" answer : %s(%d)\n", tmp, strlen(tmp)); */ |
---|
[e95d652] | 263 | /* this is returned as ['string1','string2'] */ |
---|
| 264 | _credentials_from_string(cred_list,tmp); |
---|
| 265 | while (YAP_RestartGoal()) { |
---|
| 266 | printf("another success\n"); |
---|
[202a7f9] | 267 | YAP_WriteBuffer(arg[2], tmp, 2000,YAP_WRITE_HANDLE_VARS); |
---|
[53e540d] | 268 | /* printf(" answer : %s\n", tmp); */ |
---|
[e95d652] | 269 | _credentials_from_string(cred_list,tmp); |
---|
| 270 | } |
---|
| 271 | } else { |
---|
| 272 | printf("fail, query's call is bad.. \n"); |
---|
| 273 | YAP_Exit(1); |
---|
| 274 | } |
---|
| 275 | return cred_list; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | /** |
---|
| 279 | * Make a query into prolog db |
---|
| 280 | --role acme.preferred_customer --principal coyote |
---|
| 281 | --role acme.prefer_customer.buy_rockets --principlal coyote |
---|
| 282 | */ |
---|
[202a7f9] | 283 | abac_stack_t *abac_pl_query(abac_pl_t *pl, char *role_name, char *principal) |
---|
[e95d652] | 284 | { |
---|
| 285 | abac_role_t *role = abac_role_from_string(role_name); |
---|
| 286 | abac_role_t *prin_role = abac_role_from_string(principal); |
---|
[202a7f9] | 287 | abac_stack_t *ret=NULL; |
---|
[e95d652] | 288 | |
---|
| 289 | // give up on bogus roles |
---|
| 290 | if (role == NULL || prin_role == NULL) { |
---|
| 291 | return NULL; |
---|
| 292 | } |
---|
| 293 | |
---|
[53e540d] | 294 | ret=_query(pl,role,prin_role); |
---|
[e95d652] | 295 | |
---|
| 296 | return ret; |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | /** |
---|
| 300 | * Get all the credentials (attribute/issuer cert pairs) from prolog |
---|
| 301 | * (which returns in string form) |
---|
| 302 | */ |
---|
[202a7f9] | 303 | abac_stack_t *abac_pl_credentials(abac_pl_t *pl) { |
---|
| 304 | abac_stack_t *credentials = abac_stack_new(); |
---|
[e95d652] | 305 | /* fix up a stream to stream the credentials from prolog engine */ |
---|
| 306 | return credentials; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | void abac_pl_free(abac_pl_t *pl) { |
---|
| 310 | if(pl->fptr) { |
---|
| 311 | fflush(pl->fptr); |
---|
| 312 | free(pl->fptr); |
---|
| 313 | } |
---|
| 314 | if(pl->fname) { |
---|
| 315 | unlink(pl->fname); |
---|
| 316 | free(pl->fname); |
---|
| 317 | } |
---|
| 318 | free(pl); |
---|
| 319 | } |
---|
| 320 | |
---|