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"
30 using namespace shibsp;
32 # include "SessionCache.h"
33 # include <saml/version.h>
34 using namespace opensaml::saml2md;
35 using namespace opensaml;
37 using namespace xmltooling;
42 #if defined (_MSC_VER)
43 #pragma warning( push )
44 #pragma warning( disable : 4250 )
47 class SHIBSP_DLLLOCAL Blocker : public DOMNodeFilter
50 short acceptNode(const DOMNode* node) const {
55 static SHIBSP_DLLLOCAL Blocker g_Blocker;
57 class SHIBSP_API StatusHandler : public AbstractHandler, public RemotedHandler
60 StatusHandler(const DOMElement* e, const char* appId);
61 virtual ~StatusHandler() {}
63 pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
64 void receive(DDF& in, ostream& out);
67 pair<bool,long> processMessage(const Application& application, const HTTPRequest& httpRequest, HTTPResponse& httpResponse) const;
72 #if defined (_MSC_VER)
73 #pragma warning( pop )
76 Handler* SHIBSP_DLLLOCAL StatusHandlerFactory(const pair<const DOMElement*,const char*>& p)
78 return new StatusHandler(p.first, p.second);
83 StatusHandler::StatusHandler(const DOMElement* e, const char* appId)
84 : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".StatusHandler"), &g_Blocker)
86 string address(appId);
87 address += getString("Location").second;
88 setAddress(address.c_str());
89 if (SPConfig::getConfig().isEnabled(SPConfig::InProcess)) {
90 pair<bool,const char*> acl = getString("acl");
92 string aclbuf=acl.second;
94 for (unsigned int i=0; i < aclbuf.length(); i++) {
95 if (aclbuf.at(i)==' ') {
96 m_acl.insert(aclbuf.substr(j, i-j));
100 m_acl.insert(aclbuf.substr(j, aclbuf.length()-j));
105 pair<bool,long> StatusHandler::run(SPRequest& request, bool isHandler) const
107 SPConfig& conf = SPConfig::getConfig();
108 if (conf.isEnabled(SPConfig::InProcess)) {
109 if (!m_acl.empty() && m_acl.count(request.getRemoteAddr()) == 0) {
110 m_log.error("status handler request blocked from invalid address (%s)", request.getRemoteAddr().c_str());
111 istringstream msg("Status Handler Blocked");
112 return make_pair(true,request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_FORBIDDEN));
117 if (conf.isEnabled(SPConfig::OutOfProcess)) {
118 // When out of process, we run natively and directly process the message.
119 return processMessage(request.getApplication(), request, request);
122 // When not out of process, we remote all the message processing.
123 DDF out,in = wrap(request);
124 DDFJanitor jin(in), jout(out);
125 out=request.getServiceProvider().getListenerService()->send(in);
126 return unwrap(request, out);
129 catch (XMLToolingException& ex) {
130 m_log.error("error while processing request: %s", ex.what());
131 request.setContentType("text/xml");
133 msg << "<StatusHandler>";
134 msg << "<Version Xerces-C='" << XERCES_FULLVERSIONDOT
136 << "' XML-Security-C='" << XSEC_FULLVERSIONDOT
137 << "' OpenSAML-C='" << OPENSAML_FULLVERSIONDOT
139 << "' Shibboleth='" << PACKAGE_VERSION << "'/>";
140 msg << "<Status><Exception type='" << ex.getClassName() << "'>" << ex.what() << "</Exception></Status>";
141 msg << "</StatusHandler>";
142 return make_pair(true,request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_ERROR));
144 catch (exception& ex) {
145 m_log.error("error while processing request: %s", ex.what());
146 request.setContentType("text/xml");
148 msg << "<StatusHandler>";
149 msg << "<Version Xerces-C='" << XERCES_FULLVERSIONDOT
151 << "' XML-Security-C='" << XSEC_FULLVERSIONDOT
152 << "' OpenSAML-C='" << OPENSAML_FULLVERSIONDOT
154 << "' Shibboleth='" << PACKAGE_VERSION << "'/>";
155 msg << "<Status><Exception type='std::exception'>" << ex.what() << "</Exception></Status>";
156 msg << "</StatusHandler>";
157 return make_pair(true,request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_ERROR));
161 void StatusHandler::receive(DDF& in, ostream& out)
164 const char* aid=in["application_id"].string();
165 const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : NULL;
167 // Something's horribly wrong.
168 m_log.error("couldn't find application (%s) for status request", aid ? aid : "(missing)");
169 throw ConfigurationException("Unable to locate application for status request, deleted?");
172 // Wrap a response shim.
174 DDFJanitor jout(ret);
175 auto_ptr<HTTPRequest> req(getRequest(in));
176 auto_ptr<HTTPResponse> resp(getResponse(ret));
178 // Since we're remoted, the result should either be a throw, a false/0 return,
179 // which we just return as an empty structure, or a response/redirect,
180 // which we capture in the facade and send back.
181 processMessage(*app, *req.get(), *resp.get());
185 pair<bool,long> StatusHandler::processMessage(
186 const Application& application, const HTTPRequest& httpRequest, HTTPResponse& httpResponse
190 m_log.debug("processing status request");
193 s << "<StatusHandler>";
194 const char* status = "<OK/>";
196 s << "<Version Xerces-C='" << XERCES_FULLVERSIONDOT
197 << "' XML-Security-C='" << XSEC_FULLVERSIONDOT
198 << "' OpenSAML-C='" << OPENSAML_FULLVERSIONDOT
199 << "' Shibboleth='" << PACKAGE_VERSION << "'/>";
204 // General configuration and status report.
206 SessionCache* sc = application.getServiceProvider().getSessionCache(false);
209 s << "<SessionCache><OK/></SessionCache>";
212 s << "<SessionCache><None/></SessionCache>";
215 catch (XMLToolingException& ex) {
216 s << "<SessionCache><Exception type='" << ex.getClassName() << "'>" << ex.what() << "</Exception></SessionCache>";
217 status = "<Partial/>";
219 catch (exception& ex) {
220 s << "<SessionCache><Exception type='std::exception'>" << ex.what() << "</Exception></SessionCache>";
221 status = "<Partial/>";
225 s << "<Application id='" << application.getId() << "' entityID='" << application.getString("entityID").second << "'/>";
228 vector<const Handler*> handlers;
229 application.getHandlers(handlers);
230 for (vector<const Handler*>::const_iterator h = handlers.begin(); h != handlers.end(); ++h) {
231 s << "<Handler type='" << (*h)->getType() << "' Location='" << (*h)->getString("Location").second << "'";
232 if ((*h)->getString("Binding").first)
233 s << " Binding='" << (*h)->getString("Binding").second << "'";
238 s << "<Status>" << status << "</Status></StatusHandler>";
240 httpResponse.setContentType("text/xml");
241 return make_pair(true, httpResponse.sendResponse(s));
243 return make_pair(false,0);