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 * Handler for exposing information about the internals of the SP.
24 #include "Application.h"
25 #include "exceptions.h"
26 #include "ServiceProvider.h"
27 #include "handler/AbstractHandler.h"
28 #include "handler/RemotedHandler.h"
29 #include "util/CGIParser.h"
31 using namespace shibsp;
33 # include "SessionCache.h"
34 # include <saml/version.h>
35 using namespace opensaml::saml2md;
36 using namespace opensaml;
37 using namespace xmlsignature;
39 using namespace xmltooling;
44 #if defined (_MSC_VER)
45 #pragma warning( push )
46 #pragma warning( disable : 4250 )
49 class SHIBSP_DLLLOCAL Blocker : public DOMNodeFilter
52 short acceptNode(const DOMNode* node) const {
57 static SHIBSP_DLLLOCAL Blocker g_Blocker;
59 class SHIBSP_API StatusHandler : public AbstractHandler, public RemotedHandler
62 StatusHandler(const DOMElement* e, const char* appId);
63 virtual ~StatusHandler() {}
65 pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
66 void receive(DDF& in, ostream& out);
69 pair<bool,long> processMessage(const Application& application, const HTTPRequest& httpRequest, HTTPResponse& httpResponse) const;
74 #if defined (_MSC_VER)
75 #pragma warning( pop )
78 Handler* SHIBSP_DLLLOCAL StatusHandlerFactory(const pair<const DOMElement*,const char*>& p)
80 return new StatusHandler(p.first, p.second);
83 #ifndef XMLTOOLING_NO_XMLSEC
84 vector<XSECCryptoX509*> g_NoCerts;
86 vector<string> g_NoCerts;
89 class DummyRequest : public HTTPRequest
92 DummyRequest(const char* url) : m_parser(NULL), m_url(url), m_scheme(NULL), m_query(NULL), m_port(0) {
93 #ifdef HAVE_STRCASECMP
94 if (url && !strncasecmp(url,"http://",7)) {
98 else if (url && !strncasecmp(url,"https://",8)) {
104 if (url && !strnicmp(url,"http://",7)) {
109 else if (url && !strnicmp(url,"https://",8)) {
116 throw invalid_argument("Target parameter was not an absolute URL.");
118 m_query = strchr(url,'?');
122 const char* slash = strchr(url, '/');
123 const char* colon = strchr(url, ':');
124 if (colon && colon < slash) {
125 m_hostname.assign(url, colon-url);
126 string port(colon + 1, slash - colon);
127 m_port = atoi(port.c_str());
130 m_hostname.assign(url, slash - url);
139 const char* getRequestURL() const {
142 const char* getScheme() const {
145 const char* getHostname() const {
146 return m_hostname.c_str();
148 int getPort() const {
151 const char* getRequestURI() const {
152 return m_uri.c_str();
154 const char* getMethod() const {
157 string getContentType() const {
160 long getContentLength() const {
163 string getRemoteAddr() const {
166 string getRemoteUser() const {
169 const char* getRequestBody() const {
172 const char* getQueryString() const {
175 const char* getParameter(const char* name) const
178 m_parser=new CGIParser(*this);
180 pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
181 return (bounds.first==bounds.second) ? NULL : bounds.first->second;
183 vector<const char*>::size_type getParameters(const char* name, vector<const char*>& values) const
186 m_parser=new CGIParser(*this);
188 pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
189 while (bounds.first!=bounds.second) {
190 values.push_back(bounds.first->second);
193 return values.size();
195 string getHeader(const char* name) const {
199 #ifndef XMLTOOLING_NO_XMLSEC
200 std::vector<XSECCryptoX509*>&
202 std::vector<std::string>&
204 getClientCertificates() const {
209 mutable CGIParser* m_parser;
211 const char* m_scheme;
214 string m_hostname,m_uri;
218 StatusHandler::StatusHandler(const DOMElement* e, const char* appId)
219 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".StatusHandler"), &g_Blocker)
221 string address(appId);
222 address += getString("Location").second;
223 setAddress(address.c_str());
224 if (SPConfig::getConfig().isEnabled(SPConfig::InProcess)) {
225 pair<bool,const char*> acl = getString("acl");
227 string aclbuf=acl.second;
229 for (unsigned int i=0; i < aclbuf.length(); i++) {
230 if (aclbuf.at(i)==' ') {
231 m_acl.insert(aclbuf.substr(j, i-j));
235 m_acl.insert(aclbuf.substr(j, aclbuf.length()-j));
240 pair<bool,long> StatusHandler::run(SPRequest& request, bool isHandler) const
242 SPConfig& conf = SPConfig::getConfig();
243 if (conf.isEnabled(SPConfig::InProcess)) {
244 if (!m_acl.empty() && m_acl.count(request.getRemoteAddr()) == 0) {
245 m_log.error("status handler request blocked from invalid address (%s)", request.getRemoteAddr().c_str());
246 istringstream msg("Status Handler Blocked");
247 return make_pair(true,request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_UNAUTHORIZED));
251 const char* target = request.getParameter("target");
253 // RequestMap query, so handle it inproc.
254 DummyRequest dummy(target);
255 RequestMapper::Settings settings = request.getApplication().getServiceProvider().getRequestMapper()->getSettings(dummy);
256 map<string,const char*> props;
257 settings.first->getAll(props);
259 request.setContentType("text/xml");
261 msg << "<StatusHandler>";
262 msg << "<Version Xerces-C='" << XERCES_FULLVERSIONDOT
264 << "' XML-Security-C='" << XSEC_FULLVERSIONDOT
265 << "' OpenSAML-C='" << OPENSAML_FULLVERSIONDOT
267 << "' Shibboleth='" << PACKAGE_VERSION << "'/>";
268 msg << "<RequestSettings";
269 for (map<string,const char*>::const_iterator p = props.begin(); p != props.end(); ++p)
270 msg << ' ' << p->first << "='" << p->second << "'";
271 msg << '>' << target << "</RequestSettings>";
272 msg << "<Status><OK/></Status>";
273 msg << "</StatusHandler>";
274 return make_pair(true,request.sendResponse(msg));
278 if (conf.isEnabled(SPConfig::OutOfProcess)) {
279 // When out of process, we run natively and directly process the message.
280 return processMessage(request.getApplication(), request, request);
283 // When not out of process, we remote all the message processing.
284 DDF out,in = wrap(request);
285 DDFJanitor jin(in), jout(out);
286 out=request.getServiceProvider().getListenerService()->send(in);
287 return unwrap(request, out);
290 catch (XMLToolingException& ex) {
291 m_log.error("error while processing request: %s", ex.what());
292 request.setContentType("text/xml");
294 msg << "<StatusHandler>";
295 msg << "<Version Xerces-C='" << XERCES_FULLVERSIONDOT
297 << "' XML-Security-C='" << XSEC_FULLVERSIONDOT
298 << "' OpenSAML-C='" << OPENSAML_FULLVERSIONDOT
300 << "' Shibboleth='" << PACKAGE_VERSION << "'/>";
301 msg << "<Status><Exception type='" << ex.getClassName() << "'>" << ex.what() << "</Exception></Status>";
302 msg << "</StatusHandler>";
303 return make_pair(true,request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_ERROR));
305 catch (exception& ex) {
306 m_log.error("error while processing request: %s", ex.what());
307 request.setContentType("text/xml");
309 msg << "<StatusHandler>";
310 msg << "<Version Xerces-C='" << XERCES_FULLVERSIONDOT
312 << "' XML-Security-C='" << XSEC_FULLVERSIONDOT
313 << "' OpenSAML-C='" << OPENSAML_FULLVERSIONDOT
315 << "' Shibboleth='" << PACKAGE_VERSION << "'/>";
316 msg << "<Status><Exception type='std::exception'>" << ex.what() << "</Exception></Status>";
317 msg << "</StatusHandler>";
318 return make_pair(true,request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_ERROR));
322 void StatusHandler::receive(DDF& in, ostream& out)
325 const char* aid=in["application_id"].string();
326 const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : NULL;
328 // Something's horribly wrong.
329 m_log.error("couldn't find application (%s) for status request", aid ? aid : "(missing)");
330 throw ConfigurationException("Unable to locate application for status request, deleted?");
333 // Wrap a response shim.
335 DDFJanitor jout(ret);
336 auto_ptr<HTTPRequest> req(getRequest(in));
337 auto_ptr<HTTPResponse> resp(getResponse(ret));
339 // Since we're remoted, the result should either be a throw, a false/0 return,
340 // which we just return as an empty structure, or a response/redirect,
341 // which we capture in the facade and send back.
342 processMessage(*app, *req.get(), *resp.get());
346 pair<bool,long> StatusHandler::processMessage(
347 const Application& application, const HTTPRequest& httpRequest, HTTPResponse& httpResponse
351 m_log.debug("processing status request");
354 s << "<StatusHandler>";
355 const char* status = "<OK/>";
357 s << "<Version Xerces-C='" << XERCES_FULLVERSIONDOT
358 << "' XML-Security-C='" << XSEC_FULLVERSIONDOT
359 << "' OpenSAML-C='" << OPENSAML_FULLVERSIONDOT
360 << "' Shibboleth='" << PACKAGE_VERSION << "'/>";
362 const char* param = NULL;
366 // General configuration and status report.
368 SessionCache* sc = application.getServiceProvider().getSessionCache(false);
371 s << "<SessionCache><OK/></SessionCache>";
374 s << "<SessionCache><None/></SessionCache>";
377 catch (XMLToolingException& ex) {
378 s << "<SessionCache><Exception type='" << ex.getClassName() << "'>" << ex.what() << "</Exception></SessionCache>";
379 status = "<Partial/>";
381 catch (exception& ex) {
382 s << "<SessionCache><Exception type='std::exception'>" << ex.what() << "</Exception></SessionCache>";
383 status = "<Partial/>";
386 s << "<Application id='" << application.getId() << "' entityID='" << application.getString("entityID").second << "'/>";
389 vector<const Handler*> handlers;
390 application.getHandlers(handlers);
391 for (vector<const Handler*>::const_iterator h = handlers.begin(); h != handlers.end(); ++h) {
392 s << "<Handler type='" << (*h)->getType() << "' Location='" << (*h)->getString("Location").second << "'";
393 if ((*h)->getString("Binding").first)
394 s << " Binding='" << (*h)->getString("Binding").second << "'";
399 const PropertySet* relyingParty=NULL;
400 param=httpRequest.getParameter("entityID");
402 MetadataProvider* m = application.getMetadataProvider();
404 relyingParty = application.getRelyingParty(m->getEntityDescriptor(MetadataProvider::Criteria(param)).first);
407 relyingParty = application.getRelyingParty(NULL);
408 CredentialResolver* credResolver=application.getCredentialResolver();
410 Locker credLocker(credResolver);
411 CredentialCriteria cc;
412 cc.setUsage(Credential::SIGNING_CREDENTIAL);
413 pair<bool,const char*> keyName = relyingParty->getString("keyName");
415 cc.getKeyNames().insert(keyName.second);
416 vector<const Credential*> creds;
417 credResolver->resolve(creds,&cc);
418 for (vector<const Credential*>::const_iterator c = creds.begin(); c != creds.end(); ++c) {
419 KeyInfo* kinfo = (*c)->getKeyInfo();
421 auto_ptr<KeyDescriptor> kd(KeyDescriptorBuilder::buildKeyDescriptor());
422 kd->setUse(KeyDescriptor::KEYTYPE_SIGNING);
423 kd->setKeyInfo(kinfo);
428 cc.setUsage(Credential::ENCRYPTION_CREDENTIAL);
430 cc.getKeyNames().clear();
431 credResolver->resolve(creds,&cc);
432 for (vector<const Credential*>::const_iterator c = creds.begin(); c != creds.end(); ++c) {
433 KeyInfo* kinfo = (*c)->getKeyInfo();
435 auto_ptr<KeyDescriptor> kd(KeyDescriptorBuilder::buildKeyDescriptor());
436 kd->setUse(KeyDescriptor::KEYTYPE_ENCRYPTION);
437 kd->setKeyInfo(kinfo);
444 s << "<Status>" << status << "</Status></StatusHandler>";
446 httpResponse.setContentType("text/xml");
447 return make_pair(true, httpResponse.sendResponse(s));
449 return make_pair(false,0L);