Avoid use of nullptr.
[shibboleth/cpp-sp-resolver.git] / shibresolver / resolver.cpp
1 /*
2  *  Copyright 2010 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 #include <shibsp/exceptions.h>
26 #include <shibsp/Application.h>
27 #include <shibsp/SPRequest.h>
28 #include <shibsp/ServiceProvider.h>
29 #include <shibsp/attribute/Attribute.h>
30 #include <shibsp/remoting/ListenerService.h>
31 #ifndef SHIBSP_LITE
32 # include <saml/saml2/metadata/Metadata.h>
33 # include <saml/saml2/metadata/MetadataProvider.h>
34 # include <saml/util/SAMLConstants.h>
35 # include <shibsp/attribute/filtering/AttributeFilter.h>
36 # include <shibsp/attribute/filtering/BasicFilteringContext.h>
37 # include <shibsp/attribute/resolver/AttributeExtractor.h>
38 # include <shibsp/attribute/resolver/AttributeResolver.h>
39 # include <shibsp/attribute/resolver/ResolutionContext.h>
40 # include <shibsp/metadata/MetadataProviderCriteria.h>
41 #endif
42 #include <xmltooling/XMLObjectBuilder.h>
43 #include <xmltooling/XMLToolingConfig.h>
44 #include <xmltooling/util/ParserPool.h>
45 #include <xmltooling/util/XMLHelper.h>
46
47 using namespace shibresolver;
48 using namespace shibsp;
49 #ifndef SHIBSP_LITE
50 using namespace opensaml;
51 using namespace opensaml::saml2md;
52 #endif
53 using namespace xmltooling;
54 using namespace std;
55
56 namespace shibresolver {
57     class SHIBRESOLVER_DLLLOCAL RemotedResolver : public Remoted {
58     public:
59         RemotedResolver() {}
60         ~RemotedResolver() {}
61
62         struct Transaction {
63             ~Transaction() {
64                 for_each(tokens.begin(), tokens.end(), xmltooling::cleanup<XMLObject>());
65                 for_each(inputAttrs.begin(), inputAttrs.end(), xmltooling::cleanup<Attribute>());
66                 for_each(resolvedAttrs.begin(), resolvedAttrs.end(), xmltooling::cleanup<Attribute>());
67             }
68
69             vector<const XMLObject*> tokens;
70             vector<Attribute*> inputAttrs;
71             vector<Attribute*> resolvedAttrs;
72         };
73
74         void receive(DDF& in, ostream& out);
75         void resolve(
76             const Application& app,
77             const char* issuer,
78             const vector<const XMLObject*>& tokens,
79             const vector<Attribute*>& inputAttrs,
80             vector <Attribute*>& resolvedAttrs
81             ) const;
82     };
83
84     static RemotedResolver g_Remoted;
85 };
86
87 ShibbolethResolver* ShibbolethResolver::create()
88 {
89     return new ShibbolethResolver();
90 }
91
92 ShibbolethResolver::ShibbolethResolver() : m_request(NULL), m_sp(NULL)
93 {
94 }
95
96 ShibbolethResolver::~ShibbolethResolver()
97 {
98     for_each(m_resolvedAttributes.begin(), m_resolvedAttributes.end(), xmltooling::cleanup<Attribute>());
99     if (m_sp)
100         m_sp->unlock();
101 }
102
103 void ShibbolethResolver::setRequest(const SPRequest* request)
104 {
105     m_request = request;
106 }
107
108 void ShibbolethResolver::setApplicationID(const char* appID)
109 {
110     m_appID.erase();
111     if (appID)
112         m_appID = appID;
113 }
114
115 void ShibbolethResolver::setIssuer(const char* issuer)
116 {
117     m_issuer.erase();
118     if (issuer)
119         m_issuer = issuer;
120 }
121
122 void ShibbolethResolver::addToken(const XMLObject* token)
123 {
124     if (token)
125         m_tokens.push_back(token);
126 }
127
128 void ShibbolethResolver::addAttribute(Attribute* attr)
129 {
130     if (attr)
131         m_inputAttributes.push_back(attr);
132 }
133
134 vector<Attribute*>& ShibbolethResolver::getResolvedAttributes()
135 {
136     return m_resolvedAttributes;
137 }
138
139 RequestMapper::Settings ShibbolethResolver::getSettings() const
140 {
141     if (!m_request)
142         throw ConfigurationException("Request settings not available without supplying SPRequest instance.");
143     return m_request->getRequestSettings();
144 }
145
146 void ShibbolethResolver::resolve()
147 {
148     Category& log = Category::getInstance(SHIBRESOLVER_LOGCAT);
149     SPConfig& conf = SPConfig::getConfig();
150     if (!m_request) {
151         m_sp = conf.getServiceProvider();
152         m_sp->lock();
153         if (m_appID.empty())
154             m_appID = "default";
155     }
156
157     const Application* app = m_request ? &(m_request->getApplication()) : m_sp->getApplication(m_appID.c_str());
158     if (!app)
159         throw ConfigurationException("Unable to locate application for resolution.");
160
161     if (conf.isEnabled(SPConfig::OutOfProcess)) {
162         g_Remoted.resolve(
163             *app,
164             m_issuer.c_str(),
165             m_tokens,
166             m_inputAttributes,
167             m_resolvedAttributes
168             );
169     }
170     else {
171         // When not out of process, we remote all the message processing.
172         DDF out,in = DDF("org.project-moonshot.shibresolver");
173         DDFJanitor jin(in), jout(out);
174         in.addmember("application_id").string(app->getId());
175         if (!m_issuer.empty())
176             in.addmember("issuer").string(m_issuer.c_str());
177         if (!m_tokens.empty()) {
178             DDF& tokens = in.addmember("tokens").list();
179             for (vector<const XMLObject*>::const_iterator t = m_tokens.begin(); t != m_tokens.end(); ++t) {
180                 ostringstream os;
181                 os << *(*t);
182                 tokens.add(DDF(NULL).string(os.str().c_str()));
183             }
184         }
185         if (!m_inputAttributes.empty()) {
186             DDF attr;
187             DDF& attrs = in.addmember("attributes").list();
188             for (vector<Attribute*>::const_iterator a = m_inputAttributes.begin(); a != m_inputAttributes.end(); ++a) {
189                 attr = (*a)->marshall();
190                 attrs.add(attr);
191             }
192         }
193
194         out = (m_request ? m_request->getServiceProvider() : (*m_sp)).getListenerService()->send(in);
195
196         Attribute* attribute;
197         DDF attr = out.first();
198         while (!attr.isnull()) {
199             try {
200                 attribute = Attribute::unmarshall(attr);
201                 m_resolvedAttributes.push_back(attribute);
202                 if (log.isDebugEnabled())
203                     log.debug("unmarshalled attribute (ID: %s) with %d value%s",
204                         attribute->getId(), attr.first().integer(), attr.first().integer()!=1 ? "s" : "");
205             }
206             catch (AttributeException& ex) {
207                 const char* id = attr.first().name();
208                 log.error("error unmarshalling attribute (ID: %s): %s", id ? id : "none", ex.what());
209             }
210             attr = out.next();
211         }
212     }
213 }
214
215 void RemotedResolver::receive(DDF& in, ostream& out)
216 {
217     Category& log = Category::getInstance(SHIBRESOLVER_LOGCAT);
218
219     // Find application.
220     const char* aid = in["application_id"].string();
221     const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : NULL;
222     if (!app) {
223         // Something's horribly wrong.
224         log.error("couldn't find application (%s) for resolution", aid ? aid : "(missing)");
225         throw ConfigurationException("Unable to locate application for resolution, deleted?");
226     }
227
228     DDF ret(NULL);
229     DDFJanitor jout(ret);
230
231     Transaction t;
232
233     DDF tlist = in["tokens"];
234     DDF token = tlist.first();
235     while (token.isstring()) {
236         // Parse and bind the document into an XMLObject.
237         istringstream instr(token.string());
238         DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(instr);
239         XercesJanitor<DOMDocument> janitor(doc);
240         XMLObject* xmlObject = XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true);
241         t.tokens.push_back(xmlObject);
242         janitor.release();
243         token = tlist.next();
244     }
245
246     DDF alist = in["attributes"];
247     Attribute* attribute;
248     DDF attr = alist.first();
249     while (!attr.isnull()) {
250         attribute = Attribute::unmarshall(attr);
251         t.inputAttrs.push_back(attribute);
252         if (log.isDebugEnabled())
253             log.debug("unmarshalled attribute (ID: %s) with %d value%s",
254                 attribute->getId(), attr.first().integer(), attr.first().integer()!=1 ? "s" : "");
255         attr = alist.next();
256     }
257
258     resolve(*app, in["issuer"].string(), t.tokens, t.inputAttrs, t.resolvedAttrs);
259
260     if (!t.resolvedAttrs.empty()) {
261         ret.list();
262         for (vector<Attribute*>::const_iterator a = t.resolvedAttrs.begin(); a != t.resolvedAttrs.end(); ++a) {
263             attr = (*a)->marshall();
264             ret.add(attr);
265         }
266     }
267
268     out << ret;
269 }
270
271 void RemotedResolver::resolve(
272     const Application& app,
273     const char* issuer,
274     const vector<const XMLObject*>& tokens,
275     const vector<Attribute*>& inputAttrs,
276     vector <Attribute*>& resolvedAttrs
277     ) const
278 {
279 #ifndef SHIBSP_LITE
280     Category& log = Category::getInstance(SHIBRESOLVER_LOGCAT);
281     pair<const EntityDescriptor*,const RoleDescriptor*> entity = pair<const EntityDescriptor*,const RoleDescriptor*>(NULL,NULL);
282     MetadataProvider* m = app.getMetadataProvider();
283     Locker locker(m);
284     if (issuer && *issuer) {
285         // Lookup metadata for the issuer.
286         MetadataProviderCriteria mc(app, issuer, &IDPSSODescriptor::ELEMENT_QNAME, samlconstants::SAML20P_NS);
287         entity = m->getEntityDescriptor(mc);
288         if (!entity.first) {
289             log.warn("unable to locate metadata for provider (%s)", issuer);
290         }
291         else if (!entity.second) {
292             log.warn("unable to locate SAML 2.0 identity provider role for provider (%s)", issuer);
293         }
294     }
295
296     vector<const Assertion*> assertions;
297
298     AttributeExtractor* extractor = app.getAttributeExtractor();
299     if (extractor) {
300         Locker extlocker(extractor);
301         if (entity.second) {
302             pair<bool,const char*> mprefix = app.getString("metadataAttributePrefix");
303             if (mprefix.first) {
304                 log.debug("extracting metadata-derived attributes...");
305                 try {
306                     // We pass NULL for "issuer" because the IdP isn't the one asserting metadata-based attributes.
307                     extractor->extractAttributes(app, NULL, *entity.second, resolvedAttrs);
308                     for (vector<Attribute*>::iterator a = resolvedAttrs.begin(); a != resolvedAttrs.end(); ++a) {
309                         vector<string>& ids = (*a)->getAliases();
310                         for (vector<string>::iterator id = ids.begin(); id != ids.end(); ++id)
311                             *id = mprefix.second + *id;
312                     }
313                 }
314                 catch (exception& ex) {
315                     log.error("caught exception extracting attributes: %s", ex.what());
316                 }
317             }
318         }
319         log.debug("extracting pushed attributes...");
320         for (vector<const XMLObject*>::const_iterator t = tokens.begin(); t!=tokens.end(); ++t) {
321             try {
322                 // Save off any assertions for later use by resolver.
323                 const Assertion* assertion = dynamic_cast<const Assertion*>(*t);
324                 if (assertion)
325                     assertions.push_back(assertion);
326                 extractor->extractAttributes(app, entity.second, *(*t), resolvedAttrs);
327             }
328             catch (exception& ex) {
329                 log.error("caught exception extracting attributes: %s", ex.what());
330             }
331         }
332
333         AttributeFilter* filter = app.getAttributeFilter();
334         if (filter && !resolvedAttrs.empty()) {
335             BasicFilteringContext fc(app, resolvedAttrs, entity.second);
336             Locker filtlocker(filter);
337             try {
338                 filter->filterAttributes(fc, resolvedAttrs);
339             }
340             catch (exception& ex) {
341                 log.error("caught exception filtering attributes: %s", ex.what());
342                 log.error("dumping extracted attributes due to filtering exception");
343                 for_each(resolvedAttrs.begin(), resolvedAttrs.end(), xmltooling::cleanup<shibsp::Attribute>());
344                 resolvedAttrs.clear();
345             }
346         }
347     }
348     else {
349         log.warn("no AttributeExtractor plugin installed, check log during startup");
350     }
351
352     try {
353         AttributeResolver* resolver = app.getAttributeResolver();
354         if (resolver) {
355             log.debug("resolving attributes...");
356
357             vector<Attribute*> inputs = inputAttrs;
358             inputs.insert(inputs.end(), resolvedAttrs.begin(), resolvedAttrs.end());
359
360             Locker locker(resolver);
361             auto_ptr<ResolutionContext> ctx(
362                 resolver->createResolutionContext(
363                     app,
364                     entity.first,
365                     samlconstants::SAML20P_NS,
366                     NULL,
367                     NULL,
368                     NULL,
369                     &assertions,
370                     &inputs
371                     )
372                 );
373             resolver->resolveAttributes(*ctx.get());
374             if (!ctx->getResolvedAttributes().empty())
375                 resolvedAttrs.insert(resolvedAttrs.end(), ctx->getResolvedAttributes().begin(), ctx->getResolvedAttributes().end());
376         }
377     }
378     catch (exception& ex) {
379         log.error("attribute resolution failed: %s", ex.what());
380     }
381 #else
382     throw ConfigurationException("Cannot process request using lite version of shibsp library.");
383 #endif
384 }
385
386 bool ShibbolethResolver::init(unsigned long features, const char* config, bool rethrow)
387 {
388     if (features && SPConfig::OutOfProcess) {
389 #ifndef SHIBSP_LITE
390         features = features | SPConfig::AttributeResolution | SPConfig::Metadata | SPConfig::Trust | SPConfig::Credentials;
391 #endif
392         if (!(features && SPConfig::InProcess))
393             features |= SPConfig::Listener;
394     }
395     else if (features && SPConfig::InProcess) {
396         features |= SPConfig::Listener;
397     }
398     SPConfig::getConfig().setFeatures(features);
399     if (!SPConfig::getConfig().init())
400         return false;
401     if (!SPConfig::getConfig().instantiate(config, rethrow))
402         return false;
403     return true;
404 }
405
406 /**
407     * Shuts down runtime.
408     *
409     * Each process using the library SHOULD call this function exactly once before terminating itself.
410     */
411 void ShibbolethResolver::term()
412 {
413     SPConfig::getConfig().term();
414 }
415
416
417 extern "C" int SHIBRESOLVER_EXPORTS xmltooling_extension_init(void*)
418 {
419 #ifdef SHIBRESOLVER_SHIBSP_HAS_REMOTING
420     SPConfig& conf = SPConfig::getConfig();
421     if (conf.isEnabled(SPConfig::OutOfProcess) && !conf.isEnabled(SPConfig::InProcess) && conf.isEnabled(SPConfig::Listener))
422         conf.getServiceProvider()->regListener("org.project-moonshot.shibresolver", &g_Remoted);
423 #endif
424     return 0;   // signal success
425 }
426
427 extern "C" void SHIBRESOLVER_EXPORTS xmltooling_extension_term()
428 {
429     // Factories normally get unregistered during library shutdown, so no work usually required here.
430 }