source: libabac/abac.hh @ 94605f2

abac0-leakabac0-meitvf-new-xml
Last change on this file since 94605f2 was 94605f2, checked in by Ted Faber <faber@…>, 11 years ago

checkpoint Lots of the way toward mnemonic names, some parsing fixes

  • Property mode set to 100644
File size: 11.2 KB
Line 
1#ifndef __ABAC_HH__
2#define __ABAC_HH__
3
4#include <cstdio>
5#include <stdexcept>
6#include <string>
7#include <vector>
8
9/* This file is generated from doc/ABAC.hh by doc/extract_doc.c */
10
11namespace ABAC {
12    extern "C" {
13        #include "abac.h"
14    }
15
16    class Attribute;
17    class ID;
18    class Role;
19    class Credential;
20
21    class Context {
22        public:
23            Context() { m_ctx = abac_context_new(); }
24            Context(const Context &context) { m_ctx = abac_context_dup(context.m_ctx); }
25            ~Context() { abac_context_free(m_ctx); }
26
27            int load_id_file(char *filename) { return abac_context_load_id_file(m_ctx, filename); }
28            int load_id_chunk(abac_chunk_t cert) { return abac_context_load_id_chunk(m_ctx, cert); }
29            int load_attribute_file(char *filename) { return abac_context_load_attribute_file(m_ctx, filename); }
30            int load_attribute_chunk(abac_chunk_t cert) { return abac_context_load_attribute_chunk(m_ctx, cert); }
31
32            void load_directory(char *path) { abac_context_load_directory(m_ctx, path); }
33
34
35            /* abac query, returns a vector of credentials on success, NULL on fail */
36            std::vector<Credential> query(char *role, char *principal, bool &success) {
37                abac_credential_t **creds, **end;
38                int i, success_int;
39                creds = abac_context_query(m_ctx, role, principal, &success_int);
40                success = success_int;
41
42                for (i = 0; creds[i] != NULL; ++i)
43                    ;
44
45                end = &creds[i];
46                std::vector<Credential> credentials = std::vector<Credential>(creds, end);
47                abac_context_credentials_free(creds);
48                return credentials;
49            }
50            std::vector<Credential> credentials() {
51                abac_credential_t **creds, **end;
52                int i;
53
54                creds = abac_context_credentials(m_ctx);
55                for (i = 0; creds[i] != NULL; ++i)
56                    ;
57
58                end = &creds[i];
59                std::vector<Credential> credentials = std::vector<Credential>(creds, end);
60
61                abac_context_credentials_free(creds);
62                return credentials;
63            }
64        private:
65            abac_context_t *m_ctx;
66        friend class Role;
67    };
68
69    class Role {
70        public:
71            Role() : m_role(NULL) { } // do not use: here for swig
72            Role(abac_role_t *role) { m_role = abac_role_dup(role); }
73            Role(char *role_name) { m_role = abac_role_from_string(role_name); }
74            Role(const Role &role) { m_role = abac_role_dup(role.m_role); }
75            ~Role() { abac_role_free(m_role); }
76            bool is_principal() const { return abac_role_is_principal(m_role); }
77            bool is_role() const { return abac_role_is_role(m_role); }
78            bool is_linking() const { return abac_role_is_linking(m_role); }
79
80            char *string() const { return abac_role_string(m_role); }
81            char *short_string(Context& c) const { 
82                return abac_role_short_string(m_role, c.m_ctx);
83            }
84            char *linked_role() const { return abac_role_linked_role(m_role); }
85            char *role_name() const { return abac_role_role_name(m_role); }
86            char *principal() const { return abac_role_principal(m_role); }
87
88        private:
89            abac_role_t *m_role;
90    };
91
92    class Credential {
93        public:
94            Credential() : m_cred(NULL) { } // do not use: here for swig
95            Credential(abac_credential_t *cred) :
96                m_head(abac_credential_head(cred)),
97                m_tail(abac_credential_tail(cred)),
98                m_cred(abac_credential_dup(cred))
99                    { }
100            Credential(const Credential &cred) :
101                m_head(cred.m_head),
102                m_tail(cred.m_tail),
103                m_cred(abac_credential_dup(cred.m_cred))
104                    { }
105            ~Credential() { abac_credential_free(m_cred); }
106            const Role &head() { return m_head; }
107            const Role &tail() { return m_tail; }
108            abac_chunk_t attribute_cert() { return abac_credential_attribute_cert(m_cred); }
109            abac_chunk_t issuer_cert() { return abac_credential_issuer_cert(m_cred); }
110       
111        private:
112            abac_credential_t *m_cred;
113            Role m_head, m_tail;
114    };
115
116
117    class ID {
118        public:
119            ID() : m_id(NULL) { } // do not use: required by swig
120            ID(const ID &id) { m_id = abac_id_dup(id.m_id); }
121            ~ID() { abac_id_free(m_id); }
122
123            ID(char *filename) : m_id(NULL) {
124                m_id = abac_id_from_file(filename);
125                if (m_id == NULL)
126                    throw std::invalid_argument("Could not load ID cert");
127            }
128
129            ID(abac_chunk_t chunk) : m_id(NULL) {
130                m_id = abac_id_from_chunk(chunk);
131                if (m_id == NULL)
132                    throw std::invalid_argument("Could not load ID certificate with a chunk");
133            }
134            ID(char *cn, int validity) : m_id(NULL) {
135                int ret = abac_id_generate(&m_id, cn, validity);
136                if (ret == ABAC_GENERATE_INVALID_CN)
137                    throw std::invalid_argument("CN must be alphanumeric and start with a letter");
138                if (ret == ABAC_GENERATE_INVALID_VALIDITY)
139                    throw std::invalid_argument("Validity must be > 0 days");
140            }
141            void load_privkey(char *filename) {
142                int ret = abac_id_privkey_from_file(m_id, filename);
143                if (ret != ABAC_SUCCESS)
144                    throw std::invalid_argument("Could not load private key");
145            }
146            void load_privkey_chunk(abac_chunk_t chunk) {
147                int ret = abac_id_privkey_from_chunk(m_id, chunk);
148                if (ret != ABAC_SUCCESS)
149                    throw std::invalid_argument("Could not load private key with a chunk");
150            }
151            int has_privkey() {
152                int ret= abac_id_has_privkey(m_id);
153                return ret;
154            }
155            char *keyid() { return abac_id_keyid(m_id); }
156            char *cert_filename() { return abac_id_cert_filename(m_id); }
157            void write_cert(FILE *out) { abac_id_write_cert(m_id, out); }
158            void write_cert(const std::string &name) {
159                FILE *out = fopen(name.c_str(), "a+");
160                if (out == NULL)
161                    throw std::invalid_argument("Could not open certificate file for writing");
162                write_cert(out);
163                fclose(out);
164            }
165            // Simplifies access from swig
166            void write_cert_file(const char *n) {
167                write_cert(std::string(n));
168            }
169            void write_cert_name(const char *n) {
170                write_cert(std::string(n));
171                fprintf(stderr,"ABAC::ID::write_cert_name is deprecated, please use ABAC::ID::write_cert_name\n");
172            }
173            char *privkey_filename() { return abac_id_privkey_filename(m_id); }
174            void write_privkey(FILE *out) {
175                int ret = abac_id_write_privkey(m_id, out);
176                if (ret!=ABAC_SUCCESS) throw std::logic_error("No private key loaded");
177            }
178            void write_privkey(const std::string &name) {
179                FILE *out = fopen(name.c_str(), "a+");
180                if (out == NULL)
181                    throw std::invalid_argument("Could not open privkey file for writing");
182                write_privkey(out);
183                fclose(out);
184            }
185            // Simplifies access from swig
186            void write_privkey_file(const char *name) {
187                write_privkey(std::string(name));
188            }
189            void write_privkey_name(const char *name) {
190                write_privkey(std::string(name));
191                fprintf(stderr,"ABAC::ID::write_privkey_name is deprecated, please use ABAC::ID::write_privkey_file\n");
192            }
193            abac_chunk_t cert_chunk() { return abac_id_cert_chunk(m_id); }
194            abac_chunk_t privkey_chunk() { return abac_id_privkey_chunk(m_id); }
195
196            friend class Attribute;
197
198        private:
199            abac_id_t *m_id;
200    };
201
202    class Attribute {
203        public:
204            Attribute() : m_attr(NULL) { } // do not use: required by swig
205            ~Attribute() { abac_attribute_free(m_attr); }
206
207            Attribute(ID &issuer, char *role, int validity) : m_attr(NULL) {
208                int ret = abac_attribute_create(&m_attr, issuer.m_id, role, validity);
209                if (ret == ABAC_ATTRIBUTE_ISSUER_NOKEY)
210                    throw std::invalid_argument("Issuer has no private key");
211                if (ret == ABAC_ATTRIBUTE_INVALID_ROLE)
212                    throw std::invalid_argument("Role name must be alphanumeric");
213                if (ret == ABAC_ATTRIBUTE_INVALID_VALIDITY)
214                    throw std::invalid_argument("Validity must be > 0 days");
215                if (ret == ABAC_ATTRIBUTE_INVALID_ISSUER)
216                    throw std::invalid_argument("Issuer's validity expired");
217            }
218
219
220            bool principal(char *keyid) {
221                if (baked()) throw std::logic_error("Cert is already baked");
222                return abac_attribute_principal(m_attr, keyid);
223            }
224            bool role(char *keyid, char *role) {
225                if (baked()) throw std::logic_error("Cert is already baked");
226                return abac_attribute_role(m_attr, keyid, role);
227            }
228            bool linking_role(char *keyid, char *role, char *linked) {
229                if (baked()) throw std::logic_error("Cert is already baked");
230                return abac_attribute_linking_role(m_attr, keyid, role, linked);
231            }
232            bool bake() {
233                if (baked()) throw std::logic_error("Cert is already baked");
234                return abac_attribute_bake(m_attr);
235            }
236
237            bool baked() { return abac_attribute_baked(m_attr); }
238
239            void set_output_format(char *fmt) {
240                abac_attribute_set_output_format(m_attr, fmt);
241            }
242
243            void write(FILE *out) {
244                int ret = abac_attribute_write(m_attr, out);
245                if (ret!=ABAC_SUCCESS) throw std::logic_error("Cert is not baked");
246            }
247            void write(const std::string &name) {
248                FILE *out = fopen(name.c_str(), "w");
249                if (out == NULL)
250                    throw std::invalid_argument("Could not open certificate file for writing");
251                write(out);
252                fclose(out);
253            }
254            void write_file(const char *name) {
255                int ret = abac_attribute_write_file(m_attr, name);
256                if (ret!=ABAC_SUCCESS) throw std::logic_error("Cert is not baked");
257            }
258            void write_name(const char *name) {
259                write_file(name);
260                fprintf(stderr,"ABAC::Attribute::write_name is deprecated, please use ABAC::Attribute::write_name\n");
261            }
262            abac_chunk_t cert_chunk() {
263                abac_chunk_t ret=abac_attribute_cert_chunk(m_attr);
264                if(ret.len == 0)
265                    throw std::logic_error("Cert is not baked");
266                return ret;
267            }
268
269        private:
270            abac_attribute_t *m_attr;
271    };
272}
273
274#endif /* __ABAC_HH__ */
Note: See TracBrowser for help on using the repository browser.