[461541a] | 1 | |
---|
| 2 | /* attribute_rule.c */ |
---|
| 3 | |
---|
| 4 | #define _GNU_SOURCE |
---|
| 5 | #include <stdio.h> |
---|
| 6 | #include <string.h> |
---|
| 7 | |
---|
| 8 | #include <err.h> |
---|
| 9 | #include <termios.h> |
---|
| 10 | |
---|
| 11 | #include "creddy_common.h" |
---|
[4721618] | 12 | #include "libabac_common.h" |
---|
[02036f4] | 13 | #include "abac_util.h" |
---|
[461541a] | 14 | |
---|
| 15 | void attribute_rule_main(options_t *opts) { |
---|
| 16 | int ret; |
---|
| 17 | |
---|
| 18 | if ( |
---|
| 19 | opts->issuer == NULL || |
---|
| 20 | opts->key == NULL || |
---|
| 21 | opts->attrrule == NULL || |
---|
| 22 | opts->out == NULL |
---|
| 23 | ) |
---|
| 24 | usage(opts); |
---|
| 25 | |
---|
| 26 | // issuer |
---|
| 27 | abac_id_t *issuer_id = abac_id_from_file(opts->issuer); |
---|
| 28 | if (issuer_id == NULL) |
---|
| 29 | errx(1, "Can't load cert from %s", opts->issuer); |
---|
| 30 | |
---|
| 31 | // private key |
---|
| 32 | ret = abac_id_privkey_from_file(issuer_id, opts->key); |
---|
| 33 | if (ret != ABAC_SUCCESS) |
---|
| 34 | errx(1, "Can't load private key from %s", opts->key); |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | /* chop out the role, head_string, tail_string */ |
---|
| 38 | char *head_tail[2]; |
---|
| 39 | abac_split(opts->attrrule, "<-", head_tail, &ret); |
---|
| 40 | if (ret != 2) errx(1, "Invalid access rule"); |
---|
| 41 | char *head_string = abac_xstrdup(head_tail[0]); |
---|
| 42 | char *subject_string = abac_xstrdup(head_tail[1]); |
---|
| 43 | |
---|
| 44 | char *head_role[2]; |
---|
| 45 | abac_split(head_tail[0], ".", head_role, &ret); |
---|
| 46 | if (ret != 2) errx(1, "Invalid access rule"); |
---|
| 47 | char *keyid=abac_xstrdup(head_role[0]); |
---|
| 48 | char *role =abac_xstrdup(head_role[1]); |
---|
| 49 | |
---|
| 50 | /* make sure keyid match up with issuer */ |
---|
| 51 | char *issuer=abac_id_keyid(issuer_id); |
---|
| 52 | if(strcmp(keyid,issuer)!=0) { |
---|
| 53 | errx(1, "Mismatched issuer with the access rule"); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | abac_attribute_t *attr = NULL; |
---|
| 57 | ret = abac_attribute_create(&attr, issuer_id, role, opts->validity); |
---|
| 58 | if (ret == ABAC_ATTRIBUTE_ISSUER_NOKEY) |
---|
| 59 | abort(); // should never happen |
---|
| 60 | if (ret == ABAC_ATTRIBUTE_INVALID_ROLE) |
---|
| 61 | errx(1, "Invalid role name: %s", role); |
---|
| 62 | if (ret == ABAC_ATTRIBUTE_INVALID_VALIDITY) |
---|
| 63 | errx(1, "Invalid validity: must be >= 1 second"); |
---|
| 64 | |
---|
| 65 | abac_attribute_set_head(attr,head_string); |
---|
| 66 | abac_attribute_set_tail(attr,subject_string); |
---|
| 67 | |
---|
| 68 | ret = abac_attribute_bake(attr); |
---|
| 69 | if (!ret) |
---|
| 70 | errx(1, "Couldn't bake attribute cert"); |
---|
| 71 | |
---|
| 72 | FILE *out = fopen(opts->out, "w"); |
---|
| 73 | if (out == NULL) |
---|
| 74 | err(1, "Couldn't open attr cert file %s for writing", opts->out); |
---|
| 75 | |
---|
| 76 | abac_attribute_write(attr, out); |
---|
| 77 | |
---|
| 78 | fclose(out); |
---|
| 79 | |
---|
| 80 | abac_attribute_free(attr); |
---|
| 81 | } |
---|