2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * StorageServiceSessionCache.cpp
24 * StorageService-based SessionCache implementation.
26 * Instead of optimizing this plugin with a buffering scheme that keeps objects around
27 * and avoids extra parsing steps, I'm assuming that systems that require such can
28 * layer their own cache plugin on top of this version either by delegating to it
29 * or using the remoting support. So this version will load sessions directly
30 * from the StorageService, instantiate enough to expose the Session API,
31 * and then delete everything when they're unlocked. All data in memory is always
32 * kept in sync with the StorageService (no lazy updates).
36 #include "Application.h"
37 #include "exceptions.h"
38 #include "ServiceProvider.h"
39 #include "SessionCacheEx.h"
40 #include "TransactionLog.h"
41 #include "attribute/Attribute.h"
42 #include "handler/RemotedHandler.h"
43 #include "remoting/ListenerService.h"
44 #include "util/SPConstants.h"
47 #include <boost/bind.hpp>
48 #include <boost/shared_ptr.hpp>
49 #include <xmltooling/io/HTTPRequest.h>
50 #include <xmltooling/io/HTTPResponse.h>
51 #include <xmltooling/util/DateTime.h>
52 #include <xmltooling/util/NDC.h>
53 #include <xmltooling/util/ParserPool.h>
54 #include <xmltooling/util/Threads.h>
55 #include <xmltooling/util/XMLHelper.h>
56 #include <xercesc/util/XMLUniDefs.hpp>
59 # include <saml/exceptions.h>
60 # include <saml/SAMLConfig.h>
61 # include <saml/saml2/core/Assertions.h>
62 # include <saml/saml2/metadata/Metadata.h>
63 # include <xmltooling/XMLToolingConfig.h>
64 # include <xmltooling/util/StorageService.h>
65 # include <xercesc/util/XMLStringTokenizer.hpp>
66 using namespace opensaml::saml2md;
69 # include <xmltooling/util/DateTime.h>
72 using namespace shibsp;
73 using namespace opensaml;
74 using namespace xmltooling;
75 using namespace boost;
81 class SSCache : public SessionCacheEx
83 ,public virtual Remoted
87 SSCache(const DOMElement* e);
91 void receive(DDF& in, ostream& out);
94 const Application& app,
95 const HTTPRequest& httpRequest,
96 HTTPResponse& httpResponse,
98 const EntityDescriptor* issuer=nullptr,
99 const XMLCh* protocol=nullptr,
100 const saml2::NameID* nameid=nullptr,
101 const XMLCh* authn_instant=nullptr,
102 const XMLCh* session_index=nullptr,
103 const XMLCh* authncontext_class=nullptr,
104 const XMLCh* authncontext_decl=nullptr,
105 const vector<const Assertion*>* tokens=nullptr,
106 const vector<Attribute*>* attributes=nullptr
109 insert(dummy, app, httpRequest, httpResponse, expires, issuer, protocol, nameid,
110 authn_instant, session_index, authncontext_class, authncontext_decl, tokens, attributes);
115 const Application& app,
116 const HTTPRequest& httpRequest,
117 HTTPResponse& httpResponse,
119 const EntityDescriptor* issuer=nullptr,
120 const XMLCh* protocol=nullptr,
121 const saml2::NameID* nameid=nullptr,
122 const XMLCh* authn_instant=nullptr,
123 const XMLCh* session_index=nullptr,
124 const XMLCh* authncontext_class=nullptr,
125 const XMLCh* authncontext_decl=nullptr,
126 const vector<const Assertion*>* tokens=nullptr,
127 const vector<Attribute*>* attributes=nullptr
129 vector<string>::size_type logout(
130 const Application& app,
131 const EntityDescriptor* issuer,
132 const saml2::NameID& nameid,
133 const set<string>* indexes,
135 vector<string>& sessions
137 return _logout(app, issuer, nameid, indexes, expires, sessions, 0);
140 const Application& app,
141 const HTTPRequest& request,
142 const EntityDescriptor* issuer,
143 const saml2::NameID& nameid,
144 const set<string>* indexes
147 Session* find(const Application& app, const char* key, const char* client_addr=nullptr, time_t* timeout=nullptr);
148 void remove(const Application& app, const char* key);
151 string active(const Application& app, const HTTPRequest& request) {
152 if (!m_inboundHeader.empty()) {
153 string session_id = request.getHeader(m_inboundHeader.c_str());
154 if (!session_id.empty())
157 pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_");
158 const char* session_id = request.getCookie(shib_cookie.first.c_str());
159 return (session_id ? session_id : "");
162 Session* find(const Application& app, const HTTPRequest& request, const char* client_addr=nullptr, time_t* timeout=nullptr) {
163 string id = active(app, request);
165 return find(app, id.c_str(), client_addr, timeout);
169 Session* find(const Application& app, HTTPRequest& request, const char* client_addr=nullptr, time_t* timeout=nullptr);
170 void remove(const Application& app, const HTTPRequest& request, HTTPResponse* response=nullptr);
172 unsigned long getCacheTimeout(const Application& app) {
173 // Computes offset for adjusting expiration of sessions.
174 // This can either be static, or dynamic based on the per-app session timeout or lifetime.
176 return m_cacheTimeout;
177 pair<bool,unsigned int> timeout = pair<bool,unsigned int>(false, 3600);
178 const PropertySet* props = app.getPropertySet("Sessions");
180 timeout = props->getUnsignedInt("timeout");
182 timeout.second = 3600;
184 // As long as one of the two factors is set, add them together.
185 if (timeout.second > 0 || m_cacheAllowance > 0)
186 return timeout.second + m_cacheAllowance;
188 // If timeouts are off, and there's no cache slop set, then use the lifetime.
189 timeout = pair<bool,unsigned int>(false, 28800);
191 timeout = props->getUnsignedInt("lifetime");
192 if (!timeout.first || timeout.second == 0)
193 timeout.second = 28800;
195 return timeout.second;
201 StorageService* m_storage;
202 StorageService* m_storage_lite;
207 // maintain back-mappings of NameID/SessionIndex -> session key
208 void insert(const char* key, time_t expires, const char* name, const char* index, short attempts=0);
209 vector<string>::size_type _logout(
210 const Application& app,
211 const EntityDescriptor* issuer,
212 const saml2::NameID& nameid,
213 const set<string>* indexes,
215 vector<string>& sessions,
218 bool stronglyMatches(const XMLCh* idp, const XMLCh* sp, const saml2::NameID& n1, const saml2::NameID& n2) const;
219 LogoutEvent* newLogoutEvent(const Application& app) const;
221 bool m_cacheAssertions,m_reverseIndex;
222 set<xstring> m_excludedNames;
224 const DOMElement* m_root; // Only valid during initialization
225 unsigned long m_inprocTimeout,m_cacheTimeout,m_cacheAllowance;
226 string m_inboundHeader,m_outboundHeader;
228 // inproc means we buffer sessions in memory
229 scoped_ptr<RWLock> m_lock;
230 map<string,StoredSession*> m_hashtable;
232 // management of buffered sessions
233 void dormant(const char* key);
234 static void* cleanup_fn(void*);
237 scoped_ptr<CondWait> shutdown_wait;
238 scoped_ptr<Thread> cleanup_thread;
241 class StoredSession : public virtual Session
244 StoredSession(SSCache* cache, DDF& obj) : m_obj(obj), m_cache(cache), m_expires(0), m_lastAccess(time(nullptr)) {
245 // Check for old address format.
246 if (m_obj["client_addr"].isstring()) {
247 const char* saddr = m_obj["client_addr"].string();
248 DDF addrobj = m_obj["client_addr"].structure();
249 if (saddr && *saddr) {
250 if (strchr(saddr, ':'))
251 addrobj.addmember("6").string(saddr);
253 addrobj.addmember("4").string(saddr);
257 auto_ptr_XMLCh exp(m_obj["expires"].string());
259 DateTime iso(exp.get());
261 m_expires = iso.getEpoch();
265 const char* nameid = obj["nameid"].string();
267 // Parse and bind the document into an XMLObject.
268 istringstream instr(nameid);
269 DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(instr);
270 XercesJanitor<DOMDocument> janitor(doc);
271 m_nameid.reset(saml2::NameIDBuilder::buildNameID());
272 m_nameid->unmarshall(doc->getDocumentElement(), true);
277 m_lock.reset(Mutex::create());
282 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
297 const char* getID() const {
300 const char* getApplicationID() const {
301 return m_obj["application_id"].string();
303 const char* getClientAddress() const {
304 return m_obj["client_addr"].first().string();
307 const char* getClientAddressV4() const {
308 return m_obj["client_addr"]["4"].string();
310 const char* getClientAddressV6() const {
311 return m_obj["client_addr"]["6"].string();
313 void setClientAddress(const char* client_addr) {
314 DDF obj = m_obj["client_addr"];
316 obj = m_obj.addmember("client_addr").structure();
317 if (strchr(client_addr, ':'))
318 obj.addmember("6").string(client_addr);
320 obj.addmember("4").string(client_addr);
323 const char* getEntityID() const {
324 return m_obj["entity_id"].string();
326 const char* getProtocol() const {
327 return m_obj["protocol"].string();
329 const char* getAuthnInstant() const {
330 return m_obj["authn_instant"].string();
333 const saml2::NameID* getNameID() const {
334 return m_nameid.get();
337 const char* getSessionIndex() const {
338 return m_obj["session_index"].string();
340 const char* getAuthnContextClassRef() const {
341 return m_obj["authncontext_class"].string();
343 const char* getAuthnContextDeclRef() const {
344 return m_obj["authncontext_decl"].string();
346 const vector<Attribute*>& getAttributes() const {
347 if (m_attributes.empty())
348 unmarshallAttributes();
351 const multimap<string,const Attribute*>& getIndexedAttributes() const {
352 if (m_attributeIndex.empty()) {
353 if (m_attributes.empty())
354 unmarshallAttributes();
355 for (vector<Attribute*>::const_iterator a = m_attributes.begin(); a != m_attributes.end(); ++a) {
356 const vector<string>& aliases = (*a)->getAliases();
357 for (vector<string>::const_iterator alias = aliases.begin(); alias != aliases.end(); ++alias)
358 m_attributeIndex.insert(multimap<string,const Attribute*>::value_type(*alias, *a));
361 return m_attributeIndex;
363 const vector<const char*>& getAssertionIDs() const {
365 DDF ids = m_obj["assertions"];
366 DDF id = ids.first();
367 while (id.isstring()) {
368 m_ids.push_back(id.string());
375 void validate(const Application& application, const char* client_addr, time_t* timeout);
378 void addAttributes(const vector<Attribute*>& attributes);
379 const Assertion* getAssertion(const char* id) const;
380 void addAssertion(Assertion* assertion);
383 time_t getExpiration() const { return m_expires; }
384 time_t getLastAccess() const { return m_lastAccess; }
387 void unmarshallAttributes() const;
391 scoped_ptr<saml2::NameID> m_nameid;
392 mutable map< string,boost::shared_ptr<Assertion> > m_tokens;
394 mutable vector<Attribute*> m_attributes;
395 mutable multimap<string,const Attribute*> m_attributeIndex;
396 mutable vector<const char*> m_ids;
399 time_t m_expires,m_lastAccess;
400 scoped_ptr<Mutex> m_lock;
403 SessionCache* SHIBSP_DLLLOCAL StorageServiceCacheFactory(const DOMElement* const & e)
405 return new SSCache(e);
409 Session* SessionCache::find(const Application& application, HTTPRequest& request, const char* client_addr, time_t* timeout)
411 return find(application, const_cast<const HTTPRequest&>(request), client_addr, timeout);
414 void SHIBSP_API shibsp::registerSessionCaches()
416 SPConfig::getConfig().SessionCacheManager.registerFactory(STORAGESERVICE_SESSION_CACHE, StorageServiceCacheFactory);
427 void StoredSession::unmarshallAttributes() const
429 Attribute* attribute;
430 DDF attrs = m_obj["attributes"];
431 DDF attr = attrs.first();
432 while (!attr.isnull()) {
434 attribute = Attribute::unmarshall(attr);
435 m_attributes.push_back(attribute);
436 if (m_cache->m_log.isDebugEnabled())
437 m_cache->m_log.debug("unmarshalled attribute (ID: %s) with %d value%s",
438 attribute->getId(), attr.first().integer(), attr.first().integer()!=1 ? "s" : "");
440 catch (AttributeException& ex) {
441 const char* id = attr.first().name();
442 m_cache->m_log.error("error unmarshalling attribute (ID: %s): %s", id ? id : "none", ex.what());
448 void StoredSession::validate(const Application& app, const char* client_addr, time_t* timeout)
450 time_t now = time(nullptr);
454 if (now > m_expires) {
455 m_cache->m_log.info("session expired (ID: %s)", getID());
456 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
462 const char* saddr = nullptr;
463 if (strchr(client_addr, ':'))
464 saddr = getClientAddressV6();
466 saddr = getClientAddressV4();
467 if (saddr && *saddr) {
468 if (!XMLString::equals(saddr, client_addr)) {
469 m_cache->m_log.warn("client address mismatch, client (%s), session (%s)", client_addr, saddr);
470 throw RetryableProfileException(
471 "Your IP address ($1) does not match the address recorded at the time the session was established.",
472 params(1, client_addr)
475 client_addr = nullptr; // clear out parameter as signal that session need not be updated below
478 m_cache->m_log.info("session (%s) not yet bound to client address type, binding it to (%s)", getID(), client_addr);
482 if (!timeout && !client_addr)
485 if (!SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
486 DDF in("touch::"STORAGESERVICE_SESSION_CACHE"::SessionCache"), out;
489 in.addmember("key").string(getID());
490 in.addmember("version").integer(m_obj["version"].integer());
491 in.addmember("application_id").string(app.getId());
492 if (client_addr) // signals we need to bind an additional address to the session
493 in.addmember("client_addr").string(client_addr);
494 if (timeout && *timeout) {
495 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
496 #ifndef HAVE_GMTIME_R
497 struct tm* ptime = gmtime(timeout);
500 struct tm* ptime = gmtime_r(timeout,&res);
503 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
504 in.addmember("timeout").string(timebuf);
508 out=app.getServiceProvider().getListenerService()->send(in);
515 if (out.isstruct()) {
516 // We got an updated record back.
517 m_cache->m_log.debug("session updated, reconstituting it");
519 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
520 m_attributes.clear();
521 m_attributeIndex.clear();
528 if (!m_cache->m_storage)
529 throw ConfigurationException("Session touch requires a StorageService.");
531 // Versioned read, since we already have the data in hand if it's current.
534 int curver = m_obj["version"].integer();
535 int ver = m_cache->m_storage->readText(getID(), "session", &record, &lastAccess, curver);
537 m_cache->m_log.warn("unsuccessful versioned read of session (ID: %s), cache out of sync?", getID());
538 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
542 // Adjust for expiration to recover last access time and check timeout.
543 unsigned long cacheTimeout = m_cache->getCacheTimeout(app);
544 lastAccess -= cacheTimeout;
545 if (*timeout > 0 && now - lastAccess >= *timeout) {
546 m_cache->m_log.info("session timed out (ID: %s)", getID());
547 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
550 // Update storage expiration, if possible.
552 m_cache->m_storage->updateContext(getID(), now + cacheTimeout);
554 catch (std::exception& ex) {
555 m_cache->m_log.error("failed to update session expiration: %s", ex.what());
560 // We got an updated record back.
562 istringstream in(record);
565 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
566 m_attributes.clear();
567 m_attributeIndex.clear();
572 // We may need to write back a new address into the session using a versioned update loop.
576 const char* saddr = nullptr;
577 if (strchr(client_addr, ':'))
578 saddr = getClientAddressV6();
580 saddr = getClientAddressV4();
581 // Something snuck in and bound the session to this address type, so it better match what we have.
583 if (!XMLString::equals(saddr, client_addr)) {
584 m_cache->m_log.warn("client address mismatch, client (%s), session (%s)", client_addr, saddr);
585 throw RetryableProfileException(
586 "Your IP address ($1) does not match the address recorded at the time the session was established.",
587 params(1, client_addr)
590 break; // No need to update.
593 // Bind it into the session.
594 setClientAddress(client_addr);
597 // Tentatively increment the version.
598 m_obj["version"].integer(m_obj["version"].integer() + 1);
605 ver = m_cache->m_storage->updateText(getID(), "session", record.c_str(), 0, m_obj["version"].integer() - 1);
607 catch (std::exception&) {
608 m_obj["version"].integer(m_obj["version"].integer() - 1);
613 m_obj["version"].integer(m_obj["version"].integer() - 1);
617 // Fatal problem with update.
618 m_cache->m_log.error("updateText failed on StorageService for session (%s)", getID());
619 throw IOException("Unable to update stored session.");
623 if (++attempts > 10) {
624 m_cache->m_log.error("failed to bind client address, update attempts exceeded limit");
625 throw IOException("Unable to update stored session, exceeded retry limit.");
627 m_cache->m_log.warn("storage service indicates the record is out of sync, updating with a fresh copy...");
628 ver = m_cache->m_storage->readText(getID(), "session", &record, nullptr);
630 m_cache->m_log.error("readText failed on StorageService for session (%s)", getID());
631 throw IOException("Unable to read back stored session.");
636 istringstream in(record);
640 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
641 m_attributes.clear();
642 m_attributeIndex.clear();
643 newobj["version"].integer(ver);
649 } while (ver < 0); // negative indicates a sync issue so we retry
652 throw ConfigurationException("Session touch requires a StorageService.");
661 void StoredSession::addAttributes(const vector<Attribute*>& attributes)
664 xmltooling::NDC ndc("addAttributes");
667 if (!m_cache->m_storage)
668 throw ConfigurationException("Session modification requires a StorageService.");
670 m_cache->m_log.debug("adding attributes to session (%s)", getID());
676 DDF attrs = m_obj["attributes"];
678 attrs = m_obj.addmember("attributes").list();
679 for (vector<Attribute*>::const_iterator a=attributes.begin(); a!=attributes.end(); ++a) {
680 attr = (*a)->marshall();
684 // Tentatively increment the version.
685 m_obj["version"].integer(m_obj["version"].integer()+1);
689 string record(str.str());
692 ver = m_cache->m_storage->updateText(getID(), "session", record.c_str(), 0, m_obj["version"].integer()-1);
694 catch (std::exception&) {
695 // Roll back modification to record.
696 m_obj["version"].integer(m_obj["version"].integer()-1);
697 vector<Attribute*>::size_type count = attributes.size();
699 attrs.last().destroy();
704 // Roll back modification to record.
705 m_obj["version"].integer(m_obj["version"].integer()-1);
706 vector<Attribute*>::size_type count = attributes.size();
708 attrs.last().destroy();
711 // Fatal problem with update.
712 throw IOException("Unable to update stored session.");
716 if (++attempts > 10) {
717 m_cache->m_log.error("failed to update stored session, update attempts exceeded limit");
718 throw IOException("Unable to update stored session, exceeded retry limit.");
720 m_cache->m_log.warn("storage service indicates the record is out of sync, updating with a fresh copy...");
721 ver = m_cache->m_storage->readText(getID(), "session", &record, nullptr);
723 m_cache->m_log.error("readText failed on StorageService for session (%s)", getID());
724 throw IOException("Unable to read back stored session.");
729 istringstream in(record);
733 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
734 m_attributes.clear();
735 m_attributeIndex.clear();
736 newobj["version"].integer(ver);
742 } while (ver < 0); // negative indicates a sync issue so we retry
744 // We own them now, so clean them up.
745 for_each(attributes.begin(), attributes.end(), xmltooling::cleanup<Attribute>());
748 const Assertion* StoredSession::getAssertion(const char* id) const
750 if (!m_cache->m_storage)
751 throw ConfigurationException("Assertion retrieval requires a StorageService.");
753 map< string,boost::shared_ptr<Assertion> >::const_iterator i = m_tokens.find(id);
754 if (i != m_tokens.end())
755 return i->second.get();
758 if (!m_cache->m_storage->readText(getID(), id, &tokenstr, nullptr))
759 throw FatalProfileException("Assertion not found in cache.");
761 // Parse and bind the document into an XMLObject.
762 istringstream instr(tokenstr);
763 DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(instr);
764 XercesJanitor<DOMDocument> janitor(doc);
765 boost::shared_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
768 boost::shared_ptr<Assertion> token = dynamic_pointer_cast<Assertion,XMLObject>(xmlObject);
770 throw FatalProfileException("Request for cached assertion returned an unknown object type.");
772 m_tokens[id] = token;
776 void StoredSession::addAssertion(Assertion* assertion)
779 xmltooling::NDC ndc("addAssertion");
782 if (!m_cache->m_storage)
783 throw ConfigurationException("Session modification requires a StorageService.");
785 throw FatalProfileException("Unknown object type passed to session for storage.");
787 auto_ptr_char id(assertion->getID());
788 if (!id.get() || !*id.get())
789 throw IOException("Assertion did not carry an ID.");
790 else if (strlen(id.get()) > m_cache->m_storage->getCapabilities().getKeySize())
791 throw IOException("Assertion ID ($1) exceeds allowable storage key size.", params(1, id.get()));
793 m_cache->m_log.debug("adding assertion (%s) to session (%s)", id.get(), getID());
796 if (!m_cache->m_storage->readText(getID(), "session", nullptr, &exp))
797 throw IOException("Unable to load expiration time for stored session.");
799 ostringstream tokenstr;
800 tokenstr << *assertion;
801 if (!m_cache->m_storage->createText(getID(), id.get(), tokenstr.str().c_str(), exp))
802 throw IOException("Attempted to insert duplicate assertion ID into session.");
807 DDF token = DDF(nullptr).string(id.get());
808 m_obj["assertions"].add(token);
810 // Tentatively increment the version.
811 m_obj["version"].integer(m_obj["version"].integer() + 1);
815 string record(str.str());
818 ver = m_cache->m_storage->updateText(getID(), "session", record.c_str(), 0, m_obj["version"].integer()-1);
820 catch (std::exception&) {
822 m_obj["version"].integer(m_obj["version"].integer() - 1);
823 m_cache->m_storage->deleteText(getID(), id.get());
829 m_obj["version"].integer(m_obj["version"].integer()-1);
832 // Fatal problem with update.
833 m_cache->m_log.error("updateText failed on StorageService for session (%s)", getID());
834 m_cache->m_storage->deleteText(getID(), id.get());
835 throw IOException("Unable to update stored session.");
839 if (++attempts > 10) {
840 m_cache->m_log.error("failed to update stored session, update attempts exceeded limit");
841 throw IOException("Unable to update stored session, exceeded retry limit.");
843 m_cache->m_log.warn("storage service indicates the record is out of sync, updating with a fresh copy...");
844 ver = m_cache->m_storage->readText(getID(), "session", &record, nullptr);
846 m_cache->m_log.error("readText failed on StorageService for session (%s)", getID());
847 m_cache->m_storage->deleteText(getID(), id.get());
848 throw IOException("Unable to read back stored session.");
853 istringstream in(record);
857 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
858 m_attributes.clear();
859 m_attributeIndex.clear();
860 newobj["version"].integer(ver);
866 } while (ver < 0); // negative indicates a sync issue so we retry
874 SessionCache::SessionCache()
878 SessionCache::~SessionCache()
884 void SessionCache::insert(
886 const Application& app,
887 const HTTPRequest& httpRequest,
888 HTTPResponse& httpResponse,
890 const EntityDescriptor* issuer,
891 const XMLCh* protocol,
892 const saml2::NameID* nameid,
893 const XMLCh* authn_instant,
894 const XMLCh* session_index,
895 const XMLCh* authncontext_class,
896 const XMLCh* authncontext_decl,
897 const vector<const Assertion*>* tokens,
898 const vector<Attribute*>* attributes
900 return insert(app, httpRequest, httpResponse, expires, issuer, protocol, nameid,
901 authn_instant, session_index, authncontext_class, authncontext_decl, tokens, attributes);
906 SessionCacheEx::SessionCacheEx()
910 SessionCacheEx::~SessionCacheEx()
914 SSCache::SSCache(const DOMElement* e)
915 : m_log(Category::getInstance(SHIBSP_LOGCAT".SessionCache")), inproc(true),
917 m_storage(nullptr), m_storage_lite(nullptr), m_cacheAssertions(true), m_reverseIndex(true),
919 m_root(e), m_inprocTimeout(900), m_cacheTimeout(0), m_cacheAllowance(0), shutdown(false)
921 SPConfig& conf = SPConfig::getConfig();
922 inproc = conf.isEnabled(SPConfig::InProcess);
924 static const XMLCh cacheAllowance[] = UNICODE_LITERAL_14(c,a,c,h,e,A,l,l,o,w,a,n,c,e);
925 static const XMLCh cacheAssertions[] = UNICODE_LITERAL_15(c,a,c,h,e,A,s,s,e,r,t,i,o,n,s);
926 static const XMLCh cacheTimeout[] = UNICODE_LITERAL_12(c,a,c,h,e,T,i,m,e,o,u,t);
927 static const XMLCh excludeReverseIndex[] = UNICODE_LITERAL_19(e,x,c,l,u,d,e,R,e,v,e,r,s,e,I,n,d,e,x);
928 static const XMLCh inprocTimeout[] = UNICODE_LITERAL_13(i,n,p,r,o,c,T,i,m,e,o,u,t);
929 static const XMLCh inboundHeader[] = UNICODE_LITERAL_13(i,n,b,o,u,n,d,H,e,a,d,e,r);
930 static const XMLCh maintainReverseIndex[] = UNICODE_LITERAL_20(m,a,i,n,t,a,i,n,R,e,v,e,r,s,e,I,n,d,e,x);
931 static const XMLCh outboundHeader[] = UNICODE_LITERAL_14(o,u,t,b,o,u,n,d,H,e,a,d,e,r);
932 static const XMLCh _StorageService[] = UNICODE_LITERAL_14(S,t,o,r,a,g,e,S,e,r,v,i,c,e);
933 static const XMLCh _StorageServiceLite[] = UNICODE_LITERAL_18(S,t,o,r,a,g,e,S,e,r,v,i,c,e,L,i,t,e);
935 if (e && e->hasAttributeNS(nullptr, cacheTimeout)) {
936 m_log.warn("cacheTimeout property is deprecated in favor of cacheAllowance (see documentation)");
937 m_cacheTimeout = XMLHelper::getAttrInt(e, 0, cacheTimeout);
939 m_cacheAllowance = XMLHelper::getAttrInt(e, 0, cacheAllowance);
941 m_inprocTimeout = XMLHelper::getAttrInt(e, 900, inprocTimeout);
942 m_inboundHeader = XMLHelper::getAttrString(e, nullptr, inboundHeader);
943 if (!m_inboundHeader.empty())
944 RemotedHandler::addRemotedHeader(m_inboundHeader.c_str());
945 m_outboundHeader = XMLHelper::getAttrString(e, nullptr, outboundHeader);
948 if (conf.isEnabled(SPConfig::OutOfProcess)) {
949 string ssid(XMLHelper::getAttrString(e, nullptr, _StorageService));
951 m_storage = conf.getServiceProvider()->getStorageService(ssid.c_str());
953 m_log.info("bound to StorageService (%s)", ssid.c_str());
955 m_log.warn("specified StorageService (%s) not found", ssid.c_str());
958 m_storage = conf.getServiceProvider()->getStorageService(nullptr);
960 m_log.info("bound to arbitrary StorageService");
962 throw ConfigurationException("SessionCache unable to locate StorageService, check configuration.");
965 ssid = XMLHelper::getAttrString(e, nullptr, _StorageServiceLite);
967 m_storage_lite = conf.getServiceProvider()->getStorageService(ssid.c_str());
969 m_log.info("bound to 'lite' StorageService (%s)", ssid.c_str());
971 m_log.warn("specified 'lite' StorageService (%s) not found", ssid.c_str());
973 if (!m_storage_lite) {
974 m_log.info("StorageService for 'lite' use not set, using standard StorageService");
975 m_storage_lite = m_storage;
978 m_cacheAssertions = XMLHelper::getAttrBool(e, true, cacheAssertions);
979 m_reverseIndex = XMLHelper::getAttrBool(e, true, maintainReverseIndex);
980 const XMLCh* excludedNames = e ? e->getAttributeNS(nullptr, excludeReverseIndex) : nullptr;
981 if (excludedNames && *excludedNames) {
982 XMLStringTokenizer toks(excludedNames);
983 while (toks.hasMoreTokens())
984 m_excludedNames.insert(toks.nextToken());
989 ListenerService* listener=conf.getServiceProvider()->getListenerService(false);
991 if (!conf.isEnabled(SPConfig::OutOfProcess) && !listener)
992 throw ConfigurationException("SessionCache requires a ListenerService, but none available.");
993 m_lock.reset(RWLock::create());
994 shutdown_wait.reset(CondWait::create());
995 cleanup_thread.reset(Thread::create(&cleanup_fn, this));
999 if (listener && conf.isEnabled(SPConfig::OutOfProcess)) {
1000 listener->regListener("find::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
1001 listener->regListener("remove::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
1002 listener->regListener("touch::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
1005 m_log.info("no ListenerService available, cache remoting disabled");
1014 // Shut down the cleanup thread and let it know...
1016 if (shutdown_wait.get())
1017 shutdown_wait->signal();
1018 if (cleanup_thread.get())
1019 cleanup_thread->join(nullptr);
1021 for_each(m_hashtable.begin(),m_hashtable.end(),cleanup_pair<string,StoredSession>());
1025 SPConfig& conf = SPConfig::getConfig();
1026 ListenerService* listener=conf.getServiceProvider()->getListenerService(false);
1027 if (listener && conf.isEnabled(SPConfig::OutOfProcess)) {
1028 listener->unregListener("find::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
1029 listener->unregListener("remove::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
1030 listener->unregListener("touch::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
1038 void SSCache::test()
1040 XMLCh* wide = SAMLConfig::getConfig().generateIdentifier();
1041 auto_ptr_char temp(wide);
1042 XMLString::release(&wide);
1043 m_storage->createString("SessionCacheTest", temp.get(), "Test", time(nullptr) + 60);
1044 m_storage->deleteString("SessionCacheTest", temp.get());
1047 void SSCache::insert(const char* key, time_t expires, const char* name, const char* index, short attempts)
1050 throw IOException("Exceeded retry limit.");
1053 unsigned int storageLimit = m_storage_lite->getCapabilities().getKeySize();
1054 if (strlen(name) > storageLimit) {
1055 dup = string(name).substr(0, storageLimit);
1060 DDFJanitor jobj(obj);
1062 // Since we can't guarantee uniqueness, check for an existing record.
1065 int ver = m_storage_lite->readText("NameID", name, &record, &recordexp);
1067 // Existing record, so we need to unmarshall it.
1068 istringstream in(record);
1073 obj = DDF(nullptr).structure();
1076 if (!index || !*index)
1077 index = "_shibnull";
1078 DDF sessions = obj.addmember(index);
1079 if (!sessions.islist())
1081 DDF session = DDF(nullptr).string(key);
1082 sessions.add(session);
1084 // Remarshall the record.
1088 // Try and store it back...
1090 ver = m_storage_lite->updateText("NameID", name, out.str().c_str(), max(expires, recordexp), ver);
1092 // Out of sync, or went missing, so retry.
1093 return insert(key, expires, name, index, attempts + 1);
1096 else if (!m_storage_lite->createText("NameID", name, out.str().c_str(), expires)) {
1097 // Hit a dup, so just retry, hopefully hitting the other branch.
1098 return insert(key, expires, name, index, attempts + 1);
1102 void SSCache::insert(
1104 const Application& app,
1105 const HTTPRequest& httpRequest,
1106 HTTPResponse& httpResponse,
1108 const saml2md::EntityDescriptor* issuer,
1109 const XMLCh* protocol,
1110 const saml2::NameID* nameid,
1111 const XMLCh* authn_instant,
1112 const XMLCh* session_index,
1113 const XMLCh* authncontext_class,
1114 const XMLCh* authncontext_decl,
1115 const vector<const Assertion*>* tokens,
1116 const vector<Attribute*>* attributes
1120 xmltooling::NDC ndc("insert");
1123 throw ConfigurationException("SessionCache insertion requires a StorageService.");
1125 m_log.debug("creating new session");
1127 time_t now = time(nullptr);
1128 auto_ptr_char index(session_index);
1129 auto_ptr_char entity_id(issuer ? issuer->getEntityID() : nullptr);
1130 auto_ptr_char name(nameid ? nameid->getName() : nullptr);
1132 if (name.get() && *name.get()) {
1133 // Check for a pending logout.
1134 unsigned int storageLimit = m_storage_lite->getCapabilities().getKeySize();
1135 string namebuf = name.get();
1136 if (namebuf.length() > storageLimit)
1137 namebuf = namebuf.substr(0, storageLimit);
1139 int ver = m_storage_lite->readText("Logout", namebuf.c_str(), &pending);
1142 DDFJanitor jpend(pendobj);
1143 istringstream pstr(pending);
1145 // IdP.SP.index contains logout expiration, if any.
1146 DDF deadmenwalking = pendobj[issuer ? entity_id.get() : "_shibnull"][app.getRelyingParty(issuer)->getString("entityID").second];
1147 const char* logexpstr = deadmenwalking[session_index ? index.get() : "_shibnull"].string();
1148 if (!logexpstr && session_index) // we tried an exact session match, now try for nullptr
1149 logexpstr = deadmenwalking["_shibnull"].string();
1151 auto_ptr_XMLCh dt(logexpstr);
1152 DateTime dtobj(dt.get());
1153 dtobj.parseDateTime();
1154 time_t logexp = dtobj.getEpoch();
1155 if (now - XMLToolingConfig::getConfig().clock_skew_secs < logexp)
1156 throw FatalProfileException("A logout message from your identity provider has blocked your login attempt.");
1161 XMLCh* widekey = SAMLConfig::getConfig().generateIdentifier();
1162 auto_ptr_char key(widekey);
1163 XMLString::release(&widekey);
1165 // Store session properties in DDF.
1166 DDF obj = DDF(key.get()).structure();
1167 DDFJanitor entryobj(obj);
1168 obj.addmember("version").integer(1);
1169 obj.addmember("application_id").string(app.getId());
1171 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
1172 #ifndef HAVE_GMTIME_R
1173 struct tm* ptime=gmtime(&expires);
1176 struct tm* ptime=gmtime_r(&expires,&res);
1179 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
1180 obj.addmember("expires").string(timebuf);
1182 string caddr(httpRequest.getRemoteAddr());
1183 if (!caddr.empty()) {
1184 DDF addrobj = obj.addmember("client_addr").structure();
1185 if (caddr.find(':') != string::npos)
1186 addrobj.addmember("6").string(caddr.c_str());
1188 addrobj.addmember("4").string(caddr.c_str());
1192 obj.addmember("entity_id").string(entity_id.get());
1194 auto_ptr_char prot(protocol);
1195 obj.addmember("protocol").string(prot.get());
1197 if (authn_instant) {
1198 auto_ptr_char instant(authn_instant);
1199 obj.addmember("authn_instant").string(instant.get());
1202 obj.addmember("session_index").string(index.get());
1203 if (authncontext_class) {
1204 auto_ptr_char ac(authncontext_class);
1205 obj.addmember("authncontext_class").string(ac.get());
1207 if (authncontext_decl) {
1208 auto_ptr_char ad(authncontext_decl);
1209 obj.addmember("authncontext_decl").string(ad.get());
1213 ostringstream namestr;
1215 obj.addmember("nameid").string(namestr.str().c_str());
1218 if (tokens && m_cacheAssertions) {
1219 obj.addmember("assertions").list();
1220 for (vector<const Assertion*>::const_iterator t = tokens->begin(); t!=tokens->end(); ++t) {
1221 auto_ptr_char tokenid((*t)->getID());
1222 DDF tokid = DDF(nullptr).string(tokenid.get());
1223 obj["assertions"].add(tokid);
1229 DDF attrlist = obj.addmember("attributes").list();
1230 for (vector<Attribute*>::const_iterator a=attributes->begin(); a!=attributes->end(); ++a) {
1231 attr = (*a)->marshall();
1236 ostringstream record;
1239 m_log.debug("storing new session...");
1240 unsigned long cacheTimeout = getCacheTimeout(app);
1241 if (!m_storage->createText(key.get(), "session", record.str().c_str(), now + cacheTimeout))
1242 throw FatalProfileException("Attempted to create a session with a duplicate key.");
1244 // Store the reverse mapping for logout.
1245 if (nameid && m_reverseIndex && (m_excludedNames.size() == 0 || m_excludedNames.count(nameid->getName()) == 0)) {
1247 insert(key.get(), expires, name.get(), index.get());
1249 catch (std::exception& ex) {
1250 m_log.error("error storing back mapping of NameID for logout: %s", ex.what());
1254 if (tokens && m_cacheAssertions) {
1256 for (vector<const Assertion*>::const_iterator t = tokens->begin(); t!=tokens->end(); ++t) {
1257 ostringstream tokenstr;
1259 auto_ptr_char tokenid((*t)->getID());
1260 if (!tokenid.get() || !*tokenid.get() || strlen(tokenid.get()) > m_storage->getCapabilities().getKeySize())
1261 throw IOException("Assertion ID is missing or exceeds key size of storage service.");
1262 else if (!m_storage->createText(key.get(), tokenid.get(), tokenstr.str().c_str(), now + cacheTimeout))
1263 throw IOException("Duplicate assertion ID ($1)", params(1, tokenid.get()));
1266 catch (std::exception& ex) {
1267 m_log.error("error storing assertion along with session: %s", ex.what());
1271 const char* pid = obj["entity_id"].string();
1272 const char* prot = obj["protocol"].string();
1273 m_log.info("new session created: ID (%s) IdP (%s) Protocol(%s) Address (%s)",
1274 key.get(), pid ? pid : "none", prot ? prot : "none", httpRequest.getRemoteAddr().c_str());
1276 if (!m_outboundHeader.empty())
1277 httpResponse.setResponseHeader(m_outboundHeader.c_str(), key.get());
1279 time_t cookieLifetime = 0;
1280 pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_", &cookieLifetime);
1281 string k(key.get());
1282 k += shib_cookie.second;
1284 if (cookieLifetime > 0) {
1285 cookieLifetime += now;
1286 #ifndef HAVE_GMTIME_R
1287 ptime=gmtime(&cookieLifetime);
1289 ptime=gmtime_r(&cookieLifetime,&res);
1291 char cookietimebuf[64];
1292 strftime(cookietimebuf,64,"; expires=%a, %d %b %Y %H:%M:%S GMT",ptime);
1296 httpResponse.setCookie(shib_cookie.first.c_str(), k.c_str());
1297 sessionID = key.get();
1300 bool SSCache::matches(
1301 const Application& app,
1302 const xmltooling::HTTPRequest& request,
1303 const saml2md::EntityDescriptor* issuer,
1304 const saml2::NameID& nameid,
1305 const set<string>* indexes
1308 auto_ptr_char entityID(issuer ? issuer->getEntityID() : nullptr);
1310 Session* session = find(app, request);
1312 Locker locker(session, false);
1313 if (XMLString::equals(session->getEntityID(), entityID.get()) && session->getNameID() &&
1314 stronglyMatches(issuer->getEntityID(), app.getRelyingParty(issuer)->getXMLString("entityID").second, nameid, *session->getNameID())) {
1315 return (!indexes || indexes->empty() || (session->getSessionIndex() ? (indexes->count(session->getSessionIndex())>0) : false));
1319 catch (std::exception& ex) {
1320 m_log.error("error while matching session: %s", ex.what());
1325 vector<string>::size_type SSCache::_logout(
1326 const Application& app,
1327 const saml2md::EntityDescriptor* issuer,
1328 const saml2::NameID& nameid,
1329 const set<string>* indexes,
1331 vector<string>& sessionsKilled,
1336 xmltooling::NDC ndc("logout");
1340 throw ConfigurationException("SessionCache logout requires a StorageService.");
1341 else if (attempts > 10)
1342 throw IOException("Exceeded retry limit.");
1344 auto_ptr_char entityID(issuer ? issuer->getEntityID() : nullptr);
1345 auto_ptr_char name(nameid.getName());
1347 m_log.info("request to logout sessions from (%s) for (%s)", entityID.get() ? entityID.get() : "unknown", name.get());
1349 unsigned int storageLimit = m_storage_lite->getCapabilities().getKeySize();
1350 if (strlen(name.get()) > storageLimit)
1351 const_cast<char*>(name.get())[storageLimit] = 0;
1354 DDFJanitor jobj(obj);
1359 // Record the logout to prevent post-delivered assertions.
1360 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
1361 #ifndef HAVE_GMTIME_R
1362 struct tm* ptime=gmtime(&expires);
1365 struct tm* ptime=gmtime_r(&expires,&res);
1368 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
1371 ver = m_storage_lite->readText("Logout", name.get(), &record, &oldexp);
1373 istringstream lin(record);
1377 obj = DDF(nullptr).structure();
1380 // Structure is keyed by the IdP and SP, with a member per session index containing the expiration.
1381 DDF root = obj.addmember(issuer ? entityID.get() : "_shibnull").addmember(app.getRelyingParty(issuer)->getString("entityID").second);
1383 for (set<string>::const_iterator x = indexes->begin(); x!=indexes->end(); ++x)
1384 root.addmember(x->c_str()).string(timebuf);
1387 root.addmember("_shibnull").string(timebuf);
1395 ver = m_storage_lite->updateText("Logout", name.get(), lout.str().c_str(), max(expires, oldexp), ver);
1397 // Out of sync, or went missing, so retry.
1398 return _logout(app, issuer, nameid, indexes, expires, sessionsKilled, attempts + 1);
1401 else if (!m_storage_lite->createText("Logout", name.get(), lout.str().c_str(), expires)) {
1402 // Hit a dup, so just retry, hopefully hitting the other branch.
1403 return _logout(app, issuer, nameid, indexes, expires, sessionsKilled, attempts + 1);
1410 if (!m_reverseIndex) {
1411 m_log.error("cannot support logout because maintainReverseIndex property is turned off");
1412 throw ConfigurationException("Logout is unsupported by the session cache configuration.");
1415 // Read in potentially matching sessions.
1416 ver = m_storage_lite->readText("NameID", name.get(), &record);
1418 m_log.debug("no active sessions to logout for supplied issuer and subject");
1422 istringstream in(record);
1425 // The record contains child lists for each known session index.
1427 DDF sessions = obj.first();
1428 while (sessions.islist()) {
1429 if (!indexes || indexes->empty() || indexes->count(sessions.name())) {
1430 key = sessions.first();
1431 while (key.isstring()) {
1432 // Fetch the session for comparison.
1433 Session* session = nullptr;
1435 session = find(app, key.string());
1437 catch (std::exception& ex) {
1438 m_log.error("error locating session (%s): %s", key.string(), ex.what());
1442 Locker locker(session, false);
1444 if (XMLString::equals(session->getEntityID(), entityID.get())) {
1446 if (stronglyMatches(issuer->getEntityID(), app.getRelyingParty(issuer)->getXMLString("entityID").second, nameid, *session->getNameID())) {
1447 sessionsKilled.push_back(key.string());
1451 m_log.debug("session (%s) contained a non-matching NameID, leaving it alone", key.string());
1455 m_log.debug("session (%s) established by different IdP, leaving it alone", key.string());
1459 // Session may already be gone, or it may be associated with a different application.
1460 // To be conservative, we'll leave it alone. This isn't really increasing our security
1461 // risk, because if we can't lookup the session, it's unlikely the calling logout code
1462 // can either, so there's no chance of removing the session anyway.
1463 m_log.warn("session (%s) not accessible for logout, may be gone, or associated with a different application", key.string());
1465 key = sessions.next();
1468 // No sessions left for this index?
1469 if (sessions.first().isnull())
1472 sessions = obj.next();
1475 if (obj.first().isnull())
1478 // If possible, write back the mapping record (this isn't crucial).
1481 m_storage_lite->deleteText("NameID", name.get());
1483 else if (!sessionsKilled.empty()) {
1486 if (m_storage_lite->updateText("NameID", name.get(), out.str().c_str(), 0, ver) <= 0)
1487 m_log.warn("logout mapping record changed behind us, leaving it alone");
1490 catch (std::exception& ex) {
1491 m_log.error("error updating logout mapping record: %s", ex.what());
1494 return sessionsKilled.size();
1497 bool SSCache::stronglyMatches(const XMLCh* idp, const XMLCh* sp, const saml2::NameID& n1, const saml2::NameID& n2) const
1499 if (!XMLString::equals(n1.getName(), n2.getName()))
1502 const XMLCh* s1 = n1.getFormat();
1503 const XMLCh* s2 = n2.getFormat();
1505 s1 = saml2::NameID::UNSPECIFIED;
1507 s2 = saml2::NameID::UNSPECIFIED;
1508 if (!XMLString::equals(s1,s2))
1511 s1 = n1.getNameQualifier();
1512 s2 = n2.getNameQualifier();
1517 if (!XMLString::equals(s1,s2))
1520 s1 = n1.getSPNameQualifier();
1521 s2 = n2.getSPNameQualifier();
1526 if (!XMLString::equals(s1,s2))
1532 LogoutEvent* SSCache::newLogoutEvent(const Application& app) const
1534 if (!SPConfig::getConfig().isEnabled(SPConfig::Logging))
1537 auto_ptr<TransactionLog::Event> event(SPConfig::getConfig().EventManager.newPlugin(LOGOUT_EVENT, nullptr));
1538 LogoutEvent* logout_event = dynamic_cast<LogoutEvent*>(event.get());
1540 logout_event->m_app = &app;
1542 return logout_event;
1545 m_log.warn("unable to audit event, log event object was of an incorrect type");
1548 catch (std::exception& ex) {
1549 m_log.warn("exception auditing event: %s", ex.what());
1556 Session* SSCache::find(const Application& app, const char* key, const char* client_addr, time_t* timeout)
1559 xmltooling::NDC ndc("find");
1561 StoredSession* session=nullptr;
1564 m_log.debug("searching local cache for session (%s)", key);
1566 map<string,StoredSession*>::const_iterator i=m_hashtable.find(key);
1567 if (i!=m_hashtable.end()) {
1568 // Save off and lock the session.
1569 session = i->second;
1572 m_log.debug("session found locally, validating it for use");
1580 if (!SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
1581 m_log.debug("session not found locally, remoting the search");
1582 // Remote the request.
1583 DDF in("find::"STORAGESERVICE_SESSION_CACHE"::SessionCache"), out;
1586 in.addmember("key").string(key);
1587 in.addmember("application_id").string(app.getId());
1588 if (timeout && *timeout) {
1589 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
1590 #ifndef HAVE_GMTIME_R
1591 struct tm* ptime=gmtime(timeout);
1594 struct tm* ptime=gmtime_r(timeout,&res);
1597 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
1598 in.addmember("timeout").string(timebuf);
1602 out=app.getServiceProvider().getListenerService()->send(in);
1603 if (!out.isstruct()) {
1605 m_log.debug("session not found in remote cache");
1609 // Wrap the results in a local entry and save it.
1610 session = new StoredSession(this, out);
1611 // The remote end has handled timeout issues, we handle address and expiration checks.
1620 // We're out of process, so we can search the storage service directly.
1623 throw ConfigurationException("SessionCache lookup requires a StorageService.");
1625 m_log.debug("searching for session (%s)", key);
1630 int ver = m_storage->readText(key, "session", &record, &lastAccess);
1634 m_log.debug("reconstituting session and checking validity");
1636 istringstream in(record);
1639 unsigned long cacheTimeout = getCacheTimeout(app);
1640 lastAccess -= cacheTimeout; // adjusts it back to the last time the record's timestamp was touched
1641 time_t now=time(nullptr);
1643 if (timeout && *timeout > 0 && now - lastAccess >= *timeout) {
1644 m_log.info("session timed out (ID: %s)", key);
1645 scoped_ptr<LogoutEvent> logout_event(newLogoutEvent(app));
1646 if (logout_event.get()) {
1647 logout_event->m_logoutType = LogoutEvent::LOGOUT_EVENT_INVALID;
1648 logout_event->m_sessions.push_back(key);
1649 app.getServiceProvider().getTransactionLog()->write(*logout_event);
1652 const char* eid = obj["entity_id"].string();
1655 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
1659 throw RetryableProfileException("Your session has expired, and you must re-authenticate.", namedparams(1, "entityID", eid2.c_str()));
1663 // Update storage expiration, if possible.
1665 m_storage->updateContext(key, now + cacheTimeout);
1667 catch (std::exception& ex) {
1668 m_log.error("failed to update session expiration: %s", ex.what());
1672 // Wrap the results in a local entry and save it.
1673 session = new StoredSession(this, obj);
1674 // We handled timeout issues, still need to handle address and expiration checks.
1677 throw ConfigurationException("SessionCache search requires a StorageService.");
1682 // Lock for writing and repeat the search to avoid duplication.
1684 SharedLock shared(m_lock, false);
1685 if (m_hashtable.count(key)) {
1686 // We're using an existing session entry.
1688 session = m_hashtable[key];
1692 m_hashtable[key]=session;
1698 if (!XMLString::equals(session->getApplicationID(), app.getId())) {
1699 m_log.warn("an application (%s) tried to access another application's session", app.getId());
1704 // Verify currency and update the timestamp if indicated by caller.
1706 session->validate(app, client_addr, timeout);
1710 scoped_ptr<LogoutEvent> logout_event(newLogoutEvent(app));
1711 if (logout_event.get()) {
1712 logout_event->m_logoutType = LogoutEvent::LOGOUT_EVENT_INVALID;
1713 logout_event->m_session = session;
1714 logout_event->m_sessions.push_back(session->getID());
1715 app.getServiceProvider().getTransactionLog()->write(*logout_event);
1726 Session* SSCache::find(const Application& app, HTTPRequest& request, const char* client_addr, time_t* timeout)
1728 string id = active(app, request);
1732 Session* session = find(app, id.c_str(), client_addr, timeout);
1735 HTTPResponse* response = dynamic_cast<HTTPResponse*>(&request);
1737 if (!m_outboundHeader.empty())
1738 response->setResponseHeader(m_outboundHeader.c_str(), nullptr);
1739 pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_");
1740 string exp(shib_cookie.second);
1741 exp += "; expires=Mon, 01 Jan 2001 00:00:00 GMT";
1742 response->setCookie(shib_cookie.first.c_str(), exp.c_str());
1745 catch (std::exception&) {
1746 HTTPResponse* response = dynamic_cast<HTTPResponse*>(&request);
1748 if (!m_outboundHeader.empty())
1749 response->setResponseHeader(m_outboundHeader.c_str(), nullptr);
1750 pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_");
1751 string exp(shib_cookie.second);
1752 exp += "; expires=Mon, 01 Jan 2001 00:00:00 GMT";
1753 response->setCookie(shib_cookie.first.c_str(), exp.c_str());
1760 void SSCache::remove(const Application& app, const HTTPRequest& request, HTTPResponse* response)
1763 pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_");
1765 if (!m_inboundHeader.empty())
1766 session_id = request.getHeader(m_inboundHeader.c_str());
1767 if (session_id.empty()) {
1768 const char* c = request.getCookie(shib_cookie.first.c_str());
1773 if (!session_id.empty()) {
1775 if (!m_outboundHeader.empty())
1776 response->setResponseHeader(m_outboundHeader.c_str(), nullptr);
1777 string exp(shib_cookie.second);
1778 exp += "; expires=Mon, 01 Jan 2001 00:00:00 GMT";
1779 response->setCookie(shib_cookie.first.c_str(), exp.c_str());
1781 remove(app, session_id.c_str());
1785 void SSCache::remove(const Application& app, const char* key)
1788 xmltooling::NDC ndc("remove");
1790 // Take care of local copy.
1794 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
1795 // Remove the session from storage directly.
1797 m_storage->deleteContext(key);
1798 m_log.info("removed session (%s)", key);
1800 throw ConfigurationException("SessionCache removal requires a StorageService.");
1804 // Remote the request.
1805 DDF in("remove::"STORAGESERVICE_SESSION_CACHE"::SessionCache");
1808 in.addmember("key").string(key);
1809 in.addmember("application_id").string(app.getId());
1811 DDF out = app.getServiceProvider().getListenerService()->send(in);
1816 void SSCache::dormant(const char* key)
1819 xmltooling::NDC ndc("dormant");
1822 m_log.debug("deleting local copy of session (%s)", key);
1824 // lock the cache for writing, which means we know nobody is sitting in find()
1827 // grab the entry from the table
1828 map<string,StoredSession*>::const_iterator i=m_hashtable.find(key);
1829 if (i==m_hashtable.end()) {
1834 // ok, remove the entry and lock it
1835 StoredSession* entry=i->second;
1836 m_hashtable.erase(key);
1842 // we can release the cache entry lock because we know we're not in the cache anymore
1848 void* SSCache::cleanup_fn(void* p)
1851 xmltooling::NDC ndc("cleanup");
1854 SSCache* pcache = reinterpret_cast<SSCache*>(p);
1857 // First, let's block all signals
1858 Thread::mask_all_signals();
1861 scoped_ptr<Mutex> mutex(Mutex::create());
1863 // Load our configuration details...
1864 static const XMLCh cleanupInterval[] = UNICODE_LITERAL_15(c,l,e,a,n,u,p,I,n,t,e,r,v,a,l);
1865 const XMLCh* tag=pcache->m_root ? pcache->m_root->getAttributeNS(nullptr, cleanupInterval) : nullptr;
1866 int rerun_timer = 900;
1868 rerun_timer = XMLString::parseInt(tag);
1869 if (rerun_timer <= 0)
1875 pcache->m_log.info("cleanup thread started...run every %d secs; timeout after %d secs", rerun_timer, pcache->m_inprocTimeout);
1877 while (!pcache->shutdown) {
1878 pcache->shutdown_wait->timedwait(mutex.get(), rerun_timer);
1879 if (pcache->shutdown)
1882 // Ok, let's run through the cleanup process and clean out
1883 // really old sessions. This is a two-pass process. The
1884 // first pass is done holding a read-lock while we iterate over
1885 // the cache. The second pass doesn't need a lock because
1886 // the 'deletes' will lock the cache.
1888 // Pass 1: iterate over the map and find all entries that have not been
1889 // used in the allotted timeout.
1890 vector<string> stale_keys;
1891 time_t stale = time(nullptr) - pcache->m_inprocTimeout;
1893 pcache->m_log.debug("cleanup thread running");
1895 pcache->m_lock->rdlock();
1896 for (map<string,StoredSession*>::const_iterator i = pcache->m_hashtable.begin(); i != pcache->m_hashtable.end(); ++i) {
1897 // If the last access was BEFORE the stale timeout...
1899 time_t last=i->second->getLastAccess();
1900 i->second->unlock();
1902 stale_keys.push_back(i->first);
1904 pcache->m_lock->unlock();
1906 if (!stale_keys.empty()) {
1907 pcache->m_log.info("purging %d old sessions", stale_keys.size());
1909 // Pass 2: walk through the list of stale entries and remove them from the cache
1910 for_each(stale_keys.begin(), stale_keys.end(), boost::bind(&SSCache::dormant, pcache, boost::bind(&string::c_str, _1)));
1913 pcache->m_log.debug("cleanup thread completed");
1916 pcache->m_log.info("cleanup thread exiting");
1924 void SSCache::receive(DDF& in, ostream& out)
1927 xmltooling::NDC ndc("receive");
1929 const Application* app = SPConfig::getConfig().getServiceProvider()->getApplication(in["application_id"].string());
1931 throw ListenerException("Application not found, check configuration?");
1933 if (!strcmp(in.name(),"find::"STORAGESERVICE_SESSION_CACHE"::SessionCache")) {
1934 const char* key=in["key"].string();
1936 throw ListenerException("Required parameters missing for session lookup.");
1938 // Do an unversioned read.
1941 if (!m_storage->readText(key, "session", &record, &lastAccess)) {
1942 m_log.debug("session not found in cache (%s)", key);
1944 DDFJanitor jan(ret);
1949 // Adjust for expiration to recover last access time and check timeout.
1950 unsigned long cacheTimeout = getCacheTimeout(*app);
1951 lastAccess -= cacheTimeout;
1952 time_t now=time(nullptr);
1954 // See if we need to check for a timeout.
1955 if (in["timeout"].string()) {
1957 auto_ptr_XMLCh dt(in["timeout"].string());
1958 DateTime dtobj(dt.get());
1959 dtobj.parseDateTime();
1960 timeout = dtobj.getEpoch();
1962 if (timeout > 0 && now - lastAccess >= timeout) {
1963 m_log.info("session timed out (ID: %s)", key);
1964 scoped_ptr<LogoutEvent> logout_event(newLogoutEvent(*app));
1965 if (logout_event.get()) {
1966 logout_event->m_logoutType = LogoutEvent::LOGOUT_EVENT_INVALID;
1967 logout_event->m_sessions.push_back(key);
1968 app->getServiceProvider().getTransactionLog()->write(*logout_event);
1971 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
1974 // Update storage expiration, if possible.
1976 m_storage->updateContext(key, now + cacheTimeout);
1978 catch (std::exception& ex) {
1979 m_log.error("failed to update session expiration: %s", ex.what());
1983 // Send the record back.
1986 else if (!strcmp(in.name(),"touch::"STORAGESERVICE_SESSION_CACHE"::SessionCache")) {
1987 const char* key=in["key"].string();
1989 throw ListenerException("Required parameters missing for session check.");
1990 const char* client_addr = in["client_addr"].string();
1992 // Do a read. May be unversioned if we need to bind a new client address.
1995 int curver = in["version"].integer();
1996 int ver = m_storage->readText(key, "session", &record, &lastAccess, client_addr ? 0 : curver);
1998 m_log.warn("unsuccessful read of session (ID: %s), caches out of sync?", key);
1999 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
2002 // Adjust for expiration to recover last access time and check timeout.
2003 unsigned long cacheTimeout = getCacheTimeout(*app);
2004 lastAccess -= cacheTimeout;
2005 time_t now=time(nullptr);
2007 // See if we need to check for a timeout.
2009 auto_ptr_XMLCh dt(in["timeout"].string());
2011 DateTime dtobj(dt.get());
2012 dtobj.parseDateTime();
2013 timeout = dtobj.getEpoch();
2016 if (timeout > 0 && now - lastAccess >= timeout) {
2017 m_log.info("session timed out (ID: %s)", key);
2018 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
2021 // Update storage expiration, if possible.
2023 m_storage->updateContext(key, now + cacheTimeout);
2025 catch (std::exception& ex) {
2026 m_log.error("failed to update session expiration: %s", ex.what());
2029 // We may need to write back a new address into the session using a versioned update loop.
2032 m_log.info("binding session (%s) to new client address (%s)", key, client_addr);
2034 // We have to reconstitute the session object ourselves.
2036 DDFJanitor sessionjan(sessionobj);
2037 istringstream src(record);
2039 ver = sessionobj["version"].integer();
2040 const char* saddr = nullptr;
2041 if (strchr(client_addr, ':'))
2042 saddr = sessionobj["client_addr"]["6"].string();
2044 saddr = sessionobj["client_addr"]["4"].string();
2046 // Something snuck in and bound the session to this address type, so it better match what we have.
2047 if (!XMLString::equals(saddr, client_addr)) {
2048 m_log.warn("client address mismatch, client (%s), session (%s)", client_addr, saddr);
2049 throw RetryableProfileException(
2050 "Your IP address ($1) does not match the address recorded at the time the session was established.",
2051 params(1, client_addr)
2054 break; // No need to update.
2057 // Bind it into the session.
2058 if (strchr(client_addr, ':'))
2059 sessionobj["client_addr"].addmember("6").string(client_addr);
2061 sessionobj["client_addr"].addmember("4").string(client_addr);
2064 // Tentatively increment the version.
2065 sessionobj["version"].integer(sessionobj["version"].integer() + 1);
2071 ver = m_storage->updateText(key, "session", record.c_str(), 0, ver);
2073 // Fatal problem with update.
2074 m_log.error("updateText failed on StorageService for session (%s)", key);
2075 throw IOException("Unable to update stored session.");
2079 if (++attempts > 10) {
2080 m_log.error("failed to bind client address, update attempts exceeded limit");
2081 throw IOException("Unable to update stored session, exceeded retry limit.");
2083 m_log.warn("storage service indicates the record is out of sync, updating with a fresh copy...");
2084 sessionobj["version"].integer(sessionobj["version"].integer() - 1);
2085 ver = m_storage->readText(key, "session", &record);
2087 m_log.error("readText failed on StorageService for session (%s)", key);
2088 throw IOException("Unable to read back stored session.");
2092 } while (ver < 0); // negative indicates a sync issue so we retry
2096 // Send the record back.
2101 DDFJanitor jan(ret);
2105 else if (!strcmp(in.name(),"remove::"STORAGESERVICE_SESSION_CACHE"::SessionCache")) {
2106 const char* key=in["key"].string();
2108 throw ListenerException("Required parameter missing for session removal.");
2112 DDFJanitor jan(ret);