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 <boost/scoped_ptr.hpp>
50 #include <xmltooling/io/HTTPRequest.h>
51 #include <xmltooling/io/HTTPResponse.h>
52 #include <xmltooling/util/DateTime.h>
53 #include <xmltooling/util/NDC.h>
54 #include <xmltooling/util/ParserPool.h>
55 #include <xmltooling/util/Threads.h>
56 #include <xmltooling/util/XMLHelper.h>
57 #include <xercesc/util/XMLUniDefs.hpp>
60 # include <saml/exceptions.h>
61 # include <saml/SAMLConfig.h>
62 # include <saml/saml2/core/Assertions.h>
63 # include <saml/saml2/metadata/Metadata.h>
64 # include <xmltooling/XMLToolingConfig.h>
65 # include <xmltooling/util/StorageService.h>
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
138 const Application& app,
139 const HTTPRequest& request,
140 const EntityDescriptor* issuer,
141 const saml2::NameID& nameid,
142 const set<string>* indexes
145 Session* find(const Application& app, const char* key, const char* client_addr=nullptr, time_t* timeout=nullptr);
146 void remove(const Application& app, const char* key);
149 string active(const Application& app, const HTTPRequest& request) {
150 if (!m_inboundHeader.empty()) {
151 string session_id = request.getHeader(m_inboundHeader.c_str());
152 if (!session_id.empty())
155 pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_");
156 const char* session_id = request.getCookie(shib_cookie.first.c_str());
157 return (session_id ? session_id : "");
160 Session* find(const Application& app, const HTTPRequest& request, const char* client_addr=nullptr, time_t* timeout=nullptr) {
161 string id = active(app, request);
163 return find(app, id.c_str(), client_addr, timeout);
167 Session* find(const Application& app, HTTPRequest& request, const char* client_addr=nullptr, time_t* timeout=nullptr);
168 void remove(const Application& app, const HTTPRequest& request, HTTPResponse* response=nullptr);
170 unsigned long getCacheTimeout(const Application& app) {
171 // Computes offset for adjusting expiration of sessions.
172 // This can either be static, or dynamic based on the per-app session timeout or lifetime.
174 return m_cacheTimeout;
175 pair<bool,unsigned int> timeout = pair<bool,unsigned int>(false, 3600);
176 const PropertySet* props = app.getPropertySet("Sessions");
178 timeout = props->getUnsignedInt("timeout");
180 timeout.second = 3600;
182 // As long as one of the two factors is set, add them together.
183 if (timeout.second > 0 || m_cacheAllowance > 0)
184 return timeout.second + m_cacheAllowance;
186 // If timeouts are off, and there's no cache slop set, then use the lifetime.
187 timeout = pair<bool,unsigned int>(false, 28800);
189 timeout = props->getUnsignedInt("lifetime");
190 if (!timeout.first || timeout.second == 0)
191 timeout.second = 28800;
193 return timeout.second;
199 StorageService* m_storage;
200 StorageService* m_storage_lite;
205 // maintain back-mappings of NameID/SessionIndex -> session key
206 void insert(const char* key, time_t expires, const char* name, const char* index);
207 bool stronglyMatches(const XMLCh* idp, const XMLCh* sp, const saml2::NameID& n1, const saml2::NameID& n2) const;
208 LogoutEvent* newLogoutEvent(const Application& app) const;
210 bool m_cacheAssertions;
212 const DOMElement* m_root; // Only valid during initialization
213 unsigned long m_inprocTimeout,m_cacheTimeout,m_cacheAllowance;
214 string m_inboundHeader,m_outboundHeader;
216 // inproc means we buffer sessions in memory
217 scoped_ptr<RWLock> m_lock;
218 map<string,StoredSession*> m_hashtable;
220 // management of buffered sessions
221 void dormant(const char* key);
222 static void* cleanup_fn(void*);
225 scoped_ptr<CondWait> shutdown_wait;
226 scoped_ptr<Thread> cleanup_thread;
229 class StoredSession : public virtual Session
232 StoredSession(SSCache* cache, DDF& obj) : m_obj(obj), m_cache(cache), m_expires(0), m_lastAccess(time(nullptr)) {
233 auto_ptr_XMLCh exp(m_obj["expires"].string());
235 DateTime iso(exp.get());
237 m_expires = iso.getEpoch();
241 const char* nameid = obj["nameid"].string();
243 // Parse and bind the document into an XMLObject.
244 istringstream instr(nameid);
245 DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(instr);
246 XercesJanitor<DOMDocument> janitor(doc);
247 m_nameid.reset(saml2::NameIDBuilder::buildNameID());
248 m_nameid->unmarshall(doc->getDocumentElement(), true);
253 m_lock.reset(Mutex::create());
258 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
273 const char* getID() const {
276 const char* getApplicationID() const {
277 return m_obj["application_id"].string();
279 const char* getClientAddress() const {
280 return m_obj["client_addr"].string();
282 const char* getEntityID() const {
283 return m_obj["entity_id"].string();
285 const char* getProtocol() const {
286 return m_obj["protocol"].string();
288 const char* getAuthnInstant() const {
289 return m_obj["authn_instant"].string();
292 const saml2::NameID* getNameID() const {
293 return m_nameid.get();
296 const char* getSessionIndex() const {
297 return m_obj["session_index"].string();
299 const char* getAuthnContextClassRef() const {
300 return m_obj["authncontext_class"].string();
302 const char* getAuthnContextDeclRef() const {
303 return m_obj["authncontext_decl"].string();
305 const vector<Attribute*>& getAttributes() const {
306 if (m_attributes.empty())
307 unmarshallAttributes();
310 const multimap<string,const Attribute*>& getIndexedAttributes() const {
311 if (m_attributeIndex.empty()) {
312 if (m_attributes.empty())
313 unmarshallAttributes();
314 for (vector<Attribute*>::const_iterator a = m_attributes.begin(); a != m_attributes.end(); ++a) {
315 const vector<string>& aliases = (*a)->getAliases();
316 for (vector<string>::const_iterator alias = aliases.begin(); alias != aliases.end(); ++alias)
317 m_attributeIndex.insert(multimap<string,const Attribute*>::value_type(*alias, *a));
320 return m_attributeIndex;
322 const vector<const char*>& getAssertionIDs() const {
324 DDF ids = m_obj["assertions"];
325 DDF id = ids.first();
326 while (id.isstring()) {
327 m_ids.push_back(id.string());
334 void validate(const Application& application, const char* client_addr, time_t* timeout);
337 void addAttributes(const vector<Attribute*>& attributes);
338 const Assertion* getAssertion(const char* id) const;
339 void addAssertion(Assertion* assertion);
342 time_t getExpiration() const { return m_expires; }
343 time_t getLastAccess() const { return m_lastAccess; }
346 void unmarshallAttributes() const;
350 scoped_ptr<saml2::NameID> m_nameid;
351 mutable map< string,boost::shared_ptr<Assertion> > m_tokens;
353 mutable vector<Attribute*> m_attributes;
354 mutable multimap<string,const Attribute*> m_attributeIndex;
355 mutable vector<const char*> m_ids;
358 time_t m_expires,m_lastAccess;
359 scoped_ptr<Mutex> m_lock;
362 SessionCache* SHIBSP_DLLLOCAL StorageServiceCacheFactory(const DOMElement* const & e)
364 return new SSCache(e);
368 Session* SessionCache::find(const Application& application, HTTPRequest& request, const char* client_addr, time_t* timeout)
370 return find(application, const_cast<const HTTPRequest&>(request), client_addr, timeout);
373 void SHIBSP_API shibsp::registerSessionCaches()
375 SPConfig::getConfig().SessionCacheManager.registerFactory(STORAGESERVICE_SESSION_CACHE, StorageServiceCacheFactory);
386 void StoredSession::unmarshallAttributes() const
388 Attribute* attribute;
389 DDF attrs = m_obj["attributes"];
390 DDF attr = attrs.first();
391 while (!attr.isnull()) {
393 attribute = Attribute::unmarshall(attr);
394 m_attributes.push_back(attribute);
395 if (m_cache->m_log.isDebugEnabled())
396 m_cache->m_log.debug("unmarshalled attribute (ID: %s) with %d value%s",
397 attribute->getId(), attr.first().integer(), attr.first().integer()!=1 ? "s" : "");
399 catch (AttributeException& ex) {
400 const char* id = attr.first().name();
401 m_cache->m_log.error("error unmarshalling attribute (ID: %s): %s", id ? id : "none", ex.what());
407 void StoredSession::validate(const Application& app, const char* client_addr, time_t* timeout)
409 time_t now = time(nullptr);
413 if (now > m_expires) {
414 m_cache->m_log.info("session expired (ID: %s)", getID());
415 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
421 if (!XMLString::equals(getClientAddress(),client_addr)) {
422 m_cache->m_log.warn("client address mismatch, client (%s), session (%s)", client_addr, getClientAddress());
423 throw RetryableProfileException(
424 "Your IP address ($1) does not match the address recorded at the time the session was established.",
425 params(1,client_addr)
433 if (!SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
434 DDF in("touch::"STORAGESERVICE_SESSION_CACHE"::SessionCache"), out;
437 in.addmember("key").string(getID());
438 in.addmember("version").integer(m_obj["version"].integer());
439 in.addmember("application_id").string(app.getId());
441 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
442 #ifndef HAVE_GMTIME_R
443 struct tm* ptime=gmtime(timeout);
446 struct tm* ptime=gmtime_r(timeout,&res);
449 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
450 in.addmember("timeout").string(timebuf);
454 out=app.getServiceProvider().getListenerService()->send(in);
461 if (out.isstruct()) {
462 // We got an updated record back.
464 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
465 m_attributes.clear();
466 m_attributeIndex.clear();
473 if (!m_cache->m_storage)
474 throw ConfigurationException("Session touch requires a StorageService.");
476 // Do a versioned read.
479 int curver = m_obj["version"].integer();
480 int ver = m_cache->m_storage->readText(getID(), "session", &record, &lastAccess, curver);
482 m_cache->m_log.warn("unsuccessful versioned read of session (ID: %s), cache out of sync?", getID());
483 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
486 // Adjust for expiration to recover last access time and check timeout.
487 unsigned long cacheTimeout = m_cache->getCacheTimeout(app);
488 lastAccess -= cacheTimeout;
489 if (*timeout > 0 && now - lastAccess >= *timeout) {
490 m_cache->m_log.info("session timed out (ID: %s)", getID());
491 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
494 // Update storage expiration, if possible.
496 m_cache->m_storage->updateContext(getID(), now + cacheTimeout);
498 catch (std::exception& ex) {
499 m_cache->m_log.error("failed to update session expiration: %s", ex.what());
503 // We got an updated record back.
505 istringstream in(record);
508 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
509 m_attributes.clear();
510 m_attributeIndex.clear();
515 throw ConfigurationException("Session touch requires a StorageService.");
524 void StoredSession::addAttributes(const vector<Attribute*>& attributes)
527 xmltooling::NDC ndc("addAttributes");
530 if (!m_cache->m_storage)
531 throw ConfigurationException("Session modification requires a StorageService.");
533 m_cache->m_log.debug("adding attributes to session (%s)", getID());
538 DDF attrs = m_obj["attributes"];
540 attrs = m_obj.addmember("attributes").list();
541 for (vector<Attribute*>::const_iterator a=attributes.begin(); a!=attributes.end(); ++a) {
542 attr = (*a)->marshall();
546 // Tentatively increment the version.
547 m_obj["version"].integer(m_obj["version"].integer()+1);
551 string record(str.str());
554 ver = m_cache->m_storage->updateText(getID(), "session", record.c_str(), 0, m_obj["version"].integer()-1);
556 catch (std::exception&) {
557 // Roll back modification to record.
558 m_obj["version"].integer(m_obj["version"].integer()-1);
559 vector<Attribute*>::size_type count = attributes.size();
561 attrs.last().destroy();
566 // Roll back modification to record.
567 m_obj["version"].integer(m_obj["version"].integer()-1);
568 vector<Attribute*>::size_type count = attributes.size();
570 attrs.last().destroy();
573 // Fatal problem with update.
574 throw IOException("Unable to update stored session.");
578 m_cache->m_log.warn("storage service indicates the record is out of sync, updating with a fresh copy...");
579 ver = m_cache->m_storage->readText(getID(), "session", &record, nullptr);
581 m_cache->m_log.error("readText failed on StorageService for session (%s)", getID());
582 throw IOException("Unable to read back stored session.");
587 istringstream in(record);
591 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
592 m_attributes.clear();
593 m_attributeIndex.clear();
594 newobj["version"].integer(ver);
600 } while (ver < 0); // negative indicates a sync issue so we retry
602 // We own them now, so clean them up.
603 for_each(attributes.begin(), attributes.end(), xmltooling::cleanup<Attribute>());
606 const Assertion* StoredSession::getAssertion(const char* id) const
608 if (!m_cache->m_storage)
609 throw ConfigurationException("Assertion retrieval requires a StorageService.");
611 map< string,boost::shared_ptr<Assertion> >::const_iterator i = m_tokens.find(id);
612 if (i != m_tokens.end())
613 return i->second.get();
616 if (!m_cache->m_storage->readText(getID(), id, &tokenstr, nullptr))
617 throw FatalProfileException("Assertion not found in cache.");
619 // Parse and bind the document into an XMLObject.
620 istringstream instr(tokenstr);
621 DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(instr);
622 XercesJanitor<DOMDocument> janitor(doc);
623 boost::shared_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
626 boost::shared_ptr<Assertion> token = dynamic_pointer_cast<Assertion,XMLObject>(xmlObject);
628 throw FatalProfileException("Request for cached assertion returned an unknown object type.");
630 m_tokens[id] = token;
634 void StoredSession::addAssertion(Assertion* assertion)
637 xmltooling::NDC ndc("addAssertion");
640 if (!m_cache->m_storage)
641 throw ConfigurationException("Session modification requires a StorageService.");
643 throw FatalProfileException("Unknown object type passed to session for storage.");
645 auto_ptr_char id(assertion->getID());
646 if (!id.get() || !*id.get())
647 throw IOException("Assertion did not carry an ID.");
648 else if (strlen(id.get()) > m_cache->m_storage->getCapabilities().getKeySize())
649 throw IOException("Assertion ID ($1) exceeds allowable storage key size.", params(1, id.get()));
651 m_cache->m_log.debug("adding assertion (%s) to session (%s)", id.get(), getID());
654 if (!m_cache->m_storage->readText(getID(), "session", nullptr, &exp))
655 throw IOException("Unable to load expiration time for stored session.");
657 ostringstream tokenstr;
658 tokenstr << *assertion;
659 if (!m_cache->m_storage->createText(getID(), id.get(), tokenstr.str().c_str(), exp))
660 throw IOException("Attempted to insert duplicate assertion ID into session.");
664 DDF token = DDF(nullptr).string(id.get());
665 m_obj["assertions"].add(token);
667 // Tentatively increment the version.
668 m_obj["version"].integer(m_obj["version"].integer()+1);
672 string record(str.str());
675 ver = m_cache->m_storage->updateText(getID(), "session", record.c_str(), 0, m_obj["version"].integer()-1);
677 catch (std::exception&) {
679 m_obj["version"].integer(m_obj["version"].integer()-1);
680 m_cache->m_storage->deleteText(getID(), id.get());
686 m_obj["version"].integer(m_obj["version"].integer()-1);
689 // Fatal problem with update.
690 m_cache->m_log.error("updateText failed on StorageService for session (%s)", getID());
691 m_cache->m_storage->deleteText(getID(), id.get());
692 throw IOException("Unable to update stored session.");
696 m_cache->m_log.warn("storage service indicates the record is out of sync, updating with a fresh copy...");
697 ver = m_cache->m_storage->readText(getID(), "session", &record, nullptr);
699 m_cache->m_log.error("readText failed on StorageService for session (%s)", getID());
700 m_cache->m_storage->deleteText(getID(), id.get());
701 throw IOException("Unable to read back stored session.");
706 istringstream in(record);
710 for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
711 m_attributes.clear();
712 m_attributeIndex.clear();
713 newobj["version"].integer(ver);
719 } while (ver < 0); // negative indicates a sync issue so we retry
727 SessionCache::SessionCache()
731 SessionCache::~SessionCache()
737 void SessionCache::insert(
739 const Application& app,
740 const HTTPRequest& httpRequest,
741 HTTPResponse& httpResponse,
743 const EntityDescriptor* issuer,
744 const XMLCh* protocol,
745 const saml2::NameID* nameid,
746 const XMLCh* authn_instant,
747 const XMLCh* session_index,
748 const XMLCh* authncontext_class,
749 const XMLCh* authncontext_decl,
750 const vector<const Assertion*>* tokens,
751 const vector<Attribute*>* attributes
753 return insert(app, httpRequest, httpResponse, expires, issuer, protocol, nameid,
754 authn_instant, session_index, authncontext_class, authncontext_decl, tokens, attributes);
759 SessionCacheEx::SessionCacheEx()
763 SessionCacheEx::~SessionCacheEx()
767 SSCache::SSCache(const DOMElement* e)
768 : m_log(Category::getInstance(SHIBSP_LOGCAT".SessionCache")), inproc(true),
770 m_storage(nullptr), m_storage_lite(nullptr), m_cacheAssertions(true),
772 m_root(e), m_inprocTimeout(900), m_cacheTimeout(0), m_cacheAllowance(0), shutdown(false)
774 SPConfig& conf = SPConfig::getConfig();
775 inproc = conf.isEnabled(SPConfig::InProcess);
777 static const XMLCh cacheAllowance[] = UNICODE_LITERAL_14(c,a,c,h,e,A,l,l,o,w,a,n,c,e);
778 static const XMLCh cacheAssertions[] = UNICODE_LITERAL_15(c,a,c,h,e,A,s,s,e,r,t,i,o,n,s);
779 static const XMLCh cacheTimeout[] = UNICODE_LITERAL_12(c,a,c,h,e,T,i,m,e,o,u,t);
780 static const XMLCh inprocTimeout[] = UNICODE_LITERAL_13(i,n,p,r,o,c,T,i,m,e,o,u,t);
781 static const XMLCh inboundHeader[] = UNICODE_LITERAL_13(i,n,b,o,u,n,d,H,e,a,d,e,r);
782 static const XMLCh outboundHeader[] = UNICODE_LITERAL_14(o,u,t,b,o,u,n,d,H,e,a,d,e,r);
783 static const XMLCh _StorageService[] = UNICODE_LITERAL_14(S,t,o,r,a,g,e,S,e,r,v,i,c,e);
784 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);
786 m_cacheTimeout = XMLHelper::getAttrInt(e, 0, cacheTimeout);
787 m_cacheAllowance = XMLHelper::getAttrInt(e, 0, cacheAllowance);
789 m_inprocTimeout = XMLHelper::getAttrInt(e, 900, inprocTimeout);
790 m_inboundHeader = XMLHelper::getAttrString(e, nullptr, inboundHeader);
791 if (!m_inboundHeader.empty())
792 RemotedHandler::addRemotedHeader(m_inboundHeader.c_str());
793 m_outboundHeader = XMLHelper::getAttrString(e, nullptr, outboundHeader);
796 if (conf.isEnabled(SPConfig::OutOfProcess)) {
797 string ssid(XMLHelper::getAttrString(e, nullptr, _StorageService));
799 m_storage = conf.getServiceProvider()->getStorageService(ssid.c_str());
801 m_log.info("bound to StorageService (%s)", ssid.c_str());
803 m_log.warn("specified StorageService (%s) not found", ssid.c_str());
806 m_storage = conf.getServiceProvider()->getStorageService(nullptr);
808 m_log.info("bound to arbitrary StorageService");
810 throw ConfigurationException("SessionCache unable to locate StorageService, check configuration.");
813 ssid = XMLHelper::getAttrString(e, nullptr, _StorageServiceLite);
815 m_storage_lite = conf.getServiceProvider()->getStorageService(ssid.c_str());
817 m_log.info("bound to 'lite' StorageService (%s)", ssid.c_str());
819 m_log.warn("specified 'lite' StorageService (%s) not found", ssid.c_str());
821 if (!m_storage_lite) {
822 m_log.info("StorageService for 'lite' use not set, using standard StorageService");
823 m_storage_lite = m_storage;
826 m_cacheAssertions = XMLHelper::getAttrBool(e, true, cacheAssertions);
830 ListenerService* listener=conf.getServiceProvider()->getListenerService(false);
832 if (!conf.isEnabled(SPConfig::OutOfProcess) && !listener)
833 throw ConfigurationException("SessionCache requires a ListenerService, but none available.");
834 m_lock.reset(RWLock::create());
835 shutdown_wait.reset(CondWait::create());
836 cleanup_thread.reset(Thread::create(&cleanup_fn, this));
840 if (listener && conf.isEnabled(SPConfig::OutOfProcess)) {
841 listener->regListener("find::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
842 listener->regListener("remove::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
843 listener->regListener("touch::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
846 m_log.info("no ListenerService available, cache remoting disabled");
855 // Shut down the cleanup thread and let it know...
857 if (shutdown_wait.get())
858 shutdown_wait->signal();
859 if (cleanup_thread.get())
860 cleanup_thread->join(nullptr);
862 for_each(m_hashtable.begin(),m_hashtable.end(),cleanup_pair<string,StoredSession>());
866 SPConfig& conf = SPConfig::getConfig();
867 ListenerService* listener=conf.getServiceProvider()->getListenerService(false);
868 if (listener && conf.isEnabled(SPConfig::OutOfProcess)) {
869 listener->unregListener("find::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
870 listener->unregListener("remove::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
871 listener->unregListener("touch::"STORAGESERVICE_SESSION_CACHE"::SessionCache",this);
881 auto_ptr_char temp(SAMLConfig::getConfig().generateIdentifier());
882 m_storage->createString("SessionCacheTest", temp.get(), "Test", time(nullptr) + 60);
883 m_storage->deleteString("SessionCacheTest", temp.get());
886 void SSCache::insert(const char* key, time_t expires, const char* name, const char* index)
889 unsigned int storageLimit = m_storage_lite->getCapabilities().getKeySize();
890 if (strlen(name) > storageLimit) {
891 dup = string(name).substr(0, storageLimit);
896 DDFJanitor jobj(obj);
898 // Since we can't guarantee uniqueness, check for an existing record.
901 int ver = m_storage_lite->readText("NameID", name, &record, &recordexp);
903 // Existing record, so we need to unmarshall it.
904 istringstream in(record);
909 obj = DDF(nullptr).structure();
912 if (!index || !*index)
914 DDF sessions = obj.addmember(index);
915 if (!sessions.islist())
917 DDF session = DDF(nullptr).string(key);
918 sessions.add(session);
920 // Remarshall the record.
924 // Try and store it back...
926 ver = m_storage_lite->updateText("NameID", name, out.str().c_str(), max(expires, recordexp), ver);
928 // Out of sync, or went missing, so retry.
929 return insert(key, expires, name, index);
932 else if (!m_storage_lite->createText("NameID", name, out.str().c_str(), expires)) {
933 // Hit a dup, so just retry, hopefully hitting the other branch.
934 return insert(key, expires, name, index);
938 void SSCache::insert(
940 const Application& app,
941 const HTTPRequest& httpRequest,
942 HTTPResponse& httpResponse,
944 const saml2md::EntityDescriptor* issuer,
945 const XMLCh* protocol,
946 const saml2::NameID* nameid,
947 const XMLCh* authn_instant,
948 const XMLCh* session_index,
949 const XMLCh* authncontext_class,
950 const XMLCh* authncontext_decl,
951 const vector<const Assertion*>* tokens,
952 const vector<Attribute*>* attributes
956 xmltooling::NDC ndc("insert");
959 throw ConfigurationException("SessionCache insertion requires a StorageService.");
961 m_log.debug("creating new session");
963 time_t now = time(nullptr);
964 auto_ptr_char index(session_index);
965 auto_ptr_char entity_id(issuer ? issuer->getEntityID() : nullptr);
966 auto_ptr_char name(nameid ? nameid->getName() : nullptr);
968 if (name.get() && *name.get()) {
969 // Check for a pending logout.
970 unsigned int storageLimit = m_storage_lite->getCapabilities().getKeySize();
971 string namebuf = name.get();
972 if (namebuf.length() > storageLimit)
973 namebuf = namebuf.substr(0, storageLimit);
975 int ver = m_storage_lite->readText("Logout", namebuf.c_str(), &pending);
978 DDFJanitor jpend(pendobj);
979 istringstream pstr(pending);
981 // IdP.SP.index contains logout expiration, if any.
982 DDF deadmenwalking = pendobj[issuer ? entity_id.get() : "_shibnull"][app.getRelyingParty(issuer)->getString("entityID").second];
983 const char* logexpstr = deadmenwalking[session_index ? index.get() : "_shibnull"].string();
984 if (!logexpstr && session_index) // we tried an exact session match, now try for nullptr
985 logexpstr = deadmenwalking["_shibnull"].string();
987 auto_ptr_XMLCh dt(logexpstr);
988 DateTime dtobj(dt.get());
989 dtobj.parseDateTime();
990 time_t logexp = dtobj.getEpoch();
991 if (now - XMLToolingConfig::getConfig().clock_skew_secs < logexp)
992 throw FatalProfileException("A logout message from your identity provider has blocked your login attempt.");
997 XMLCh* widekey = SAMLConfig::getConfig().generateIdentifier();
998 auto_ptr_char key(widekey);
999 XMLString::release(&widekey);
1001 // Store session properties in DDF.
1002 DDF obj = DDF(key.get()).structure();
1003 DDFJanitor entryobj(obj);
1004 obj.addmember("version").integer(1);
1005 obj.addmember("application_id").string(app.getId());
1007 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
1008 #ifndef HAVE_GMTIME_R
1009 struct tm* ptime=gmtime(&expires);
1012 struct tm* ptime=gmtime_r(&expires,&res);
1015 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
1016 obj.addmember("expires").string(timebuf);
1018 obj.addmember("client_addr").string(httpRequest.getRemoteAddr().c_str());
1020 obj.addmember("entity_id").string(entity_id.get());
1022 auto_ptr_char prot(protocol);
1023 obj.addmember("protocol").string(prot.get());
1025 if (authn_instant) {
1026 auto_ptr_char instant(authn_instant);
1027 obj.addmember("authn_instant").string(instant.get());
1030 obj.addmember("session_index").string(index.get());
1031 if (authncontext_class) {
1032 auto_ptr_char ac(authncontext_class);
1033 obj.addmember("authncontext_class").string(ac.get());
1035 if (authncontext_decl) {
1036 auto_ptr_char ad(authncontext_decl);
1037 obj.addmember("authncontext_decl").string(ad.get());
1041 ostringstream namestr;
1043 obj.addmember("nameid").string(namestr.str().c_str());
1046 if (tokens && m_cacheAssertions) {
1047 obj.addmember("assertions").list();
1048 for (vector<const Assertion*>::const_iterator t = tokens->begin(); t!=tokens->end(); ++t) {
1049 auto_ptr_char tokenid((*t)->getID());
1050 DDF tokid = DDF(nullptr).string(tokenid.get());
1051 obj["assertions"].add(tokid);
1057 DDF attrlist = obj.addmember("attributes").list();
1058 for (vector<Attribute*>::const_iterator a=attributes->begin(); a!=attributes->end(); ++a) {
1059 attr = (*a)->marshall();
1064 ostringstream record;
1067 m_log.debug("storing new session...");
1068 unsigned long cacheTimeout = getCacheTimeout(app);
1069 if (!m_storage->createText(key.get(), "session", record.str().c_str(), now + cacheTimeout))
1070 throw FatalProfileException("Attempted to create a session with a duplicate key.");
1072 // Store the reverse mapping for logout.
1075 insert(key.get(), expires, name.get(), index.get());
1077 catch (std::exception& ex) {
1078 m_log.error("error storing back mapping of NameID for logout: %s", ex.what());
1081 if (tokens && m_cacheAssertions) {
1083 for (vector<const Assertion*>::const_iterator t = tokens->begin(); t!=tokens->end(); ++t) {
1084 ostringstream tokenstr;
1086 auto_ptr_char tokenid((*t)->getID());
1087 if (!tokenid.get() || !*tokenid.get() || strlen(tokenid.get()) > m_storage->getCapabilities().getKeySize())
1088 throw IOException("Assertion ID is missing or exceeds key size of storage service.");
1089 else if (!m_storage->createText(key.get(), tokenid.get(), tokenstr.str().c_str(), now + cacheTimeout))
1090 throw IOException("Duplicate assertion ID ($1)", params(1, tokenid.get()));
1093 catch (std::exception& ex) {
1094 m_log.error("error storing assertion along with session: %s", ex.what());
1098 const char* pid = obj["entity_id"].string();
1099 const char* prot = obj["protocol"].string();
1100 m_log.info("new session created: ID (%s) IdP (%s) Protocol(%s) Address (%s)",
1101 key.get(), pid ? pid : "none", prot ? prot : "none", httpRequest.getRemoteAddr().c_str());
1103 if (!m_outboundHeader.empty())
1104 httpResponse.setResponseHeader(m_outboundHeader.c_str(), key.get());
1106 time_t cookieLifetime = 0;
1107 pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_", &cookieLifetime);
1108 string k(key.get());
1109 k += shib_cookie.second;
1111 if (cookieLifetime > 0) {
1112 cookieLifetime += now;
1113 #ifndef HAVE_GMTIME_R
1114 ptime=gmtime(&cookieLifetime);
1116 ptime=gmtime_r(&cookieLifetime,&res);
1118 char cookietimebuf[64];
1119 strftime(cookietimebuf,64,"; expires=%a, %d %b %Y %H:%M:%S GMT",ptime);
1123 httpResponse.setCookie(shib_cookie.first.c_str(), k.c_str());
1124 sessionID = key.get();
1127 bool SSCache::matches(
1128 const Application& app,
1129 const xmltooling::HTTPRequest& request,
1130 const saml2md::EntityDescriptor* issuer,
1131 const saml2::NameID& nameid,
1132 const set<string>* indexes
1135 auto_ptr_char entityID(issuer ? issuer->getEntityID() : nullptr);
1137 Session* session = find(app, request);
1139 Locker locker(session, false);
1140 if (XMLString::equals(session->getEntityID(), entityID.get()) && session->getNameID() &&
1141 stronglyMatches(issuer->getEntityID(), app.getRelyingParty(issuer)->getXMLString("entityID").second, nameid, *session->getNameID())) {
1142 return (!indexes || indexes->empty() || (session->getSessionIndex() ? (indexes->count(session->getSessionIndex())>0) : false));
1146 catch (std::exception& ex) {
1147 m_log.error("error while matching session: %s", ex.what());
1152 vector<string>::size_type SSCache::logout(
1153 const Application& app,
1154 const saml2md::EntityDescriptor* issuer,
1155 const saml2::NameID& nameid,
1156 const set<string>* indexes,
1158 vector<string>& sessionsKilled
1162 xmltooling::NDC ndc("logout");
1166 throw ConfigurationException("SessionCache logout requires a StorageService.");
1168 auto_ptr_char entityID(issuer ? issuer->getEntityID() : nullptr);
1169 auto_ptr_char name(nameid.getName());
1171 m_log.info("request to logout sessions from (%s) for (%s)", entityID.get() ? entityID.get() : "unknown", name.get());
1173 unsigned int storageLimit = m_storage_lite->getCapabilities().getKeySize();
1174 if (strlen(name.get()) > storageLimit)
1175 const_cast<char*>(name.get())[storageLimit] = 0;
1178 DDFJanitor jobj(obj);
1183 // Record the logout to prevent post-delivered assertions.
1184 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
1185 #ifndef HAVE_GMTIME_R
1186 struct tm* ptime=gmtime(&expires);
1189 struct tm* ptime=gmtime_r(&expires,&res);
1192 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
1195 ver = m_storage_lite->readText("Logout", name.get(), &record, &oldexp);
1197 istringstream lin(record);
1201 obj = DDF(nullptr).structure();
1204 // Structure is keyed by the IdP and SP, with a member per session index containing the expiration.
1205 DDF root = obj.addmember(issuer ? entityID.get() : "_shibnull").addmember(app.getRelyingParty(issuer)->getString("entityID").second);
1207 for (set<string>::const_iterator x = indexes->begin(); x!=indexes->end(); ++x)
1208 root.addmember(x->c_str()).string(timebuf);
1211 root.addmember("_shibnull").string(timebuf);
1219 ver = m_storage_lite->updateText("Logout", name.get(), lout.str().c_str(), max(expires, oldexp), ver);
1221 // Out of sync, or went missing, so retry.
1222 return logout(app, issuer, nameid, indexes, expires, sessionsKilled);
1225 else if (!m_storage_lite->createText("Logout", name.get(), lout.str().c_str(), expires)) {
1226 // Hit a dup, so just retry, hopefully hitting the other branch.
1227 return logout(app, issuer, nameid, indexes, expires, sessionsKilled);
1234 // Read in potentially matching sessions.
1235 ver = m_storage_lite->readText("NameID", name.get(), &record);
1237 m_log.debug("no active sessions to logout for supplied issuer and subject");
1241 istringstream in(record);
1244 // The record contains child lists for each known session index.
1246 DDF sessions = obj.first();
1247 while (sessions.islist()) {
1248 if (!indexes || indexes->empty() || indexes->count(sessions.name())) {
1249 key = sessions.first();
1250 while (key.isstring()) {
1251 // Fetch the session for comparison.
1252 Session* session = nullptr;
1254 session = find(app, key.string());
1256 catch (std::exception& ex) {
1257 m_log.error("error locating session (%s): %s", key.string(), ex.what());
1261 Locker locker(session, false);
1263 if (XMLString::equals(session->getEntityID(), entityID.get())) {
1265 if (stronglyMatches(issuer->getEntityID(), app.getRelyingParty(issuer)->getXMLString("entityID").second, nameid, *session->getNameID())) {
1266 sessionsKilled.push_back(key.string());
1270 m_log.debug("session (%s) contained a non-matching NameID, leaving it alone", key.string());
1274 m_log.debug("session (%s) established by different IdP, leaving it alone", key.string());
1278 // Session's gone, so...
1279 sessionsKilled.push_back(key.string());
1282 key = sessions.next();
1285 // No sessions left for this index?
1286 if (sessions.first().isnull())
1289 sessions = obj.next();
1292 if (obj.first().isnull())
1295 // If possible, write back the mapping record (this isn't crucial).
1298 m_storage_lite->deleteText("NameID", name.get());
1300 else if (!sessionsKilled.empty()) {
1303 if (m_storage_lite->updateText("NameID", name.get(), out.str().c_str(), 0, ver) <= 0)
1304 m_log.warn("logout mapping record changed behind us, leaving it alone");
1307 catch (std::exception& ex) {
1308 m_log.error("error updating logout mapping record: %s", ex.what());
1311 return sessionsKilled.size();
1314 bool SSCache::stronglyMatches(const XMLCh* idp, const XMLCh* sp, const saml2::NameID& n1, const saml2::NameID& n2) const
1316 if (!XMLString::equals(n1.getName(), n2.getName()))
1319 const XMLCh* s1 = n1.getFormat();
1320 const XMLCh* s2 = n2.getFormat();
1322 s1 = saml2::NameID::UNSPECIFIED;
1324 s2 = saml2::NameID::UNSPECIFIED;
1325 if (!XMLString::equals(s1,s2))
1328 s1 = n1.getNameQualifier();
1329 s2 = n2.getNameQualifier();
1334 if (!XMLString::equals(s1,s2))
1337 s1 = n1.getSPNameQualifier();
1338 s2 = n2.getSPNameQualifier();
1343 if (!XMLString::equals(s1,s2))
1349 LogoutEvent* SSCache::newLogoutEvent(const Application& app) const
1351 if (!SPConfig::getConfig().isEnabled(SPConfig::Logging))
1354 auto_ptr<TransactionLog::Event> event(SPConfig::getConfig().EventManager.newPlugin(LOGOUT_EVENT, nullptr));
1355 LogoutEvent* logout_event = dynamic_cast<LogoutEvent*>(event.get());
1357 logout_event->m_app = &app;
1359 return logout_event;
1362 m_log.warn("unable to audit event, log event object was of an incorrect type");
1365 catch (std::exception& ex) {
1366 m_log.warn("exception auditing event: %s", ex.what());
1373 Session* SSCache::find(const Application& app, const char* key, const char* client_addr, time_t* timeout)
1376 xmltooling::NDC ndc("find");
1378 StoredSession* session=nullptr;
1381 m_log.debug("searching local cache for session (%s)", key);
1383 map<string,StoredSession*>::const_iterator i=m_hashtable.find(key);
1384 if (i!=m_hashtable.end()) {
1385 // Save off and lock the session.
1386 session = i->second;
1389 m_log.debug("session found locally, validating it for use");
1397 if (!SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
1398 m_log.debug("session not found locally, remoting the search");
1399 // Remote the request.
1400 DDF in("find::"STORAGESERVICE_SESSION_CACHE"::SessionCache"), out;
1403 in.addmember("key").string(key);
1404 in.addmember("application_id").string(app.getId());
1405 if (timeout && *timeout) {
1406 // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps.
1407 #ifndef HAVE_GMTIME_R
1408 struct tm* ptime=gmtime(timeout);
1411 struct tm* ptime=gmtime_r(timeout,&res);
1414 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
1415 in.addmember("timeout").string(timebuf);
1419 out=app.getServiceProvider().getListenerService()->send(in);
1420 if (!out.isstruct()) {
1422 m_log.debug("session not found in remote cache");
1426 // Wrap the results in a local entry and save it.
1427 session = new StoredSession(this, out);
1428 // The remote end has handled timeout issues, we handle address and expiration checks.
1437 // We're out of process, so we can search the storage service directly.
1440 throw ConfigurationException("SessionCache lookup requires a StorageService.");
1442 m_log.debug("searching for session (%s)", key);
1447 int ver = m_storage->readText(key, "session", &record, &lastAccess);
1451 m_log.debug("reconstituting session and checking validity");
1453 istringstream in(record);
1456 unsigned long cacheTimeout = getCacheTimeout(app);
1457 lastAccess -= cacheTimeout; // adjusts it back to the last time the record's timestamp was touched
1458 time_t now=time(nullptr);
1460 if (timeout && *timeout > 0 && now - lastAccess >= *timeout) {
1461 m_log.info("session timed out (ID: %s)", key);
1462 scoped_ptr<LogoutEvent> logout_event(newLogoutEvent(app));
1463 if (logout_event.get()) {
1464 logout_event->m_logoutType = LogoutEvent::LOGOUT_EVENT_INVALID;
1465 logout_event->m_sessions.push_back(key);
1466 app.getServiceProvider().getTransactionLog()->write(*logout_event);
1469 const char* eid = obj["entity_id"].string();
1472 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
1476 throw RetryableProfileException("Your session has expired, and you must re-authenticate.", namedparams(1, "entityID", eid2.c_str()));
1480 // Update storage expiration, if possible.
1482 m_storage->updateContext(key, now + cacheTimeout);
1484 catch (std::exception& ex) {
1485 m_log.error("failed to update session expiration: %s", ex.what());
1489 // Wrap the results in a local entry and save it.
1490 session = new StoredSession(this, obj);
1491 // We handled timeout issues, still need to handle address and expiration checks.
1494 throw ConfigurationException("SessionCache search requires a StorageService.");
1499 // Lock for writing and repeat the search to avoid duplication.
1501 SharedLock shared(m_lock, false);
1502 if (m_hashtable.count(key)) {
1503 // We're using an existing session entry.
1505 session = m_hashtable[key];
1509 m_hashtable[key]=session;
1515 if (!XMLString::equals(session->getApplicationID(), app.getId())) {
1516 m_log.error("an application (%s) tried to access another application's session", app.getId());
1521 // Verify currency and update the timestamp if indicated by caller.
1523 session->validate(app, client_addr, timeout);
1527 scoped_ptr<LogoutEvent> logout_event(newLogoutEvent(app));
1528 if (logout_event.get()) {
1529 logout_event->m_logoutType = LogoutEvent::LOGOUT_EVENT_INVALID;
1530 logout_event->m_session = session;
1531 logout_event->m_sessions.push_back(session->getID());
1532 app.getServiceProvider().getTransactionLog()->write(*logout_event);
1543 Session* SSCache::find(const Application& app, HTTPRequest& request, const char* client_addr, time_t* timeout)
1545 string id = active(app, request);
1549 Session* session = find(app, id.c_str(), client_addr, timeout);
1552 HTTPResponse* response = dynamic_cast<HTTPResponse*>(&request);
1554 if (!m_outboundHeader.empty())
1555 response->setResponseHeader(m_outboundHeader.c_str(), nullptr);
1556 pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_");
1557 string exp(shib_cookie.second);
1558 exp += "; expires=Mon, 01 Jan 2001 00:00:00 GMT";
1559 response->setCookie(shib_cookie.first.c_str(), exp.c_str());
1562 catch (std::exception&) {
1563 HTTPResponse* response = dynamic_cast<HTTPResponse*>(&request);
1565 if (!m_outboundHeader.empty())
1566 response->setResponseHeader(m_outboundHeader.c_str(), nullptr);
1567 pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_");
1568 string exp(shib_cookie.second);
1569 exp += "; expires=Mon, 01 Jan 2001 00:00:00 GMT";
1570 response->setCookie(shib_cookie.first.c_str(), exp.c_str());
1577 void SSCache::remove(const Application& app, const HTTPRequest& request, HTTPResponse* response)
1580 pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_");
1582 if (!m_inboundHeader.empty())
1583 session_id = request.getHeader(m_inboundHeader.c_str());
1584 if (session_id.empty()) {
1585 const char* c = request.getCookie(shib_cookie.first.c_str());
1590 if (!session_id.empty()) {
1592 if (!m_outboundHeader.empty())
1593 response->setResponseHeader(m_outboundHeader.c_str(), nullptr);
1594 string exp(shib_cookie.second);
1595 exp += "; expires=Mon, 01 Jan 2001 00:00:00 GMT";
1596 response->setCookie(shib_cookie.first.c_str(), exp.c_str());
1598 remove(app, session_id.c_str());
1602 void SSCache::remove(const Application& app, const char* key)
1605 xmltooling::NDC ndc("remove");
1607 // Take care of local copy.
1611 if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
1612 // Remove the session from storage directly.
1614 m_storage->deleteContext(key);
1615 m_log.info("removed session (%s)", key);
1617 throw ConfigurationException("SessionCache removal requires a StorageService.");
1621 // Remote the request.
1622 DDF in("remove::"STORAGESERVICE_SESSION_CACHE"::SessionCache");
1625 in.addmember("key").string(key);
1626 in.addmember("application_id").string(app.getId());
1628 DDF out = app.getServiceProvider().getListenerService()->send(in);
1633 void SSCache::dormant(const char* key)
1636 xmltooling::NDC ndc("dormant");
1639 m_log.debug("deleting local copy of session (%s)", key);
1641 // lock the cache for writing, which means we know nobody is sitting in find()
1644 // grab the entry from the table
1645 map<string,StoredSession*>::const_iterator i=m_hashtable.find(key);
1646 if (i==m_hashtable.end()) {
1651 // ok, remove the entry and lock it
1652 StoredSession* entry=i->second;
1653 m_hashtable.erase(key);
1659 // we can release the cache entry lock because we know we're not in the cache anymore
1665 void* SSCache::cleanup_fn(void* p)
1668 xmltooling::NDC ndc("cleanup");
1671 SSCache* pcache = reinterpret_cast<SSCache*>(p);
1674 // First, let's block all signals
1675 Thread::mask_all_signals();
1678 scoped_ptr<Mutex> mutex(Mutex::create());
1680 // Load our configuration details...
1681 static const XMLCh cleanupInterval[] = UNICODE_LITERAL_15(c,l,e,a,n,u,p,I,n,t,e,r,v,a,l);
1682 const XMLCh* tag=pcache->m_root ? pcache->m_root->getAttributeNS(nullptr, cleanupInterval) : nullptr;
1683 int rerun_timer = 900;
1685 rerun_timer = XMLString::parseInt(tag);
1686 if (rerun_timer <= 0)
1692 pcache->m_log.info("cleanup thread started...run every %d secs; timeout after %d secs", rerun_timer, pcache->m_inprocTimeout);
1694 while (!pcache->shutdown) {
1695 pcache->shutdown_wait->timedwait(mutex.get(), rerun_timer);
1696 if (pcache->shutdown)
1699 // Ok, let's run through the cleanup process and clean out
1700 // really old sessions. This is a two-pass process. The
1701 // first pass is done holding a read-lock while we iterate over
1702 // the cache. The second pass doesn't need a lock because
1703 // the 'deletes' will lock the cache.
1705 // Pass 1: iterate over the map and find all entries that have not been
1706 // used in the allotted timeout.
1707 vector<string> stale_keys;
1708 time_t stale = time(nullptr) - pcache->m_inprocTimeout;
1710 pcache->m_log.debug("cleanup thread running");
1712 pcache->m_lock->rdlock();
1713 for (map<string,StoredSession*>::const_iterator i = pcache->m_hashtable.begin(); i != pcache->m_hashtable.end(); ++i) {
1714 // If the last access was BEFORE the stale timeout...
1716 time_t last=i->second->getLastAccess();
1717 i->second->unlock();
1719 stale_keys.push_back(i->first);
1721 pcache->m_lock->unlock();
1723 if (!stale_keys.empty()) {
1724 pcache->m_log.info("purging %d old sessions", stale_keys.size());
1726 // Pass 2: walk through the list of stale entries and remove them from the cache
1727 for_each(stale_keys.begin(), stale_keys.end(), boost::bind(&SSCache::dormant, pcache, boost::bind(&string::c_str, _1)));
1730 pcache->m_log.debug("cleanup thread completed");
1733 pcache->m_log.info("cleanup thread exiting");
1741 void SSCache::receive(DDF& in, ostream& out)
1744 xmltooling::NDC ndc("receive");
1746 const Application* app = SPConfig::getConfig().getServiceProvider()->getApplication(in["application_id"].string());
1748 throw ListenerException("Application not found, check configuration?");
1750 if (!strcmp(in.name(),"find::"STORAGESERVICE_SESSION_CACHE"::SessionCache")) {
1751 const char* key=in["key"].string();
1753 throw ListenerException("Required parameters missing for session lookup.");
1755 // Do an unversioned read.
1758 if (!m_storage->readText(key, "session", &record, &lastAccess)) {
1760 DDFJanitor jan(ret);
1765 // Adjust for expiration to recover last access time and check timeout.
1766 unsigned long cacheTimeout = getCacheTimeout(*app);
1767 lastAccess -= cacheTimeout;
1768 time_t now=time(nullptr);
1770 // See if we need to check for a timeout.
1771 if (in["timeout"].string()) {
1773 auto_ptr_XMLCh dt(in["timeout"].string());
1774 DateTime dtobj(dt.get());
1775 dtobj.parseDateTime();
1776 timeout = dtobj.getEpoch();
1778 if (timeout > 0 && now - lastAccess >= timeout) {
1779 m_log.info("session timed out (ID: %s)", key);
1780 scoped_ptr<LogoutEvent> logout_event(newLogoutEvent(*app));
1781 if (logout_event.get()) {
1782 logout_event->m_logoutType = LogoutEvent::LOGOUT_EVENT_INVALID;
1783 logout_event->m_sessions.push_back(key);
1784 app->getServiceProvider().getTransactionLog()->write(*logout_event);
1787 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
1790 // Update storage expiration, if possible.
1792 m_storage->updateContext(key, now + cacheTimeout);
1794 catch (std::exception& ex) {
1795 m_log.error("failed to update session expiration: %s", ex.what());
1799 // Send the record back.
1802 else if (!strcmp(in.name(),"touch::"STORAGESERVICE_SESSION_CACHE"::SessionCache")) {
1803 const char* key=in["key"].string();
1805 throw ListenerException("Required parameters missing for session check.");
1807 // Do a versioned read.
1810 int curver = in["version"].integer();
1811 int ver = m_storage->readText(key, "session", &record, &lastAccess, curver);
1813 m_log.warn("unsuccessful versioned read of session (ID: %s), caches out of sync?", key);
1814 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
1817 // Adjust for expiration to recover last access time and check timeout.
1818 unsigned long cacheTimeout = getCacheTimeout(*app);
1819 lastAccess -= cacheTimeout;
1820 time_t now=time(nullptr);
1822 // See if we need to check for a timeout.
1824 auto_ptr_XMLCh dt(in["timeout"].string());
1826 DateTime dtobj(dt.get());
1827 dtobj.parseDateTime();
1828 timeout = dtobj.getEpoch();
1831 if (timeout > 0 && now - lastAccess >= timeout) {
1832 m_log.info("session timed out (ID: %s)", key);
1833 throw RetryableProfileException("Your session has expired, and you must re-authenticate.");
1836 // Update storage expiration, if possible.
1838 m_storage->updateContext(key, now + cacheTimeout);
1840 catch (std::exception& ex) {
1841 m_log.error("failed to update session expiration: %s", ex.what());
1845 // Send the record back.
1850 DDFJanitor jan(ret);
1854 else if (!strcmp(in.name(),"remove::"STORAGESERVICE_SESSION_CACHE"::SessionCache")) {
1855 const char* key=in["key"].string();
1857 throw ListenerException("Required parameter missing for session removal.");
1861 DDFJanitor jan(ret);