more cleanup/revert
[moonshot.git] / mech_eap / 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_provider
49 {
50 public:
51     gss_eap_attr_provider(void) {}
52     virtual ~gss_eap_attr_provider(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_provider *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_provider *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_provider *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_provider(const gss_eap_attr_provider&);
117     gss_eap_attr_provider& operator=(const gss_eap_attr_provider&);
118 };
119
120 typedef gss_eap_attr_provider *(*gss_eap_attr_create_factory)(void);
121
122 struct gss_eap_attr_ctx : gss_eap_attr_provider
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_provider *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_provider *getProvider(unsigned int type) const;
189     gss_eap_attr_provider *getProvider(const gss_buffer_t prefix) const;
190
191     static void
192     registerProvider(unsigned int type, gss_eap_attr_create_factory factory);
193
194 private:
195     /* make non-copyable */
196     gss_eap_attr_ctx(const gss_eap_attr_ctx&);
197     gss_eap_attr_ctx& operator=(const gss_eap_attr_ctx&);
198
199     gss_eap_attr_provider *m_providers[ATTR_TYPE_MAX];
200 };
201
202 #include "util_radius.h"
203 #include "util_saml.h"
204 #include "util_shib.h"
205
206 #include <string>
207 #include <new>
208
209 static inline void
210 duplicateBuffer(gss_buffer_desc &src, gss_buffer_t dst)
211 {
212     OM_uint32 minor;
213
214     if (GSS_ERROR(duplicateBuffer(&minor, &src, dst)))
215         throw new std::bad_alloc();
216 }
217
218 static inline void
219 duplicateBuffer(std::string &str, gss_buffer_t buffer)
220 {
221     gss_buffer_desc tmp;
222
223     tmp.length = str.length();
224     tmp.value = (char *)str.c_str();
225
226     duplicateBuffer(tmp, buffer);
227 }
228
229 #else
230 struct gss_eap_attr_ctx;
231 #endif
232
233 #ifdef __cplusplus
234 extern "C" {
235 #endif
236
237 struct gss_eap_attr_ctx *
238 gssEapCreateAttrContext(gss_cred_id_t acceptorCred,
239                         gss_ctx_id_t acceptorCtx);
240
241 OM_uint32
242 gssEapInquireName(OM_uint32 *minor,
243                   gss_name_t name,
244                   int *name_is_MN,
245                   gss_OID *MN_mech,
246                   gss_buffer_set_t *attrs);
247
248 OM_uint32
249 gssEapGetNameAttribute(OM_uint32 *minor,
250                        gss_name_t name,
251                        gss_buffer_t attr,
252                        int *authenticated,
253                        int *complete,
254                        gss_buffer_t value,
255                        gss_buffer_t display_value,
256                        int *more);
257
258 OM_uint32
259 gssEapDeleteNameAttribute(OM_uint32 *minor,
260                           gss_name_t name,
261                           gss_buffer_t attr);
262
263 OM_uint32
264 gssEapSetNameAttribute(OM_uint32 *minor,
265                        gss_name_t name,
266                        int complete,
267                        gss_buffer_t attr,
268                        gss_buffer_t value);
269
270 OM_uint32
271 gssEapExportAttrContext(OM_uint32 *minor,
272                         gss_name_t name,
273                         gss_buffer_t buffer);
274
275 OM_uint32
276 gssEapImportAttrContext(OM_uint32 *minor,
277                         gss_buffer_t buffer,
278                         gss_name_t name);
279
280 OM_uint32
281 gssEapDuplicateAttrContext(OM_uint32 *minor,
282                            gss_name_t in,
283                            gss_name_t out);
284
285 OM_uint32
286 gssEapMapNameToAny(OM_uint32 *minor,
287                    gss_name_t name,
288                    int authenticated,
289                    gss_buffer_t type_id,
290                    gss_any_t *output);
291
292 OM_uint32
293 gssEapReleaseAnyNameMapping(OM_uint32 *minor,
294                             gss_name_t name,
295                             gss_buffer_t type_id,
296                             gss_any_t *input);
297
298 OM_uint32
299 gssEapReleaseAttrContext(OM_uint32 *minor,
300                          gss_name_t name);
301
302 OM_uint32
303 gssEapAttrProvidersInit(OM_uint32 *minor);
304
305 OM_uint32
306 gssEapAttrProvidersFinalize(OM_uint32 *minor);
307
308 #ifdef __cplusplus
309 }
310 #endif
311
312 #endif /* _UTIL_ATTR_H_ */