Move credential usage enum to Credential class.
[shibboleth/cpp-sp.git] / shibsp / handler / impl / StatusHandler.cpp
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * StatusHandler.cpp
19  * 
20  * Handler for exposing information about the internals of the SP.
21  */
22
23 #include "internal.h"
24 #include "Application.h"
25 #include "exceptions.h"
26 #include "ServiceProvider.h"
27 #include "handler/AbstractHandler.h"
28 #include "handler/RemotedHandler.h"
29
30 using namespace shibsp;
31 #ifndef SHIBSP_LITE
32 # include "SessionCache.h"
33 # include <saml/version.h>
34 using namespace opensaml::saml2md;
35 using namespace opensaml;
36 #endif
37 using namespace xmltooling;
38 using namespace std;
39
40 namespace shibsp {
41
42 #if defined (_MSC_VER)
43     #pragma warning( push )
44     #pragma warning( disable : 4250 )
45 #endif
46
47     class SHIBSP_DLLLOCAL Blocker : public DOMNodeFilter
48     {
49     public:
50         short acceptNode(const DOMNode* node) const {
51             return FILTER_REJECT;
52         }
53     };
54
55     static SHIBSP_DLLLOCAL Blocker g_Blocker;
56
57     class SHIBSP_API StatusHandler : public AbstractHandler, public RemotedHandler
58     {
59     public:
60         StatusHandler(const DOMElement* e, const char* appId);
61         virtual ~StatusHandler() {}
62
63         pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
64         void receive(DDF& in, ostream& out);
65
66     private:
67         pair<bool,long> processMessage(const Application& application, const HTTPRequest& httpRequest, HTTPResponse& httpResponse) const;
68
69         set<string> m_acl;
70     };
71
72 #if defined (_MSC_VER)
73     #pragma warning( pop )
74 #endif
75
76     Handler* SHIBSP_DLLLOCAL StatusHandlerFactory(const pair<const DOMElement*,const char*>& p)
77     {
78         return new StatusHandler(p.first, p.second);
79     }
80
81 };
82
83 StatusHandler::StatusHandler(const DOMElement* e, const char* appId)
84     : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".StatusHandler"), &g_Blocker)
85 {
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");
91         if (acl.first) {
92             string aclbuf=acl.second;
93             int j = 0;
94             for (unsigned int i=0;  i < aclbuf.length();  i++) {
95                 if (aclbuf.at(i)==' ') {
96                     m_acl.insert(aclbuf.substr(j, i-j));
97                     j = i+1;
98                 }
99             }
100             m_acl.insert(aclbuf.substr(j, aclbuf.length()-j));
101         }
102     }
103 }
104
105 pair<bool,long> StatusHandler::run(SPRequest& request, bool isHandler) const
106 {
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));
113         }
114     }
115     
116     try {
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);
120         }
121         else {
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);
127         }
128     }
129     catch (XMLToolingException& ex) {
130         m_log.error("error while processing request: %s", ex.what());
131         request.setContentType("text/xml");
132         stringstream msg;
133         msg << "<StatusHandler>";
134             msg << "<Version Xerces-C='" << XERCES_FULLVERSIONDOT
135 #ifndef SHIBSP_LITE
136                 << "' XML-Security-C='" << XSEC_FULLVERSIONDOT
137                 << "' OpenSAML-C='" << OPENSAML_FULLVERSIONDOT
138 #endif
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));
143     }
144     catch (exception& ex) {
145         m_log.error("error while processing request: %s", ex.what());
146         request.setContentType("text/xml");
147         stringstream msg;
148         msg << "<StatusHandler>";
149             msg << "<Version Xerces-C='" << XERCES_FULLVERSIONDOT
150 #ifndef SHIBSP_LITE
151                 << "' XML-Security-C='" << XSEC_FULLVERSIONDOT
152                 << "' OpenSAML-C='" << OPENSAML_FULLVERSIONDOT
153 #endif
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));
158     }
159 }
160
161 void StatusHandler::receive(DDF& in, ostream& out)
162 {
163     // Find application.
164     const char* aid=in["application_id"].string();
165     const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : NULL;
166     if (!app) {
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?");
170     }
171     
172     // Wrap a response shim.
173     DDF ret(NULL);
174     DDFJanitor jout(ret);
175     auto_ptr<HTTPRequest> req(getRequest(in));
176     auto_ptr<HTTPResponse> resp(getResponse(ret));
177         
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());
182     out << ret;
183 }
184
185 pair<bool,long> StatusHandler::processMessage(
186     const Application& application, const HTTPRequest& httpRequest, HTTPResponse& httpResponse
187     ) const
188 {
189 #ifndef SHIBSP_LITE
190     m_log.debug("processing status request");
191
192     stringstream s;
193     s << "<StatusHandler>";
194     const char* status = "<OK/>";
195
196     s << "<Version Xerces-C='" << XERCES_FULLVERSIONDOT
197         << "' XML-Security-C='" << XSEC_FULLVERSIONDOT
198         << "' OpenSAML-C='" << OPENSAML_FULLVERSIONDOT
199         << "' Shibboleth='" << PACKAGE_VERSION << "'/>";
200
201     if (false) {
202     }
203     else {
204         // General configuration and status report.
205         try {
206             SessionCache* sc = application.getServiceProvider().getSessionCache(false);
207             if (sc) {
208                 sc->test();
209                 s << "<SessionCache><OK/></SessionCache>";
210             }
211             else {
212                 s << "<SessionCache><None/></SessionCache>";
213             }
214         }
215         catch (XMLToolingException& ex) {
216             s << "<SessionCache><Exception type='" << ex.getClassName() << "'>" << ex.what() << "</Exception></SessionCache>";
217             status = "<Partial/>";
218         }
219         catch (exception& ex) {
220             s << "<SessionCache><Exception type='std::exception'>" << ex.what() << "</Exception></SessionCache>";
221             status = "<Partial/>";
222         }
223     }
224
225     s << "<Application id='" << application.getId() << "' entityID='" << application.getString("entityID").second << "'/>";
226
227     s << "<Handlers>";
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 << "'";
234         s << "/>";
235     }
236     s << "</Handlers>";
237
238     s << "<Status>" << status << "</Status></StatusHandler>";
239
240     httpResponse.setContentType("text/xml");
241     return make_pair(true, httpResponse.sendResponse(s));
242 #else
243     return make_pair(false,0);
244 #endif
245 }