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" |
---|
12 | #include "libabac_common.h" |
---|
13 | #include "abac_util.h" |
---|
14 | |
---|
15 | void add_tail(abac_attribute_t *a, char *t) { |
---|
16 | char *roles[256]; /* The roles in t, separated by ' & ' */ |
---|
17 | int nroles = 256; /* Number of slots in roles and then number found by |
---|
18 | abac_split */ |
---|
19 | int i; /* Scratch */ |
---|
20 | |
---|
21 | abac_split(t, " & ", roles, &nroles); |
---|
22 | |
---|
23 | for ( i = 0; i < nroles; i++ ) { |
---|
24 | char *terms[3]; /* The terms in roles[i] */ |
---|
25 | int nterms = 3; /* Number of slots and then number used in terms */ |
---|
26 | |
---|
27 | abac_split(roles[i], ".", terms, &nterms); |
---|
28 | switch (nterms) { |
---|
29 | default: |
---|
30 | err(1, "Cannot parse term!!?"); |
---|
31 | break; |
---|
32 | case 1: |
---|
33 | abac_attribute_principal(a, terms[0]); |
---|
34 | break; |
---|
35 | case 2: |
---|
36 | abac_attribute_role(a, terms[0], terms[1]); |
---|
37 | break; |
---|
38 | case 3: |
---|
39 | abac_attribute_linking_role(a, terms[0], terms[1], terms[2]); |
---|
40 | break; |
---|
41 | } |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | |
---|
46 | |
---|
47 | void attribute_rule_main(options_t *opts) { |
---|
48 | int ret; |
---|
49 | |
---|
50 | if ( |
---|
51 | opts->issuer == NULL || |
---|
52 | opts->key == NULL || |
---|
53 | opts->attrrule == NULL || |
---|
54 | opts->out == NULL |
---|
55 | ) |
---|
56 | usage(opts); |
---|
57 | |
---|
58 | // issuer |
---|
59 | abac_id_t *issuer_id = abac_id_from_file(opts->issuer); |
---|
60 | if (issuer_id == NULL) |
---|
61 | errx(1, "Can't load cert from %s", opts->issuer); |
---|
62 | |
---|
63 | // private key |
---|
64 | ret = abac_id_privkey_from_file(issuer_id, opts->key); |
---|
65 | if (ret != ABAC_SUCCESS) |
---|
66 | errx(1, "Can't load private key from %s", opts->key); |
---|
67 | |
---|
68 | |
---|
69 | /* chop out the role, head_string, tail_string */ |
---|
70 | char *head_tail[2]; |
---|
71 | abac_split(opts->attrrule, "<-", head_tail, &ret); |
---|
72 | if (ret != 2) errx(1, "Invalid access rule"); |
---|
73 | |
---|
74 | char *head_role[2]; |
---|
75 | abac_split(head_tail[0], ".", head_role, &ret); |
---|
76 | if (ret != 2) errx(1, "Invalid access rule"); |
---|
77 | char *keyid=abac_xstrdup(head_role[0]); |
---|
78 | char *role =abac_xstrdup(head_role[1]); |
---|
79 | |
---|
80 | /* make sure keyid match up with issuer */ |
---|
81 | char *issuer=abac_id_keyid(issuer_id); |
---|
82 | if(strcmp(keyid,issuer)!=0) { |
---|
83 | errx(1, "Mismatched issuer with the access rule"); |
---|
84 | } |
---|
85 | |
---|
86 | abac_attribute_t *attr = NULL; |
---|
87 | ret = abac_attribute_create(&attr, issuer_id, role, opts->validity); |
---|
88 | if (ret == ABAC_ATTRIBUTE_ISSUER_NOKEY) |
---|
89 | abort(); // should never happen |
---|
90 | if (ret == ABAC_ATTRIBUTE_INVALID_ROLE) |
---|
91 | errx(1, "Invalid role name: %s", role); |
---|
92 | if (ret == ABAC_ATTRIBUTE_INVALID_VALIDITY) |
---|
93 | errx(1, "Invalid validity: must be >= 1 second"); |
---|
94 | |
---|
95 | add_tail(attr, head_tail[1]); |
---|
96 | |
---|
97 | ret = abac_attribute_bake(attr); |
---|
98 | if (!ret) |
---|
99 | errx(1, "Couldn't bake attribute cert"); |
---|
100 | |
---|
101 | FILE *out = fopen(opts->out, "w"); |
---|
102 | if (out == NULL) |
---|
103 | err(1, "Couldn't open attr cert file %s for writing", opts->out); |
---|
104 | |
---|
105 | abac_attribute_write(attr, out); |
---|
106 | |
---|
107 | fclose(out); |
---|
108 | |
---|
109 | abac_attribute_free(attr); |
---|
110 | } |
---|