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 <xmltooling/unicode.h>
33 # include <saml/util/CGIParser.h>
34 # include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
35 # include <xsec/enc/XSECCryptoException.hpp>
36 # include <xsec/framework/XSECException.hpp>
37 # include <xsec/framework/XSECProvider.hpp>
40 using namespace shibsp;
41 using namespace opensaml;
42 using namespace xmltooling;
43 using namespace log4cpp;
44 using namespace xercesc;
49 class SHIBSP_DLLLOCAL RemotedRequest : public virtual HTTPRequest
52 mutable CGIParser* m_parser;
53 mutable vector<XSECCryptoX509*> m_certs;
55 RemotedRequest(DDF& input) : m_input(input), m_parser(NULL) {}
56 virtual ~RemotedRequest() {
57 for_each(m_certs.begin(), m_certs.end(), xmltooling::cleanup<XSECCryptoX509>());
62 const char* getScheme() const {
63 return m_input["scheme"].string();
65 const char* getHostname() const {
66 return m_input["hostname"].string();
69 return m_input["port"].integer();
71 std::string getContentType() const {
72 DDF s = m_input["content_type"];
73 return s.string() ? s.string() : "";
75 long getContentLength() const {
76 return m_input["content_length"].integer();
78 const char* getRequestBody() const {
79 return m_input["body"].string();
82 const char* getParameter(const char* name) const;
83 std::vector<const char*>::size_type getParameters(const char* name, std::vector<const char*>& values) const;
85 std::string getRemoteUser() const {
86 DDF s = m_input["remote_user"];
87 return s.string() ? s.string() : "";
89 std::string getRemoteAddr() const {
90 DDF s = m_input["client_addr"];
91 return s.string() ? s.string() : "";
94 const std::vector<XSECCryptoX509*>& getClientCertificates() const;
97 const char* getMethod() const {
98 return m_input["method"].string();
100 const char* getRequestURI() const {
101 return m_input["uri"].string();
103 const char* getRequestURL() const {
104 return m_input["url"].string();
106 const char* getQueryString() const {
107 return m_input["query"].string();
109 std::string getHeader(const char* name) const {
110 DDF s = m_input["headers"][name];
111 return s.string() ? s.string() : "";
115 class SHIBSP_DLLLOCAL RemotedResponse : public virtual HTTPResponse
119 RemotedResponse(DDF& output) : m_output(output) {}
120 virtual ~RemotedResponse() {}
123 long sendResponse(std::istream& inputStream, long status);
126 void setResponseHeader(const char* name, const char* value);
127 long sendRedirect(const char* url);
131 const char* RemotedRequest::getParameter(const char* name) const
134 m_parser=new CGIParser(*this);
136 pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
137 return (bounds.first==bounds.second) ? NULL : bounds.first->second;
140 std::vector<const char*>::size_type RemotedRequest::getParameters(const char* name, std::vector<const char*>& values) const
143 m_parser=new CGIParser(*this);
145 pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
146 while (bounds.first!=bounds.second) {
147 values.push_back(bounds.first->second);
150 return values.size();
153 const std::vector<XSECCryptoX509*>& RemotedRequest::getClientCertificates() const
155 if (m_certs.empty()) {
156 DDF cert = m_input["certificates"].first();
157 while (cert.isstring()) {
159 auto_ptr<XSECCryptoX509> x509(XSECPlatformUtils::g_cryptoProvider->X509());
160 x509->loadX509Base64Bin(cert.string(), cert.strlen());
161 m_certs.push_back(x509.release());
163 catch(XSECException& e) {
164 auto_ptr_char temp(e.getMsg());
165 Category::getInstance(SHIBSP_LOGCAT".SPRequest").error("XML-Security exception loading client certificate: %s", temp.get());
167 catch(XSECCryptoException& e) {
168 Category::getInstance(SHIBSP_LOGCAT".SPRequest").error("XML-Security exception loading client certificate: %s", e.getMsg());
176 long RemotedResponse::sendResponse(std::istream& in, long status)
182 msg.append(buf,in.gcount());
184 if (!m_output.isstruct())
185 m_output.structure();
186 m_output.addmember("response.data").string(msg.c_str());
187 m_output.addmember("response.status").integer(status);
191 void RemotedResponse::setResponseHeader(const char* name, const char* value)
193 if (!m_output.isstruct())
194 m_output.structure();
195 DDF hdrs = m_output["headers"];
197 hdrs = m_output.addmember("headers").structure();
198 hdrs.addmember(name).string(value);
201 long RemotedResponse::sendRedirect(const char* url)
203 if (!m_output.isstruct())
204 m_output.structure();
205 m_output.addmember("redirect").string(url);
206 return HTTPResponse::XMLTOOLING_HTTP_STATUS_MOVED;
211 void RemotedHandler::setAddress(const char* address)
213 if (!m_address.empty())
214 throw ConfigurationException("Cannot register a remoting address twice for the same Handler.");
216 SPConfig& conf = SPConfig::getConfig();
217 if (!conf.isEnabled(SPConfig::InProcess)) {
218 ListenerService* listener = conf.getServiceProvider()->getListenerService(false);
220 listener->regListener(m_address.c_str(),this);
222 Category::getInstance(SHIBSP_LOGCAT".Handler").info("no ListenerService available, handler remoting disabled");
226 RemotedHandler::~RemotedHandler()
228 SPConfig& conf = SPConfig::getConfig();
229 ListenerService* listener=conf.getServiceProvider()->getListenerService(false);
230 if (listener && conf.isEnabled(SPConfig::OutOfProcess) && !conf.isEnabled(SPConfig::InProcess))
231 listener->unregListener(m_address.c_str(),this);
234 DDF RemotedHandler::wrap(const SPRequest& request, const vector<string>* headers, bool certs) const
236 DDF in = DDF(m_address.c_str()).structure();
237 in.addmember("scheme").string(request.getScheme());
238 in.addmember("hostname").string(request.getHostname());
239 in.addmember("port").integer(request.getPort());
240 in.addmember("content_type").string(request.getContentType().c_str());
241 in.addmember("content_length").integer(request.getContentLength());
242 in.addmember("body").string(request.getRequestBody());
243 in.addmember("remote_user").string(request.getRemoteUser().c_str());
244 in.addmember("client_addr").string(request.getRemoteAddr().c_str());
245 in.addmember("method").string(request.getMethod());
246 in.addmember("uri").string(request.getRequestURI());
247 in.addmember("url").string(request.getRequestURL());
248 in.addmember("query").string(request.getQueryString());
252 DDF hin = in.addmember("headers").structure();
253 for (vector<string>::const_iterator h = headers->begin(); h!=headers->end(); ++h) {
254 hdr = request.getHeader(h->c_str());
256 hin.addmember(h->c_str()).string(hdr.c_str());
262 const vector<XSECCryptoX509*>& xvec = request.getClientCertificates();
264 DDF clist = in.addmember("certificates").list();
265 for (vector<XSECCryptoX509*>::const_iterator x = xvec.begin(); x!=xvec.end(); ++x) {
266 DDF x509 = DDF(NULL).string((*x)->getDEREncodingSB().rawCharBuffer());
271 const vector<string>& xvec = request.getClientCertificates();
273 DDF clist = in.addmember("certificates").list();
274 for (vector<string>::const_iterator x = xvec.begin(); x!=xvec.end(); ++x) {
275 DDF x509 = DDF(NULL).string(x->c_str());
285 pair<bool,long> RemotedHandler::unwrap(SPRequest& request, DDF& out) const
287 DDF h = out["headers"];
289 while (h.isstring()) {
290 request.setResponseHeader(h.name(), h.string());
295 return make_pair(true, request.sendRedirect(h.string()));
298 istringstream s(h["data"].string());
299 return make_pair(true, request.sendResponse(s, h["status"].integer()));
301 return make_pair(false,0);
304 HTTPRequest* RemotedHandler::getRequest(DDF& in) const
307 return new RemotedRequest(in);
309 throw ConfigurationException("Cannot process message using lite version of shibsp library.");
313 HTTPResponse* RemotedHandler::getResponse(DDF& out) const
316 return new RemotedResponse(out);
318 throw ConfigurationException("Cannot process message using lite version of shibsp library.");