Change license header.
[shibboleth/cpp-sp.git] / shibsp / binding / impl / XMLProtocolProvider.cpp
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  */\r
20 \r
21 /**\r
22  * XMLProtocolProvider.cpp\r
23  *\r
24  * XML-based protocol provider.\r
25  */\r
26 \r
27 #include "internal.h"\r
28 #include "exceptions.h"\r
29 #include "binding/ProtocolProvider.h"\r
30 #include "util/DOMPropertySet.h"\r
31 #include "util/SPConstants.h"\r
32 \r
33 #include <map>\r
34 #include <xmltooling/io/HTTPResponse.h>\r
35 #include <xmltooling/util/NDC.h>\r
36 #include <xmltooling/util/ReloadableXMLFile.h>\r
37 #include <xmltooling/util/Threads.h>\r
38 #include <xmltooling/util/XMLHelper.h>\r
39 #include <xercesc/util/XMLUniDefs.hpp>\r
40 \r
41 using shibspconstants::SHIB2SPPROTOCOLS_NS;\r
42 using namespace shibsp;\r
43 using namespace xmltooling;\r
44 using namespace std;\r
45 \r
46 namespace shibsp {\r
47 \r
48     static const XMLCh _id[] =          UNICODE_LITERAL_2(i,d);\r
49     static const XMLCh Binding[] =      UNICODE_LITERAL_7(B,i,n,d,i,n,g);\r
50     static const XMLCh Initiator[] =    UNICODE_LITERAL_9(I,n,i,t,i,a,t,o,r);\r
51     static const XMLCh Protocol[] =     UNICODE_LITERAL_8(P,r,o,t,o,c,o,l);\r
52     static const XMLCh Protocols[] =    UNICODE_LITERAL_9(P,r,o,t,o,c,o,l,s);\r
53     static const XMLCh Service[] =      UNICODE_LITERAL_7(S,e,r,v,i,c,e);\r
54 \r
55 #if defined (_MSC_VER)\r
56     #pragma warning( push )\r
57     #pragma warning( disable : 4250 )\r
58 #endif\r
59 \r
60     class SHIBSP_DLLLOCAL XMLProtocolProviderImpl : public DOMNodeFilter, DOMPropertySet\r
61     {\r
62     public:\r
63         XMLProtocolProviderImpl(const DOMElement* e, Category& log);\r
64         ~XMLProtocolProviderImpl() {\r
65             for (protmap_t::iterator i = m_map.begin(); i != m_map.end(); ++i) {\r
66                 delete i->second.first;\r
67                 for_each(i->second.second.begin(), i->second.second.end(), xmltooling::cleanup<PropertySet>());\r
68             }\r
69             if (m_document)\r
70                 m_document->release();\r
71         }\r
72 \r
73         void setDocument(DOMDocument* doc) {\r
74             m_document = doc;\r
75         }\r
76 \r
77 #ifdef SHIBSP_XERCESC_SHORT_ACCEPTNODE\r
78         short\r
79 #else\r
80         FilterAction\r
81 #endif\r
82         acceptNode(const DOMNode* node) const {\r
83             return FILTER_REJECT;\r
84         }\r
85 \r
86     private:\r
87         DOMDocument* m_document;\r
88         // Map of protocol/service pair to an Initiator propset plus an array of Binding propsets.\r
89         typedef map< pair<string,string>, pair< PropertySet*,vector<const PropertySet*> > > protmap_t;\r
90         protmap_t m_map;\r
91 \r
92         friend class SHIBSP_DLLLOCAL XMLProtocolProvider;\r
93     };\r
94 \r
95     class XMLProtocolProvider : public ProtocolProvider, public ReloadableXMLFile\r
96     {\r
97     public:\r
98         XMLProtocolProvider(const DOMElement* e)\r
99                 : ReloadableXMLFile(e, Category::getInstance(SHIBSP_LOGCAT".ProtocolProvider.XML")), m_impl(nullptr) {\r
100             background_load(); // guarantees an exception or the policy is loaded\r
101         }\r
102 \r
103         ~XMLProtocolProvider() {\r
104             shutdown();\r
105             delete m_impl;\r
106         }\r
107 \r
108         const PropertySet* getInitiator(const char* protocol, const char* service) const {\r
109             XMLProtocolProviderImpl::protmap_t::const_iterator i = m_impl->m_map.find(pair<string,string>(protocol,service));\r
110             return (i != m_impl->m_map.end()) ? i->second.first : nullptr;\r
111         }\r
112 \r
113         const vector<const PropertySet*>& getBindings(const char* protocol, const char* service) const {\r
114             XMLProtocolProviderImpl::protmap_t::const_iterator i = m_impl->m_map.find(pair<string,string>(protocol,service));\r
115             return (i != m_impl->m_map.end()) ? i->second.second : m_noBindings;\r
116         }\r
117 \r
118     protected:\r
119         pair<bool,DOMElement*> load(bool backup);\r
120         pair<bool,DOMElement*> background_load();\r
121 \r
122     private:\r
123         static vector<const PropertySet*> m_noBindings;\r
124         XMLProtocolProviderImpl* m_impl;\r
125     };\r
126 \r
127 #if defined (_MSC_VER)\r
128     #pragma warning( pop )\r
129 #endif\r
130 \r
131     ProtocolProvider* SHIBSP_DLLLOCAL XMLProtocolProviderFactory(const DOMElement* const & e)\r
132     {\r
133         return new XMLProtocolProvider(e);\r
134     }\r
135 }\r
136 \r
137 void SHIBSP_API shibsp::registerProtocolProviders()\r
138 {\r
139     SPConfig::getConfig().ProtocolProviderManager.registerFactory(XML_PROTOCOL_PROVIDER, XMLProtocolProviderFactory);\r
140 }\r
141 \r
142 ProtocolProvider::ProtocolProvider()\r
143 {\r
144 }\r
145 \r
146 ProtocolProvider::~ProtocolProvider()\r
147 {\r
148 }\r
149 \r
150 vector<const PropertySet*> XMLProtocolProvider::m_noBindings;\r
151 \r
152 XMLProtocolProviderImpl::XMLProtocolProviderImpl(const DOMElement* e, Category& log) : m_document(nullptr)\r
153 {\r
154 #ifdef _DEBUG\r
155     xmltooling::NDC ndc("XMLProtocolProviderImpl");\r
156 #endif\r
157     //typedef map< pair<string,string>, pair< PropertySet*,vector<const PropertySet*> > > protmap_t;\r
158 \r
159     if (!XMLHelper::isNodeNamed(e, SHIB2SPPROTOCOLS_NS, Protocols))\r
160         throw ConfigurationException("XML ProtocolProvider requires prot:Protocols at root of configuration.");\r
161 \r
162     e = XMLHelper::getFirstChildElement(e, SHIB2SPPROTOCOLS_NS, Protocol);\r
163     while (e) {\r
164         string id = XMLHelper::getAttrString(e, nullptr, _id);\r
165         if (!id.empty()) {\r
166             const DOMElement* svc = XMLHelper::getFirstChildElement(e, SHIB2SPPROTOCOLS_NS, Service);\r
167             while (svc) {\r
168                 string svcid = XMLHelper::getAttrString(svc, nullptr, _id);\r
169                 if (!svcid.empty() && m_map.count(make_pair(id,svcid)) == 0) {\r
170                     pair< PropertySet*,vector<const PropertySet*> >& entry = m_map[make_pair(id,svcid)];\r
171                     // Wrap the Initiator in a propset, if any.\r
172                     const DOMElement* child = XMLHelper::getFirstChildElement(svc, SHIB2SPPROTOCOLS_NS, Initiator);\r
173                     if (child) {\r
174                         DOMPropertySet* initprop = new DOMPropertySet();\r
175                         entry.first = initprop;\r
176                         initprop->load(child, nullptr, this);\r
177                     }\r
178                     else {\r
179                         entry.first = nullptr;\r
180                     }\r
181 \r
182                     // Walk the Bindings.\r
183                     child = XMLHelper::getFirstChildElement(svc, SHIB2SPPROTOCOLS_NS, Binding);\r
184                     while (child) {\r
185                         DOMPropertySet* bindprop = new DOMPropertySet();\r
186                         entry.second.push_back(bindprop);\r
187                         bindprop->load(child, nullptr, this);\r
188                         child = XMLHelper::getNextSiblingElement(child, SHIB2SPPROTOCOLS_NS, Binding);\r
189                     }\r
190                 }\r
191                 svc = XMLHelper::getNextSiblingElement(svc, SHIB2SPPROTOCOLS_NS, Service);\r
192             }\r
193         }\r
194         e = XMLHelper::getNextSiblingElement(e, SHIB2SPPROTOCOLS_NS, Protocol);\r
195     }\r
196 }\r
197 \r
198 \r
199 pair<bool,DOMElement*> XMLProtocolProvider::load(bool backup)\r
200 {\r
201     // Load from source using base class.\r
202     pair<bool,DOMElement*> raw = ReloadableXMLFile::load(backup);\r
203 \r
204     // If we own it, wrap it.\r
205     XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : nullptr);\r
206 \r
207     XMLProtocolProviderImpl* impl = new XMLProtocolProviderImpl(raw.second, m_log);\r
208 \r
209     // If we held the document, transfer it to the impl. If we didn't, it's a no-op.\r
210     impl->setDocument(docjanitor.release());\r
211 \r
212     // Perform the swap inside a lock.\r
213     if (m_lock)\r
214         m_lock->wrlock();\r
215     SharedLock locker(m_lock, false);\r
216     delete m_impl;\r
217     m_impl = impl;\r
218 \r
219 \r
220     return make_pair(false,(DOMElement*)nullptr);\r
221 }\r
222 \r
223 pair<bool,DOMElement*> XMLProtocolProvider::background_load()\r
224 {\r
225     try {\r
226         return load(false);\r
227     }\r
228     catch (long& ex) {\r
229         if (ex == HTTPResponse::XMLTOOLING_HTTP_STATUS_NOTMODIFIED)\r
230             m_log.info("remote resource (%s) unchanged", m_source.c_str());\r
231         if (!m_loaded && !m_backing.empty())\r
232             return load(true);\r
233         throw;\r
234     }\r
235     catch (exception&) {\r
236         if (!m_loaded && !m_backing.empty())\r
237             return load(true);\r
238         throw;\r
239     }\r
240 }\r