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