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 "Application.h"
25 #include "exceptions.h"
26 #include "ServiceProvider.h"
27 #include "handler/RemotedHandler.h"
30 #include <xmltooling/unicode.h>
33 # include "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 xercesc;
48 class SHIBSP_DLLLOCAL RemotedRequest : public virtual HTTPRequest
51 mutable CGIParser* m_parser;
52 mutable vector<XSECCryptoX509*> m_certs;
54 RemotedRequest(DDF& input) : m_input(input), m_parser(NULL) {}
55 virtual ~RemotedRequest() {
56 for_each(m_certs.begin(), m_certs.end(), xmltooling::cleanup<XSECCryptoX509>());
61 const char* getScheme() const {
62 return m_input["scheme"].string();
64 const char* getHostname() const {
65 return m_input["hostname"].string();
68 return m_input["port"].integer();
70 std::string getContentType() const {
71 DDF s = m_input["content_type"];
72 return s.string() ? s.string() : "";
74 long getContentLength() const {
75 return m_input["content_length"].integer();
77 const char* getRequestBody() const {
78 return m_input["body"].string();
81 const char* getParameter(const char* name) const;
82 std::vector<const char*>::size_type getParameters(const char* name, std::vector<const char*>& values) const;
84 std::string getRemoteUser() const {
85 DDF s = m_input["remote_user"];
86 return s.string() ? s.string() : "";
88 std::string getRemoteAddr() const {
89 DDF s = m_input["client_addr"];
90 return s.string() ? s.string() : "";
93 const std::vector<XSECCryptoX509*>& getClientCertificates() const;
96 const char* getMethod() const {
97 return m_input["method"].string();
99 const char* getRequestURI() const {
100 return m_input["uri"].string();
102 const char* getRequestURL() const {
103 return m_input["url"].string();
105 const char* getQueryString() const {
106 return m_input["query"].string();
108 std::string getHeader(const char* name) const {
109 DDF s = m_input["headers"][name];
110 return s.string() ? s.string() : "";
114 class SHIBSP_DLLLOCAL RemotedResponse : public virtual HTTPResponse
118 RemotedResponse(DDF& output) : m_output(output) {}
119 virtual ~RemotedResponse() {}
122 long sendResponse(std::istream& inputStream, long status);
125 void setResponseHeader(const char* name, const char* value);
126 long sendRedirect(const char* url);
130 const char* RemotedRequest::getParameter(const char* name) const
133 m_parser=new CGIParser(*this);
135 pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
136 return (bounds.first==bounds.second) ? NULL : bounds.first->second;
139 std::vector<const char*>::size_type RemotedRequest::getParameters(const char* name, std::vector<const char*>& values) const
142 m_parser=new CGIParser(*this);
144 pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
145 while (bounds.first!=bounds.second) {
146 values.push_back(bounds.first->second);
149 return values.size();
152 const std::vector<XSECCryptoX509*>& RemotedRequest::getClientCertificates() const
154 if (m_certs.empty()) {
155 DDF certs = m_input["certificates"];
156 DDF cert = certs.first();
157 while (cert.string()) {
159 auto_ptr<XSECCryptoX509> x509(XSECPlatformUtils::g_cryptoProvider->X509());
160 if (strstr(cert.string(), "BEGIN"))
161 x509->loadX509PEM(cert.string(), cert.strlen());
163 x509->loadX509Base64Bin(cert.string(), cert.strlen());
164 m_certs.push_back(x509.release());
166 catch(XSECException& e) {
167 auto_ptr_char temp(e.getMsg());
168 Category::getInstance(SHIBSP_LOGCAT".SPRequest").error("XML-Security exception loading client certificate: %s", temp.get());
170 catch(XSECCryptoException& e) {
171 Category::getInstance(SHIBSP_LOGCAT".SPRequest").error("XML-Security exception loading client certificate: %s", e.getMsg());
179 long RemotedResponse::sendResponse(std::istream& in, long status)
185 msg.append(buf,in.gcount());
187 if (!m_output.isstruct())
188 m_output.structure();
189 m_output.addmember("response.data").string(msg.c_str());
190 m_output.addmember("response.status").integer(status);
194 void RemotedResponse::setResponseHeader(const char* name, const char* value)
196 if (!m_output.isstruct())
197 m_output.structure();
198 DDF hdrs = m_output["headers"];
200 hdrs = m_output.addmember("headers").list();
201 DDF h = DDF(name).string(value);
205 long RemotedResponse::sendRedirect(const char* url)
207 if (!m_output.isstruct())
208 m_output.structure();
209 m_output.addmember("redirect").string(url);
210 return HTTPResponse::XMLTOOLING_HTTP_STATUS_MOVED;
215 void RemotedHandler::setAddress(const char* address)
217 if (!m_address.empty())
218 throw ConfigurationException("Cannot register a remoting address twice for the same Handler.");
220 SPConfig& conf = SPConfig::getConfig();
221 if (!conf.isEnabled(SPConfig::InProcess)) {
222 ListenerService* listener = conf.getServiceProvider()->getListenerService(false);
224 listener->regListener(m_address.c_str(),this);
226 Category::getInstance(SHIBSP_LOGCAT".Handler").info("no ListenerService available, handler remoting disabled");
230 RemotedHandler::~RemotedHandler()
232 SPConfig& conf = SPConfig::getConfig();
233 ListenerService* listener=conf.getServiceProvider()->getListenerService(false);
234 if (listener && conf.isEnabled(SPConfig::OutOfProcess) && !conf.isEnabled(SPConfig::InProcess))
235 listener->unregListener(m_address.c_str(),this);
238 DDF RemotedHandler::wrap(const SPRequest& request, const vector<string>* headers, bool certs) const
240 DDF in = DDF(m_address.c_str()).structure();
241 in.addmember("application_id").string(request.getApplication().getId());
242 in.addmember("scheme").string(request.getScheme());
243 in.addmember("hostname").string(request.getHostname());
244 in.addmember("port").integer(request.getPort());
245 in.addmember("content_type").string(request.getContentType().c_str());
246 in.addmember("content_length").integer(request.getContentLength());
247 in.addmember("body").string(request.getRequestBody());
248 in.addmember("remote_user").string(request.getRemoteUser().c_str());
249 in.addmember("client_addr").string(request.getRemoteAddr().c_str());
250 in.addmember("method").string(request.getMethod());
251 in.addmember("uri").string(request.getRequestURI());
252 in.addmember("url").string(request.getRequestURL());
253 in.addmember("query").string(request.getQueryString());
257 DDF hin = in.addmember("headers").structure();
258 for (vector<string>::const_iterator h = headers->begin(); h!=headers->end(); ++h) {
259 hdr = request.getHeader(h->c_str());
261 hin.addmember(h->c_str()).string(hdr.c_str());
267 const vector<XSECCryptoX509*>& xvec = request.getClientCertificates();
269 DDF clist = in.addmember("certificates").list();
270 for (vector<XSECCryptoX509*>::const_iterator x = xvec.begin(); x!=xvec.end(); ++x) {
271 DDF x509 = DDF(NULL).string((*x)->getDEREncodingSB().rawCharBuffer());
276 const vector<string>& xvec = request.getClientCertificates();
278 DDF clist = in.addmember("certificates").list();
279 for (vector<string>::const_iterator x = xvec.begin(); x!=xvec.end(); ++x) {
280 DDF x509 = DDF(NULL).string(x->c_str());
290 pair<bool,long> RemotedHandler::unwrap(SPRequest& request, DDF& out) const
292 DDF h = out["headers"];
294 while (hdr.isstring()) {
295 #ifdef HAVE_STRCASECMP
296 if (!strcasecmp(hdr.name(), "Content-Type"))
298 if (!stricmp(hdr.name(), "Content-Type"))
300 request.setContentType(hdr.string());
302 request.setResponseHeader(hdr.name(), hdr.string());
307 return make_pair(true, request.sendRedirect(h.string()));
310 istringstream s(h["data"].string());
311 return make_pair(true, request.sendResponse(s, h["status"].integer()));
313 return make_pair(false,0L);
316 HTTPRequest* RemotedHandler::getRequest(DDF& in) const
319 return new RemotedRequest(in);
321 throw ConfigurationException("Cannot process message using lite version of shibsp library.");
325 HTTPResponse* RemotedHandler::getResponse(DDF& out) const
328 return new RemotedResponse(out);
330 throw ConfigurationException("Cannot process message using lite version of shibsp library.");