Session dumping handler.
[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 = request.getSession();
112     if (!session) {
113         s << "A valid session was not found.</pre></body></html>" << endl;
114         request.setContentType("text/html");
115         request.setResponseHeader("Expires","01-Jan-1997 12:00:00 GMT");
116         request.setResponseHeader("Cache-Control","private,no-store,no-cache");
117         return make_pair(true, request.sendResponse(s));
118     }
119
120     s << "<u>Miscellaneous</u>" << endl;
121
122     s << "<strong>Client Address</strong>: " << (session->getClientAddress() ? session->getClientAddress() : "(none)") << endl;
123     s << "<strong>Identity Provider</strong>: " << (session->getEntityID() ? session->getEntityID() : "(none)") << endl;
124     s << "<strong>SSO Protocol</strong>: " << (session->getProtocol() ? session->getProtocol() : "(none)") << endl;
125     s << "<strong>Authentication Time</strong>: " << (session->getAuthnInstant() ? session->getAuthnInstant() : "(none)") << endl;
126     s << "<strong>Authentication Context Class</strong>: " << (session->getAuthnContextClassRef() ? session->getAuthnContextClassRef() : "(none)") << endl;
127     s << "<strong>Authentication Context Decl</strong>: " << (session->getAuthnContextDeclRef() ? session->getAuthnContextDeclRef() : "(none)") << endl;
128     s << "<strong>Session Expiration (barring inactivity)</strong>: ";
129     if (session->getExpiration())
130         s << ((session->getExpiration() - time(NULL)) / 60) << " minute(s)" << endl;
131     else
132         s << "Infinite" << endl;
133     
134     s << endl << "<u>Attributes</u>" << endl;
135
136     string key;
137     vector<string>::size_type count=0;
138     const multimap<string,const Attribute*>& attributes = session->getIndexedAttributes();
139     for (multimap<string,const Attribute*>::const_iterator a = attributes.begin(); a != attributes.end(); ++a) {
140         if (a->first != key) {
141             if (a != attributes.begin()) {
142                 if (m_values)
143                     s << endl;
144                 else {
145                     s << count << " value(s)" << endl;
146                     count = 0;
147                 }
148             }
149             s << "<strong>" << a->second->getId() << "</strong>: ";
150         }
151
152         if (m_values) {
153             const vector<string>& vals = a->second->getSerializedValues();
154             for (vector<string>::const_iterator v = vals.begin(); v!=vals.end(); ++v) {
155                 if (v != vals.begin() || a->first == key)
156                     s << ';';
157                 string::size_type pos = v->find_first_of(';',string::size_type(0));
158                 if (pos!=string::npos) {
159                     string value(*v);
160                     for (; pos != string::npos; pos = value.find_first_of(';',pos)) {
161                         value.insert(pos, "\\");
162                         pos += 2;
163                     }
164                     s << value;
165                 }
166                 else {
167                     s << *v;
168                 }
169             }
170         }
171         else {
172             count += a->second->getSerializedValues().size();
173         }
174     }
175
176     if (!m_values && !attributes.empty())
177         s << count << " value(s)" << endl;
178     
179     s << "</pre></body></html>";
180     request.setContentType("text/html; charset=UTF-8");
181     request.setResponseHeader("Expires","01-Jan-1997 12:00:00 GMT");
182     request.setResponseHeader("Cache-Control","private,no-store,no-cache");
183     return make_pair(true, request.sendResponse(s));
184 }