source: libabac/abac.hh @ 923b4dd

mei_rt2mei_rt2_fix_1meiyap-rt1rt2
Last change on this file since 923b4dd was 923b4dd, checked in by Ted Faber <faber@…>, 12 years ago

Changes to support swig

  • Property mode set to 100644
File size: 5.1 KB
Line 
1#ifndef __ABAC_HH__
2#define __ABAC_HH__
3
4#include <string>
5#include <vector>
6
7namespace ABAC {
8    extern "C" {
9        #include "abac.h"
10    }
11
12    class Role {
13        public:
14            Role() : m_role(NULL), m_oset(NULL) { } // do not use: here for swig
15            Role(abac_role_t *role): m_role(abac_role_dup(role)), m_oset(NULL)
16                { }
17            Role(abac_oset_t *oset): m_role(NULL), m_oset(abac_oset_dup(oset)) 
18                { }
19            Role(char *role_name) : m_role(abac_role_from_string(role_name)), 
20                m_oset(NULL) { }
21            Role(const Role &role) { 
22                if ( role.m_role ) {
23                    m_role = abac_role_dup(role.m_role);
24                    m_oset = NULL;
25                }
26                else {
27                    m_role =  NULL;
28                    m_oset =abac_oset_dup(role.m_oset);
29                }
30            }
31            ~Role() { 
32                if ( m_role) abac_role_free(m_role);
33                if ( m_oset) abac_oset_free(m_oset);
34            }
35
36            bool is_principal() const { 
37                if (m_role) return abac_role_is_principal(m_role); 
38                else return abac_oset_is_principal(m_oset); 
39            }
40            bool is_role() const { 
41                if (m_role) return abac_role_is_role(m_role); 
42                else return abac_oset_is_oset(m_oset); 
43            }
44            bool is_linking() const { 
45                if (m_role) return abac_role_is_linking(m_role); 
46                else return abac_oset_is_linking(m_oset); 
47            }
48            bool is_oset() const { return m_oset; }
49
50            char *string() const { 
51                if ( m_role) return abac_role_string(m_role);
52                else return abac_oset_string(m_oset);
53            }
54            char *linked_role() const { 
55                if (m_role) return abac_role_linked_role(m_role);
56                else return abac_oset_linked_role(m_oset);
57            }
58            char *role_name() const { 
59                if (m_role) return abac_role_role_name(m_role);
60                else return abac_oset_oset_name(m_oset);
61            }
62            char *principal() const { 
63                if (m_role) return abac_role_principal(m_role);
64                else return abac_oset_principal(m_oset);
65            }
66
67        private:
68            abac_role_t *m_role;
69            abac_oset_t *m_oset;
70    };
71
72    class Credential {
73        public:
74            Credential() : m_cred(NULL) { } // do not use: here for swig
75            Credential(abac_credential_t *cred) :
76                m_head(), m_tail(), m_cred(abac_credential_dup(cred)) { 
77                    if ( abac_credential_holds_roles(cred)) {
78                        m_head = Role(abac_credential_head(cred));
79                        m_tail = Role(abac_credential_tail(cred));
80                    }
81                    else {
82                        m_head = Role(abac_credential_head_oset(cred));
83                        m_tail = Role(abac_credential_tail_oset(cred));
84                    }
85            }
86            Credential(const Credential &cred) :
87                m_head(cred.m_head),
88                m_tail(cred.m_tail),
89                m_cred(abac_credential_dup(cred.m_cred))
90                    { }
91            ~Credential() { abac_credential_free(m_cred); }
92
93            const Role &head() { return m_head; }
94            const Role &tail() { return m_tail; }
95            abac_chunk_t attribute_cert() { return abac_credential_attribute_cert(m_cred); }
96            abac_chunk_t issuer_cert() { return abac_credential_issuer_cert(m_cred); }
97       
98        private:
99            abac_credential_t *m_cred;
100            Role m_head, m_tail;
101    };
102
103    class Context {
104        public:
105            Context() { m_ctx = abac_context_new(); }
106            Context(const Context &context) { m_ctx = abac_context_dup(context.m_ctx); }
107            ~Context() { abac_context_free(m_ctx); }
108
109            /* see abac.h for possible return values */
110            int load_id_file(char *filename) { return abac_context_load_id_file(m_ctx, filename); }
111            int load_id_chunk(abac_chunk_t cert) { return abac_context_load_id_chunk(m_ctx, cert); }
112            int load_attribute_file(char *filename) { return abac_context_load_attribute_file(m_ctx, filename); }
113            int load_attribute_chunk(abac_chunk_t cert) { return abac_context_load_attribute_chunk(m_ctx, cert); }
114
115            /* load an entire directory full of certs */
116            void load_directory(char *path) { abac_context_load_directory(m_ctx, path); }
117
118            /* abac query, returns a vector of credentials on success, NULL on fail */
119            std::vector<Credential> query(char *role, char *principal, bool &success) {
120                abac_credential_t **creds, **end;
121                int i, success_int;
122
123                creds = abac_context_query(m_ctx, role, principal, &success_int);
124                success = success_int;
125
126                for (i = 0; creds[i] != NULL; ++i)
127                    ;
128
129                end = &creds[i];
130                std::vector<Credential> credentials = std::vector<Credential>(creds, end);
131
132                abac_context_credentials_free(creds);
133
134                return credentials;
135            }
136
137            std::vector<Credential> credentials() {
138                abac_credential_t **creds, **end;
139                int i;
140
141                creds = abac_context_credentials(m_ctx);
142                for (i = 0; creds[i] != NULL; ++i)
143                    ;
144
145                end = &creds[i];
146                std::vector<Credential> credentials = std::vector<Credential>(creds, end);
147
148                abac_context_credentials_free(creds);
149                return credentials;
150            }
151
152        private:
153            abac_context_t *m_ctx;
154    };
155}
156
157#endif /* __ABAC_HH__ */
Note: See TracBrowser for help on using the repository browser.