2 * Copyright (c) 2010, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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
33 * Copyright 2001-2009 Internet2
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
39 * http://www.apache.org/licenses/LICENSE-2.0
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.
49 * Local attribute provider implementation.
52 #include <xmltooling/XMLObject.h>
54 #include <saml/saml2/core/Assertions.h>
56 #include <shibsp/exceptions.h>
57 #include <shibsp/attribute/SimpleAttribute.h>
58 #include <shibresolver/resolver.h>
62 #include "gssapiP_eap.h"
64 using namespace shibsp;
65 using namespace shibresolver;
66 using namespace opensaml::saml2md;
67 using namespace opensaml;
68 using namespace xmltooling;
71 gss_eap_shib_attr_provider::gss_eap_shib_attr_provider(void)
73 m_authenticated = false;
76 gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
78 for_each(m_attributes.begin(),
80 xmltooling::cleanup<Attribute>())
85 gss_eap_shib_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
86 const gss_eap_attr_provider *ctx)
88 const gss_eap_shib_attr_provider *shib;
90 if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx)) {
94 m_authenticated = false;
96 shib = static_cast<const gss_eap_shib_attr_provider *>(ctx);
98 m_attributes = duplicateAttributes(shib->getAttributes());
99 m_authenticated = shib->authenticated();
106 addRadiusAttribute(const gss_eap_attr_provider *provider,
107 const gss_buffer_t attribute,
110 const gss_eap_shib_attr_provider *shib;
111 const gss_eap_radius_attr_provider *radius;
112 int authenticated, complete, more = -1;
113 vector <string> attributeIds(1);
116 radius = static_cast<const gss_eap_radius_attr_provider *>(provider);
117 shib = static_cast<const gss_eap_shib_attr_provider *>(data);
119 assert(radius != NULL && shib != NULL);
121 string attributeName =
122 gss_eap_attr_ctx::composeAttributeName(ATTR_TYPE_RADIUS, attribute);
124 attributeIds.push_back(attributeName);
125 a = new SimpleAttribute(attributeIds);
130 gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
133 if (!radius->getAttribute(attribute,
141 string attributeValue((char *)value.value, value.length);
142 a->getValues().push_back(attributeValue);
144 gss_release_buffer(&minor, &value);
147 shib->getAttributes().push_back(a);
153 gss_eap_shib_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
154 const gss_cred_id_t gssCred,
155 const gss_ctx_id_t gssCtx)
157 const gss_eap_saml_assertion_provider *saml;
158 const gss_eap_radius_attr_provider *radius;
159 gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
160 ShibbolethResolver *resolver;
163 if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
166 saml = static_cast<const gss_eap_saml_assertion_provider *>
167 (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
168 radius = static_cast<const gss_eap_radius_attr_provider *>
169 (m_manager->getProvider(ATTR_TYPE_RADIUS));
171 resolver = ShibbolethResolver::create();
173 if (gssCred != GSS_C_NO_CREDENTIAL &&
174 gssEapDisplayName(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE) {
175 resolver->setApplicationID((const char *)nameBuf.value);
176 gss_release_buffer(&minor, &nameBuf);
179 m_authenticated = false;
181 if (radius != NULL) {
182 radius->getAttributeTypes(addRadiusAttribute, (void *)this);
183 m_authenticated = radius->authenticated();
186 if (saml != NULL && saml->getAssertion() != NULL) {
187 resolver->addToken(saml->getAssertion());
189 m_authenticated = saml->authenticated();
194 m_attributes = resolver->getResolvedAttributes();
195 resolver->getResolvedAttributes().clear();
196 } catch (exception &e) {
208 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
212 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
213 a != m_attributes.end();
216 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
217 s != (*a)->getAliases().end();
219 if (attr->length == (*s).length() &&
220 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
230 gss_eap_shib_attr_provider::setAttribute(int complete,
231 const gss_buffer_t attr,
232 const gss_buffer_t value)
234 string attrStr((char *)attr->value, attr->length);
235 vector <string> ids(1, attrStr);
236 SimpleAttribute *a = new SimpleAttribute(ids);
238 if (value->length != 0) {
239 string valueStr((char *)value->value, value->length);
241 a->getValues().push_back(valueStr);
244 m_attributes.push_back(a);
245 m_authenticated = false;
251 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
255 i = getAttributeIndex(attr);
257 m_attributes.erase(m_attributes.begin() + i);
259 m_authenticated = false;
265 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
268 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
269 a != m_attributes.end();
272 gss_buffer_desc attribute;
274 attribute.value = (void *)((*a)->getId());
275 attribute.length = strlen((char *)attribute.value);
277 if (!addAttribute(this, &attribute, data))
285 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
287 const Attribute *ret = NULL;
289 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
290 a != m_attributes.end();
293 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
294 s != (*a)->getAliases().end();
296 if (attr->length == (*s).length() &&
297 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
310 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
314 gss_buffer_t display_value,
317 const Attribute *shibAttr = NULL;
319 int nvalues, i = *more;
323 shibAttr = getAttribute(attr);
324 if (shibAttr == NULL)
327 nvalues = shibAttr->valueCount();
331 else if (i >= nvalues)
334 buf.value = (void *)shibAttr->getString(*more);
335 buf.length = strlen((char *)buf.value);
337 if (buf.length != 0) {
339 duplicateBuffer(buf, value);
341 if (display_value != NULL)
342 duplicateBuffer(buf, display_value);
345 if (authenticated != NULL)
346 *authenticated = m_authenticated;
347 if (complete != NULL)
357 gss_eap_shib_attr_provider::mapToAny(int authenticated,
358 gss_buffer_t type_id) const
362 if (authenticated && !m_authenticated)
363 return (gss_any_t)NULL;
365 vector <Attribute *>v = duplicateAttributes(m_attributes);
367 output = (gss_any_t)new vector <Attribute *>(v);
373 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
374 gss_any_t input) const
376 vector <Attribute *> *v = ((vector <Attribute *> *)input);
381 gss_eap_shib_attr_provider::exportToBuffer(gss_buffer_t buffer) const
387 buffer->value = NULL;
389 obj.addmember("version").integer(1);
390 obj.addmember("authenticated").integer(m_authenticated);
392 attrs = obj.addmember("attributes").list();
393 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
394 a != m_attributes.end(); ++a) {
395 DDF attr = (*a)->marshall();
401 string str = sink.str();
403 duplicateBuffer(str, buffer);
409 gss_eap_shib_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
410 const gss_buffer_t buffer)
412 if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
415 if (buffer->length == 0)
418 assert(m_authenticated == false);
419 assert(m_attributes.size() == 0);
422 string str((const char *)buffer->value, buffer->length);
423 istringstream source(str);
427 if (obj["version"].integer() != 1)
430 m_authenticated = (obj["authenticated"].integer() != 0);
432 DDF attrs = obj["attributes"];
433 DDF attr = attrs.first();
434 while (!attr.isnull()) {
435 Attribute *attribute = Attribute::unmarshall(attr);
436 m_attributes.push_back(attribute);
446 gss_eap_shib_attr_provider::init(void)
448 if (!ShibbolethResolver::init())
451 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL, NULL, createAttrContext);
457 gss_eap_shib_attr_provider::finalize(void)
459 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
460 ShibbolethResolver::term();
464 gss_eap_shib_attr_provider::mapException(OM_uint32 *minor,
465 std::exception &e) const
467 if (typeid(e) == typeid(AttributeException))
468 *minor = GSSEAP_SHIB_ATTR_FAILURE;
469 else if (typeid(e) == typeid(AttributeExtractionException))
470 *minor = GSSEAP_SHIB_ATTR_EXTRACT_FAILURE;
471 else if (typeid(e) == typeid(AttributeFilteringException))
472 *minor = GSSEAP_SHIB_ATTR_FILTER_FAILURE;
473 else if (typeid(e) == typeid(AttributeResolutionException))
474 *minor = GSSEAP_SHIB_ATTR_RESOLVE_FAILURE;
475 else if (typeid(e) == typeid(ConfigurationException))
476 *minor = GSSEAP_SHIB_CONFIG_FAILURE;
477 else if (typeid(e) == typeid(ListenerException))
478 *minor = GSSEAP_SHIB_LISTENER_FAILURE;
480 return GSS_S_CONTINUE_NEEDED;
482 return GSS_S_FAILURE;
485 gss_eap_attr_provider *
486 gss_eap_shib_attr_provider::createAttrContext(void)
488 return new gss_eap_shib_attr_provider;
492 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
494 DDF obj = src->marshall();
495 Attribute *attribute = Attribute::unmarshall(obj);
502 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
504 vector <Attribute *> dst;
506 for (vector<Attribute *>::const_iterator a = src.begin();
509 dst.push_back(duplicateAttribute(*a));
515 gssEapLocalAttrProviderInit(OM_uint32 *minor)
517 if (!gss_eap_shib_attr_provider::init()) {
518 *minor = GSSEAP_SHIB_INIT_FAILURE;
519 return GSS_S_FAILURE;
521 return GSS_S_COMPLETE;
525 gssEapLocalAttrProviderFinalize(OM_uint32 *minor)
527 gss_eap_shib_attr_provider::finalize();
528 return GSS_S_COMPLETE;