cleanup
[mech_eap.orig] / 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 struct gss_eap_attr_ctx;
45
46 struct gss_eap_attr_provider
47 {
48 public:
49     gss_eap_attr_provider(const gss_eap_attr_ctx *ctx,
50                           gss_cred_id_t acceptorCred = GSS_C_NO_CREDENTIAL,
51                           gss_ctx_id_t acceptorCtx = GSS_C_NO_CONTEXT)
52     {
53         m_source = ctx;
54     }
55
56     gss_eap_attr_provider(const gss_eap_attr_provider &ctx) {}
57     virtual ~gss_eap_attr_provider(void) {}
58
59     typedef bool
60     gss_eap_attr_enumeration_cb(const gss_eap_attr_provider *provider,
61                                 const gss_buffer_t attribute,
62                                 void *data);
63
64     virtual bool getAttributeTypes(gss_eap_attr_enumeration_cb, void *data) const
65     {
66         return false;
67     }
68
69     virtual void setAttribute(int complete,
70                               const gss_buffer_t attr,
71                               const gss_buffer_t value) {}
72     virtual void deleteAttribute(const gss_buffer_t value) {}
73     virtual bool getAttribute(const gss_buffer_t attr,
74                               int *authenticated,
75                               int *complete,
76                               gss_buffer_t value,
77                               gss_buffer_t display_value,
78                               int *more) const { return false; }
79
80     virtual gss_any_t mapToAny(int authenticated,
81                                gss_buffer_t type_id) const { return NULL; }
82     virtual void releaseAnyNameMapping(gss_buffer_t type_id,
83                                        gss_any_t input) const {}
84
85     virtual void marshall(gss_buffer_t buffer) const {}
86     virtual bool unmarshall(const gss_eap_attr_ctx *ctx,
87                             const gss_buffer_t buffer) { return false; }
88
89     static bool init() { return true; }
90     static void finalize() {}
91
92     static gss_eap_attr_provider *
93     createAttrContext(const gss_eap_attr_ctx *ctx,
94                            gss_cred_id_t acceptorCred,
95                            gss_ctx_id_t acceptorCtx) { return NULL; }
96
97 protected:
98     const gss_eap_attr_ctx *m_source;
99 };
100
101 typedef gss_eap_attr_provider * (*gss_eap_attr_create_cb)(
102     const gss_eap_attr_ctx *ctx,
103     gss_cred_id_t acceptorCred,
104     gss_ctx_id_t acceptorCtx);
105
106 struct gss_eap_attr_ctx : gss_eap_attr_provider
107 {
108 public:
109     gss_eap_attr_ctx(const gss_eap_attr_ctx *ctx,
110                      gss_cred_id_t acceptorCred,
111                      gss_ctx_id_t acceptorCtx) :
112         gss_eap_attr_provider(ctx, acceptorCred, acceptorCtx) {}
113
114     gss_eap_attr_ctx(const gss_eap_attr_ctx &ctx);
115
116     ~gss_eap_attr_ctx(void);
117
118     static gss_eap_attr_ctx *createAttrContext(gss_cred_id_t acceptorCred,
119                                                     gss_ctx_id_t acceptorCtx);
120
121     bool getAttributeTypes(gss_eap_attr_enumeration_cb, void *data) const;
122     bool getAttributeTypes(gss_buffer_set_t *attrs);
123
124     void setAttribute(int complete,
125                       const gss_buffer_t attr,
126                       const gss_buffer_t value);
127     void deleteAttribute(const gss_buffer_t value);
128     bool getAttribute(const gss_buffer_t attr,
129                       int *authenticated,
130                       int *complete,
131                       gss_buffer_t value,
132                       gss_buffer_t display_value,
133                       int *more) const;
134     gss_any_t mapToAny(int authenticated,
135                        gss_buffer_t type_id) const;
136     void releaseAnyNameMapping(gss_buffer_t type_id,
137                                gss_any_t input) const;
138
139     void marshall(gss_buffer_t buffer) const;
140     bool unmarshall(const gss_eap_attr_ctx *ctx,
141                     const gss_buffer_t buffer);
142     static bool init();
143     static void finalize();
144
145     static unsigned int
146     attributePrefixToType(const gss_buffer_t prefix);
147
148     static gss_buffer_t
149     attributeTypeToPrefix(unsigned int type);
150
151     static void
152     decomposeAttributeName(const gss_buffer_t attribute,
153                            gss_buffer_t prefix,
154                            gss_buffer_t suffix);
155     static void
156     composeAttributeName(const gss_buffer_t prefix,
157                          const gss_buffer_t suffix,
158                          gss_buffer_t attribute);
159     static void
160     decomposeAttributeName(const gss_buffer_t attribute,
161                            unsigned int *type,
162                            gss_buffer_t suffix);
163     static void
164     composeAttributeName(unsigned int type,
165                          const gss_buffer_t suffix,
166                          gss_buffer_t attribute);
167
168     gss_eap_attr_provider *getProvider(unsigned int type) const;
169     gss_eap_attr_provider *getProvider(const gss_buffer_t prefix) const;
170
171 private:
172     gss_eap_attr_provider *m_providers[ATTR_TYPE_MAX];
173 };
174
175 #include "util_radius.h"
176 #include "util_saml.h"
177 #include "util_shib.h"
178
179 #include <string>
180 #include <new>
181
182 static inline void
183 duplicateBuffer(gss_buffer_desc &src, gss_buffer_t dst)
184 {
185     OM_uint32 minor;
186
187     if (GSS_ERROR(duplicateBuffer(&minor, &src, dst)))
188         throw new std::bad_alloc();
189 }
190
191 static inline void
192 duplicateBuffer(std::string &str, gss_buffer_t buffer)
193 {
194     gss_buffer_desc tmp;
195
196     tmp.length = str.length();
197     tmp.value = (char *)str.c_str();
198
199     duplicateBuffer(tmp, buffer);
200 }
201
202 #else
203 struct gss_eap_attr_ctx;
204 #endif
205
206 #ifdef __cplusplus
207 extern "C" {
208 #endif
209
210 struct gss_eap_attr_ctx *
211 gssEapCreateAttrContext(gss_cred_id_t acceptorCred,
212                         gss_ctx_id_t acceptorCtx);
213
214 OM_uint32
215 gssEapInquireName(OM_uint32 *minor,
216                   gss_name_t name,
217                   int *name_is_MN,
218                   gss_OID *MN_mech,
219                   gss_buffer_set_t *attrs);
220
221 OM_uint32
222 gssEapGetNameAttribute(OM_uint32 *minor,
223                        gss_name_t name,
224                        gss_buffer_t attr,
225                        int *authenticated,
226                        int *complete,
227                        gss_buffer_t value,
228                        gss_buffer_t display_value,
229                        int *more);
230
231 OM_uint32
232 gssEapDeleteNameAttribute(OM_uint32 *minor,
233                           gss_name_t name,
234                           gss_buffer_t attr);
235
236 OM_uint32
237 gssEapSetNameAttribute(OM_uint32 *minor,
238                        gss_name_t name,
239                        int complete,
240                        gss_buffer_t attr,
241                        gss_buffer_t value);
242
243 OM_uint32
244 gssEapExportAttrContext(OM_uint32 *minor,
245                         gss_name_t name,
246                         gss_buffer_t buffer);
247
248 OM_uint32
249 gssEapImportAttrContext(OM_uint32 *minor,
250                         gss_buffer_t buffer,
251                         gss_name_t name);
252
253 OM_uint32
254 gssEapDuplicateAttrContext(OM_uint32 *minor,
255                            gss_name_t in,
256                            gss_name_t out);
257
258 OM_uint32
259 gssEapMapNameToAny(OM_uint32 *minor,
260                    gss_name_t name,
261                    int authenticated,
262                    gss_buffer_t type_id,
263                    gss_any_t *output);
264
265 OM_uint32
266 gssEapReleaseAnyNameMapping(OM_uint32 *minor,
267                             gss_name_t name,
268                             gss_buffer_t type_id,
269                             gss_any_t *input);
270
271 OM_uint32
272 gssEapReleaseAttrContext(OM_uint32 *minor,
273                          gss_name_t name);
274
275 OM_uint32
276 gssEapAttrProvidersInit(OM_uint32 *minor);
277
278 OM_uint32
279 gssEapAttrProvidersFinalize(OM_uint32 *minor);
280
281 #ifdef __cplusplus
282 }
283 #endif
284
285 #endif /* _UTIL_ATTR_H_ */