New attribute provider SPI
[mech_eap.orig] / util_shib.cpp
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  * Copyright 2001-2009 Internet2
34  * 
35  * Licensed under the Apache License, Version 2.0 (the "License");
36  * you may not use this file except in compliance with the License.
37  * You may obtain a copy of the License at
38  *
39  *     http://www.apache.org/licenses/LICENSE-2.0
40  *
41  * Unless required by applicable law or agreed to in writing, software
42  * distributed under the License is distributed on an "AS IS" BASIS,
43  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
44  * See the License for the specific language governing permissions and
45  * limitations under the License.
46  */
47
48 #include "gssapiP_eap.h"
49
50 #include <shibsp/Application.h>
51 #include <shibsp/exceptions.h>
52 #include <shibsp/SPConfig.h>
53 #include <shibsp/ServiceProvider.h>
54 #include <shibsp/attribute/Attribute.h>
55 #include <shibsp/attribute/SimpleAttribute.h>
56 #include <shibsp/attribute/resolver/ResolutionContext.h>
57 #include <shibsp/handler/AssertionConsumerService.h>
58 #include <shibsp/metadata/MetadataProviderCriteria.h>
59 #include <shibsp/util/SPConstants.h>
60
61 #include <saml/saml1/core/Assertions.h>
62 #include <saml/saml2/core/Assertions.h>
63 #include <saml/saml2/metadata/Metadata.h>
64 #include <xercesc/util/XMLUniDefs.hpp>
65 #include <xmltooling/XMLToolingConfig.h>
66 #include <xmltooling/util/XMLHelper.h>
67
68 #include "resolver.h"
69
70 using namespace shibsp;
71 using namespace shibresolver;
72 using namespace opensaml::saml2md;
73 using namespace opensaml;
74 using namespace xmltooling::logging;
75 using namespace xmltooling;
76 using namespace xercesc;
77 using namespace std;
78
79 static vector <Attribute *>
80 duplicateAttributes(const vector <Attribute *>src);
81
82 gss_eap_shib_attr_provider::gss_eap_shib_attr_provider(const gss_eap_attr_ctx *ctx,
83                                                        gss_cred_id_t gssCred,
84                                                        gss_ctx_id_t gssCtx)
85     : gss_eap_attr_provider(ctx, gssCred, gssCtx)
86 {
87     if (gssCtx == GSS_C_NO_CONTEXT) {
88         gss_eap_shib_attr_provider *shib;
89
90         shib = dynamic_cast<gss_eap_shib_attr_provider *>
91             (ctx->getProvider(ATTR_TYPE_LOCAL));
92         if (shib != NULL)
93             m_attributes = duplicateAttributes(shib->m_attributes);
94     } else {
95         gss_eap_saml_assertion_provider *saml;
96         gss_eap_radius_attr_provider *radius;
97         gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
98         ShibbolethResolver *resolver = NULL;
99         OM_uint32 minor;
100
101         saml = dynamic_cast<gss_eap_saml_assertion_provider *>
102             (ctx->getProvider(ATTR_TYPE_SAML_ASSERTION));
103         radius = dynamic_cast<gss_eap_radius_attr_provider *>
104             (ctx->getProvider(ATTR_TYPE_RADIUS));
105
106         if (radius == NULL)
107             return;
108
109         if (gssCred != GSS_C_NO_CREDENTIAL &&
110             gss_display_name(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE)
111             resolver->setApplicationID((const char *)nameBuf.value);
112
113         if (saml != NULL && saml->getAssertion() != NULL)
114             resolver->addToken(saml->getAssertion());
115
116         /* TODO inject RADIUS attribute types */
117
118         resolver->resolveAttributes(m_attributes);
119
120         gss_release_buffer(&minor, &nameBuf);
121
122         delete resolver;
123     }
124 }
125
126 gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
127 {
128     for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
129 }
130
131 int
132 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
133 {
134     int i = 0;
135
136     for (vector<Attribute *>::const_iterator a = m_attributes.begin();
137          a != m_attributes.end();
138          ++a)
139     {
140         for (vector<string>::const_iterator s = (*a)->getAliases().begin();
141              s != (*a)->getAliases().end();
142              ++s) {
143             if (attr->length == (*s).length() &&
144                 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
145                 return i;
146             }
147         }
148     }
149
150     return -1;
151 }
152
153 void
154 gss_eap_shib_attr_provider::setAttribute(int complete,
155                                          const gss_buffer_t attr,
156                                          const gss_buffer_t value)
157 {
158     string attrStr((char *)attr->value, attr->length);
159     vector <string> ids(1);
160
161     ids.push_back(attrStr);
162
163     SimpleAttribute *a = new SimpleAttribute(ids);
164
165     if (value->length != 0) {
166         string valStr((char *)value->value, value->length);
167
168         a->getValues().push_back(valStr);        
169     }
170
171     m_attributes.push_back(a);
172 }
173
174 void
175 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
176 {
177     int i;
178
179     i = getAttributeIndex(attr);
180     if (i >= 0)
181         m_attributes.erase(m_attributes.begin() + i);
182 }
183
184 bool
185 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
186                                               void *data) const
187 {
188     for (vector<Attribute*>::const_iterator a = m_attributes.begin();
189         a != m_attributes.end();
190         ++a)
191     {
192         gss_buffer_desc attribute;
193
194         attribute.value = (void *)((*a)->getId());
195         attribute.length = strlen((char *)attribute.value);
196
197         if (!addAttribute(this, &attribute, data))
198             return false;
199     }
200
201     return true;
202 }
203
204 const Attribute *
205 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
206 {
207     const Attribute *ret = NULL;
208
209     for (vector<Attribute *>::const_iterator a = m_attributes.begin();
210          a != m_attributes.end();
211          ++a)
212     {
213         for (vector<string>::const_iterator s = (*a)->getAliases().begin();
214              s != (*a)->getAliases().end();
215              ++s) {
216             if (attr->length == (*s).length() &&
217                 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
218                 ret = *a;
219                 break;
220             }
221         }
222         if (ret != NULL)
223             break;
224     }
225
226     return ret;
227 }
228
229 bool
230 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
231                                          int *authenticated,
232                                          int *complete,
233                                          gss_buffer_t value,
234                                          gss_buffer_t display_value,
235                                          int *more) const
236 {
237     const Attribute *shibAttr = NULL;
238     gss_buffer_desc buf;
239
240     shibAttr = getAttribute(attr);
241     if (shibAttr == NULL)
242         return false;
243
244     if (*more == -1) {
245         *more = 0;
246     } else if (*more >= (int)shibAttr->valueCount()) {
247         *more = 0;
248         return true;
249     }
250
251     buf.value = (void *)shibAttr->getString(*more);
252     buf.length = strlen((char *)buf.value);
253
254     duplicateBuffer(buf, value);
255  
256     *authenticated = TRUE;
257     *complete = FALSE;
258
259     return true;
260 }
261
262 gss_any_t
263 gss_eap_shib_attr_provider::mapToAny(int authenticated,
264                                      gss_buffer_t type_id) const
265 {
266     gss_any_t output;
267
268     vector <Attribute *>v = duplicateAttributes(m_attributes);
269
270     output = (gss_any_t)new vector <Attribute *>(v);
271
272     return output;
273 }
274
275 void
276 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
277                                                   gss_any_t input) const
278 {
279     vector <Attribute *> *v = ((vector <Attribute *> *)input);
280     delete v;
281 }
282
283 void
284 gss_eap_shib_attr_provider::marshall(gss_buffer_t buffer) const
285 {
286 }
287
288 gss_eap_attr_provider *
289 gss_eap_shib_attr_provider::unmarshall(const gss_eap_attr_ctx *ctx,
290                                        const gss_buffer_t buffer)
291 {
292     return NULL;
293 }
294
295 bool
296 gss_eap_shib_attr_provider::init(void)
297 {
298     return ShibbolethResolver::init();
299 }
300
301 void
302 gss_eap_shib_attr_provider::finalize(void)
303 {
304     ShibbolethResolver::term();
305 }
306
307 gss_eap_attr_provider *
308 gss_eap_shib_attr_provider::createAttrContext(const gss_eap_attr_ctx *ctx,
309                                               gss_cred_id_t gssCred,
310                                               gss_ctx_id_t gssCtx)
311 {
312     return new gss_eap_shib_attr_provider(ctx, gssCred, gssCtx);
313 }
314
315 static Attribute *
316 duplicateAttribute(const Attribute *src)
317 {
318     Attribute *attribute;
319
320     DDF obj = src->marshall();
321     attribute = Attribute::unmarshall(obj);
322     obj.destroy();
323
324     return attribute;
325 }
326
327 static vector <Attribute *>
328 duplicateAttributes(const vector <Attribute *>src)
329 {
330     vector <Attribute *> dst;
331
332     for (vector<Attribute *>::const_iterator a = src.begin();
333          a != src.end();
334          ++a)
335         dst.push_back(duplicateAttribute(*a));
336
337     return dst;
338 }