From: Scott Cantor Date: Mon, 9 Oct 2006 16:29:07 +0000 (+0000) Subject: Moved ReplayCache into xmltooling X-Git-Tag: 1.0-alpha1~168 X-Git-Url: http://www.project-moonshot.org/gitweb/?p=shibboleth%2Fcpp-xmltooling.git;a=commitdiff_plain;h=1c001850947731f8e242bc3d9acbbb146eef6914 Moved ReplayCache into xmltooling --- diff --git a/xmltooling/Makefile.am b/xmltooling/Makefile.am index 38cd95e..8099251 100644 --- a/xmltooling/Makefile.am +++ b/xmltooling/Makefile.am @@ -81,6 +81,7 @@ utilinclude_HEADERS = \ util/DateTime.h \ util/NDC.h \ util/ParserPool.h \ + util/ReplayCache.h \ util/StorageService.h \ util/Threads.h \ util/XMLConstants.h \ @@ -144,6 +145,7 @@ libxmltooling_la_SOURCES = \ util/DateTime.cpp \ util/NDC.cpp \ util/ParserPool.cpp \ + util/ReplayCache.cpp \ util/StorageService.cpp \ util/XMLConstants.cpp \ util/XMLHelper.cpp \ diff --git a/xmltooling/XMLToolingConfig.cpp b/xmltooling/XMLToolingConfig.cpp index 5427e6f..e9bfc5e 100644 --- a/xmltooling/XMLToolingConfig.cpp +++ b/xmltooling/XMLToolingConfig.cpp @@ -30,6 +30,7 @@ #include "signature/CredentialResolver.h" #include "soap/SOAP.h" #include "util/NDC.h" +#include "util/ReplayCache.h" #include "util/StorageService.h" #include "util/XMLConstants.h" #include "validation/Validator.h" @@ -143,6 +144,12 @@ bool XMLToolingInternalConfig::log_config(const char* config) return true; } +void XMLToolingConfig::setReplayCache(ReplayCache* replayCache) +{ + delete m_replayCache; + m_replayCache = replayCache; +} + bool XMLToolingInternalConfig::init() { #ifdef _DEBUG @@ -233,6 +240,9 @@ void XMLToolingInternalConfig::term() KeyResolverManager.deregisterFactories(); #endif + delete m_replayCache; + m_replayCache = NULL; + for (vector::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) { #if defined(WIN32) FARPROC fn=GetProcAddress(static_cast(*i),"xmltooling_extension_term"); diff --git a/xmltooling/XMLToolingConfig.h b/xmltooling/XMLToolingConfig.h index e1ce7f0..737fe31 100644 --- a/xmltooling/XMLToolingConfig.h +++ b/xmltooling/XMLToolingConfig.h @@ -41,6 +41,7 @@ namespace xmlsignature { namespace xmltooling { + class XMLTOOL_API ReplayCache; class XMLTOOL_API StorageService; class XMLTOOL_API TrustEngine; class XMLTOOL_API XSECCryptoX509CRL; @@ -56,7 +57,10 @@ namespace xmltooling { { MAKE_NONCOPYABLE(XMLToolingConfig); protected: - XMLToolingConfig() : clock_skew_secs(180) {} + XMLToolingConfig() : m_replayCache(NULL), clock_skew_secs(180) {} + + /** Global ReplayCache instance. */ + ReplayCache* m_replayCache; public: virtual ~XMLToolingConfig() {} @@ -128,7 +132,25 @@ namespace xmltooling { * @return reference to a validating parser pool. */ virtual ParserPool& getValidatingParser() const=0; - + + /** + * Sets the global ReplayCache instance. + * This method must be externally synchronized with any code that uses the object. + * Any previously set object is destroyed. + * + * @param replayCache new ReplayCache instance to store + */ + void setReplayCache(ReplayCache* replayCache); + + /** + * Returns the global ReplayCache instance. + * + * @return global ReplayCache or NULL + */ + ReplayCache* getReplayCache() const { + return m_replayCache; + } + /** * List of catalog files to load into validating parser pool at initialization time. * Like other path settings, the separator depends on the platform diff --git a/xmltooling/util/ReplayCache.cpp b/xmltooling/util/ReplayCache.cpp new file mode 100644 index 0000000..6ed4ab2 --- /dev/null +++ b/xmltooling/util/ReplayCache.cpp @@ -0,0 +1,47 @@ +/* + * Copyright 2001-2006 Internet2 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * ReplayCache.cpp + * + * Helper class on top of StorageService for detecting message replay. + */ + +#include "internal.h" +#include "util/ReplayCache.h" + +using namespace xmltooling; +using namespace std; + +ReplayCache::ReplayCache(StorageService* storage) : m_storage(storage) +{ + if (!m_storage) + m_storage = XMLToolingConfig::getConfig().StorageServiceManager.newPlugin(MEMORY_STORAGE_SERVICE, NULL); +} + +ReplayCache::~ReplayCache() +{ + delete m_storage; +} + +bool ReplayCache::check(const char* context, const char* s, time_t expires) +{ + // In storage already? + if (m_storage->readString(context, s)) + return false; + m_storage->createText(context, s, "x", expires); + return true; +} diff --git a/xmltooling/util/ReplayCache.h b/xmltooling/util/ReplayCache.h new file mode 100644 index 0000000..94fd22d --- /dev/null +++ b/xmltooling/util/ReplayCache.h @@ -0,0 +1,66 @@ +/* + * Copyright 2001-2006 Internet2 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file xmltooling/util/ReplayCache.h + * + * Helper class on top of StorageService for detecting message replay. + */ + +#ifndef __xmltooling_replay_h__ +#define __xmltooling_replay_h__ + +#include + +namespace xmltooling { + + /** + * Helper class on top of StorageService for detecting message replay. + */ + class XMLTOOL_API ReplayCache + { + MAKE_NONCOPYABLE(ReplayCache); + public: + + /** + * Creates a replay cache on top of a particular StorageService. + * + * @param storage pointer to a StorageService, or NULL to keep cache in memory + */ + ReplayCache(StorageService* storage=NULL); + + virtual ~ReplayCache(); + + /** + * Returns true iff the check value is not found in the cache, and stores it. + * + * @param context a context label to subdivide the cache + * @param s value to check + * @param expires time for disposal of value from cache + */ + virtual bool check(const char* context, const char* s, time_t expires); + + bool check(const char* context, const XMLCh* str, time_t expires) { + auto_ptr_char temp(str); + return check(context, temp.get(), expires); + } + + private: + StorageService* m_storage; + }; +}; + +#endif /* __xmltooling_replay_h__ */