source: creddy/attribute.c @ 4721618

abac0-leakabac0-meimei-idmei-rt0-nmei_rt0tvf-new-xml
Last change on this file since 4721618 was 4721618, checked in by Mei <mei@…>, 11 years ago

1) tested out python and perl test scripts along with

abac_chunk_t calls in libabac's abac.hh

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