2 * Copyright 2010-2011 JANET(UK)
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 * An embeddable component interface to Shibboleth SP attribute processing.
25 #include <shibsp/exceptions.h>
26 #include <shibsp/Application.h>
27 #include <shibsp/GSSRequest.h>
28 #include <shibsp/SPRequest.h>
29 #include <shibsp/ServiceProvider.h>
30 #include <shibsp/attribute/Attribute.h>
31 #include <shibsp/remoting/ListenerService.h>
33 # include <saml/saml2/metadata/Metadata.h>
34 # include <saml/saml2/metadata/MetadataProvider.h>
35 # include <saml/util/SAMLConstants.h>
36 # include <shibsp/attribute/filtering/AttributeFilter.h>
37 # include <shibsp/attribute/filtering/BasicFilteringContext.h>
38 # include <shibsp/attribute/resolver/AttributeExtractor.h>
39 # include <shibsp/attribute/resolver/AttributeResolver.h>
40 # include <shibsp/attribute/resolver/ResolutionContext.h>
41 # include <shibsp/metadata/MetadataProviderCriteria.h>
43 #include <xmltooling/XMLObjectBuilder.h>
44 #include <xmltooling/XMLToolingConfig.h>
45 #include <xmltooling/impl/AnyElement.h>
46 #include <xmltooling/util/ParserPool.h>
47 #include <xmltooling/util/XMLHelper.h>
48 #include <xercesc/util/Base64.hpp>
50 using namespace shibresolver;
51 using namespace shibsp;
53 using namespace opensaml;
54 using namespace opensaml::saml2md;
56 using namespace xmltooling;
59 namespace shibresolver {
60 class SHIBRESOLVER_DLLLOCAL RemotedResolver : public Remoted {
67 for_each(tokens.begin(), tokens.end(), xmltooling::cleanup<XMLObject>());
68 for_each(inputAttrs.begin(), inputAttrs.end(), xmltooling::cleanup<Attribute>());
69 for_each(resolvedAttrs.begin(), resolvedAttrs.end(), xmltooling::cleanup<Attribute>());
72 vector<const XMLObject*> tokens;
73 vector<Attribute*> inputAttrs;
74 vector<Attribute*> resolvedAttrs;
77 void receive(DDF& in, ostream& out);
79 const Application& app,
81 const vector<const XMLObject*>& tokens,
82 const vector<Attribute*>& inputAttrs,
83 vector <Attribute*>& resolvedAttrs
87 static RemotedResolver g_Remoted;
90 ShibbolethResolver* ShibbolethResolver::create()
92 return new ShibbolethResolver();
95 ShibbolethResolver::ShibbolethResolver() : m_request(NULL), m_sp(NULL)
96 #ifdef SHIBRESOLVER_HAVE_GSSAPI
102 ShibbolethResolver::~ShibbolethResolver()
104 #ifdef SHIBRESOLVER_HAVE_GSSAPI
107 for_each(m_resolvedAttributes.begin(), m_resolvedAttributes.end(), xmltooling::cleanup<Attribute>());
112 void ShibbolethResolver::setRequest(const SPRequest* request)
115 #if defined(SHIBSP_HAVE_GSSAPI) && defined (SHIBRESOLVER_HAVE_GSSAPI)
117 const GSSRequest* gss = dynamic_cast<const GSSRequest*>(request);
119 // TODO: fix API to prevent destruction of contexts
120 gss_ctx_id_t ctx = gss->getGSSContext();
127 void ShibbolethResolver::setApplicationID(const char* appID)
134 void ShibbolethResolver::setIssuer(const char* issuer)
141 void ShibbolethResolver::addToken(const XMLObject* token)
144 m_tokens.push_back(token);
147 #ifdef SHIBRESOLVER_HAVE_GSSAPI
148 void ShibbolethResolver::addToken(gss_ctx_id_t* ctx)
155 if (ctx && *ctx != GSS_C_NO_CONTEXT) {
156 OM_uint32 major, minor;
157 gss_buffer_desc contextbuf = GSS_C_EMPTY_BUFFER;
159 major = gss_export_sec_context(&minor, ctx, &contextbuf);
160 if (major == GSS_S_COMPLETE) {
161 addToken(&contextbuf);
162 gss_release_buffer(&minor, &contextbuf);
165 Category::getInstance(SHIBRESOLVER_LOGCAT).error("error exporting GSS context");
170 void ShibbolethResolver::addToken(const gss_buffer_t contextbuf)
173 XMLByte* out=Base64::encode(reinterpret_cast<const XMLByte*>(contextbuf->value), contextbuf->length, &len);
176 s.append(reinterpret_cast<char*>(out), len);
177 auto_ptr_XMLCh temp(s.c_str());
178 #ifdef SHIBSP_XERCESC_HAS_XMLBYTE_RELEASE
179 XMLString::release(&out);
181 XMLString::release((char**)&out);
183 static const XMLCh _GSSAPI[] = UNICODE_LITERAL_6(G,S,S,A,P,I);
184 m_gsswrapper = new AnyElementImpl(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _GSSAPI);
185 m_gsswrapper->setTextContent(temp.get());
188 Category::getInstance(SHIBRESOLVER_LOGCAT).error("error while base64-encoding GSS context");
193 void ShibbolethResolver::addAttribute(Attribute* attr)
196 m_inputAttributes.push_back(attr);
199 vector<Attribute*>& ShibbolethResolver::getResolvedAttributes()
201 return m_resolvedAttributes;
204 RequestMapper::Settings ShibbolethResolver::getSettings() const
207 throw ConfigurationException("Request settings not available without supplying SPRequest instance.");
208 return m_request->getRequestSettings();
211 void ShibbolethResolver::resolve()
213 Category& log = Category::getInstance(SHIBRESOLVER_LOGCAT);
214 SPConfig& conf = SPConfig::getConfig();
216 m_sp = conf.getServiceProvider();
222 const Application* app = m_request ? &(m_request->getApplication()) : m_sp->getApplication(m_appID.c_str());
224 throw ConfigurationException("Unable to locate application for resolution.");
228 m_tokens.push_back(m_gsswrapper);
231 if (conf.isEnabled(SPConfig::OutOfProcess)) {
241 // When not out of process, we remote all the message processing.
242 DDF out,in = DDF("org.project-moonshot.shibresolver");
243 DDFJanitor jin(in), jout(out);
244 in.addmember("application_id").string(app->getId());
245 if (!m_issuer.empty())
246 in.addmember("issuer").string(m_issuer.c_str());
247 if (!m_tokens.empty()) {
248 DDF& tokens = in.addmember("tokens").list();
249 for (vector<const XMLObject*>::const_iterator t = m_tokens.begin(); t != m_tokens.end(); ++t) {
252 tokens.add(DDF(NULL).string(os.str().c_str()));
255 if (!m_inputAttributes.empty()) {
257 DDF& attrs = in.addmember("attributes").list();
258 for (vector<Attribute*>::const_iterator a = m_inputAttributes.begin(); a != m_inputAttributes.end(); ++a) {
259 attr = (*a)->marshall();
264 out = (m_request ? m_request->getServiceProvider() : (*m_sp)).getListenerService()->send(in);
266 Attribute* attribute;
267 DDF attr = out.first();
268 while (!attr.isnull()) {
270 attribute = Attribute::unmarshall(attr);
271 m_resolvedAttributes.push_back(attribute);
272 if (log.isDebugEnabled())
273 log.debug("unmarshalled attribute (ID: %s) with %d value%s",
274 attribute->getId(), attr.first().integer(), attr.first().integer()!=1 ? "s" : "");
276 catch (AttributeException& ex) {
277 const char* id = attr.first().name();
278 log.error("error unmarshalling attribute (ID: %s): %s", id ? id : "none", ex.what());
285 void RemotedResolver::receive(DDF& in, ostream& out)
287 Category& log = Category::getInstance(SHIBRESOLVER_LOGCAT);
290 const char* aid = in["application_id"].string();
291 const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : NULL;
293 // Something's horribly wrong.
294 log.error("couldn't find application (%s) for resolution", aid ? aid : "(missing)");
295 throw ConfigurationException("Unable to locate application for resolution, deleted?");
299 DDFJanitor jout(ret);
303 DDF tlist = in["tokens"];
304 DDF token = tlist.first();
305 while (token.isstring()) {
306 // Parse and bind the document into an XMLObject.
307 istringstream instr(token.string());
308 DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(instr);
309 XercesJanitor<DOMDocument> janitor(doc);
310 XMLObject* xmlObject = XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true);
311 t.tokens.push_back(xmlObject);
313 token = tlist.next();
316 DDF alist = in["attributes"];
317 Attribute* attribute;
318 DDF attr = alist.first();
319 while (!attr.isnull()) {
320 attribute = Attribute::unmarshall(attr);
321 t.inputAttrs.push_back(attribute);
322 if (log.isDebugEnabled())
323 log.debug("unmarshalled attribute (ID: %s) with %d value%s",
324 attribute->getId(), attr.first().integer(), attr.first().integer()!=1 ? "s" : "");
328 resolve(*app, in["issuer"].string(), t.tokens, t.inputAttrs, t.resolvedAttrs);
330 if (!t.resolvedAttrs.empty()) {
332 for (vector<Attribute*>::const_iterator a = t.resolvedAttrs.begin(); a != t.resolvedAttrs.end(); ++a) {
333 attr = (*a)->marshall();
341 void RemotedResolver::resolve(
342 const Application& app,
344 const vector<const XMLObject*>& tokens,
345 const vector<Attribute*>& inputAttrs,
346 vector <Attribute*>& resolvedAttrs
350 Category& log = Category::getInstance(SHIBRESOLVER_LOGCAT);
351 pair<const EntityDescriptor*,const RoleDescriptor*> entity = make_pair((EntityDescriptor*)NULL, (RoleDescriptor*)NULL);
352 MetadataProvider* m = app.getMetadataProvider(false);
355 log.warn("no metadata providers are configured");
357 else if (issuer && *issuer) {
358 // Lookup metadata for the issuer.
359 MetadataProviderCriteria mc(app, issuer, &IDPSSODescriptor::ELEMENT_QNAME, samlconstants::SAML20P_NS);
360 entity = m->getEntityDescriptor(mc);
362 log.warn("unable to locate metadata for provider (%s)", issuer);
364 else if (!entity.second) {
365 log.warn("unable to locate SAML 2.0 identity provider role for provider (%s)", issuer);
369 vector<const Assertion*> assertions;
371 AttributeExtractor* extractor = app.getAttributeExtractor();
373 Locker extlocker(extractor);
375 pair<bool,const char*> mprefix = app.getString("metadataAttributePrefix");
377 log.debug("extracting metadata-derived attributes...");
379 // We pass NULL for "issuer" because the IdP isn't the one asserting metadata-based attributes.
380 extractor->extractAttributes(app, NULL, *entity.second, resolvedAttrs);
381 for (vector<Attribute*>::iterator a = resolvedAttrs.begin(); a != resolvedAttrs.end(); ++a) {
382 vector<string>& ids = (*a)->getAliases();
383 for (vector<string>::iterator id = ids.begin(); id != ids.end(); ++id)
384 *id = mprefix.second + *id;
387 catch (exception& ex) {
388 log.error("caught exception extracting attributes: %s", ex.what());
392 log.debug("extracting pushed attributes...");
393 for (vector<const XMLObject*>::const_iterator t = tokens.begin(); t!=tokens.end(); ++t) {
395 // Save off any assertions for later use by resolver.
396 const Assertion* assertion = dynamic_cast<const Assertion*>(*t);
398 assertions.push_back(assertion);
399 extractor->extractAttributes(app, entity.second, *(*t), resolvedAttrs);
401 catch (exception& ex) {
402 log.error("caught exception extracting attributes: %s", ex.what());
406 AttributeFilter* filter = app.getAttributeFilter();
407 if (filter && !resolvedAttrs.empty()) {
408 BasicFilteringContext fc(app, resolvedAttrs, entity.second);
409 Locker filtlocker(filter);
411 filter->filterAttributes(fc, resolvedAttrs);
413 catch (exception& ex) {
414 log.error("caught exception filtering attributes: %s", ex.what());
415 log.error("dumping extracted attributes due to filtering exception");
416 for_each(resolvedAttrs.begin(), resolvedAttrs.end(), xmltooling::cleanup<shibsp::Attribute>());
417 resolvedAttrs.clear();
422 log.warn("no AttributeExtractor plugin installed, check log during startup");
426 AttributeResolver* resolver = app.getAttributeResolver();
428 log.debug("resolving attributes...");
430 vector<Attribute*> inputs = inputAttrs;
431 inputs.insert(inputs.end(), resolvedAttrs.begin(), resolvedAttrs.end());
433 Locker locker(resolver);
434 auto_ptr<ResolutionContext> ctx(
435 resolver->createResolutionContext(
438 samlconstants::SAML20P_NS,
446 resolver->resolveAttributes(*ctx.get());
447 if (!ctx->getResolvedAttributes().empty())
448 resolvedAttrs.insert(resolvedAttrs.end(), ctx->getResolvedAttributes().begin(), ctx->getResolvedAttributes().end());
451 catch (exception& ex) {
452 log.error("attribute resolution failed: %s", ex.what());
455 throw ConfigurationException("Cannot process request using lite version of shibsp library.");
459 bool ShibbolethResolver::init(unsigned long features, const char* config, bool rethrow)
461 if (features && SPConfig::OutOfProcess) {
463 features = features | SPConfig::AttributeResolution | SPConfig::Metadata | SPConfig::Trust | SPConfig::Credentials;
465 if (!(features && SPConfig::InProcess))
466 features |= SPConfig::Listener;
468 else if (features && SPConfig::InProcess) {
469 features |= SPConfig::Listener;
471 SPConfig::getConfig().setFeatures(features);
472 if (!SPConfig::getConfig().init())
474 if (!SPConfig::getConfig().instantiate(config, rethrow))
480 * Shuts down runtime.
482 * Each process using the library SHOULD call this function exactly once before terminating itself.
484 void ShibbolethResolver::term()
486 SPConfig::getConfig().term();
490 extern "C" int SHIBRESOLVER_EXPORTS xmltooling_extension_init(void*)
492 #ifdef SHIBRESOLVER_SHIBSP_HAS_REMOTING
493 SPConfig& conf = SPConfig::getConfig();
494 if (conf.isEnabled(SPConfig::OutOfProcess) && !conf.isEnabled(SPConfig::InProcess) && conf.isEnabled(SPConfig::Listener))
495 conf.getServiceProvider()->regListener("org.project-moonshot.shibresolver", &g_Remoted);
497 return 0; // signal success
500 extern "C" void SHIBRESOLVER_EXPORTS xmltooling_extension_term()
502 // Factories normally get unregistered during library shutdown, so no work usually required here.