Add composite name support (for when it shows up in libraries).
[shibboleth/cpp-sp-resolver.git] / src / shibresolver / resolver.cpp
1 /*
2  *  Copyright 2010-2011 JANET(UK)
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * resolver.cpp
19  *
20  * An embeddable component interface to Shibboleth SP attribute processing.
21  */
22
23 #include "internal.h"
24
25 #ifdef SHIBRESOLVER_HAVE_GSSAPI_COMPOSITE_NAME
26 # include <gssapi/gssapi_ext.h>
27 #endif
28
29 #include <shibsp/exceptions.h>
30 #include <shibsp/Application.h>
31 #include <shibsp/GSSRequest.h>
32 #include <shibsp/SPRequest.h>
33 #include <shibsp/ServiceProvider.h>
34 #include <shibsp/attribute/Attribute.h>
35 #include <shibsp/remoting/ListenerService.h>
36 #ifndef SHIBSP_LITE
37 # include <saml/saml2/metadata/Metadata.h>
38 # include <saml/saml2/metadata/MetadataProvider.h>
39 # include <saml/util/SAMLConstants.h>
40 # include <shibsp/attribute/filtering/AttributeFilter.h>
41 # include <shibsp/attribute/filtering/BasicFilteringContext.h>
42 # include <shibsp/attribute/resolver/AttributeExtractor.h>
43 # include <shibsp/attribute/resolver/AttributeResolver.h>
44 # include <shibsp/attribute/resolver/ResolutionContext.h>
45 # include <shibsp/metadata/MetadataProviderCriteria.h>
46 #endif
47 #include <xmltooling/XMLObjectBuilder.h>
48 #include <xmltooling/XMLToolingConfig.h>
49 #include <xmltooling/impl/AnyElement.h>
50 #include <xmltooling/util/ParserPool.h>
51 #include <xmltooling/util/XMLHelper.h>
52 #include <xercesc/util/Base64.hpp>
53
54 using namespace shibresolver;
55 using namespace shibsp;
56 #ifndef SHIBSP_LITE
57 using namespace opensaml;
58 using namespace opensaml::saml2md;
59 #endif
60 using namespace xmltooling;
61 using namespace std;
62
63 namespace shibresolver {
64     class SHIBRESOLVER_DLLLOCAL RemotedResolver : public Remoted {
65     public:
66         RemotedResolver() {}
67         ~RemotedResolver() {}
68
69         struct Transaction {
70             ~Transaction() {
71                 for_each(tokens.begin(), tokens.end(), xmltooling::cleanup<XMLObject>());
72                 for_each(inputAttrs.begin(), inputAttrs.end(), xmltooling::cleanup<Attribute>());
73                 for_each(resolvedAttrs.begin(), resolvedAttrs.end(), xmltooling::cleanup<Attribute>());
74             }
75
76             vector<const XMLObject*> tokens;
77             vector<Attribute*> inputAttrs;
78             vector<Attribute*> resolvedAttrs;
79         };
80
81         void receive(DDF& in, ostream& out);
82         void resolve(
83             const Application& app,
84             const char* issuer,
85             const vector<const XMLObject*>& tokens,
86             const vector<Attribute*>& inputAttrs,
87             vector <Attribute*>& resolvedAttrs
88             ) const;
89     };
90
91     static RemotedResolver g_Remoted;
92 };
93
94 ShibbolethResolver* ShibbolethResolver::create()
95 {
96     return new ShibbolethResolver();
97 }
98
99 ShibbolethResolver::ShibbolethResolver() : m_request(NULL), m_sp(NULL)
100 #ifdef SHIBRESOLVER_HAVE_GSSAPI
101         ,m_gsswrapper(NULL)
102 #endif
103 {
104 }
105
106 ShibbolethResolver::~ShibbolethResolver()
107 {
108 #ifdef SHIBRESOLVER_HAVE_GSSAPI
109     delete m_gsswrapper;
110 #endif
111     for_each(m_resolvedAttributes.begin(), m_resolvedAttributes.end(), xmltooling::cleanup<Attribute>());
112     if (m_sp)
113         m_sp->unlock();
114 }
115
116 void ShibbolethResolver::setRequest(const SPRequest* request)
117 {
118     m_request = request;
119 #if defined(SHIBSP_HAVE_GSSAPI) && defined (SHIBRESOLVER_HAVE_GSSAPI)
120     if (request) {
121         const GSSRequest* gss = dynamic_cast<const GSSRequest*>(request);
122         if (gss) {
123             // TODO: fix API to prevent destruction of contexts
124             gss_ctx_id_t ctx = gss->getGSSContext();
125             addToken(&ctx);
126         }
127     }
128 #endif
129 }
130
131 void ShibbolethResolver::setApplicationID(const char* appID)
132 {
133     m_appID.erase();
134     if (appID)
135         m_appID = appID;
136 }
137
138 void ShibbolethResolver::setIssuer(const char* issuer)
139 {
140     m_issuer.erase();
141     if (issuer)
142         m_issuer = issuer;
143 }
144
145 void ShibbolethResolver::addToken(const XMLObject* token)
146 {
147     if (token)
148         m_tokens.push_back(token);
149 }
150
151 #ifdef SHIBRESOLVER_HAVE_GSSAPI
152 void ShibbolethResolver::addToken(gss_ctx_id_t* ctx)
153 {
154     if (m_gsswrapper) {
155         delete m_gsswrapper;
156         m_gsswrapper = NULL;
157     }
158
159     if (ctx && *ctx != GSS_C_NO_CONTEXT) {
160         OM_uint32 major, minor;
161         gss_buffer_desc contextbuf = GSS_C_EMPTY_BUFFER;
162
163         major = gss_export_sec_context(&minor, ctx, &contextbuf);
164         if (major == GSS_S_COMPLETE) {
165             addToken(&contextbuf);
166             gss_release_buffer(&minor, &contextbuf);
167         }
168         else {
169             Category::getInstance(SHIBRESOLVER_LOGCAT).error("error exporting GSS context");
170         }
171     }
172 }
173
174 void ShibbolethResolver::addToken(const gss_buffer_t contextbuf)
175 {
176     if (m_gsswrapper) {
177         delete m_gsswrapper;
178         m_gsswrapper = NULL;
179     }
180
181     xsecsize_t len=0;
182     XMLByte* out=Base64::encode(reinterpret_cast<const XMLByte*>(contextbuf->value), contextbuf->length, &len);
183     if (out) {
184         string s;
185         s.append(reinterpret_cast<char*>(out), len);
186         auto_ptr_XMLCh temp(s.c_str());
187 #ifdef SHIBSP_XERCESC_HAS_XMLBYTE_RELEASE
188         XMLString::release(&out);
189 #else
190         XMLString::release((char**)&out);
191 #endif
192         static const XMLCh _GSSAPI[] = UNICODE_LITERAL_13(G,S,S,A,P,I,C,o,n,t,e,x,t);
193         m_gsswrapper = new AnyElementImpl(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _GSSAPI);
194         m_gsswrapper->setTextContent(temp.get());
195     }
196     else {
197         Category::getInstance(SHIBRESOLVER_LOGCAT).error("error while base64-encoding GSS context");
198     }
199 }
200
201 #ifdef SHIBRESOLVER_HAVE_GSSAPI_COMPOSITE_NAME
202 void ShibbolethResolver::addToken(gss_name_t name)
203 {
204     if (m_gsswrapper) {
205         delete m_gsswrapper;
206         m_gsswrapper = NULL;
207     }
208
209     OM_uint32 major, minor;
210     gss_buffer_desc namebuf = GSS_C_EMPTY_BUFFER;
211
212     major = gss_export_name_composite(&minor, name, &namebuf);
213     if (major == GSS_S_COMPLETE) {
214         xsecsize_t len=0;
215         XMLByte* out=Base64::encode(reinterpret_cast<const XMLByte*>(namebuf.value), namebuf.length, &len);
216         if (out) {
217             string s;
218             s.append(reinterpret_cast<char*>(out), len);
219             auto_ptr_XMLCh temp(s.c_str());
220     #ifdef SHIBSP_XERCESC_HAS_XMLBYTE_RELEASE
221             XMLString::release(&out);
222     #else
223             XMLString::release((char**)&out);
224     #endif
225             static const XMLCh _GSSAPI[] = UNICODE_LITERAL_10(G,S,S,A,P,I,N,a,m,e);
226             m_gsswrapper = new AnyElementImpl(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _GSSAPI);
227             m_gsswrapper->setTextContent(temp.get());
228         }
229         else {
230             Category::getInstance(SHIBRESOLVER_LOGCAT).error("error while base64-encoding GSS name");
231         }
232         gss_release_buffer(&minor, &namebuf);
233     }
234     else {
235         Category::getInstance(SHIBRESOLVER_LOGCAT).error("error exporting GSS name");
236     }
237 }
238 #endif
239 #endif
240
241 void ShibbolethResolver::addAttribute(Attribute* attr)
242 {
243     if (attr)
244         m_inputAttributes.push_back(attr);
245 }
246
247 vector<Attribute*>& ShibbolethResolver::getResolvedAttributes()
248 {
249     return m_resolvedAttributes;
250 }
251
252 RequestMapper::Settings ShibbolethResolver::getSettings() const
253 {
254     if (!m_request)
255         throw ConfigurationException("Request settings not available without supplying SPRequest instance.");
256     return m_request->getRequestSettings();
257 }
258
259 void ShibbolethResolver::resolve()
260 {
261     Category& log = Category::getInstance(SHIBRESOLVER_LOGCAT);
262     SPConfig& conf = SPConfig::getConfig();
263     if (!m_request) {
264         m_sp = conf.getServiceProvider();
265         m_sp->lock();
266         if (m_appID.empty())
267             m_appID = "default";
268     }
269
270     const Application* app = m_request ? &(m_request->getApplication()) : m_sp->getApplication(m_appID.c_str());
271     if (!app)
272         throw ConfigurationException("Unable to locate application for resolution.");
273
274 #ifdef SHIBRESOLVER_HAVE_GSSAPI
275     if (m_gsswrapper)
276         m_tokens.push_back(m_gsswrapper);
277 #endif
278
279     if (conf.isEnabled(SPConfig::OutOfProcess)) {
280         g_Remoted.resolve(
281             *app,
282             m_issuer.c_str(),
283             m_tokens,
284             m_inputAttributes,
285             m_resolvedAttributes
286             );
287     }
288     else {
289         // When not out of process, we remote all the message processing.
290         DDF out,in = DDF("org.project-moonshot.shibresolver");
291         DDFJanitor jin(in), jout(out);
292         in.addmember("application_id").string(app->getId());
293         if (!m_issuer.empty())
294             in.addmember("issuer").string(m_issuer.c_str());
295         if (!m_tokens.empty()) {
296             DDF& tokens = in.addmember("tokens").list();
297             for (vector<const XMLObject*>::const_iterator t = m_tokens.begin(); t != m_tokens.end(); ++t) {
298                 ostringstream os;
299                 os << *(*t);
300                 tokens.add(DDF(NULL).string(os.str().c_str()));
301             }
302         }
303         if (!m_inputAttributes.empty()) {
304             DDF attr;
305             DDF& attrs = in.addmember("attributes").list();
306             for (vector<Attribute*>::const_iterator a = m_inputAttributes.begin(); a != m_inputAttributes.end(); ++a) {
307                 attr = (*a)->marshall();
308                 attrs.add(attr);
309             }
310         }
311
312         out = (m_request ? m_request->getServiceProvider() : (*m_sp)).getListenerService()->send(in);
313
314         Attribute* attribute;
315         DDF attr = out.first();
316         while (!attr.isnull()) {
317             try {
318                 attribute = Attribute::unmarshall(attr);
319                 m_resolvedAttributes.push_back(attribute);
320                 if (log.isDebugEnabled())
321                     log.debug("unmarshalled attribute (ID: %s) with %d value%s",
322                         attribute->getId(), attr.first().integer(), attr.first().integer()!=1 ? "s" : "");
323             }
324             catch (AttributeException& ex) {
325                 const char* id = attr.first().name();
326                 log.error("error unmarshalling attribute (ID: %s): %s", id ? id : "none", ex.what());
327             }
328             attr = out.next();
329         }
330     }
331 }
332
333 void RemotedResolver::receive(DDF& in, ostream& out)
334 {
335     Category& log = Category::getInstance(SHIBRESOLVER_LOGCAT);
336
337     // Find application.
338     const char* aid = in["application_id"].string();
339     const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : NULL;
340     if (!app) {
341         // Something's horribly wrong.
342         log.error("couldn't find application (%s) for resolution", aid ? aid : "(missing)");
343         throw ConfigurationException("Unable to locate application for resolution, deleted?");
344     }
345
346     DDF ret(NULL);
347     DDFJanitor jout(ret);
348
349     Transaction t;
350
351     DDF tlist = in["tokens"];
352     DDF token = tlist.first();
353     while (token.isstring()) {
354         // Parse and bind the document into an XMLObject.
355         istringstream instr(token.string());
356         DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(instr);
357         XercesJanitor<DOMDocument> janitor(doc);
358         XMLObject* xmlObject = XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true);
359         t.tokens.push_back(xmlObject);
360         janitor.release();
361         token = tlist.next();
362     }
363
364     DDF alist = in["attributes"];
365     Attribute* attribute;
366     DDF attr = alist.first();
367     while (!attr.isnull()) {
368         attribute = Attribute::unmarshall(attr);
369         t.inputAttrs.push_back(attribute);
370         if (log.isDebugEnabled())
371             log.debug("unmarshalled attribute (ID: %s) with %d value%s",
372                 attribute->getId(), attr.first().integer(), attr.first().integer()!=1 ? "s" : "");
373         attr = alist.next();
374     }
375
376     resolve(*app, in["issuer"].string(), t.tokens, t.inputAttrs, t.resolvedAttrs);
377
378     if (!t.resolvedAttrs.empty()) {
379         ret.list();
380         for (vector<Attribute*>::const_iterator a = t.resolvedAttrs.begin(); a != t.resolvedAttrs.end(); ++a) {
381             attr = (*a)->marshall();
382             ret.add(attr);
383         }
384     }
385
386     out << ret;
387 }
388
389 void RemotedResolver::resolve(
390     const Application& app,
391     const char* issuer,
392     const vector<const XMLObject*>& tokens,
393     const vector<Attribute*>& inputAttrs,
394     vector <Attribute*>& resolvedAttrs
395     ) const
396 {
397 #ifndef SHIBSP_LITE
398     Category& log = Category::getInstance(SHIBRESOLVER_LOGCAT);
399     string issuerstr(issuer ? issuer : "");
400     pair<const EntityDescriptor*,const RoleDescriptor*> entity = make_pair((EntityDescriptor*)NULL, (RoleDescriptor*)NULL);
401     MetadataProvider* m = app.getMetadataProvider(false);
402     Locker locker(m);
403     if (!m) {
404         log.warn("no metadata providers are configured");
405     }
406     else {
407         if (!issuerstr.empty()) {
408             // Attempt to locate an issuer based on input token.
409             for (vector<const XMLObject*>::const_iterator t = tokens.begin(); t!=tokens.end(); ++t) {
410                 const saml2::Assertion* assertion = dynamic_cast<const saml2::Assertion*>(*t);
411                 if (assertion && assertion->getIssuer()) {
412                     auto_ptr_char iss(assertion->getIssuer()->getName());
413                     if (iss.get() && *iss.get()) {
414                         issuerstr = iss.get();
415                         break;
416                     }
417                 }
418             }
419             if (!issuerstr.empty()) {
420                 log.info("setting issuer based on input token (%s)", issuerstr.c_str());
421             }
422         }
423
424         if (!issuerstr.empty()) {
425             // Lookup metadata for the issuer.
426             MetadataProviderCriteria idpmc(app, issuerstr.c_str(), &IDPSSODescriptor::ELEMENT_QNAME, samlconstants::SAML20P_NS);
427             entity = m->getEntityDescriptor(idpmc);
428             if (!entity.first) {
429                 log.warn("unable to locate metadata for provider (%s)", issuerstr.c_str());
430             }
431             else if (!entity.second) {
432                 MetadataProviderCriteria aamc(app, issuerstr.c_str(), &AttributeAuthorityDescriptor::ELEMENT_QNAME, samlconstants::SAML20P_NS);
433                 entity = m->getEntityDescriptor(aamc);
434                 if (!entity.second) {
435                     log.warn("unable to locate SAML 2.0 IdP or AA role for provider (%s)", issuerstr.c_str());
436                 }
437             }
438         }
439     }
440
441     vector<const Assertion*> assertions;
442
443     AttributeExtractor* extractor = app.getAttributeExtractor();
444     if (extractor) {
445         Locker extlocker(extractor);
446         if (entity.second) {
447             pair<bool,const char*> mprefix = app.getString("metadataAttributePrefix");
448             if (mprefix.first) {
449                 log.debug("extracting metadata-derived attributes...");
450                 try {
451                     // We pass NULL for "issuer" because the IdP isn't the one asserting metadata-based attributes.
452                     extractor->extractAttributes(app, NULL, *entity.second, resolvedAttrs);
453                     for (vector<Attribute*>::iterator a = resolvedAttrs.begin(); a != resolvedAttrs.end(); ++a) {
454                         vector<string>& ids = (*a)->getAliases();
455                         for (vector<string>::iterator id = ids.begin(); id != ids.end(); ++id)
456                             *id = mprefix.second + *id;
457                     }
458                 }
459                 catch (exception& ex) {
460                     log.error("caught exception extracting attributes: %s", ex.what());
461                 }
462             }
463         }
464         log.debug("extracting pushed attributes...");
465         for (vector<const XMLObject*>::const_iterator t = tokens.begin(); t!=tokens.end(); ++t) {
466             try {
467                 // Save off any assertions for later use by resolver.
468                 const Assertion* assertion = dynamic_cast<const Assertion*>(*t);
469                 if (assertion)
470                     assertions.push_back(assertion);
471                 extractor->extractAttributes(app, entity.second, *(*t), resolvedAttrs);
472             }
473             catch (exception& ex) {
474                 log.error("caught exception extracting attributes: %s", ex.what());
475             }
476         }
477
478         AttributeFilter* filter = app.getAttributeFilter();
479         if (filter && !resolvedAttrs.empty()) {
480             BasicFilteringContext fc(app, resolvedAttrs, entity.second);
481             Locker filtlocker(filter);
482             try {
483                 filter->filterAttributes(fc, resolvedAttrs);
484             }
485             catch (exception& ex) {
486                 log.error("caught exception filtering attributes: %s", ex.what());
487                 log.error("dumping extracted attributes due to filtering exception");
488                 for_each(resolvedAttrs.begin(), resolvedAttrs.end(), xmltooling::cleanup<shibsp::Attribute>());
489                 resolvedAttrs.clear();
490             }
491         }
492     }
493     else {
494         log.warn("no AttributeExtractor plugin installed, check log during startup");
495     }
496
497     try {
498         AttributeResolver* resolver = app.getAttributeResolver();
499         if (resolver) {
500             log.debug("resolving attributes...");
501
502             vector<Attribute*> inputs = inputAttrs;
503             inputs.insert(inputs.end(), resolvedAttrs.begin(), resolvedAttrs.end());
504
505             Locker locker(resolver);
506             auto_ptr<ResolutionContext> ctx(
507                 resolver->createResolutionContext(
508                     app,
509                     entity.first,
510                     samlconstants::SAML20P_NS,
511                     NULL,
512                     NULL,
513                     NULL,
514                     &assertions,
515                     &inputs
516                     )
517                 );
518             resolver->resolveAttributes(*ctx.get());
519             if (!ctx->getResolvedAttributes().empty())
520                 resolvedAttrs.insert(resolvedAttrs.end(), ctx->getResolvedAttributes().begin(), ctx->getResolvedAttributes().end());
521         }
522     }
523     catch (exception& ex) {
524         log.error("attribute resolution failed: %s", ex.what());
525     }
526 #else
527     throw ConfigurationException("Cannot process request using lite version of shibsp library.");
528 #endif
529 }
530
531 bool ShibbolethResolver::init(unsigned long features, const char* config, bool rethrow)
532 {
533     if (features && SPConfig::OutOfProcess) {
534 #ifndef SHIBSP_LITE
535         features = features | SPConfig::AttributeResolution | SPConfig::Metadata | SPConfig::Trust | SPConfig::Credentials;
536 #endif
537         if (!(features && SPConfig::InProcess))
538             features |= SPConfig::Listener;
539     }
540     else if (features && SPConfig::InProcess) {
541         features |= SPConfig::Listener;
542     }
543     SPConfig::getConfig().setFeatures(features);
544     if (!SPConfig::getConfig().init())
545         return false;
546     if (!SPConfig::getConfig().instantiate(config, rethrow))
547         return false;
548     return true;
549 }
550
551 /**
552     * Shuts down runtime.
553     *
554     * Each process using the library SHOULD call this function exactly once before terminating itself.
555     */
556 void ShibbolethResolver::term()
557 {
558     SPConfig::getConfig().term();
559 }
560
561
562 extern "C" int SHIBRESOLVER_EXPORTS xmltooling_extension_init(void*)
563 {
564 #ifdef SHIBRESOLVER_SHIBSP_HAS_REMOTING
565     SPConfig& conf = SPConfig::getConfig();
566     if (conf.isEnabled(SPConfig::OutOfProcess) && !conf.isEnabled(SPConfig::InProcess) && conf.isEnabled(SPConfig::Listener))
567         conf.getServiceProvider()->regListener("org.project-moonshot.shibresolver", &g_Remoted);
568 #endif
569     return 0;   // signal success
570 }
571
572 extern "C" void SHIBRESOLVER_EXPORTS xmltooling_extension_term()
573 {
574     // Factories normally get unregistered during library shutdown, so no work usually required here.
575 }