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