Catch errors retrieving session.
[shibboleth/cpp-sp.git] / shibsp / handler / impl / SessionHandler.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  * SessionHandler.cpp
19  * 
20  * Handler for dumping information about an active session.
21  */
22
23 #include "internal.h"
24 #include "Application.h"
25 #include "exceptions.h"
26 #include "ServiceProvider.h"
27 #include "SessionCache.h"
28 #include "SPRequest.h"
29 #include "attribute/Attribute.h"
30 #include "handler/AbstractHandler.h"
31
32 #include <ctime>
33
34 using namespace shibsp;
35 using namespace xmltooling;
36 using namespace std;
37
38 namespace shibsp {
39
40 #if defined (_MSC_VER)
41     #pragma warning( push )
42     #pragma warning( disable : 4250 )
43 #endif
44
45     class SHIBSP_DLLLOCAL Blocker : public DOMNodeFilter
46     {
47     public:
48         short acceptNode(const DOMNode* node) const {
49             return FILTER_REJECT;
50         }
51     };
52
53     static SHIBSP_DLLLOCAL Blocker g_Blocker;
54
55     class SHIBSP_API SessionHandler : public AbstractHandler
56     {
57     public:
58         SessionHandler(const DOMElement* e, const char* appId);
59         virtual ~SessionHandler() {}
60
61         pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
62
63     private:
64         bool m_values;
65         set<string> m_acl;
66     };
67
68 #if defined (_MSC_VER)
69     #pragma warning( pop )
70 #endif
71
72     Handler* SHIBSP_DLLLOCAL SessionHandlerFactory(const pair<const DOMElement*,const char*>& p)
73     {
74         return new SessionHandler(p.first, p.second);
75     }
76
77 };
78
79 SessionHandler::SessionHandler(const DOMElement* e, const char* appId)
80     : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".SessionHandler"), &g_Blocker), m_values(false)
81 {
82     pair<bool,const char*> acl = getString("acl");
83     if (acl.first) {
84         string aclbuf=acl.second;
85         int j = 0;
86         for (unsigned int i=0;  i < aclbuf.length();  i++) {
87             if (aclbuf.at(i)==' ') {
88                 m_acl.insert(aclbuf.substr(j, i-j));
89                 j = i+1;
90             }
91         }
92         m_acl.insert(aclbuf.substr(j, aclbuf.length()-j));
93     }
94
95     pair<bool,bool> flag = getBool("showAttributeValues");
96     if (flag.first)
97         m_values = flag.second;
98 }
99
100 pair<bool,long> SessionHandler::run(SPRequest& request, bool isHandler) const
101 {
102     if (!m_acl.empty() && m_acl.count(request.getRemoteAddr()) == 0) {
103         m_log.error("session handler request blocked from invalid address (%s)", request.getRemoteAddr().c_str());
104         istringstream msg("Session Handler Blocked");
105         return make_pair(true,request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_UNAUTHORIZED));
106     }
107
108     stringstream s;
109     s << "<html><head><title>Session Summary</title></head><body><pre>" << endl;
110
111     Session* session = NULL;
112     try {
113         session = request.getSession();
114         if (!session) {
115             s << "A valid session was not found.</pre></body></html>" << endl;
116             request.setContentType("text/html");
117             request.setResponseHeader("Expires","01-Jan-1997 12:00:00 GMT");
118             request.setResponseHeader("Cache-Control","private,no-store,no-cache");
119             return make_pair(true, request.sendResponse(s));
120         }
121     }
122     catch (exception& ex) {
123         s << "Exception while retrieving active session:" << endl
124             << '\t' << ex.what() << "</pre></body></html>" << endl;
125         request.setContentType("text/html");
126         request.setResponseHeader("Expires","01-Jan-1997 12:00:00 GMT");
127         request.setResponseHeader("Cache-Control","private,no-store,no-cache");
128         return make_pair(true, request.sendResponse(s));
129     }
130
131     s << "<u>Miscellaneous</u>" << endl;
132
133     s << "<strong>Client Address:</strong> " << (session->getClientAddress() ? session->getClientAddress() : "(none)") << endl;
134     s << "<strong>Identity Provider:</strong> " << (session->getEntityID() ? session->getEntityID() : "(none)") << endl;
135     s << "<strong>SSO Protocol:</strong> " << (session->getProtocol() ? session->getProtocol() : "(none)") << endl;
136     s << "<strong>Authentication Time:</strong> " << (session->getAuthnInstant() ? session->getAuthnInstant() : "(none)") << endl;
137     s << "<strong>Authentication Context Class:</strong> " << (session->getAuthnContextClassRef() ? session->getAuthnContextClassRef() : "(none)") << endl;
138     s << "<strong>Authentication Context Decl:</strong> " << (session->getAuthnContextDeclRef() ? session->getAuthnContextDeclRef() : "(none)") << endl;
139     s << "<strong>Session Expiration (barring inactivity):</strong> ";
140     if (session->getExpiration())
141         s << ((session->getExpiration() - time(NULL)) / 60) << " minute(s)" << endl;
142     else
143         s << "Infinite" << endl;
144
145     s << endl << "<u>Attributes</u>" << endl;
146
147     string key;
148     vector<string>::size_type count=0;
149     const multimap<string,const Attribute*>& attributes = session->getIndexedAttributes();
150     for (multimap<string,const Attribute*>::const_iterator a = attributes.begin(); a != attributes.end(); ++a) {
151         if (a->first != key) {
152             if (a != attributes.begin()) {
153                 if (m_values)
154                     s << endl;
155                 else {
156                     s << count << " value(s)" << endl;
157                     count = 0;
158                 }
159             }
160             s << "<strong>" << a->second->getId() << "</strong>: ";
161         }
162
163         if (m_values) {
164             const vector<string>& vals = a->second->getSerializedValues();
165             for (vector<string>::const_iterator v = vals.begin(); v!=vals.end(); ++v) {
166                 if (v != vals.begin() || a->first == key)
167                     s << ';';
168                 string::size_type pos = v->find_first_of(';',string::size_type(0));
169                 if (pos!=string::npos) {
170                     string value(*v);
171                     for (; pos != string::npos; pos = value.find_first_of(';',pos)) {
172                         value.insert(pos, "\\");
173                         pos += 2;
174                     }
175                     s << value;
176                 }
177                 else {
178                     s << *v;
179                 }
180             }
181         }
182         else {
183             count += a->second->getSerializedValues().size();
184         }
185     }
186
187     if (!m_values && !attributes.empty())
188         s << count << " value(s)" << endl;
189     
190     s << "</pre></body></html>";
191     request.setContentType("text/html; charset=UTF-8");
192     request.setResponseHeader("Expires","01-Jan-1997 12:00:00 GMT");
193     request.setResponseHeader("Cache-Control","private,no-store,no-cache");
194     return make_pair(true, request.sendResponse(s));
195 }