source: creddy/attribute_rule.c @ 9e063cb

abac0-leakabac0-meimei-idmei-rt0-nmei_rt0tvf-new-xml
Last change on this file since 9e063cb 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: 2.2 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
14void attribute_rule_main(options_t *opts) {
15    int ret;
16
17    if (
18        opts->issuer == NULL ||
19        opts->key == NULL ||
20        opts->attrrule == NULL ||
21        opts->out == NULL
22    )
23        usage(opts);
24
25    // issuer
26    abac_id_t *issuer_id = abac_id_from_file(opts->issuer);
27    if (issuer_id == NULL)
28        errx(1, "Can't load cert from %s", opts->issuer);
29
30    // private key
31    ret = abac_id_privkey_from_file(issuer_id, opts->key);
32    if (ret != ABAC_SUCCESS)
33        errx(1, "Can't load private key from %s", opts->key);
34
35
36    /* chop out the role, head_string, tail_string */
37    char *head_tail[2];
38    abac_split(opts->attrrule, "<-", head_tail, &ret);
39    if (ret != 2) errx(1, "Invalid access rule");
40    char *head_string = abac_xstrdup(head_tail[0]);
41    char *subject_string = abac_xstrdup(head_tail[1]);
42
43    char *head_role[2];
44    abac_split(head_tail[0], ".", head_role, &ret);
45    if (ret != 2) errx(1, "Invalid access rule");
46    char *keyid=abac_xstrdup(head_role[0]);
47    char *role =abac_xstrdup(head_role[1]);
48
49    /* make sure keyid match up with issuer */
50    char *issuer=abac_id_keyid(issuer_id);
51    if(strcmp(keyid,issuer)!=0) {
52        errx(1, "Mismatched issuer with the access rule");
53    }
54
55    abac_attribute_t *attr = NULL;
56    ret = abac_attribute_create(&attr, issuer_id, role, opts->validity);
57    if (ret == ABAC_ATTRIBUTE_ISSUER_NOKEY)
58        abort(); // should never happen
59    if (ret == ABAC_ATTRIBUTE_INVALID_ROLE)
60        errx(1, "Invalid role name: %s", role);
61    if (ret == ABAC_ATTRIBUTE_INVALID_VALIDITY)
62        errx(1, "Invalid validity: must be >= 1 second");
63
64    abac_attribute_set_head(attr,head_string);
65    abac_attribute_set_tail(attr,subject_string);
66
67    ret = abac_attribute_bake(attr);
68    if (!ret)
69        errx(1, "Couldn't bake attribute cert");
70
71    FILE *out = fopen(opts->out, "w");
72    if (out == NULL)
73        err(1, "Couldn't open attr cert file %s for writing", opts->out);
74
75    abac_attribute_write(attr, out);
76
77    fclose(out);
78
79    abac_attribute_free(attr);
80}
Note: See TracBrowser for help on using the repository browser.