1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20 
21 /**
22  * ChainingSessionInitiator.cpp
23  *
24  * Chains together multiple SessionInitiator handlers in sequence.
25  */
26 
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "handler/AbstractHandler.h"
30 #include "handler/SessionInitiator.h"
31 #include "util/SPConstants.h"
32 
33 #define BOOST_BIND_GLOBAL_PLACEHOLDERS
34 #include <boost/bind.hpp>
35 #include <boost/ptr_container/ptr_vector.hpp>
36 #include <xercesc/util/XMLUniDefs.hpp>
37 #include <xmltooling/util/XMLHelper.h>
38 
39 using namespace shibsp;
40 using namespace xmltooling;
41 using namespace boost;
42 using namespace std;
43 
44 namespace shibsp {
45 
46 #if defined (_MSC_VER)
47     #pragma warning( push )
48     #pragma warning( disable : 4250 )
49 #endif
50 
51     class SHIBSP_DLLLOCAL ChainingSessionInitiator : public SessionInitiator, public AbstractHandler
52     {
53     public:
54         ChainingSessionInitiator(const DOMElement* e, const char* appId, bool deprecationSupport=true);
~ChainingSessionInitiator()55         virtual ~ChainingSessionInitiator() {}
56 
57         pair<bool,long> run(SPRequest& request, string& entityID, bool isHandler=true) const;
58 
59 #ifndef SHIBSP_LITE
generateMetadata(opensaml::saml2md::SPSSODescriptor & role,const char * handlerURL) const60         void generateMetadata(opensaml::saml2md::SPSSODescriptor& role, const char* handlerURL) const {
61             doGenerateMetadata(role, handlerURL);   // assumes all chains support the RequestInitiator protocol
62             for_each(m_handlers.begin(), m_handlers.end(), boost::bind(&SessionInitiator::generateMetadata, _1, boost::ref(role), handlerURL));
63         }
64 #endif
65 
66     private:
67         ptr_vector<SessionInitiator> m_handlers;
68     };
69 
70 #if defined (_MSC_VER)
71     #pragma warning( pop )
72 #endif
73 
74     static const XMLCh _SessionInitiator[] =    UNICODE_LITERAL_16(S,e,s,s,i,o,n,I,n,i,t,i,a,t,o,r);
75     static const XMLCh _type[] =                UNICODE_LITERAL_4(t,y,p,e);
76 
77     class SHIBSP_DLLLOCAL SessionInitiatorNodeFilter : public DOMNodeFilter
78     {
79     public:
acceptNode(const DOMNode * node) const80         FilterAction acceptNode(const DOMNode* node) const {
81             if (XMLString::equals(node->getLocalName(), _SessionInitiator))
82                 return FILTER_REJECT;
83             return FILTER_ACCEPT;
84         }
85     };
86 
87     static SHIBSP_DLLLOCAL SessionInitiatorNodeFilter g_SINFilter;
88 
ChainingSessionInitiatorFactory(const pair<const DOMElement *,const char * > & p,bool deprecationSupport)89     SessionInitiator* SHIBSP_DLLLOCAL ChainingSessionInitiatorFactory(const pair<const DOMElement*,const char*>& p, bool deprecationSupport)
90     {
91         return new ChainingSessionInitiator(p.first, p.second, deprecationSupport);
92     }
93 };
94 
ChainingSessionInitiator(const DOMElement * e,const char * appId,bool deprecationSupport)95 ChainingSessionInitiator::ChainingSessionInitiator(const DOMElement* e, const char* appId, bool deprecationSupport)
96     : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT ".SessionInitiator.Chaining"), &g_SINFilter)
97 {
98     SPConfig& conf = SPConfig::getConfig();
99 
100     // Load up the chain of handlers.
101     e = e ? XMLHelper::getFirstChildElement(e, _SessionInitiator) : nullptr;
102     while (e) {
103         string t(XMLHelper::getAttrString(e, nullptr, _type));
104         if (!t.empty()) {
105             try {
106                 auto_ptr<SessionInitiator> np(conf.SessionInitiatorManager.newPlugin(t.c_str(), make_pair(e, appId), deprecationSupport));
107                 m_handlers.push_back(np.get());
108                 np.release();
109                 m_handlers.back().setParent(this);
110             }
111             catch (std::exception& ex) {
112                 m_log.error("caught exception processing embedded SessionInitiator element: %s", ex.what());
113             }
114         }
115         e = XMLHelper::getNextSiblingElement(e, _SessionInitiator);
116     }
117 
118     m_supportedOptions.insert("isPassive");
119 }
120 
run(SPRequest & request,string & entityID,bool isHandler) const121 pair<bool,long> ChainingSessionInitiator::run(SPRequest& request, string& entityID, bool isHandler) const
122 {
123     if (!checkCompatibility(request, isHandler))
124         return make_pair(false, 0L);
125 
126     pair<bool,long> ret;
127     for (ptr_vector<SessionInitiator>::const_iterator i = m_handlers.begin(); i != m_handlers.end(); ++i) {
128         ret = i->run(request, entityID, isHandler);
129         if (ret.first)
130             return ret;
131     }
132     throw ConfigurationException("None of the configured SessionInitiators handled the request.");
133 }
134