2 * Copyright (c) 2011, 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_ctx *manager,
107 const gss_eap_attr_provider *provider,
108 const gss_buffer_t attribute,
111 const gss_eap_radius_attr_provider *radius;
112 const gss_eap_shib_attr_provider *shib;
113 int authenticated, complete, more = -1;
114 vector <string> attributeIds(1);
117 radius = static_cast<const gss_eap_radius_attr_provider *>(provider);
118 shib = static_cast<const gss_eap_shib_attr_provider *>(data);
120 assert(radius != NULL && shib != NULL);
122 string attributeName =
123 manager->composeAttributeName(ATTR_TYPE_RADIUS, attribute);
125 attributeIds.push_back(attributeName);
126 a = new SimpleAttribute(attributeIds);
131 gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
134 if (!radius->getAttribute(attribute,
142 string attributeValue((char *)value.value, value.length);
143 a->getValues().push_back(attributeValue);
145 gss_release_buffer(&minor, &value);
148 shib->getAttributes().push_back(a);
154 gss_eap_shib_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
155 const gss_cred_id_t gssCred,
156 const gss_ctx_id_t gssCtx)
158 const gss_eap_saml_assertion_provider *saml;
159 const gss_eap_radius_attr_provider *radius;
161 gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
164 if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
167 saml = static_cast<const gss_eap_saml_assertion_provider *>
168 (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
169 radius = static_cast<const gss_eap_radius_attr_provider *>
170 (m_manager->getProvider(ATTR_TYPE_RADIUS));
172 auto_ptr<ShibbolethResolver> resolver(ShibbolethResolver::create());
175 * For now, leave ApplicationID defaulted.
176 * Later on, we could allow this via config option to the mechanism
177 * or rely on an SPRequest interface to pass in a URI identifying the
181 if (gssCred != GSS_C_NO_CREDENTIAL &&
182 gssEapDisplayName(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE) {
183 resolver->setApplicationID((const char *)nameBuf.value);
184 gss_release_buffer(&minor, &nameBuf);
188 m_authenticated = false;
190 if (radius != NULL) {
191 radius->getAttributeTypes(addRadiusAttribute, (void *)this);
192 m_authenticated = radius->authenticated();
195 if (saml != NULL && saml->getAssertion() != NULL) {
196 resolver->addToken(saml->getAssertion());
198 m_authenticated = saml->authenticated();
203 m_attributes = resolver->getResolvedAttributes();
204 resolver->getResolvedAttributes().clear();
205 } catch (exception &e) {
207 fprintf(stderr, "%s", e.what());
215 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
219 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
220 a != m_attributes.end();
223 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
224 s != (*a)->getAliases().end();
226 if (attr->length == (*s).length() &&
227 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
237 gss_eap_shib_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
238 const gss_buffer_t attr,
239 const gss_buffer_t value)
241 string attrStr((char *)attr->value, attr->length);
242 vector <string> ids(1, attrStr);
243 SimpleAttribute *a = new SimpleAttribute(ids);
245 if (value->length != 0) {
246 string valueStr((char *)value->value, value->length);
248 a->getValues().push_back(valueStr);
251 m_attributes.push_back(a);
252 m_authenticated = false;
258 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
262 i = getAttributeIndex(attr);
264 m_attributes.erase(m_attributes.begin() + i);
266 m_authenticated = false;
272 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
275 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
276 a != m_attributes.end();
279 gss_buffer_desc attribute;
281 attribute.value = (void *)((*a)->getId());
282 attribute.length = strlen((char *)attribute.value);
284 if (!addAttribute(m_manager, this, &attribute, data))
292 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
294 const Attribute *ret = NULL;
296 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
297 a != m_attributes.end();
300 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
301 s != (*a)->getAliases().end();
303 if (attr->length == (*s).length() &&
304 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
317 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
321 gss_buffer_t display_value,
324 const Attribute *shibAttr = NULL;
326 int nvalues, i = *more;
330 shibAttr = getAttribute(attr);
331 if (shibAttr == NULL)
334 nvalues = shibAttr->valueCount();
338 else if (i >= nvalues)
341 buf.value = (void *)shibAttr->getSerializedValues()[*more].c_str();
342 buf.length = strlen((char *)buf.value);
344 if (buf.length != 0) {
346 duplicateBuffer(buf, value);
348 if (display_value != NULL)
349 duplicateBuffer(buf, display_value);
352 if (authenticated != NULL)
353 *authenticated = m_authenticated;
354 if (complete != NULL)
364 gss_eap_shib_attr_provider::mapToAny(int authenticated,
365 gss_buffer_t type_id GSSEAP_UNUSED) const
369 if (authenticated && !m_authenticated)
370 return (gss_any_t)NULL;
372 vector <Attribute *>v = duplicateAttributes(m_attributes);
374 output = (gss_any_t)new vector <Attribute *>(v);
380 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
381 gss_any_t input) const
383 vector <Attribute *> *v = ((vector <Attribute *> *)input);
388 gss_eap_shib_attr_provider::prefix(void) const
394 gss_eap_shib_attr_provider::name(void) const
404 if (ddf.isstruct()) {
405 DDF elem = ddf.first();
408 throw new std::bad_alloc;
410 while (!elem.isnull()) {
411 if (json_array_append_new(json, ddfToJson(elem)) != 0) {
413 throw new std::bad_alloc;
418 } else if (ddf.islist()) {
419 DDF elem = ddf.first();
420 json = json_object();
422 throw new std::bad_alloc;
424 while (!elem.isnull()) {
425 if (json_object_set(json, elem.name(), ddfToJson(elem)) != 0) {
427 throw new std::bad_alloc;
432 } else if (ddf.isstring()) {
433 json = json_string(ddf.string());
434 } else if (ddf.isint()) {
435 json = json_integer(ddf.integer());
436 } else if (ddf.isfloat()) {
437 json = json_real(ddf.floating());
438 } else if (ddf.isempty() || ddf.ispointer()) {
439 json = json_object();
440 } else if (ddf.isnull()) {
443 assert(0 && "Invalid DDF object");
447 throw new std::bad_alloc;
453 jsonToDdf(json_t *json)
457 switch (json_typeof(json)) {
459 void *iter = json_object_iter(json);
461 while (iter != NULL) {
462 const char *key = json_object_iter_key(iter);
463 json_t *value = json_object_iter_value(iter);
464 ddf.add(jsonToDdf(value).name(key));
465 iter = json_object_iter_next(json, iter);
472 for (i = 0; i < json_array_size(json); i++) {
473 DDF value = jsonToDdf(json_array_get(json, i));
479 ddf.string(json_string_value(json));
482 ddf.integer(json_integer_value(json));
485 ddf.floating(json_real_value(json));
501 gss_eap_shib_attr_provider::jsonRepresentation(void) const
507 throw new std::bad_alloc;
509 /* FIXME check json_object_set_new return value */
510 json_object_set_new(obj, "authenticated", json_integer(m_authenticated));
512 attrs = json_array();
515 throw new std::bad_alloc;
518 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
519 a != m_attributes.end(); ++a) {
520 DDF attr = (*a)->marshall();
521 /* FIXME check json_array_append_new return value */
522 json_array_append_new(attrs, ddfToJson(attr));
525 json_object_set_new(obj, "attributes", attrs);
531 gss_eap_shib_attr_provider::initWithJsonObject(const gss_eap_attr_ctx *ctx,
537 if (!gss_eap_attr_provider::initWithJsonObject(ctx, obj))
540 assert(m_authenticated == false);
541 assert(m_attributes.size() == 0);
543 m_authenticated = json_integer_value(json_object_get(obj, "authenticated"));
545 attrs = json_object_get(obj, "attributes");
547 for (i = 0; i < json_array_size(attrs); i++) {
548 DDF attr = jsonToDdf(json_array_get(attrs, i));
549 Attribute *attribute = Attribute::unmarshall(attr);
550 m_attributes.push_back(attribute);
557 gss_eap_shib_attr_provider::init(void)
559 if (!ShibbolethResolver::init())
562 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL, createAttrContext);
568 gss_eap_shib_attr_provider::finalize(void)
570 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
571 ShibbolethResolver::term();
575 gss_eap_shib_attr_provider::mapException(OM_uint32 *minor,
576 std::exception &e) const
578 if (typeid(e) == typeid(AttributeException))
579 *minor = GSSEAP_SHIB_ATTR_FAILURE;
580 else if (typeid(e) == typeid(AttributeExtractionException))
581 *minor = GSSEAP_SHIB_ATTR_EXTRACT_FAILURE;
582 else if (typeid(e) == typeid(AttributeFilteringException))
583 *minor = GSSEAP_SHIB_ATTR_FILTER_FAILURE;
584 else if (typeid(e) == typeid(AttributeResolutionException))
585 *minor = GSSEAP_SHIB_ATTR_RESOLVE_FAILURE;
586 else if (typeid(e) == typeid(ConfigurationException))
587 *minor = GSSEAP_SHIB_CONFIG_FAILURE;
588 else if (typeid(e) == typeid(ListenerException))
589 *minor = GSSEAP_SHIB_LISTENER_FAILURE;
591 return GSS_S_CONTINUE_NEEDED;
593 return GSS_S_FAILURE;
596 gss_eap_attr_provider *
597 gss_eap_shib_attr_provider::createAttrContext(void)
599 return new gss_eap_shib_attr_provider;
603 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
605 DDF obj = src->marshall();
606 Attribute *attribute = Attribute::unmarshall(obj);
613 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
615 vector <Attribute *> dst;
617 for (vector<Attribute *>::const_iterator a = src.begin();
620 dst.push_back(duplicateAttribute(*a));
626 gssEapLocalAttrProviderInit(OM_uint32 *minor)
628 if (!gss_eap_shib_attr_provider::init()) {
629 *minor = GSSEAP_SHIB_INIT_FAILURE;
630 return GSS_S_FAILURE;
632 return GSS_S_COMPLETE;
636 gssEapLocalAttrProviderFinalize(OM_uint32 *minor)
638 gss_eap_shib_attr_provider::finalize();
641 return GSS_S_COMPLETE;