const char* getEntityID() const {\r
return m_obj["entity_id"].string();\r
}\r
+ const char* getProtocol() const {\r
+ return m_obj["protocol"].string();\r
+ }\r
const char* getAuthnInstant() const {\r
return m_obj["authn_instant"].string();\r
}\r
const Application& application,\r
const char* client_addr=NULL,\r
const saml2md::EntityDescriptor* issuer=NULL,\r
+ const XMLCh* protocol=NULL,\r
const saml2::NameID* nameid=NULL,\r
- const char* authn_instant=NULL,\r
- const char* session_index=NULL,\r
- const char* authncontext_class=NULL,\r
- const char* authncontext_decl=NULL,\r
+ const XMLCh* authn_instant=NULL,\r
+ const XMLCh* session_index=NULL,\r
+ const XMLCh* authncontext_class=NULL,\r
+ const XMLCh* authncontext_decl=NULL,\r
const vector<const Assertion*>* tokens=NULL,\r
const multimap<string,Attribute*>* attributes=NULL\r
);\r
Session* find(const char* key, const Application& application, const char* client_addr=NULL, time_t* timeout=NULL);\r
- void remove(const char* key, const Application& application, const char* client_addr);\r
+ void remove(const char* key, const Application& application);\r
+ void remove(\r
+ const saml2md::EntityDescriptor* issuer,\r
+ const saml2::NameID& nameid,\r
+ const char* index,\r
+ const Application& application,\r
+ vector<string>& sessions\r
+ );\r
\r
Category& m_log;\r
StorageService* m_storage;\r
+\r
+ private:\r
+ // maintain back-mappings of NameID/SessionIndex -> session key\r
+ void insert(const char* key, time_t expires, const char* name, const char* index);\r
+\r
+ bool stronglyMatches(const XMLCh* idp, const XMLCh* sp, const saml2::NameID& n1, const saml2::NameID& n2) const;\r
};\r
\r
SessionCache* SHIBSP_DLLLOCAL StorageServiceCacheFactory(const DOMElement* const & e)\r
\r
ostringstream tokenstr;\r
tokenstr << *assertion;\r
- m_cache->m_storage->createText(getID(), id.get(), tokenstr.str().c_str(), exp);\r
+ if (!m_cache->m_storage->createText(getID(), id.get(), tokenstr.str().c_str(), exp))\r
+ throw IOException("Attempted to insert duplicate assertion ID into session.");\r
\r
int ver;\r
do {\r
\r
ListenerService* listener=conf.getServiceProvider()->getListenerService(false);\r
if (listener && conf.isEnabled(SPConfig::OutOfProcess)) {\r
- listener->regListener("insert::"REMOTED_SESSION_CACHE"::SessionCache",this);\r
listener->regListener("find::"REMOTED_SESSION_CACHE"::SessionCache",this);\r
listener->regListener("remove::"REMOTED_SESSION_CACHE"::SessionCache",this);\r
listener->regListener("touch::"REMOTED_SESSION_CACHE"::SessionCache",this);\r
SPConfig& conf = SPConfig::getConfig();\r
ListenerService* listener=conf.getServiceProvider()->getListenerService(false);\r
if (listener && conf.isEnabled(SPConfig::OutOfProcess)) {\r
- listener->unregListener("insert::"REMOTED_SESSION_CACHE"::SessionCache",this);\r
listener->unregListener("find::"REMOTED_SESSION_CACHE"::SessionCache",this);\r
listener->unregListener("remove::"REMOTED_SESSION_CACHE"::SessionCache",this);\r
listener->unregListener("touch::"REMOTED_SESSION_CACHE"::SessionCache",this);\r
}\r
}\r
\r
+void SSCache::insert(const char* key, time_t expires, const char* name, const char* index)\r
+{\r
+ string dup;\r
+ if (strlen(name) > 255) {\r
+ dup = string(name).substr(0,255);\r
+ name = dup.c_str();\r
+ }\r
+\r
+ DDF obj;\r
+ DDFJanitor jobj(obj);\r
+\r
+ // Since we can't guarantee uniqueness, check for an existing record.\r
+ string record;\r
+ time_t recordexp;\r
+ int ver = m_storage->readText("NameID", name, &record, &recordexp);\r
+ if (ver > 0) {\r
+ // Existing record, so we need to unmarshall it.\r
+ istringstream in(record);\r
+ in >> obj;\r
+ }\r
+ else {\r
+ // New record.\r
+ obj.structure();\r
+ }\r
+\r
+ if (!index || !*index)\r
+ index = "_shibnull";\r
+ DDF sessions = obj.addmember(index);\r
+ if (!sessions.islist())\r
+ sessions.list();\r
+ DDF session = DDF(NULL).string(key);\r
+ sessions.add(session);\r
+\r
+ // Remarshall the record.\r
+ ostringstream out;\r
+ out << obj;\r
+\r
+ // Try and store it back...\r
+ if (ver > 0) {\r
+ ver = m_storage->updateText("NameID", name, out.str().c_str(), max(expires, recordexp), ver);\r
+ if (ver <= 0) {\r
+ // Out of sync, or went missing, so retry.\r
+ return insert(key, expires, name, index);\r
+ }\r
+ }\r
+ else if (!m_storage->createText("NameID", name, out.str().c_str(), expires)) {\r
+ // Hit a dup, so just retry, hopefully hitting the other branch.\r
+ return insert(key, expires, name, index);\r
+ }\r
+}\r
+\r
string SSCache::insert(\r
time_t expires,\r
const Application& application,\r
const char* client_addr,\r
const saml2md::EntityDescriptor* issuer,\r
+ const XMLCh* protocol,\r
const saml2::NameID* nameid,\r
- const char* authn_instant,\r
- const char* session_index,\r
- const char* authncontext_class,\r
- const char* authncontext_decl,\r
+ const XMLCh* authn_instant,\r
+ const XMLCh* session_index,\r
+ const XMLCh* authncontext_class,\r
+ const XMLCh* authncontext_decl,\r
const vector<const Assertion*>* tokens,\r
const multimap<string,Attribute*>* attributes\r
)\r
DDF obj = DDF(key.get()).structure();\r
obj.addmember("version").integer(1);\r
obj.addmember("application_id").string(application.getId());\r
- if (expires > 0) {\r
- // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps. \r
+\r
+ // On 64-bit Windows, time_t doesn't fit in a long, so I'm using ISO timestamps. \r
#ifndef HAVE_GMTIME_R\r
- struct tm* ptime=gmtime(&expires);\r
+ struct tm* ptime=gmtime(&expires);\r
#else\r
- struct tm res;\r
- struct tm* ptime=gmtime_r(&expires,&res);\r
+ struct tm res;\r
+ struct tm* ptime=gmtime_r(&expires,&res);\r
#endif\r
- char timebuf[32];\r
- strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);\r
- obj.addmember("expires").string(timebuf);\r
- }\r
+ char timebuf[32];\r
+ strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);\r
+ obj.addmember("expires").string(timebuf);\r
+\r
if (client_addr)\r
obj.addmember("client_addr").string(client_addr);\r
if (issuer) {\r
auto_ptr_char entity_id(issuer->getEntityID());\r
obj.addmember("entity_id").string(entity_id.get());\r
}\r
- if (authn_instant)\r
- obj.addmember("authn_instant").string(authn_instant);\r
+ if (protocol) {\r
+ auto_ptr_char prot(protocol);\r
+ obj.addmember("protocol").string(prot.get());\r
+ }\r
+ if (authn_instant) {\r
+ auto_ptr_char instant(authn_instant);\r
+ obj.addmember("authn_instant").string(instant.get());\r
+ }\r
+ auto_ptr_char index(session_index);\r
if (session_index)\r
- obj.addmember("session_index").string(session_index);\r
- if (authncontext_class)\r
- obj.addmember("authncontext_class").string(authncontext_class);\r
- if (authncontext_decl)\r
- obj.addmember("authncontext_decl").string(authncontext_decl);\r
+ obj.addmember("session_index").string(index.get());\r
+ if (authncontext_class) {\r
+ auto_ptr_char ac(authncontext_class);\r
+ obj.addmember("authncontext_class").string(ac.get());\r
+ }\r
+ if (authncontext_decl) {\r
+ auto_ptr_char ad(authncontext_decl);\r
+ obj.addmember("authncontext_decl").string(ad.get());\r
+ }\r
\r
if (nameid) {\r
ostringstream namestr;\r
\r
m_log.debug("storing new session...");\r
time_t now = time(NULL);\r
- m_storage->createText(key.get(), "session", record.str().c_str(), now + m_cacheTimeout);\r
+ if (!m_storage->createText(key.get(), "session", record.str().c_str(), now + m_cacheTimeout))\r
+ throw FatalProfileException("Attempted to create a session with a duplicate key.");\r
+ \r
+ // Store the reverse mapping for logout.\r
+ auto_ptr_char name(nameid ? nameid->getName() : NULL);\r
+ try {\r
+ if (name.get())\r
+ insert(key.get(), expires, name.get(), index.get());\r
+ }\r
+ catch (exception& ex) {\r
+ m_log.error("error storing back mapping of NameID for logout: %s", ex.what());\r
+ }\r
+\r
if (tokens) {\r
try {\r
for (vector<const Assertion*>::const_iterator t = tokens->begin(); t!=tokens->end(); ++t) {\r
ostringstream tokenstr;\r
tokenstr << *(*t);\r
auto_ptr_char tokenid((*t)->getID());\r
- m_storage->createText(key.get(), tokenid.get(), tokenstr.str().c_str(), now + m_cacheTimeout);\r
+ if (!m_storage->createText(key.get(), tokenid.get(), tokenstr.str().c_str(), now + m_cacheTimeout))\r
+ throw IOException("duplicate assertion ID ($1)", params(1, tokenid.get()));\r
}\r
}\r
catch (exception& ex) {\r
}\r
\r
const char* pid = obj["entity_id"].string();\r
- m_log.debug("new session created: SessionID (%s) IdP (%s) Address (%s)", key.get(), pid ? pid : "none", client_addr);\r
+ m_log.info("new session created: SessionID (%s) IdP (%s) Address (%s)", key.get(), pid ? pid : "none", client_addr);\r
\r
// Transaction Logging\r
- auto_ptr_char name(nameid ? nameid->getName() : NULL);\r
TransactionLog* xlog = application.getServiceProvider().getTransactionLog();\r
Locker locker(xlog);\r
xlog->log.infoStream() <<\r
for (multimap<string,Attribute*>::const_iterator a=attributes->begin(); a!=attributes->end(); ++a)\r
xlog->log.infoStream() << "\t" << a->second->getId() << " (" << a->second->valueCount() << " values)";\r
xlog->log.info("}");\r
- for_each(attributes->begin(), attributes->end(), cleanup_pair<string,Attribute>());\r
}\r
\r
return key.get();\r
m_log.debug("comparing client address %s against %s", client_addr, obj["client_addr"].string());\r
if (strcmp(obj["client_addr"].string(),client_addr)) {\r
m_log.warn("client address mismatch");\r
- remove(key, application, client_addr);\r
+ remove(key, application);\r
RetryableProfileException ex(\r
"Your IP address ($1) does not match the address recorded at the time the session was established.",\r
params(1,client_addr)\r
\r
if (timeout && *timeout > 0 && now - lastAccess >= *timeout) {\r
m_log.info("session timed out (ID: %s)", key);\r
- remove(key, application, client_addr);\r
+ remove(key, application);\r
RetryableProfileException ex("Your session has expired, and you must re-authenticate.");\r
string eid(obj["entity_id"].string());\r
obj.destroy();\r
iso.parseDateTime();\r
if (now > iso.getEpoch()) {\r
m_log.info("session expired (ID: %s)", key);\r
- remove(key, application, client_addr);\r
+ remove(key, application);\r
RetryableProfileException ex("Your session has expired, and you must re-authenticate.");\r
string eid(obj["entity_id"].string());\r
obj.destroy();\r
}\r
}\r
\r
-void SSCache::remove(const char* key, const Application& application, const char* client_addr)\r
+void SSCache::remove(const char* key, const Application& application)\r
{\r
#ifdef _DEBUG\r
xmltooling::NDC ndc("remove");\r
#endif\r
\r
- m_log.debug("removing session (%s)", key);\r
-\r
m_storage->deleteContext(key);\r
+ m_log.info("removed session (%s)", key);\r
\r
TransactionLog* xlog = application.getServiceProvider().getTransactionLog();\r
Locker locker(xlog);\r
xlog->log.info("Destroyed session (applicationId: %s) (ID: %s)", application.getId(), key);\r
}\r
\r
-void SSCache::receive(DDF& in, ostream& out)\r
+void SSCache::remove(\r
+ const saml2md::EntityDescriptor* issuer,\r
+ const saml2::NameID& nameid,\r
+ const char* index,\r
+ const Application& application,\r
+ vector<string>& sessionsKilled\r
+ )\r
{\r
#ifdef _DEBUG\r
- xmltooling::NDC ndc("receive");\r
+ xmltooling::NDC ndc("remove");\r
#endif\r
\r
- if (!strcmp(in.name(),"insert::"REMOTED_SESSION_CACHE"::SessionCache")) {\r
- auto_ptr_char key(SAMLConfig::getConfig().generateIdentifier());\r
- in.name(key.get());\r
+ auto_ptr_char entityID(issuer ? issuer->getEntityID() : NULL);\r
+ auto_ptr_char name(nameid.getName());\r
\r
- DDF tokens = in["tokens"].remove();\r
- DDFJanitor tjan(tokens);\r
- \r
- m_log.debug("storing new session...");\r
- ostringstream record;\r
- record << in;\r
- time_t now = time(NULL);\r
- m_storage->createText(key.get(), "session", record.str().c_str(), now + m_cacheTimeout);\r
- if (tokens.islist()) {\r
- try {\r
- DDF token = tokens.first();\r
- while (token.isstring()) {\r
- m_storage->createText(key.get(), token.name(), token.string(), now + m_cacheTimeout);\r
- token = tokens.next();\r
+ m_log.info(\r
+ "request to logout sessions from (%s) for (%s) for session index (%s)",\r
+ entityID.get() ? entityID.get() : "unknown", name.get(), index ? index : "all"\r
+ );\r
+\r
+ if (strlen(name.get()) > 255)\r
+ const_cast<char*>(name.get())[255] = 0;\r
+\r
+ // Read in potentially matching sessions.\r
+ string record;\r
+ int ver = m_storage->readText("NameID", name.get(), &record);\r
+ if (ver == 0) {\r
+ m_log.debug("no active sessions to remove for supplied issuer and name identifier");\r
+ return;\r
+ }\r
+\r
+ DDF obj;\r
+ DDFJanitor jobj(obj);\r
+ istringstream in(record);\r
+ in >> obj;\r
+\r
+ // The record contains child lists for each known session index.\r
+ DDF key;\r
+ DDF sessions = obj.first();\r
+ while (sessions.islist()) {\r
+ if (!index || !strcmp(sessions.name(), index)) {\r
+ key = sessions.first();\r
+ while (key.isstring()) {\r
+ // Fetch the session for comparison and possible removal.\r
+ Session* session = find(key.string(), application);\r
+ Locker locker(session);\r
+ if (session) {\r
+ // Same issuer?\r
+ if (XMLString::equals(session->getEntityID(), entityID.get())) {\r
+ // Same NameID?\r
+ if (stronglyMatches(issuer->getEntityID(), application.getXMLString("entityID").second, nameid, *session->getNameID())) {\r
+ sessionsKilled.push_back(key.string());\r
+ remove(key.string(), application); // let this throw to detect errors in case the full logout fails?\r
+ key.destroy();\r
+ }\r
+ else {\r
+ m_log.debug("session (%s) contained a non-matching NameID, leaving it alone", key.string());\r
+ }\r
+ }\r
+ else {\r
+ m_log.debug("session (%s) established by different IdP, leaving it alone", key.string());\r
+ }\r
}\r
+ else {\r
+ // Session's gone, so...\r
+ sessionsKilled.push_back(key.string());\r
+ key.destroy();\r
+ }\r
+ key = sessions.next();\r
}\r
- catch (IOException& ex) {\r
- m_log.error("error storing assertion along with session: %s", ex.what());\r
- }\r
+\r
+ // No sessions left for this index?\r
+ if (sessions.first().isnull())\r
+ sessions.destroy();\r
}\r
- const char* pid = in["entity_id"].string();\r
- m_log.debug("new session created: SessionID (%s) IdP (%s) Address (%s)", key.get(), pid ? pid : "none", in["client_addr"].string());\r
+ sessions = obj.next();\r
+ }\r
\r
- DDF ret = DDF(NULL).structure();\r
- DDFJanitor jan(ret);\r
- ret.addmember("key").string(key.get());\r
- out << ret;\r
+ if (obj.first().isnull())\r
+ obj.destroy();\r
+\r
+ // If possible, write back the mapping record (this isn't crucial).\r
+ try {\r
+ if (obj.isnull()) {\r
+ m_storage->deleteText("NameID", name.get());\r
+ }\r
+ else if (!sessionsKilled.empty()) {\r
+ ostringstream out;\r
+ out << obj;\r
+ if (m_storage->updateText("NameID", name.get(), out.str().c_str(), 0, ver) <= 0)\r
+ m_log.warn("logout mapping record changed behind us, leaving it alone");\r
+ }\r
}\r
- else if (!strcmp(in.name(),"find::"REMOTED_SESSION_CACHE"::SessionCache")) {\r
+ catch (exception& ex) {\r
+ m_log.error("error updating logout mapping record: %s", ex.what());\r
+ }\r
+}\r
+\r
+bool SSCache::stronglyMatches(const XMLCh* idp, const XMLCh* sp, const saml2::NameID& n1, const saml2::NameID& n2) const\r
+{\r
+ if (!XMLString::equals(n1.getName(), n2.getName()))\r
+ return false;\r
+ \r
+ const XMLCh* s1 = n1.getFormat();\r
+ const XMLCh* s2 = n2.getFormat();\r
+ if (!s1 || !*s1)\r
+ s1 = saml2::NameID::UNSPECIFIED;\r
+ if (!s2 || !*s2)\r
+ s2 = saml2::NameID::UNSPECIFIED;\r
+ if (!XMLString::equals(s1,s2))\r
+ return false;\r
+ \r
+ s1 = n1.getNameQualifier();\r
+ s2 = n2.getNameQualifier();\r
+ if (!s1 || !*s1)\r
+ s1 = idp;\r
+ if (!s2 || !*s2)\r
+ s2 = idp;\r
+ if (!XMLString::equals(s1,s2))\r
+ return false;\r
+\r
+ s1 = n1.getSPNameQualifier();\r
+ s2 = n2.getSPNameQualifier();\r
+ if (!s1 || !*s1)\r
+ s1 = sp;\r
+ if (!s2 || !*s2)\r
+ s2 = sp;\r
+ if (!XMLString::equals(s1,s2))\r
+ return false;\r
+\r
+ return true;\r
+}\r
+\r
+void SSCache::receive(DDF& in, ostream& out)\r
+{\r
+#ifdef _DEBUG\r
+ xmltooling::NDC ndc("receive");\r
+#endif\r
+\r
+ if (!strcmp(in.name(),"find::"REMOTED_SESSION_CACHE"::SessionCache")) {\r
const char* key=in["key"].string();\r
if (!key)\r
throw ListenerException("Required parameters missing for session removal.");\r
\r
+ const Application* app = SPConfig::getConfig().getServiceProvider()->getApplication(in["application_id"].string());\r
+ if (!app)\r
+ throw ListenerException("Application not found, check configuration?");\r
+\r
// Do an unversioned read.\r
string record;\r
time_t lastAccess;\r
\r
if (timeout > 0 && now - lastAccess >= timeout) {\r
m_log.info("session timed out (ID: %s)", key);\r
- remove(key,*(SPConfig::getConfig().getServiceProvider()->getApplication("default")),NULL);\r
+ remove(key,*app);\r
throw RetryableProfileException("Your session has expired, and you must re-authenticate.");\r
} \r
\r
const char* key=in["key"].string();\r
if (!key)\r
throw ListenerException("Required parameter missing for session removal.");\r
- \r
- remove(key,*(SPConfig::getConfig().getServiceProvider()->getApplication("default")),NULL);\r
+\r
+ const Application* app = SPConfig::getConfig().getServiceProvider()->getApplication(in["application_id"].string());\r
+ if (!app)\r
+ throw ListenerException("Application not found, check configuration?");\r
+\r
+ remove(key,*app);\r
DDF ret(NULL);\r
DDFJanitor jan(ret);\r
out << ret;\r