source: creddy/attribute.c @ b8a6c918

abac0-leak
Last change on this file since b8a6c918 was ced246e, checked in by Ted Faber <faber@…>, 11 years ago

Use our API in side these operations

  • Property mode set to 100644
File size: 2.9 KB
Line 
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
13void 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    char *head_string=NULL;
44    char *subject_string=NULL;
45
46    for (i = 0; i < opts->num_subjects; ++i) {
47        char *string=NULL;
48        subject_t *cur = &opts->subjects[i];
49
50        // if we have a cert we need to get its ID
51        if (cur->cert) {
52            abac_id_t *subject = abac_id_from_file(cur->cert);
53            if (subject == NULL)
54                errx(1, "Can't load subject cert from %s", cur->cert);
55            cur->id = xstrdup(abac_id_keyid(subject));
56            abac_id_free(subject);
57        }
58
59        // just a principal, add it
60        if (!cur->role) {
61            abac_attribute_principal(attr, cur->id);
62        }
63
64        // either role or linking role
65        else {
66            char *role = cur->role;
67            char *start[3];
68            int name_parts = 0, j;
69
70            start[name_parts++] = role;
71
72            // split the role string up into name parts (turn . into \0)
73            for (j = 0; role[j] != '\0'; ++j)
74                if (role[j] == '.') {
75                    if (name_parts == 3) {
76                        printf("bad subject role name (too many dots)\n");
77                        usage(opts);
78                    }
79                    start[name_parts++] = &role[j+1];
80                    role[j] = 0;
81                }
82
83            // role
84            if (name_parts == 1) {
85                abac_attribute_role(attr, cur->id, start[0]);
86            }
87            // linking role
88            else {
89                abac_attribute_linking_role(attr, cur->id, start[0], start[1]);
90            }
91        }
92    }
93
94    ret = abac_attribute_bake(attr);
95    if (!ret)
96        errx(1, "Couldn't bake attribute cert");
97
98    FILE *out = fopen(opts->out, "w");
99    if (out == NULL)
100        err(1, "Couldn't open attr cert file %s for writing", opts->out);
101
102    abac_attribute_write(attr, out);
103
104    fclose(out);
105
106    abac_attribute_free(attr);
107}
Note: See TracBrowser for help on using the repository browser.