[8bd77b5] | 1 | |
---|
| 2 | /* abac.c using prolog */ |
---|
| 3 | |
---|
| 4 | #include <assert.h> |
---|
| 5 | #include <err.h> |
---|
| 6 | #include <glob.h> |
---|
| 7 | |
---|
| 8 | #include <chunk.h> |
---|
| 9 | |
---|
| 10 | #include "abac_internal.h" |
---|
| 11 | |
---|
| 12 | #include "abac_pl_yap.h" |
---|
| 13 | #include "abac_util.h" |
---|
| 14 | #include "abac_verifier.h" |
---|
| 15 | |
---|
| 16 | static int debug=0; |
---|
| 17 | |
---|
| 18 | struct _abac_context_t { |
---|
| 19 | abac_pl_t *pl; |
---|
| 20 | }; |
---|
| 21 | |
---|
| 22 | /** |
---|
| 23 | * Init the library. |
---|
| 24 | */ |
---|
| 25 | void libabac_init(void) { |
---|
| 26 | void libabac_deinit(void); |
---|
| 27 | static int has_been_init = 0; |
---|
| 28 | |
---|
| 29 | // called every time a context is created, so only do it once |
---|
| 30 | if (!has_been_init) { |
---|
| 31 | abac_verifier_init(); |
---|
| 32 | atexit(libabac_deinit); |
---|
| 33 | has_been_init = 1; |
---|
| 34 | } |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | * Deinit the library. |
---|
| 39 | */ |
---|
| 40 | void libabac_deinit(void) { |
---|
| 41 | abac_verifier_deinit(); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | /** |
---|
| 45 | * Create a new abac context. |
---|
| 46 | */ |
---|
| 47 | abac_context_t *abac_context_new(void) { |
---|
| 48 | libabac_init(); |
---|
| 49 | |
---|
| 50 | abac_context_t *ctx = abac_xmalloc(sizeof(abac_context_t)); |
---|
| 51 | ctx->pl = abac_pl_new(); |
---|
| 52 | return ctx; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | /** |
---|
| 56 | * Deep copy an abac context. -- XXX dummy stub |
---|
| 57 | */ |
---|
| 58 | abac_context_t *abac_context_dup(abac_context_t *ctx) { |
---|
| 59 | assert(ctx != NULL); |
---|
| 60 | abac_context_t *dup = abac_xmalloc(sizeof(abac_context_t)); |
---|
| 61 | return dup; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | * Free an abac context. |
---|
| 67 | */ |
---|
| 68 | void abac_context_free(abac_context_t *ctx) { |
---|
| 69 | assert(ctx != NULL); |
---|
| 70 | |
---|
| 71 | abac_pl_free(ctx->pl); |
---|
| 72 | free(ctx); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * Load an ID cert from an abac_id_t. |
---|
| 77 | */ |
---|
| 78 | int abac_context_load_id_id(abac_context_t *ctx, abac_id_t *id) { |
---|
| 79 | assert(ctx != NULL); assert(id); |
---|
| 80 | abac_id_credential_t *id_cert=NULL; |
---|
| 81 | int ret = abac_verifier_load_id_id(id,&id_cert); |
---|
| 82 | if(id_cert) { |
---|
| 83 | int add_ret = abac_pl_add_type_credential(ctx->pl, id_cert); |
---|
| 84 | } |
---|
| 85 | return ret; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /** |
---|
| 89 | * Load an ID cert from a file. |
---|
| 90 | */ |
---|
[5110d42] | 91 | int abac_context_load_id_id_file_key_file(abac_context_t *ctx, char *filename, char *keyfilename) { |
---|
[d037f54] | 92 | assert(ctx != NULL); assert(filename != NULL); assert(keyfilename!=NULL); |
---|
[8bd77b5] | 93 | abac_id_credential_t *id_cert=NULL; |
---|
[5110d42] | 94 | int ret = abac_verifier_load_id_file_key_file(filename,keyfilename,&id_cert); |
---|
| 95 | if(id_cert) { |
---|
| 96 | int add_ret = abac_pl_add_type_credential(ctx->pl, id_cert); |
---|
| 97 | } |
---|
| 98 | return ret; |
---|
| 99 | } |
---|
| 100 | /** |
---|
| 101 | * Load an ID cert from a file, it may contain key part but not necessary. |
---|
| 102 | */ |
---|
| 103 | int abac_context_load_id_idkey_file(abac_context_t *ctx, char *filename) { |
---|
| 104 | assert(ctx != NULL); assert(filename != NULL); |
---|
| 105 | abac_id_credential_t *id_cert=NULL; |
---|
| 106 | int ret = abac_verifier_load_idkey_file(filename,&id_cert); |
---|
[8bd77b5] | 107 | if(id_cert) { |
---|
| 108 | int add_ret = abac_pl_add_type_credential(ctx->pl, id_cert); |
---|
| 109 | } |
---|
| 110 | return ret; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | * Load an ID cert from a chunk. |
---|
| 115 | */ |
---|
| 116 | int abac_context_load_id_chunk(abac_context_t *ctx, abac_chunk_t cert) { |
---|
| 117 | assert(ctx != NULL); |
---|
| 118 | abac_id_credential_t *id_cert=NULL; |
---|
| 119 | chunk_t cert_chunk = { cert.ptr, cert.len }; |
---|
| 120 | int ret=abac_verifier_load_id_chunk(cert_chunk,&id_cert); |
---|
| 121 | if(id_cert) { |
---|
| 122 | int add_ret = abac_pl_add_type_credential(ctx->pl, id_cert); |
---|
| 123 | } |
---|
| 124 | return ret; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | * Load an attribute cert from an abac_attribute_t. |
---|
| 129 | */ |
---|
| 130 | int abac_context_load_attribute_attribute(abac_context_t *ctx, abac_attribute_t *ptr) |
---|
| 131 | { |
---|
[5d06689] | 132 | int ret, add_ret; |
---|
| 133 | abac_credential_t *cred=NULL; |
---|
| 134 | |
---|
| 135 | assert(ctx != NULL); |
---|
| 136 | |
---|
| 137 | ret = abac_verifier_load_attribute_cert_attribute(ptr, &cred); |
---|
| 138 | if (ret == ABAC_CERT_SUCCESS) { |
---|
| 139 | add_ret = abac_pl_add_credential(ctx->pl, cred); |
---|
| 140 | assert(add_ret != ABAC_PL_CRED_INVALID); |
---|
| 141 | } |
---|
| 142 | |
---|
[8bd77b5] | 143 | return ret; |
---|
[5d06689] | 144 | |
---|
[8bd77b5] | 145 | } |
---|
| 146 | |
---|
| 147 | /** |
---|
| 148 | * Load an attribute cert from a file. |
---|
| 149 | */ |
---|
| 150 | int abac_context_load_attribute_file(abac_context_t *ctx, char *filename) { |
---|
| 151 | int ret, add_ret; |
---|
[5110d42] | 152 | abac_credential_t *cred=NULL; |
---|
[8bd77b5] | 153 | |
---|
| 154 | assert(ctx != NULL); assert(filename != NULL); |
---|
| 155 | |
---|
| 156 | ret = abac_verifier_load_attribute_cert_file(filename, &cred); |
---|
| 157 | if (ret == ABAC_CERT_SUCCESS) { |
---|
| 158 | add_ret = abac_pl_add_credential(ctx->pl, cred); |
---|
| 159 | assert(add_ret != ABAC_PL_CRED_INVALID); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | return ret; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | /** |
---|
| 166 | * Load an attribute cert from a chunk. |
---|
| 167 | */ |
---|
| 168 | int abac_context_load_attribute_chunk(abac_context_t *ctx, abac_chunk_t cert) { |
---|
| 169 | int ret, add_ret; |
---|
[5110d42] | 170 | abac_credential_t *cred=NULL; |
---|
[8bd77b5] | 171 | |
---|
| 172 | assert(ctx != NULL); |
---|
| 173 | |
---|
| 174 | chunk_t cert_chunk = { cert.ptr, cert.len }; |
---|
| 175 | |
---|
| 176 | ret = abac_verifier_load_attribute_cert_chunk(cert_chunk, &cred); |
---|
| 177 | if (ret == ABAC_CERT_SUCCESS) { |
---|
| 178 | add_ret = abac_pl_add_credential(ctx->pl, cred); |
---|
| 179 | assert(add_ret != ABAC_PL_CRED_INVALID); |
---|
| 180 | abac_credential_free(cred); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | return ret; |
---|
| 184 | } |
---|
| 185 | |
---|
[d037f54] | 186 | #define KEY_PAT "/*_private.{der,pem}" |
---|
[8bd77b5] | 187 | #define ID_PAT "/*_ID.{der,pem}" |
---|
| 188 | #define ATTR_PAT "/*_attr.der" |
---|
[5110d42] | 189 | #define IDKEY_PAT "/*_IDKEY.{der,pem}" |
---|
[8bd77b5] | 190 | |
---|
[d037f54] | 191 | char *_make_key_filename(char *cert_file) |
---|
| 192 | { |
---|
| 193 | char *p=strstr(cert_file,"_ID."); |
---|
| 194 | char *head=strndup(cert_file,p-cert_file); |
---|
| 195 | char *tail=p+4; |
---|
| 196 | char *keyfile=NULL; |
---|
| 197 | asprintf(&keyfile,"%s_private.%s",head,tail); |
---|
| 198 | free(head); |
---|
| 199 | return keyfile; |
---|
| 200 | } |
---|
[8bd77b5] | 201 | /** |
---|
| 202 | * Load a directory full of certs. |
---|
| 203 | */ |
---|
| 204 | void abac_context_load_directory(abac_context_t *ctx, char *path) { |
---|
[d037f54] | 205 | char *glob_pat, *key_pat; |
---|
[8bd77b5] | 206 | glob_t glob_buf; |
---|
| 207 | int i, ret; |
---|
| 208 | |
---|
| 209 | assert(ctx != NULL); assert(path != NULL); |
---|
| 210 | |
---|
| 211 | int dirlen = strlen(path); |
---|
[5110d42] | 212 | |
---|
| 213 | /* make sure pick the larger one */ |
---|
| 214 | glob_pat = abac_xmalloc(dirlen + sizeof(IDKEY_PAT)); |
---|
[8bd77b5] | 215 | memcpy(glob_pat, path, dirlen); |
---|
| 216 | |
---|
| 217 | // first load ID certs |
---|
| 218 | memcpy(glob_pat + dirlen, ID_PAT, sizeof(ID_PAT)); |
---|
| 219 | glob(glob_pat, GLOB_BRACE, NULL, &glob_buf); // TODO check for error |
---|
| 220 | for (i = 0; i < glob_buf.gl_pathc; ++i) { |
---|
[d037f54] | 221 | /* blah_ID.pem */ |
---|
[8bd77b5] | 222 | char *cert_file = glob_buf.gl_pathv[i]; |
---|
[d037f54] | 223 | /* blah_private.pem */ |
---|
| 224 | char *key_file=_make_key_filename(cert_file); |
---|
| 225 | if(debug) { |
---|
| 226 | printf("--> ID cert... %s\n", cert_file); |
---|
| 227 | printf("--> KEY cert... %s\n", key_file); |
---|
| 228 | } |
---|
[5110d42] | 229 | ret = abac_context_load_id_id_file_key_file(ctx, cert_file, key_file); |
---|
[8bd77b5] | 230 | if (ret != ABAC_CERT_SUCCESS) |
---|
| 231 | warnx("Couldn't load ID cert %s", cert_file); |
---|
| 232 | } |
---|
| 233 | globfree(&glob_buf); |
---|
| 234 | |
---|
[5110d42] | 235 | // first load IDKEY certs |
---|
| 236 | memcpy(glob_pat + dirlen, IDKEY_PAT, sizeof(IDKEY_PAT)); |
---|
| 237 | glob(glob_pat, GLOB_BRACE, NULL, &glob_buf); // TODO check for error |
---|
| 238 | for (i = 0; i < glob_buf.gl_pathc; ++i) { |
---|
| 239 | /* blah_IDKEY.pem */ |
---|
| 240 | char *certkey_file = glob_buf.gl_pathv[i]; |
---|
| 241 | if(debug) { |
---|
| 242 | printf("--> IDKEY certkey... %s\n", certkey_file); |
---|
| 243 | } |
---|
| 244 | ret = abac_context_load_id_idkey_file(ctx, certkey_file); |
---|
| 245 | if (ret != ABAC_CERT_SUCCESS) |
---|
| 246 | warnx("Couldn't load ID/KEY IDKEY cert %s", certkey_file); |
---|
| 247 | } |
---|
| 248 | globfree(&glob_buf); |
---|
| 249 | |
---|
[8bd77b5] | 250 | memcpy(glob_pat + dirlen, ATTR_PAT, sizeof(ATTR_PAT)); |
---|
| 251 | glob(glob_pat, 0, NULL, &glob_buf); // TODO check for error |
---|
| 252 | for (i = 0; i < glob_buf.gl_pathc; ++i) { |
---|
| 253 | char *cert_file = glob_buf.gl_pathv[i]; |
---|
| 254 | |
---|
| 255 | if(debug) printf("--> attr file... %s\n", cert_file); |
---|
| 256 | ret = abac_context_load_attribute_file(ctx, cert_file); |
---|
| 257 | if (ret != ABAC_CERT_SUCCESS) |
---|
| 258 | warnx("Couldn't load attribute cert %s", cert_file); |
---|
[5110d42] | 259 | if(debug) printf("Loaded.. %s\n", cert_file); |
---|
[8bd77b5] | 260 | } |
---|
| 261 | globfree(&glob_buf); |
---|
| 262 | |
---|
| 263 | free(glob_pat); |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | /** |
---|
| 267 | * Run a query on the data in an abac context. Returns a NULL-terminated array |
---|
| 268 | * of abac_credential_t. Success/failure in *success. |
---|
| 269 | * queryfor(either role or oset), with(either keyid or object type) |
---|
| 270 | */ |
---|
| 271 | abac_credential_t **abac_context_query(abac_context_t *ctx, char *queryfor, char *with, int *success) { |
---|
| 272 | if(debug) { |
---|
| 273 | printf("abac_context_query about(%s) with(%s)\n", queryfor, with); |
---|
| 274 | } |
---|
| 275 | abac_credential_t **credentials = NULL, *cur; |
---|
| 276 | assert(ctx != NULL); assert(queryfor != NULL); |
---|
| 277 | assert(with != NULL); assert(success != NULL); |
---|
| 278 | |
---|
| 279 | abac_stack_t *result = abac_pl_query(ctx->pl, queryfor, with); |
---|
| 280 | |
---|
| 281 | int size = abac_stack_size(result); |
---|
[d9c3886] | 282 | if (size > 0) { |
---|
| 283 | *success = 1; |
---|
| 284 | } else { |
---|
| 285 | // XXX NOT SURE YET.. |
---|
| 286 | // return partial proof |
---|
| 287 | *success = 0; |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | // make the array (leave space to NULL terminate it) |
---|
| 291 | // n.b., even if the list is empty, we still return an array that |
---|
| 292 | // only contains the NULL terminator |
---|
| 293 | credentials = abac_xmalloc(sizeof(abac_credential_t *) * (size + 1)); |
---|
| 294 | int i = 0; |
---|
| 295 | if(size) { |
---|
| 296 | while(i<size) { |
---|
| 297 | cur=(abac_credential_t *) abac_stack_pop(result); |
---|
| 298 | credentials[i++] = cur; |
---|
| 299 | } |
---|
| 300 | } |
---|
| 301 | credentials[i] = NULL; |
---|
| 302 | |
---|
| 303 | if(result) |
---|
| 304 | abac_stack_free(result); |
---|
| 305 | |
---|
| 306 | return credentials; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | abac_credential_t **abac_context_query_with_structure(abac_context_t *ctx, |
---|
| 310 | abac_aspect_t *queryfor, abac_aspect_t *with, int *success) |
---|
| 311 | { |
---|
| 312 | if(debug) { |
---|
| 313 | printf("abac_context_query_with_structure\n"); |
---|
| 314 | } |
---|
| 315 | abac_credential_t **credentials = NULL, *cur; |
---|
| 316 | assert(ctx != NULL); assert(queryfor != NULL); |
---|
| 317 | assert(with != NULL); assert(success != NULL); |
---|
| 318 | |
---|
| 319 | abac_stack_t *result = abac_pl_query_with_structure(ctx->pl, queryfor, with); |
---|
| 320 | |
---|
| 321 | int size = abac_stack_size(result); |
---|
[8bd77b5] | 322 | if (size > 0) { |
---|
| 323 | *success = 1; |
---|
| 324 | } else { |
---|
| 325 | // XXX NOT SURE YET.. |
---|
| 326 | // return partial proof |
---|
| 327 | *success = 0; |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | // make the array (leave space to NULL terminate it) |
---|
| 331 | // n.b., even if the list is empty, we still return an array that |
---|
| 332 | // only contains the NULL terminator |
---|
| 333 | credentials = abac_xmalloc(sizeof(abac_credential_t *) * (size + 1)); |
---|
| 334 | int i = 0; |
---|
| 335 | if(size) { |
---|
| 336 | while(i<size) { |
---|
| 337 | cur=(abac_credential_t *) abac_stack_pop(result); |
---|
| 338 | credentials[i++] = cur; |
---|
| 339 | } |
---|
| 340 | } |
---|
| 341 | credentials[i] = NULL; |
---|
| 342 | |
---|
| 343 | if(result) |
---|
| 344 | abac_stack_free(result); |
---|
| 345 | |
---|
| 346 | return credentials; |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | /** |
---|
| 350 | * A NULL-terminated array of all the credentials in the context. |
---|
| 351 | */ |
---|
| 352 | abac_credential_t **abac_context_credentials(abac_context_t *ctx) { |
---|
| 353 | abac_credential_t **credentials = NULL, *cur; |
---|
| 354 | assert(ctx != NULL); |
---|
| 355 | |
---|
| 356 | abac_stack_t *result = abac_pl_credentials(ctx->pl); |
---|
| 357 | int size = abac_stack_size(result); |
---|
| 358 | |
---|
| 359 | // make the array (leave space to NULL terminate it) |
---|
| 360 | // n.b., even if the list is empty, we still return an array that |
---|
| 361 | // only contains the NULL terminator |
---|
| 362 | credentials = abac_xmalloc(sizeof(abac_credential_t *) * (size + 1)); |
---|
| 363 | int i = 0; |
---|
| 364 | if(size) { |
---|
| 365 | while(i<size) { |
---|
| 366 | cur=(abac_credential_t *) abac_stack_pop(result); |
---|
| 367 | /* if not in there yet, add into it */ |
---|
| 368 | credentials[i++] = cur; |
---|
| 369 | } |
---|
| 370 | } |
---|
| 371 | credentials[i] = NULL; |
---|
| 372 | |
---|
| 373 | if(result) |
---|
| 374 | abac_stack_free(result); |
---|
| 375 | return credentials; |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | /** |
---|
| 379 | * Frees a NULL-terminated list of credentials. |
---|
| 380 | */ |
---|
| 381 | void abac_context_credentials_free(abac_credential_t **credentials) { |
---|
| 382 | int i; |
---|
| 383 | |
---|
| 384 | if (credentials == NULL) |
---|
| 385 | return; |
---|
| 386 | |
---|
| 387 | for (i = 0; credentials[i] != NULL; ++i) |
---|
| 388 | abac_credential_free(credentials[i]); |
---|
| 389 | free(credentials); |
---|
| 390 | } |
---|
| 391 | |
---|
[5110d42] | 392 | /** |
---|
| 393 | * A NULL-terminated array of all the principals in the context. |
---|
| 394 | */ |
---|
| 395 | abac_id_credential_t **abac_context_principals(abac_context_t *ctx) { |
---|
| 396 | abac_id_credential_t **principals = NULL, *cur; |
---|
| 397 | assert(ctx != NULL); |
---|
| 398 | |
---|
| 399 | abac_stack_t *result = abac_pl_principals(ctx->pl); |
---|
| 400 | |
---|
| 401 | int size = abac_stack_size(result); |
---|
| 402 | |
---|
| 403 | // make the array (leave space to NULL terminate it) |
---|
| 404 | // n.b., even if the list is empty, we still return an array that |
---|
| 405 | // only contains the NULL terminator |
---|
| 406 | principals = abac_xmalloc(sizeof(abac_id_credential_t *) * (size + 1)); |
---|
| 407 | int i = 0; |
---|
| 408 | if(size) { |
---|
| 409 | while(i<size) { |
---|
| 410 | cur=(abac_id_credential_t *) abac_stack_pop(result); |
---|
| 411 | /* if not in there yet, add into it */ |
---|
| 412 | principals[i++] = cur; |
---|
| 413 | } |
---|
| 414 | } |
---|
| 415 | principals[i] = NULL; |
---|
| 416 | |
---|
| 417 | if(result) |
---|
| 418 | abac_stack_free(result); |
---|
| 419 | return principals; |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | /** |
---|
| 423 | * Frees a NULL-terminated list of principals. |
---|
| 424 | */ |
---|
| 425 | void abac_context_principals_free(abac_id_credential_t **principals) { |
---|
| 426 | int i; |
---|
| 427 | |
---|
| 428 | if (principals == NULL) |
---|
| 429 | return; |
---|
| 430 | |
---|
| 431 | for (i = 0; principals[i] != NULL; ++i) |
---|
| 432 | abac_id_credential_free(principals[i]); |
---|
| 433 | free(principals); |
---|
| 434 | } |
---|
| 435 | |
---|