1 | |
---|
2 | /* attribute.c */ |
---|
3 | |
---|
4 | #define _GNU_SOURCE |
---|
5 | #include <stdio.h> |
---|
6 | |
---|
7 | #include <err.h> |
---|
8 | #include <termios.h> |
---|
9 | |
---|
10 | #include "libabac_common.h" |
---|
11 | #include "creddy_common.h" |
---|
12 | |
---|
13 | void attribute_main(options_t *opts) { |
---|
14 | int i, ret= 1; |
---|
15 | |
---|
16 | if ( |
---|
17 | opts->issuer == NULL || |
---|
18 | opts->key == NULL || |
---|
19 | opts->role == NULL || |
---|
20 | opts->out == NULL |
---|
21 | ) |
---|
22 | usage(opts); |
---|
23 | |
---|
24 | // issuer |
---|
25 | abac_id_t *issuer_id = abac_id_from_file(opts->issuer); |
---|
26 | if (issuer_id == NULL) |
---|
27 | errx(1, "Can't load cert from %s", opts->issuer); |
---|
28 | |
---|
29 | // private key |
---|
30 | ret = abac_id_privkey_from_file(issuer_id, opts->key); |
---|
31 | if (ret != ABAC_SUCCESS) |
---|
32 | errx(1, "Can't load private key from %s", opts->key); |
---|
33 | |
---|
34 | abac_attribute_t *attr = NULL; |
---|
35 | ret = abac_attribute_create(&attr, issuer_id, opts->role, opts->validity); |
---|
36 | if (ret == ABAC_ATTRIBUTE_ISSUER_NOKEY) |
---|
37 | abort(); // should never happen |
---|
38 | if (ret == ABAC_ATTRIBUTE_INVALID_ROLE) |
---|
39 | errx(1, "Invalid role name: %s", opts->role); |
---|
40 | if (ret == ABAC_ATTRIBUTE_INVALID_VALIDITY) |
---|
41 | errx(1, "Invalid validity: must be >= 1 second"); |
---|
42 | |
---|
43 | for (i = 0; i < opts->num_subjects; ++i) { |
---|
44 | subject_t *cur = &opts->subjects[i]; |
---|
45 | |
---|
46 | // if we have a cert we need to get its ID |
---|
47 | if (cur->cert) { |
---|
48 | abac_id_t *subject = abac_id_from_file(cur->cert); |
---|
49 | if (subject == NULL) |
---|
50 | errx(1, "Can't load subject cert from %s", cur->cert); |
---|
51 | cur->id = xstrdup(abac_id_keyid(subject)); |
---|
52 | abac_id_free(subject); |
---|
53 | } |
---|
54 | |
---|
55 | // just a principal, add it |
---|
56 | if (!cur->role) { |
---|
57 | abac_attribute_principal(attr, cur->id); |
---|
58 | } |
---|
59 | |
---|
60 | // either role or linking role |
---|
61 | else { |
---|
62 | char *role = cur->role; |
---|
63 | char *start[3]; |
---|
64 | int name_parts = 0, j; |
---|
65 | |
---|
66 | start[name_parts++] = role; |
---|
67 | |
---|
68 | // split the role string up into name parts (turn . into \0) |
---|
69 | for (j = 0; role[j] != '\0'; ++j) |
---|
70 | if (role[j] == '.') { |
---|
71 | if (name_parts == 3) { |
---|
72 | printf("bad subject role name (too many dots)\n"); |
---|
73 | usage(opts); |
---|
74 | } |
---|
75 | start[name_parts++] = &role[j+1]; |
---|
76 | role[j] = 0; |
---|
77 | } |
---|
78 | |
---|
79 | // role |
---|
80 | if (name_parts == 1) { |
---|
81 | abac_attribute_role(attr, cur->id, start[0]); |
---|
82 | } |
---|
83 | // linking role |
---|
84 | else { |
---|
85 | abac_attribute_linking_role(attr, cur->id, start[0], start[1]); |
---|
86 | } |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | ret = abac_attribute_bake(attr); |
---|
91 | if (!ret) |
---|
92 | errx(1, "Couldn't bake attribute cert"); |
---|
93 | |
---|
94 | FILE *out = fopen(opts->out, "w"); |
---|
95 | if (out == NULL) |
---|
96 | err(1, "Couldn't open attr cert file %s for writing", opts->out); |
---|
97 | |
---|
98 | abac_attribute_write(attr, out); |
---|
99 | |
---|
100 | fclose(out); |
---|
101 | |
---|
102 | abac_attribute_free(attr); |
---|
103 | } |
---|