Add ability to generate additional metadata content based on config.
[shibboleth/sp.git] / shibsp / handler / impl / MetadataGenerator.cpp
1 /*
2  *  Copyright 2001-2010 Internet2
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  * MetadataGenerator.cpp
19  *
20  * Handler for generating "approximate" metadata based on SP configuration.
21  */
22
23 #include "internal.h"
24 #include "Application.h"
25 #include "exceptions.h"
26 #include "ServiceProvider.h"
27 #include "SPRequest.h"
28 #include "handler/AbstractHandler.h"
29 #include "handler/RemotedHandler.h"
30
31 #ifndef SHIBSP_LITE
32 # include "attribute/resolver/AttributeExtractor.h"
33 # include "metadata/MetadataProviderCriteria.h"
34 # include <saml/exceptions.h>
35 # include <saml/SAMLConfig.h>
36 # include <saml/signature/ContentReference.h>
37 # include <saml/saml2/metadata/Metadata.h>
38 # include <saml/saml2/metadata/MetadataProvider.h>
39 # include <xmltooling/XMLToolingConfig.h>
40 # include <xmltooling/security/Credential.h>
41 # include <xmltooling/security/CredentialCriteria.h>
42 # include <xmltooling/security/SecurityHelper.h>
43 # include <xmltooling/signature/Signature.h>
44 # include <xmltooling/util/ParserPool.h>
45 # include <xmltooling/util/PathResolver.h>
46 # include <xercesc/framework/LocalFileInputSource.hpp>
47 # include <xercesc/framework/Wrapper4InputSource.hpp>
48 #endif
49
50
51 using namespace shibsp;
52 #ifndef SHIBSP_LITE
53 using namespace opensaml::saml2md;
54 using namespace opensaml;
55 using namespace xmlsignature;
56 #endif
57 using namespace xmltooling;
58 using namespace std;
59
60 namespace shibsp {
61
62 #if defined (_MSC_VER)
63     #pragma warning( push )
64     #pragma warning( disable : 4250 )
65 #endif
66
67     class SHIBSP_DLLLOCAL Blocker : public DOMNodeFilter
68     {
69     public:
70 #ifdef SHIBSP_XERCESC_SHORT_ACCEPTNODE
71         short
72 #else
73         FilterAction
74 #endif
75         acceptNode(const DOMNode* node) const {
76             return FILTER_REJECT;
77         }
78     };
79
80     static SHIBSP_DLLLOCAL Blocker g_Blocker;
81
82     class SHIBSP_API MetadataGenerator : public AbstractHandler, public RemotedHandler
83     {
84     public:
85         MetadataGenerator(const DOMElement* e, const char* appId);
86         virtual ~MetadataGenerator() {
87 #ifndef SHIBSP_LITE
88             delete m_uiinfo;
89             delete m_org;
90             delete m_entityAttrs;
91             for_each(m_contacts.begin(), m_contacts.end(), xmltooling::cleanup<ContactPerson>());
92             for_each(m_reqAttrs.begin(), m_reqAttrs.end(), xmltooling::cleanup<RequestedAttribute>());
93             for_each(m_attrConsumers.begin(), m_attrConsumers.end(), xmltooling::cleanup<AttributeConsumingService>());
94 #endif
95         }
96
97         pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
98         void receive(DDF& in, ostream& out);
99
100     private:
101         pair<bool,long> processMessage(
102             const Application& application,
103             const char* handlerURL,
104             const char* entityID,
105             HTTPResponse& httpResponse
106             ) const;
107
108         set<string> m_acl;
109 #ifndef SHIBSP_LITE
110         string m_salt;
111         short m_http,m_https;
112         vector<string> m_bases;
113         UIInfo* m_uiinfo;
114         Organization* m_org;
115         EntityAttributes* m_entityAttrs;
116         vector<ContactPerson*> m_contacts;
117         vector<RequestedAttribute*> m_reqAttrs;
118         vector<AttributeConsumingService*> m_attrConsumers;
119 #endif
120     };
121
122 #if defined (_MSC_VER)
123     #pragma warning( pop )
124 #endif
125
126     Handler* SHIBSP_DLLLOCAL MetadataGeneratorFactory(const pair<const DOMElement*,const char*>& p)
127     {
128         return new MetadataGenerator(p.first, p.second);
129     }
130
131 };
132
133 MetadataGenerator::MetadataGenerator(const DOMElement* e, const char* appId)
134     : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".MetadataGenerator"), &g_Blocker)
135 #ifndef SHIBSP_LITE
136         ,m_http(0), m_https(0), m_uiinfo(nullptr), m_org(nullptr), m_entityAttrs(nullptr)
137 #endif
138 {
139     string address(appId);
140     address += getString("Location").second;
141     setAddress(address.c_str());
142     if (SPConfig::getConfig().isEnabled(SPConfig::InProcess)) {
143         pair<bool,const char*> acl = getString("acl");
144         if (acl.first) {
145             string aclbuf=acl.second;
146             int j = 0;
147             for (unsigned int i=0;  i < aclbuf.length();  i++) {
148                 if (aclbuf.at(i)==' ') {
149                     m_acl.insert(aclbuf.substr(j, i-j));
150                     j = i+1;
151                 }
152             }
153             m_acl.insert(aclbuf.substr(j, aclbuf.length()-j));
154         }
155     }
156
157 #ifndef SHIBSP_LITE
158     static XMLCh EndpointBase[] = UNICODE_LITERAL_12(E,n,d,p,o,i,n,t,B,a,s,e);
159
160     pair<bool,const char*> salt = getString("salt");
161     if (salt.first)
162         m_salt = salt.second;
163
164     pair<bool,bool> flag = getBool("http");
165     if (flag.first)
166         m_http = flag.second ? 1 : -1;
167     flag = getBool("https");
168     if (flag.first)
169         m_https = flag.second ? 1 : -1;
170
171     e = XMLHelper::getFirstChildElement(e);
172     while (e) {
173         if (XMLString::equals(e->getLocalName(), EndpointBase) && e->hasChildNodes()) {
174             auto_ptr_char base(e->getFirstChild()->getNodeValue());
175             if (base.get() && *base.get())
176                 m_bases.push_back(base.get());
177         }
178         else {
179             // Try and parse the object.
180             auto_ptr<XMLObject> child(XMLObjectBuilder::buildOneFromElement(const_cast<DOMElement*>(e)));
181             ContactPerson* cp = dynamic_cast<ContactPerson*>(child.get());
182             if (cp) {
183                 child.release();
184                 m_contacts.push_back(cp);
185             }
186             else {
187                 RequestedAttribute* req = dynamic_cast<RequestedAttribute*>(child.get());
188                 if (req) {
189                     child.release();
190                     m_reqAttrs.push_back(req);
191                 }
192                 else {
193                     AttributeConsumingService* acs = dynamic_cast<AttributeConsumingService*>(child.get());
194                     if (acs) {
195                         child.release();
196                         m_attrConsumers.push_back(acs);
197                     }
198                     else {
199                         UIInfo* info = dynamic_cast<UIInfo*>(child.get());
200                         if (info) {
201                             if (!m_uiinfo) {
202                                 child.release();
203                                 m_uiinfo = info;
204                             }
205                             else {
206                                 m_log.warn("skipping duplicate UIInfo element");
207                             }
208                         }
209                         else {
210                             Organization* org = dynamic_cast<Organization*>(child.get());
211                             if (org) {
212                                 if (!m_org) {
213                                     child.release();
214                                     m_org = org;
215                                 }
216                                 else {
217                                     m_log.warn("skipping duplicate Organization element");
218                                 }
219                             }
220                             else {
221                                 EntityAttributes* ea = dynamic_cast<EntityAttributes*>(child.get());
222                                 if (ea) {
223                                     if (!m_entityAttrs) {
224                                         child.release();
225                                         m_entityAttrs = ea;
226                                     }
227                                     else {
228                                         m_log.warn("skipping duplicate EntityAttributes element");
229                                     }
230                                 }
231                             }
232                         }
233                     }
234                 }
235             }
236         }
237         e = XMLHelper::getNextSiblingElement(e);
238     }
239 #endif
240 }
241
242 pair<bool,long> MetadataGenerator::run(SPRequest& request, bool isHandler) const
243 {
244     SPConfig& conf = SPConfig::getConfig();
245     if (conf.isEnabled(SPConfig::InProcess)) {
246         if (!m_acl.empty() && m_acl.count(request.getRemoteAddr()) == 0) {
247             m_log.error("request for metadata blocked from invalid address (%s)", request.getRemoteAddr().c_str());
248             istringstream msg("Metadata Request Blocked");
249             return make_pair(true,request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_FORBIDDEN));
250         }
251     }
252
253     try {
254         if (conf.isEnabled(SPConfig::OutOfProcess)) {
255             // When out of process, we run natively and directly process the message.
256             return processMessage(request.getApplication(), request.getHandlerURL(), request.getParameter("entityID"), request);
257         }
258         else {
259             // When not out of process, we remote all the message processing.
260             DDF out,in = DDF(m_address.c_str());
261             in.addmember("application_id").string(request.getApplication().getId());
262             in.addmember("handler_url").string(request.getHandlerURL());
263             if (request.getParameter("entityID"))
264                 in.addmember("entity_id").string(request.getParameter("entityID"));
265             DDFJanitor jin(in), jout(out);
266
267             out=request.getServiceProvider().getListenerService()->send(in);
268             return unwrap(request, out);
269         }
270     }
271     catch (exception& ex) {
272         m_log.error("error while processing request: %s", ex.what());
273         istringstream msg("Metadata Request Failed");
274         return make_pair(true,request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_ERROR));
275     }
276 }
277
278 void MetadataGenerator::receive(DDF& in, ostream& out)
279 {
280     // Find application.
281     const char* aid=in["application_id"].string();
282     const char* hurl=in["handler_url"].string();
283     const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : nullptr;
284     if (!app) {
285         // Something's horribly wrong.
286         m_log.error("couldn't find application (%s) for metadata request", aid ? aid : "(missing)");
287         throw ConfigurationException("Unable to locate application for metadata request, deleted?");
288     }
289     else if (!hurl) {
290         throw ConfigurationException("Missing handler_url parameter in remoted method call.");
291     }
292
293     // Wrap a response shim.
294     DDF ret(nullptr);
295     DDFJanitor jout(ret);
296     auto_ptr<HTTPResponse> resp(getResponse(ret));
297
298     // Since we're remoted, the result should either be a throw, a false/0 return,
299     // which we just return as an empty structure, or a response/redirect,
300     // which we capture in the facade and send back.
301     processMessage(*app, hurl, in["entity_id"].string(), *resp.get());
302     out << ret;
303 }
304
305 pair<bool,long> MetadataGenerator::processMessage(
306     const Application& application, const char* handlerURL, const char* entityID, HTTPResponse& httpResponse
307     ) const
308 {
309 #ifndef SHIBSP_LITE
310     m_log.debug("processing metadata request");
311
312     const PropertySet* relyingParty=nullptr;
313     if (entityID) {
314         MetadataProvider* m=application.getMetadataProvider();
315         Locker locker(m);
316         MetadataProviderCriteria mc(application, entityID);
317         relyingParty = application.getRelyingParty(m->getEntityDescriptor(mc).first);
318     }
319     else {
320         relyingParty = &application;
321     }
322
323     EntityDescriptor* entity;
324     pair<bool,const char*> prop = getString("template");
325     if (prop.first) {
326         // Load a template to use for our metadata.
327         string templ(prop.second);
328         XMLToolingConfig::getConfig().getPathResolver()->resolve(templ, PathResolver::XMLTOOLING_CFG_FILE);
329         auto_ptr_XMLCh widenit(templ.c_str());
330         LocalFileInputSource src(widenit.get());
331         Wrapper4InputSource dsrc(&src,false);
332         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
333         XercesJanitor<DOMDocument> docjan(doc);
334         auto_ptr<XMLObject> xmlobj(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
335         docjan.release();
336         entity = dynamic_cast<EntityDescriptor*>(xmlobj.get());
337         if (!entity)
338             throw ConfigurationException("Template file ($1) did not contain an EntityDescriptor", params(1, templ.c_str()));
339         xmlobj.release();
340     }
341     else {
342         entity = EntityDescriptorBuilder::buildEntityDescriptor();
343     }
344
345     if (!entity->getID()) {
346         string hashinput = m_salt + relyingParty->getString("entityID").second;
347         string hashed = '_' + SecurityHelper::doHash("SHA1", hashinput.c_str(), hashinput.length());
348         auto_ptr_XMLCh widenit(hashed.c_str());
349         entity->setID(widenit.get());
350     }
351
352     auto_ptr<EntityDescriptor> wrapper(entity);
353     pair<bool,unsigned int> cache = getUnsignedInt("cacheDuration");
354     if (cache.first) {
355         entity->setCacheDuration(cache.second);
356     }
357     cache = getUnsignedInt("validUntil");
358     if (cache.first)
359         entity->setValidUntil(time(nullptr) + cache.second);
360     entity->setEntityID(relyingParty->getXMLString("entityID").second);
361
362     if (m_org && !entity->getOrganization())
363         entity->setOrganization(m_org->cloneOrganization());
364
365     for (vector<ContactPerson*>::const_iterator cp = m_contacts.begin(); cp != m_contacts.end(); ++cp)
366         entity->getContactPersons().push_back((*cp)->cloneContactPerson());    
367
368     if (m_entityAttrs) {
369         if (!entity->getExtensions())
370             entity->setExtensions(ExtensionsBuilder::buildExtensions());
371         entity->getExtensions()->getUnknownXMLObjects().push_back(m_entityAttrs->cloneEntityAttributes());
372     }
373
374     SPSSODescriptor* role;
375     if (entity->getSPSSODescriptors().empty()) {
376         role = SPSSODescriptorBuilder::buildSPSSODescriptor();
377         entity->getSPSSODescriptors().push_back(role);
378     }
379     else {
380         role = entity->getSPSSODescriptors().front();
381     }
382
383     if (m_uiinfo) {
384         if (!role->getExtensions())
385             role->setExtensions(ExtensionsBuilder::buildExtensions());
386         role->getExtensions()->getUnknownXMLObjects().push_back(m_uiinfo->cloneUIInfo());
387     }
388
389     for (vector<AttributeConsumingService*>::const_iterator acs = m_attrConsumers.begin(); acs != m_attrConsumers.end(); ++acs)
390         role->getAttributeConsumingServices().push_back((*acs)->cloneAttributeConsumingService());
391
392     if (!m_reqAttrs.empty()) {
393         int index = 1;
394         const vector<AttributeConsumingService*>& svcs = const_cast<const SPSSODescriptor*>(role)->getAttributeConsumingServices();
395         for (vector<AttributeConsumingService*>::const_iterator s =svcs.begin(); s != svcs.end(); ++s) {
396             pair<bool,int> i = (*s)->getIndex();
397             if (i.first && index == i.second)
398                 index = i.second + 1;
399         }
400         AttributeConsumingService* svc = AttributeConsumingServiceBuilder::buildAttributeConsumingService();
401         role->getAttributeConsumingServices().push_back(svc);
402         svc->setIndex(index);
403         ServiceName* sn = ServiceNameBuilder::buildServiceName();
404         svc->getServiceNames().push_back(sn);
405         sn->setName(entity->getEntityID());
406         static const XMLCh english[] = UNICODE_LITERAL_2(e,n);
407         sn->setLang(english);
408         for (vector<RequestedAttribute*>::const_iterator req = m_reqAttrs.begin(); req != m_reqAttrs.end(); ++req)
409             svc->getRequestedAttributes().push_back((*req)->cloneRequestedAttribute());
410     }
411
412     // Policy flags.
413     prop = relyingParty->getString("signing");
414     if (prop.first && (!strcmp(prop.second,"true") || !strcmp(prop.second,"front")))
415         role->AuthnRequestsSigned(true);
416     pair<bool,bool> flagprop = relyingParty->getBool("requireSignedAssertions");
417     if (flagprop.first && flagprop.second)
418         role->WantAssertionsSigned(true);
419
420     // Ask each handler to generate itself.
421     vector<const Handler*> handlers;
422     application.getHandlers(handlers);
423     for (vector<const Handler*>::const_iterator h = handlers.begin(); h != handlers.end(); ++h) {
424         if (m_bases.empty()) {
425             if (strncmp(handlerURL, "https", 5) == 0) {
426                 if (m_https >= 0)
427                     (*h)->generateMetadata(*role, handlerURL);
428                 if (m_http == 1) {
429                     string temp(handlerURL);
430                     temp.erase(4, 1);
431                     (*h)->generateMetadata(*role, temp.c_str());
432                 }
433             }
434             else {
435                 if (m_http >= 0)
436                     (*h)->generateMetadata(*role, handlerURL);
437                 if (m_https == 1) {
438                     string temp(handlerURL);
439                     temp.insert(temp.begin() + 4, 's');
440                     (*h)->generateMetadata(*role, temp.c_str());
441                 }
442             }
443         }
444         else {
445             for (vector<string>::const_iterator b = m_bases.begin(); b != m_bases.end(); ++b)
446                 (*h)->generateMetadata(*role, b->c_str());
447         }
448     }
449
450     AttributeExtractor* extractor = application.getAttributeExtractor();
451     if (extractor) {
452         Locker extlocker(extractor);
453         extractor->generateMetadata(*role);
454     }
455
456     CredentialResolver* credResolver=application.getCredentialResolver();
457     if (credResolver) {
458         Locker credLocker(credResolver);
459         CredentialCriteria cc;
460         prop = relyingParty->getString("keyName");
461         if (prop.first)
462             cc.getKeyNames().insert(prop.second);
463         vector<const Credential*> signingcreds,enccreds;
464         cc.setUsage(Credential::SIGNING_CREDENTIAL);
465         credResolver->resolve(signingcreds, &cc);
466         cc.setUsage(Credential::ENCRYPTION_CREDENTIAL);
467         credResolver->resolve(enccreds, &cc);
468
469         for (vector<const Credential*>::const_iterator c = signingcreds.begin(); c != signingcreds.end(); ++c) {
470             KeyInfo* kinfo = (*c)->getKeyInfo();
471             if (kinfo) {
472                 KeyDescriptor* kd = KeyDescriptorBuilder::buildKeyDescriptor();
473                 kd->setKeyInfo(kinfo);
474                 const XMLCh* use = KeyDescriptor::KEYTYPE_SIGNING;
475                 for (vector<const Credential*>::iterator match = enccreds.begin(); match != enccreds.end(); ++match) {
476                     if (*match == *c) {
477                         use = nullptr;
478                         enccreds.erase(match);
479                         break;
480                     }
481                 }
482                 kd->setUse(use);
483                 role->getKeyDescriptors().push_back(kd);
484             }
485         }
486
487         for (vector<const Credential*>::const_iterator c = enccreds.begin(); c != enccreds.end(); ++c) {
488             KeyInfo* kinfo = (*c)->getKeyInfo();
489             if (kinfo) {
490                 KeyDescriptor* kd = KeyDescriptorBuilder::buildKeyDescriptor();
491                 kd->setUse(KeyDescriptor::KEYTYPE_ENCRYPTION);
492                 kd->setKeyInfo(kinfo);
493                 role->getKeyDescriptors().push_back(kd);
494             }
495         }
496     }
497
498     // Stream for response.
499     stringstream s;
500
501     // Self-sign it?
502     pair<bool,bool> flag = getBool("signing");
503     if (flag.first && flag.second) {
504         if (credResolver) {
505             Locker credLocker(credResolver);
506             // Fill in criteria to use.
507             CredentialCriteria cc;
508             cc.setUsage(Credential::SIGNING_CREDENTIAL);
509             prop = getString("keyName");
510             if (prop.first)
511                 cc.getKeyNames().insert(prop.second);
512             pair<bool,const XMLCh*> sigalg = getXMLString("signingAlg");
513             pair<bool,const XMLCh*> digalg = getXMLString("digestAlg");
514             if (sigalg.first)
515                 cc.setXMLAlgorithm(sigalg.second);
516             const Credential* cred = credResolver->resolve(&cc);
517             if (!cred)
518                 throw XMLSecurityException("Unable to obtain signing credential to use.");
519
520             // Pretty-print it first and then read it back in.
521             stringstream pretty;
522             XMLHelper::serialize(entity->marshall(), pretty, true);
523             DOMDocument* prettydoc = XMLToolingConfig::getConfig().getParser().parse(pretty);
524             auto_ptr<XMLObject> prettyentity(XMLObjectBuilder::buildOneFromElement(prettydoc->getDocumentElement(), true));
525
526             Signature* sig = SignatureBuilder::buildSignature();
527             dynamic_cast<EntityDescriptor*>(prettyentity.get())->setSignature(sig);
528             if (sigalg.first)
529                 sig->setSignatureAlgorithm(sigalg.second);
530             if (digalg.first) {
531                 opensaml::ContentReference* cr = dynamic_cast<opensaml::ContentReference*>(sig->getContentReference());
532                 if (cr)
533                     cr->setDigestAlgorithm(digalg.second);
534             }
535
536             // Sign while marshalling.
537             vector<Signature*> sigs(1,sig);
538             prettyentity->marshall(prettydoc,&sigs,cred);
539             s << *prettyentity;
540         }
541         else {
542             throw FatalProfileException("Can't self-sign metadata, no credential resolver found.");
543         }
544     }
545     else {
546         // Pretty-print it directly to client.
547         XMLHelper::serialize(entity->marshall(), s, true);
548     }
549
550     prop = getString("mimeType");
551     httpResponse.setContentType(prop.first ? prop.second : "application/samlmetadata+xml");
552     return make_pair(true, httpResponse.sendResponse(s));
553 #else
554     return make_pair(false,0L);
555 #endif
556 }