maek noncopyable classes
[mech_eap.git] / util_attr.h
1 /*
2  * Copyright (c) 2010, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #ifndef _UTIL_ATTR_H_
34 #define _UTIL_ATTR_H_ 1
35
36 #define ATTR_TYPE_RADIUS            0U
37 #define ATTR_TYPE_SAML_ASSERTION    1U
38 #define ATTR_TYPE_SAML              2U
39 #define ATTR_TYPE_LOCAL             3U
40 #define ATTR_TYPE_MIN               ATTR_TYPE_RADIUS
41 #define ATTR_TYPE_MAX               (ATTR_TYPE_LOCAL + 1U)
42
43 #ifdef __cplusplus
44 #include <string>
45
46 struct gss_eap_attr_ctx;
47
48 struct gss_eap_attr_source
49 {
50 public:
51     gss_eap_attr_source(void) {}
52     virtual ~gss_eap_attr_source(void) {}
53
54     bool initWithManager(const gss_eap_attr_ctx *manager)
55     {
56         m_manager = manager;
57         return true;
58     }
59
60     virtual bool initFromExistingContext(const gss_eap_attr_ctx *manager,
61                                          const gss_eap_attr_source *ctx)
62     {
63         return initWithManager(manager);
64     }
65
66     virtual bool initFromGssContext(const gss_eap_attr_ctx *manager,
67                                     const gss_cred_id_t cred,
68                                     const gss_ctx_id_t ctx)
69     {
70         return initWithManager(manager);
71     }
72
73     typedef bool
74     gss_eap_attr_enumeration_cb(const gss_eap_attr_source *source,
75                                 const gss_buffer_t attribute,
76                                 void *data);
77
78     virtual bool getAttributeTypes(gss_eap_attr_enumeration_cb, void *data) const
79     {
80         return false;
81     }
82
83     virtual void setAttribute(int complete,
84                               const gss_buffer_t attr,
85                               const gss_buffer_t value) {}
86     virtual void deleteAttribute(const gss_buffer_t value) {}
87     virtual bool getAttribute(const gss_buffer_t attr,
88                               int *authenticated,
89                               int *complete,
90                               gss_buffer_t value,
91                               gss_buffer_t display_value,
92                               int *more) const { return false; }
93
94     virtual gss_any_t mapToAny(int authenticated,
95                                gss_buffer_t type_id) const { return NULL; }
96     virtual void releaseAnyNameMapping(gss_buffer_t type_id,
97                                        gss_any_t input) const {}
98
99     virtual void exportToBuffer(gss_buffer_t buffer) const {}
100     virtual bool initFromBuffer(const gss_eap_attr_ctx *manager,
101                                 const gss_buffer_t buffer)
102     {
103         return initWithManager(manager);
104     }
105
106     static bool init() { return true; }
107     static void finalize() {}
108
109     static gss_eap_attr_source *createAttrContext(void) { return NULL; }
110
111 protected:
112     const gss_eap_attr_ctx *m_manager;
113
114 private:
115     /* make non-copyable */
116     gss_eap_attr_source(const gss_eap_attr_source&);
117     gss_eap_attr_source& operator=(const gss_eap_attr_source&);
118 };
119
120 typedef gss_eap_attr_source *(*gss_eap_attr_create_factory)(void);
121
122 struct gss_eap_attr_ctx : gss_eap_attr_source
123 {
124 public:
125     gss_eap_attr_ctx(void);
126     ~gss_eap_attr_ctx(void);
127
128     bool initFromExistingContext(const gss_eap_attr_ctx *manager,
129                                  const gss_eap_attr_source *ctx);
130     bool initFromGssContext(const gss_eap_attr_ctx *manager,
131                             const gss_cred_id_t cred,
132                             const gss_ctx_id_t ctx);
133
134     bool getAttributeTypes(gss_eap_attr_enumeration_cb, void *data) const;
135     bool getAttributeTypes(gss_buffer_set_t *attrs);
136
137     void setAttribute(int complete,
138                       const gss_buffer_t attr,
139                       const gss_buffer_t value);
140     void deleteAttribute(const gss_buffer_t value);
141     bool getAttribute(const gss_buffer_t attr,
142                       int *authenticated,
143                       int *complete,
144                       gss_buffer_t value,
145                       gss_buffer_t display_value,
146                       int *more) const;
147     gss_any_t mapToAny(int authenticated,
148                        gss_buffer_t type_id) const;
149     void releaseAnyNameMapping(gss_buffer_t type_id,
150                                gss_any_t input) const;
151
152     void exportToBuffer(gss_buffer_t buffer) const;
153     bool initFromBuffer(const gss_eap_attr_ctx *ctx,
154                         const gss_buffer_t buffer);
155     static bool init();
156     static void finalize();
157
158     static unsigned int
159     attributePrefixToType(const gss_buffer_t prefix);
160
161     static const gss_buffer_t
162     attributeTypeToPrefix(unsigned int type);
163
164     static void
165     decomposeAttributeName(const gss_buffer_t attribute,
166                            gss_buffer_t prefix,
167                            gss_buffer_t suffix);
168     static void
169     composeAttributeName(const gss_buffer_t prefix,
170                          const gss_buffer_t suffix,
171                          gss_buffer_t attribute);
172     static void
173     decomposeAttributeName(const gss_buffer_t attribute,
174                            unsigned int *type,
175                            gss_buffer_t suffix);
176     static void
177     composeAttributeName(unsigned int type,
178                          const gss_buffer_t suffix,
179                          gss_buffer_t attribute);
180
181     static std::string
182     composeAttributeName(const gss_buffer_t prefix,
183                          const gss_buffer_t suffix);
184     static std::string
185     composeAttributeName(unsigned int type,
186                          const gss_buffer_t suffix);
187
188     gss_eap_attr_source *getProvider(unsigned int type) const;
189     gss_eap_attr_source *getProvider(const gss_buffer_t prefix) const;
190
191 private:
192     /* make non-copyable */
193     gss_eap_attr_ctx(const gss_eap_attr_ctx&);
194     gss_eap_attr_ctx& operator=(const gss_eap_attr_ctx&);
195
196     gss_eap_attr_source *m_sources[ATTR_TYPE_MAX];
197 };
198
199 #include "util_radius.h"
200 #include "util_saml.h"
201 #include "util_shib.h"
202
203 #include <string>
204 #include <new>
205
206 static inline void
207 duplicateBuffer(gss_buffer_desc &src, gss_buffer_t dst)
208 {
209     OM_uint32 minor;
210
211     if (GSS_ERROR(duplicateBuffer(&minor, &src, dst)))
212         throw new std::bad_alloc();
213 }
214
215 static inline void
216 duplicateBuffer(std::string &str, gss_buffer_t buffer)
217 {
218     gss_buffer_desc tmp;
219
220     tmp.length = str.length();
221     tmp.value = (char *)str.c_str();
222
223     duplicateBuffer(tmp, buffer);
224 }
225
226 #else
227 struct gss_eap_attr_ctx;
228 #endif
229
230 #ifdef __cplusplus
231 extern "C" {
232 #endif
233
234 struct gss_eap_attr_ctx *
235 gssEapCreateAttrContext(gss_cred_id_t acceptorCred,
236                         gss_ctx_id_t acceptorCtx);
237
238 OM_uint32
239 gssEapInquireName(OM_uint32 *minor,
240                   gss_name_t name,
241                   int *name_is_MN,
242                   gss_OID *MN_mech,
243                   gss_buffer_set_t *attrs);
244
245 OM_uint32
246 gssEapGetNameAttribute(OM_uint32 *minor,
247                        gss_name_t name,
248                        gss_buffer_t attr,
249                        int *authenticated,
250                        int *complete,
251                        gss_buffer_t value,
252                        gss_buffer_t display_value,
253                        int *more);
254
255 OM_uint32
256 gssEapDeleteNameAttribute(OM_uint32 *minor,
257                           gss_name_t name,
258                           gss_buffer_t attr);
259
260 OM_uint32
261 gssEapSetNameAttribute(OM_uint32 *minor,
262                        gss_name_t name,
263                        int complete,
264                        gss_buffer_t attr,
265                        gss_buffer_t value);
266
267 OM_uint32
268 gssEapExportAttrContext(OM_uint32 *minor,
269                         gss_name_t name,
270                         gss_buffer_t buffer);
271
272 OM_uint32
273 gssEapImportAttrContext(OM_uint32 *minor,
274                         gss_buffer_t buffer,
275                         gss_name_t name);
276
277 OM_uint32
278 gssEapDuplicateAttrContext(OM_uint32 *minor,
279                            gss_name_t in,
280                            gss_name_t out);
281
282 OM_uint32
283 gssEapMapNameToAny(OM_uint32 *minor,
284                    gss_name_t name,
285                    int authenticated,
286                    gss_buffer_t type_id,
287                    gss_any_t *output);
288
289 OM_uint32
290 gssEapReleaseAnyNameMapping(OM_uint32 *minor,
291                             gss_name_t name,
292                             gss_buffer_t type_id,
293                             gss_any_t *input);
294
295 OM_uint32
296 gssEapReleaseAttrContext(OM_uint32 *minor,
297                          gss_name_t name);
298
299 OM_uint32
300 gssEapAttrProvidersInit(OM_uint32 *minor);
301
302 OM_uint32
303 gssEapAttrProvidersFinalize(OM_uint32 *minor);
304
305 #ifdef __cplusplus
306 }
307 #endif
308
309 #endif /* _UTIL_ATTR_H_ */