2 * Copyright 2001-2010 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 "SPRequest.h"
28 #include "handler/AbstractHandler.h"
29 #include "handler/RemotedHandler.h"
30 #include "util/CGIParser.h"
32 #include <xmltooling/version.h>
34 #ifdef HAVE_SYS_UTSNAME_H
35 # include <sys/utsname.h>
38 using namespace shibsp;
40 # include "SessionCache.h"
41 # include "metadata/MetadataProviderCriteria.h"
42 # include <saml/version.h>
43 # include <saml/saml2/metadata/Metadata.h>
44 # include <xmltooling/security/Credential.h>
45 # include <xmltooling/security/CredentialCriteria.h>
46 using namespace opensaml::saml2md;
47 using namespace opensaml;
48 using namespace xmlsignature;
50 using namespace xmltooling;
55 #if defined (_MSC_VER)
56 #pragma warning( push )
57 #pragma warning( disable : 4250 )
60 class SHIBSP_DLLLOCAL Blocker : public DOMNodeFilter
63 #ifdef SHIBSP_XERCESC_SHORT_ACCEPTNODE
68 acceptNode(const DOMNode* node) const {
73 static SHIBSP_DLLLOCAL Blocker g_Blocker;
75 class SHIBSP_API StatusHandler : public AbstractHandler, public RemotedHandler
78 StatusHandler(const DOMElement* e, const char* appId);
79 virtual ~StatusHandler() {}
81 pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
82 void receive(DDF& in, ostream& out);
85 pair<bool,long> processMessage(const Application& application, const HTTPRequest& httpRequest, HTTPResponse& httpResponse) const;
86 ostream& systemInfo(ostream& os) const;
91 #if defined (_MSC_VER)
92 #pragma warning( pop )
95 Handler* SHIBSP_DLLLOCAL StatusHandlerFactory(const pair<const DOMElement*,const char*>& p)
97 return new StatusHandler(p.first, p.second);
100 #ifndef XMLTOOLING_NO_XMLSEC
101 vector<XSECCryptoX509*> g_NoCerts;
103 vector<string> g_NoCerts;
106 static char _x2c(const char *what)
110 digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
112 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
116 class DummyRequest : public HTTPRequest
119 DummyRequest(const char* url) : m_parser(nullptr), m_url(url), m_scheme(nullptr), m_query(nullptr), m_port(0) {
120 #ifdef HAVE_STRCASECMP
121 if (url && !strncasecmp(url,"http://",7)) {
125 else if (url && !strncasecmp(url,"https://",8)) {
131 if (url && !strnicmp(url,"http://",7)) {
136 else if (url && !strnicmp(url,"https://",8)) {
143 throw invalid_argument("Target parameter was not an absolute URL.");
145 m_query = strchr(url,'?');
149 const char* slash = strchr(url, '/');
150 const char* colon = strchr(url, ':');
151 if (colon && colon < slash) {
152 m_hostname.assign(url, colon-url);
153 string port(colon + 1, slash - colon);
154 m_port = atoi(port.c_str());
157 m_hostname.assign(url, slash - url);
165 else if (*slash != '%') {
170 if (!isxdigit(*slash) || !isxdigit(*(slash+1)))
171 throw invalid_argument("Bad request, contained unsupported encoded characters.");
172 m_uri += _x2c(slash);
183 const char* getRequestURL() const {
186 const char* getScheme() const {
189 const char* getHostname() const {
190 return m_hostname.c_str();
192 int getPort() const {
195 const char* getRequestURI() const {
196 return m_uri.c_str();
198 const char* getMethod() const {
201 string getContentType() const {
204 long getContentLength() const {
207 string getRemoteAddr() const {
210 string getRemoteUser() const {
213 const char* getRequestBody() const {
216 const char* getQueryString() const {
219 const char* getParameter(const char* name) const
222 m_parser=new CGIParser(*this);
224 pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
225 return (bounds.first==bounds.second) ? nullptr : bounds.first->second;
227 vector<const char*>::size_type getParameters(const char* name, vector<const char*>& values) const
230 m_parser=new CGIParser(*this);
232 pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
233 while (bounds.first!=bounds.second) {
234 values.push_back(bounds.first->second);
237 return values.size();
239 string getHeader(const char* name) const {
243 #ifndef XMLTOOLING_NO_XMLSEC
244 std::vector<XSECCryptoX509*>&
246 std::vector<std::string>&
248 getClientCertificates() const {
253 mutable CGIParser* m_parser;
255 const char* m_scheme;
258 string m_hostname,m_uri;
262 StatusHandler::StatusHandler(const DOMElement* e, const char* appId)
263 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".StatusHandler"), &g_Blocker)
265 string address(appId);
266 address += getString("Location").second;
267 setAddress(address.c_str());
268 if (SPConfig::getConfig().isEnabled(SPConfig::InProcess)) {
269 pair<bool,const char*> acl = getString("acl");
271 string aclbuf=acl.second;
273 for (unsigned int i=0; i < aclbuf.length(); i++) {
274 if (aclbuf.at(i)==' ') {
275 m_acl.insert(aclbuf.substr(j, i-j));
279 m_acl.insert(aclbuf.substr(j, aclbuf.length()-j));
284 pair<bool,long> StatusHandler::run(SPRequest& request, bool isHandler) const
286 SPConfig& conf = SPConfig::getConfig();
287 if (conf.isEnabled(SPConfig::InProcess)) {
288 if (!m_acl.empty() && m_acl.count(request.getRemoteAddr()) == 0) {
289 m_log.error("status handler request blocked from invalid address (%s)", request.getRemoteAddr().c_str());
290 istringstream msg("Status Handler Blocked");
291 return make_pair(true,request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_FORBIDDEN));
295 const char* target = request.getParameter("target");
297 // RequestMap query, so handle it inproc.
298 DummyRequest dummy(target);
299 RequestMapper::Settings settings = request.getApplication().getServiceProvider().getRequestMapper()->getSettings(dummy);
300 map<string,const char*> props;
301 settings.first->getAll(props);
303 request.setContentType("text/xml");
305 msg << "<StatusHandler>";
306 msg << "<Version Xerces-C='" << XERCES_FULLVERSIONDOT
307 << "' XML-Tooling-C='" << XMLTOOLING_FULLVERSIONDOT
309 << "' XML-Security-C='" << XSEC_FULLVERSIONDOT
310 << "' OpenSAML-C='" << OPENSAML_FULLVERSIONDOT
312 << "' Shibboleth='" << PACKAGE_VERSION << "'/>";
313 systemInfo(msg) << "<RequestSettings";
314 for (map<string,const char*>::const_iterator p = props.begin(); p != props.end(); ++p)
315 msg << ' ' << p->first << "='" << p->second << "'";
316 msg << '>' << target << "</RequestSettings>";
317 msg << "<Status><OK/></Status>";
318 msg << "</StatusHandler>";
319 return make_pair(true,request.sendResponse(msg));
323 if (conf.isEnabled(SPConfig::OutOfProcess)) {
324 // When out of process, we run natively and directly process the message.
325 return processMessage(request.getApplication(), request, request);
328 // When not out of process, we remote all the message processing.
329 DDF out,in = wrap(request);
330 DDFJanitor jin(in), jout(out);
331 out=request.getServiceProvider().getListenerService()->send(in);
332 return unwrap(request, out);
335 catch (XMLToolingException& ex) {
336 m_log.error("error while processing request: %s", ex.what());
337 request.setContentType("text/xml");
339 msg << "<StatusHandler>";
340 msg << "<Version Xerces-C='" << XERCES_FULLVERSIONDOT
341 << "' XML-Tooling-C='" << XMLTOOLING_FULLVERSIONDOT
343 << "' XML-Security-C='" << XSEC_FULLVERSIONDOT
344 << "' OpenSAML-C='" << OPENSAML_FULLVERSIONDOT
346 << "' Shibboleth='" << PACKAGE_VERSION << "'/>";
347 systemInfo(msg) << "<Status><Exception type='" << ex.getClassName() << "'>" << ex.what() << "</Exception></Status>";
348 msg << "</StatusHandler>";
349 return make_pair(true,request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_ERROR));
351 catch (exception& ex) {
352 m_log.error("error while processing request: %s", ex.what());
353 request.setContentType("text/xml");
355 msg << "<StatusHandler>";
356 msg << "<Version Xerces-C='" << XERCES_FULLVERSIONDOT
357 << "' XML-Tooling-C='" << XMLTOOLING_FULLVERSIONDOT
359 << "' XML-Security-C='" << XSEC_FULLVERSIONDOT
360 << "' OpenSAML-C='" << OPENSAML_FULLVERSIONDOT
362 << "' Shibboleth='" << PACKAGE_VERSION << "'/>";
363 systemInfo(msg) << "<Status><Exception type='std::exception'>" << ex.what() << "</Exception></Status>";
364 msg << "</StatusHandler>";
365 return make_pair(true,request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_ERROR));
369 void StatusHandler::receive(DDF& in, ostream& out)
372 const char* aid=in["application_id"].string();
373 const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : nullptr;
375 // Something's horribly wrong.
376 m_log.error("couldn't find application (%s) for status request", aid ? aid : "(missing)");
377 throw ConfigurationException("Unable to locate application for status request, deleted?");
380 // Wrap a response shim.
382 DDFJanitor jout(ret);
383 auto_ptr<HTTPRequest> req(getRequest(in));
384 auto_ptr<HTTPResponse> resp(getResponse(ret));
386 // Since we're remoted, the result should either be a throw, a false/0 return,
387 // which we just return as an empty structure, or a response/redirect,
388 // which we capture in the facade and send back.
389 processMessage(*app, *req.get(), *resp.get());
393 pair<bool,long> StatusHandler::processMessage(
394 const Application& application, const HTTPRequest& httpRequest, HTTPResponse& httpResponse
398 m_log.debug("processing status request");
401 s << "<StatusHandler>";
402 const char* status = "<OK/>";
404 s << "<Version Xerces-C='" << XERCES_FULLVERSIONDOT
405 << "' XML-Tooling-C='" << XMLTOOLING_FULLVERSIONDOT
406 << "' XML-Security-C='" << XSEC_FULLVERSIONDOT
407 << "' OpenSAML-C='" << OPENSAML_FULLVERSIONDOT
408 << "' Shibboleth='" << PACKAGE_VERSION << "'/>";
412 const char* param = nullptr;
416 // General configuration and status report.
418 SessionCache* sc = application.getServiceProvider().getSessionCache(false);
421 s << "<SessionCache><OK/></SessionCache>";
424 s << "<SessionCache><None/></SessionCache>";
427 catch (XMLToolingException& ex) {
428 s << "<SessionCache><Exception type='" << ex.getClassName() << "'>" << ex.what() << "</Exception></SessionCache>";
429 status = "<Partial/>";
431 catch (exception& ex) {
432 s << "<SessionCache><Exception type='std::exception'>" << ex.what() << "</Exception></SessionCache>";
433 status = "<Partial/>";
436 const PropertySet* relyingParty=nullptr;
437 param=httpRequest.getParameter("entityID");
439 MetadataProvider* m = application.getMetadataProvider();
441 relyingParty = application.getRelyingParty(m->getEntityDescriptor(MetadataProviderCriteria(application, param)).first);
444 relyingParty = &application;
447 s << "<Application id='" << application.getId() << "' entityID='" << relyingParty->getString("entityID").second << "'/>";
450 vector<const Handler*> handlers;
451 application.getHandlers(handlers);
452 for (vector<const Handler*>::const_iterator h = handlers.begin(); h != handlers.end(); ++h) {
453 s << "<Handler type='" << (*h)->getType() << "' Location='" << (*h)->getString("Location").second << "'";
454 if ((*h)->getString("Binding").first)
455 s << " Binding='" << (*h)->getString("Binding").second << "'";
460 CredentialResolver* credResolver=application.getCredentialResolver();
462 Locker credLocker(credResolver);
463 CredentialCriteria cc;
464 cc.setUsage(Credential::SIGNING_CREDENTIAL);
465 pair<bool,const char*> keyName = relyingParty->getString("keyName");
467 cc.getKeyNames().insert(keyName.second);
468 vector<const Credential*> creds;
469 credResolver->resolve(creds,&cc);
470 for (vector<const Credential*>::const_iterator c = creds.begin(); c != creds.end(); ++c) {
471 KeyInfo* kinfo = (*c)->getKeyInfo();
473 auto_ptr<KeyDescriptor> kd(KeyDescriptorBuilder::buildKeyDescriptor());
474 kd->setUse(KeyDescriptor::KEYTYPE_SIGNING);
475 kd->setKeyInfo(kinfo);
480 cc.setUsage(Credential::ENCRYPTION_CREDENTIAL);
482 cc.getKeyNames().clear();
483 credResolver->resolve(creds,&cc);
484 for (vector<const Credential*>::const_iterator c = creds.begin(); c != creds.end(); ++c) {
485 KeyInfo* kinfo = (*c)->getKeyInfo();
487 auto_ptr<KeyDescriptor> kd(KeyDescriptorBuilder::buildKeyDescriptor());
488 kd->setUse(KeyDescriptor::KEYTYPE_ENCRYPTION);
489 kd->setKeyInfo(kinfo);
496 s << "<Status>" << status << "</Status></StatusHandler>";
498 httpResponse.setContentType("text/xml");
499 return make_pair(true, httpResponse.sendResponse(s));
501 return make_pair(false,0L);
505 typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
507 ostream& StatusHandler::systemInfo(ostream& os) const
509 #if defined(HAVE_SYS_UTSNAME_H)
510 struct utsname sysinfo;
511 if (uname(&sysinfo) == 0) {
513 if (*sysinfo.sysname)
514 os << " sysname='" << sysinfo.sysname << "'";
515 if (*sysinfo.nodename)
516 os << " nodename='" << sysinfo.nodename << "'";
517 if (*sysinfo.release)
518 os << " release='" << sysinfo.release << "'";
519 if (*sysinfo.version)
520 os << " version='" << sysinfo.version << "'";
521 if (*sysinfo.machine)
522 os << " machine='" << sysinfo.machine << "'";
526 OSVERSIONINFOEX osvi;
527 memset(&osvi, 0, sizeof(OSVERSIONINFOEX));
528 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
529 if(GetVersionEx((OSVERSIONINFO*)&osvi)) {
531 << " version='" << osvi.dwMajorVersion << "." << osvi.dwMinorVersion << "'"
532 << " build='" << osvi.dwBuildNumber << "'";
533 if (osvi.wServicePackMajor > 0)
534 os << " servicepack='" << osvi.wServicePackMajor << "." << osvi.wServicePackMinor << "'";
535 switch (osvi.wProductType) {
536 case VER_NT_WORKSTATION:
537 os << " producttype='Workstation'";
540 case VER_NT_DOMAIN_CONTROLLER:
541 os << " producttype='Server'";
546 memset(&si, 0, sizeof(SYSTEM_INFO));
547 PGNSI pGNSI = (PGNSI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
552 switch (si.dwProcessorType) {
553 case PROCESSOR_ARCHITECTURE_INTEL:
554 os << " arch='i386'";
556 case PROCESSOR_ARCHITECTURE_AMD64:
557 os << " arch='x86_64'";
559 case PROCESSOR_ARCHITECTURE_IA64:
560 os << " arch='IA64'";
563 os << " cpucount='" << si.dwNumberOfProcessors << "'";
566 memset(&ms, 0, sizeof(MEMORYSTATUSEX));
567 ms.dwLength = sizeof(MEMORYSTATUSEX);
568 if (GlobalMemoryStatusEx(&ms)) {
569 os << " memory='" << (ms.ullTotalPhys / (1024 * 1024)) << "M'";