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