source: creddy/attribute.c @ 8f58012

abac0-leakabac0-meicompt_changesgec13mei-idmei-rt0-nmei_rt0mei_rt2mei_rt2_fix_1meiyap-rt1meiyap1rt2tvf-new-xml
Last change on this file since 8f58012 was 00d21a1, checked in by Mike Ryan <mikeryan@…>, 13 years ago

finer granularity over validity periods

API: ID and attr certs take seconds for validity period
command line: validity option takes a suffix for time period

fixes #20

this is an API-breaking change, so a bump to 0.2.0 is necessary

  • Property mode set to 100644
File size: 3.2 KB
Line 
1#define GNU_SOURCE
2
3#include <err.h>
4#include <termios.h>
5
6#include <credentials/keys/private_key.h>
7
8#include <creddy.h>
9
10#include "creddy_common.h"
11
12void attribute_main(options_t *opts) {
13    int i, ret, role_len = 1;
14
15    if (
16        opts->issuer == NULL ||
17        opts->key == NULL ||
18        opts->role == NULL ||
19        opts->out == NULL
20    )
21        usage(opts);
22
23
24    // issuer
25    creddy_id_t *issuer = creddy_id_from_file(opts->issuer);
26    if (issuer == NULL)
27        errx(1, "Can't load cert from %s", opts->issuer);
28
29    // private key
30    ret = creddy_id_load_privkey(issuer, opts->key);
31    if (!ret)
32        errx(1, "Can't load private key from %s", opts->key);
33
34    creddy_attribute_t *attr = NULL;
35    ret = creddy_attribute_create(&attr, issuer, opts->role, opts->validity);
36    if (ret == CREDDY_ATTRIBUTE_ISSUER_NOKEY)
37        abort(); // should never happen
38    if (ret == CREDDY_ATTRIBUTE_INVALID_ROLE)
39        errx(1, "Invalid role name: %s", opts->role);
40    if (ret == CREDDY_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            creddy_id_t *subject = creddy_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(creddy_id_keyid(subject));
52            creddy_id_free(subject);
53        }
54
55        // just a principal, add it
56        if (!cur->role) {
57            ret = creddy_attribute_principal(attr, cur->id);
58            if (!ret)
59                errx(1, "Invalid principal: %s", cur->id);
60        }
61
62        // either role or linking role
63        else {
64            char *role = cur->role;
65            char *start[3];
66            int name_parts = 0, j;
67
68            start[name_parts++] = role;
69
70            // split the role string up into name parts (turn . into \0)
71            for (j = 0; role[j] != '\0'; ++j)
72                if (role[j] == '.') {
73                    if (name_parts == 3) {
74                        printf("bad subject role name (too many dots)\n");
75                        usage(opts);
76                    }
77                    start[name_parts++] = &role[j+1];
78                    role[j] = 0;
79                }
80
81            // role
82            if (name_parts == 1) {
83                ret = creddy_attribute_role(attr, cur->id, start[0]);
84                if (!ret)
85                    errx(1, "Invalid role: %s.%s", cur->id, start[0]);
86            }
87
88            // linking role
89            else {
90                ret = creddy_attribute_linking_role(attr, cur->id, start[0], start[1]);
91                if (!ret)
92                    errx(1, "Invalid linking role: %s.%s.%s", cur->id, start[0], start[1]);
93            }
94        }
95    }
96
97    ret = creddy_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    creddy_attribute_write(attr, out);
106
107    fclose(out);
108
109    creddy_attribute_free(attr);
110}
Note: See TracBrowser for help on using the repository browser.