source: creddy/attribute_rule.c @ ced246e

abac0-leakabac0-meitvf-new-xml
Last change on this file since ced246e 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_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
15void 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[3]);
40                break;
41        }
42    }
43}
44
45
46
47void 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    char *head_string = abac_xstrdup(head_tail[0]);
74    char *subject_string = abac_xstrdup(head_tail[1]);
75
76    char *head_role[2];
77    abac_split(head_tail[0], ".", head_role, &ret);
78    if (ret != 2) errx(1, "Invalid access rule");
79    char *keyid=abac_xstrdup(head_role[0]);
80    char *role =abac_xstrdup(head_role[1]);
81
82    /* make sure keyid match up with issuer */
83    char *issuer=abac_id_keyid(issuer_id);
84    if(strcmp(keyid,issuer)!=0) {
85        errx(1, "Mismatched issuer with the access rule");
86    }
87
88    abac_attribute_t *attr = NULL;
89    ret = abac_attribute_create(&attr, issuer_id, role, opts->validity);
90    if (ret == ABAC_ATTRIBUTE_ISSUER_NOKEY)
91        abort(); // should never happen
92    if (ret == ABAC_ATTRIBUTE_INVALID_ROLE)
93        errx(1, "Invalid role name: %s", role);
94    if (ret == ABAC_ATTRIBUTE_INVALID_VALIDITY)
95        errx(1, "Invalid validity: must be >= 1 second");
96
97    add_tail(attr, head_tail[1]);
98
99    ret = abac_attribute_bake(attr);
100    if (!ret)
101        errx(1, "Couldn't bake attribute cert");
102
103    FILE *out = fopen(opts->out, "w");
104    if (out == NULL)
105        err(1, "Couldn't open attr cert file %s for writing", opts->out);
106
107    abac_attribute_write(attr, out);
108
109    fclose(out);
110
111    abac_attribute_free(attr);
112}
Note: See TracBrowser for help on using the repository browser.