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
RevLine 
[085f159]1
[461541a]2/* attribute.c */
[01044ac]3
[461541a]4#define _GNU_SOURCE
5#include <stdio.h>
[aa33ad9]6
[461541a]7#include <err.h>
8#include <termios.h>
[04f5da1]9
[4721618]10#include "libabac_common.h"
[04f5da1]11#include "creddy_common.h"
[01044ac]12
[085f159]13void attribute_main(options_t *opts) {
[461541a]14    int i, ret= 1;
[ee5afdd]15
[085f159]16    if (
17        opts->issuer == NULL ||
18        opts->key == NULL ||
19        opts->role == NULL ||
20        opts->out == NULL
21    )
22        usage(opts);
23
[aa33ad9]24    // issuer
[461541a]25    abac_id_t *issuer_id = abac_id_from_file(opts->issuer);
26    if (issuer_id == NULL)
[aa33ad9]27        errx(1, "Can't load cert from %s", opts->issuer);
[980a7b6]28
[aa33ad9]29    // private key
[461541a]30    ret = abac_id_privkey_from_file(issuer_id, opts->key);
31    if (ret != ABAC_SUCCESS)
[aa33ad9]32        errx(1, "Can't load private key from %s", opts->key);
[980a7b6]33
[461541a]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)
[aa33ad9]37        abort(); // should never happen
[461541a]38    if (ret == ABAC_ATTRIBUTE_INVALID_ROLE)
[aa33ad9]39        errx(1, "Invalid role name: %s", opts->role);
[461541a]40    if (ret == ABAC_ATTRIBUTE_INVALID_VALIDITY)
[00d21a1]41        errx(1, "Invalid validity: must be >= 1 second");
[ee5afdd]42
[461541a]43    char *head_string=NULL;
44    char *subject_string=NULL;
45
[aa33ad9]46    for (i = 0; i < opts->num_subjects; ++i) {
[461541a]47        char *string=NULL;
[aa33ad9]48        subject_t *cur = &opts->subjects[i];
[ee5afdd]49
[aa33ad9]50        // if we have a cert we need to get its ID
51        if (cur->cert) {
[461541a]52            abac_id_t *subject = abac_id_from_file(cur->cert);
[002b25a]53            if (subject == NULL)
54                errx(1, "Can't load subject cert from %s", cur->cert);
[461541a]55            cur->id = xstrdup(abac_id_keyid(subject));
56            abac_id_free(subject);
[ee5afdd]57        }
58
[aa33ad9]59        // just a principal, add it
60        if (!cur->role) {
[ced246e]61            abac_attribute_principal(attr, cur->id);
[aa33ad9]62        }
[085f159]63
[aa33ad9]64        // either role or linking role
65        else {
[980a7b6]66            char *role = cur->role;
67            char *start[3];
68            int name_parts = 0, j;
[085f159]69
[980a7b6]70            start[name_parts++] = role;
[085f159]71
[aa33ad9]72            // split the role string up into name parts (turn . into \0)
[980a7b6]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
[aa33ad9]83            // role
84            if (name_parts == 1) {
[ced246e]85                abac_attribute_role(attr, cur->id, start[0]);
[aa33ad9]86            }
87            // linking role
88            else {
[ced246e]89                abac_attribute_linking_role(attr, cur->id, start[0], start[1]);
[aa33ad9]90            }
[980a7b6]91        }
[085f159]92    }
93
[461541a]94    ret = abac_attribute_bake(attr);
[002b25a]95    if (!ret)
[aa33ad9]96        errx(1, "Couldn't bake attribute cert");
[085f159]97
98    FILE *out = fopen(opts->out, "w");
99    if (out == NULL)
[aa33ad9]100        err(1, "Couldn't open attr cert file %s for writing", opts->out);
101
[461541a]102    abac_attribute_write(attr, out);
[aa33ad9]103
[085f159]104    fclose(out);
105
[461541a]106    abac_attribute_free(attr);
[085f159]107}
Note: See TracBrowser for help on using the repository browser.