2 * Copyright 2001-2007 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * Base class for handlers that need SP request/response layer to be remoted.
24 #include "exceptions.h"
25 #include "ServiceProvider.h"
26 #include "handler/RemotedHandler.h"
29 #include <log4cpp/Category.hh>
30 #include <saml/util/CGIParser.h>
31 #include <xmltooling/unicode.h>
32 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
33 #include <xsec/enc/XSECCryptoException.hpp>
34 #include <xsec/framework/XSECException.hpp>
35 #include <xsec/framework/XSECProvider.hpp>
37 using namespace shibsp;
38 using namespace opensaml;
39 using namespace xmltooling;
40 using namespace log4cpp;
41 using namespace xercesc;
45 class SHIBSP_DLLLOCAL RemotedRequest : public virtual opensaml::HTTPRequest
48 mutable CGIParser* m_parser;
49 mutable vector<XSECCryptoX509*> m_certs;
51 RemotedRequest(DDF& input) : m_input(input), m_parser(NULL) {}
52 virtual ~RemotedRequest() {
53 for_each(m_certs.begin(), m_certs.end(), xmltooling::cleanup<XSECCryptoX509>());
58 const char* getScheme() const {
59 return m_input["scheme"].string();
61 const char* getHostname() const {
62 return m_input["hostname"].string();
65 return m_input["port"].integer();
67 std::string getContentType() const {
68 DDF s = m_input["content_type"];
69 return s.string() ? s.string() : "";
71 long getContentLength() const {
72 return m_input["content_length"].integer();
74 const char* getRequestBody() const {
75 return m_input["body"].string();
78 const char* getParameter(const char* name) const;
79 std::vector<const char*>::size_type getParameters(const char* name, std::vector<const char*>& values) const;
81 std::string getRemoteUser() const {
82 DDF s = m_input["remote_user"];
83 return s.string() ? s.string() : "";
85 std::string getRemoteAddr() const {
86 DDF s = m_input["client_addr"];
87 return s.string() ? s.string() : "";
90 const std::vector<XSECCryptoX509*>& getClientCertificates() const;
93 const char* getMethod() const {
94 return m_input["method"].string();
96 const char* getRequestURI() const {
97 return m_input["uri"].string();
99 const char* getRequestURL() const {
100 return m_input["url"].string();
102 const char* getQueryString() const {
103 return m_input["query"].string();
105 std::string getHeader(const char* name) const {
106 DDF s = m_input["headers"][name];
107 return s.string() ? s.string() : "";
111 class SHIBSP_DLLLOCAL RemotedResponse : public virtual opensaml::HTTPResponse
115 RemotedResponse(DDF& output) : m_output(output) {}
116 virtual ~RemotedResponse() {}
119 long sendResponse(std::istream& inputStream, long status);
122 void setResponseHeader(const char* name, const char* value);
123 long sendRedirect(const char* url);
127 const char* RemotedRequest::getParameter(const char* name) const
130 m_parser=new CGIParser(*this);
132 pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
133 return (bounds.first==bounds.second) ? NULL : bounds.first->second;
136 std::vector<const char*>::size_type RemotedRequest::getParameters(const char* name, std::vector<const char*>& values) const
139 m_parser=new CGIParser(*this);
141 pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
142 while (bounds.first!=bounds.second) {
143 values.push_back(bounds.first->second);
146 return values.size();
149 const std::vector<XSECCryptoX509*>& RemotedRequest::getClientCertificates() const
151 if (m_certs.empty()) {
152 DDF cert = m_input["certificates"].first();
153 while (cert.isstring()) {
155 auto_ptr<XSECCryptoX509> x509(XSECPlatformUtils::g_cryptoProvider->X509());
156 x509->loadX509Base64Bin(cert.string(), cert.strlen());
157 m_certs.push_back(x509.release());
159 catch(XSECException& e) {
160 auto_ptr_char temp(e.getMsg());
161 Category::getInstance(SHIBSP_LOGCAT".SPRequest").error("XML-Security exception loading client certificate: %s", temp.get());
163 catch(XSECCryptoException& e) {
164 Category::getInstance(SHIBSP_LOGCAT".SPRequest").error("XML-Security exception loading client certificate: %s", e.getMsg());
172 long RemotedResponse::sendResponse(std::istream& in, long status)
178 msg.append(buf,in.gcount());
180 if (!m_output.isstruct())
181 m_output.structure();
182 m_output.addmember("response.data").string(msg.c_str());
183 m_output.addmember("response.status").integer(status);
187 void RemotedResponse::setResponseHeader(const char* name, const char* value)
189 if (!m_output.isstruct())
190 m_output.structure();
191 DDF hdrs = m_output["headers"];
193 hdrs = m_output.addmember("headers").structure();
194 hdrs.addmember(name).string(value);
197 long RemotedResponse::sendRedirect(const char* url)
199 if (!m_output.isstruct())
200 m_output.structure();
201 m_output.addmember("redirect").string(url);
202 return HTTPResponse::SAML_HTTP_STATUS_MOVED;
206 void RemotedHandler::setAddress(const char* address)
208 if (!m_address.empty())
209 throw ConfigurationException("Cannot register a remoting address twice for the same Handler.");
211 SPConfig& conf = SPConfig::getConfig();
212 if (conf.isEnabled(SPConfig::OutOfProcess)) {
213 ListenerService* listener = conf.getServiceProvider()->getListenerService(false);
215 listener->regListener(m_address.c_str(),this);
217 Category::getInstance(SHIBSP_LOGCAT".Handler").info("no ListenerService available, handler remoting disabled");
221 RemotedHandler::~RemotedHandler()
223 SPConfig& conf = SPConfig::getConfig();
224 ListenerService* listener=conf.getServiceProvider()->getListenerService(false);
225 if (listener && conf.isEnabled(SPConfig::OutOfProcess))
226 listener->unregListener(m_address.c_str(),this);
229 DDF RemotedHandler::wrap(const SPRequest& request, const vector<string>* headers, bool certs) const
231 DDF in = DDF(m_address.c_str()).structure();
232 in.addmember("scheme").string(request.getScheme());
233 in.addmember("hostname").string(request.getHostname());
234 in.addmember("port").integer(request.getPort());
235 in.addmember("content_type").string(request.getContentType().c_str());
236 in.addmember("content_length").integer(request.getContentLength());
237 in.addmember("body").string(request.getRequestBody());
238 in.addmember("remote_user").string(request.getRemoteUser().c_str());
239 in.addmember("client_addr").string(request.getRemoteAddr().c_str());
240 in.addmember("method").string(request.getMethod());
241 in.addmember("uri").string(request.getRequestURI());
242 in.addmember("url").string(request.getRequestURL());
243 in.addmember("query").string(request.getQueryString());
247 DDF hin = in.addmember("headers").structure();
248 for (vector<string>::const_iterator h = headers->begin(); h!=headers->end(); ++h) {
249 hdr = request.getHeader(h->c_str());
251 hin.addmember(h->c_str()).string(hdr.c_str());
256 const vector<XSECCryptoX509*>& xvec = request.getClientCertificates();
258 DDF clist = in.addmember("certificates").list();
259 for (vector<XSECCryptoX509*>::const_iterator x = xvec.begin(); x!=xvec.end(); ++x) {
260 DDF x509 = DDF(NULL).string((*x)->getDEREncodingSB().rawCharBuffer());
269 pair<bool,long> RemotedHandler::unwrap(SPRequest& request, DDF& out) const
271 DDF h = out["headers"];
273 while (h.isstring()) {
274 request.setResponseHeader(h.name(), h.string());
279 return make_pair(true, request.sendRedirect(h.string()));
282 istringstream s(h["data"].string());
283 return make_pair(true, static_cast<GenericResponse&>(request).sendResponse(s, h["status"].integer()));
285 return make_pair(false,0);
288 HTTPRequest* RemotedHandler::getRequest(DDF& in) const
290 return new RemotedRequest(in);
293 HTTPResponse* RemotedHandler::getResponse(DDF& out) const
295 return new RemotedResponse(out);