Detach IPassword from IPasswordFile 82/207382/2
authorsangwan.kwon <sangwan.kwon@samsung.com>
Tue, 4 Jun 2019 07:26:06 +0000 (16:26 +0900)
committersangwan.kwon <sangwan.kwon@samsung.com>
Wed, 5 Jun 2019 02:18:04 +0000 (11:18 +0900)
Change-Id: Id1acf0c087a516ee33456b6b48f826277d8ba8be
Signed-off-by: sangwan.kwon <sangwan.kwon@samsung.com>
src/server/plugin/CMakeLists.txt
src/server/plugin/generic-backend/ipassword-file.h
src/server/plugin/generic-backend/ipassword.cpp [new file with mode: 0644]
src/server/plugin/generic-backend/ipassword.h [new file with mode: 0644]
src/server/plugin/sw-backend/password-file.cpp
src/server/plugin/sw-backend/password-file.h
src/server/plugin/tz-backend/password-file.cpp
src/server/plugin/tz-backend/password-file.h
tests/CMakeLists.txt

index 19791a29434280c4b8fb567a4dde7fb7f21134d0..2435b2176375c38bc53ed19f9f53f31de07b529b 100644 (file)
@@ -15,6 +15,7 @@ INCLUDE_DIRECTORIES(
     )
 
 SET(SW_BACKEND_SOURCES
+    generic-backend/ipassword.cpp
     generic-backend/password-file-buffer.cpp
     sw-backend/password-file.cpp
     )
@@ -49,6 +50,8 @@ INCLUDE_DIRECTORIES(
     )
 
 SET(TZ_BACKEND_SOURCES
+    generic-backend/ipassword.cpp
+    generic-backend/password-file-buffer.cpp
     tz-backend/password-file.cpp
     )
 
index 33246cf88c3fdf1d6d1a48e2d7e1e095244d579f..dfd6ebe358bfa2ae9e545d4b5b5e3cab4283a232 100644 (file)
 #ifndef _IPASSWORD_FILE_H_
 #define _IPASSWORD_FILE_H_
 
+#include <generic-backend/ipassword.h>
+
 #include <string>
-#include <memory>
 
 #include <time.h>
 
-#include <limits>
-
 namespace AuthPasswd {
 
-constexpr time_t PASSWORD_INFINITE_EXPIRATION_TIME = std::numeric_limits<time_t>::max();
-
 struct IPasswordFile {
        IPasswordFile() = default;
        virtual ~IPasswordFile() = default;
@@ -42,7 +39,7 @@ struct IPasswordFile {
 
        virtual void setPassword(unsigned int passwdType, const std::string &password) = 0;
        virtual bool checkPassword(unsigned int passwdType,
-                                                          const std::string &password) const = 0;
+                                                          const std::string &password) = 0;
 
        virtual bool isPasswordActive(unsigned int passwdType) const = 0;
 
diff --git a/src/server/plugin/generic-backend/ipassword.cpp b/src/server/plugin/generic-backend/ipassword.cpp
new file mode 100644 (file)
index 0000000..9bac00c
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ *  Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  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
+ */
+#include <generic-backend/ipassword.h>
+#include <generic-backend/password-file-buffer.h>
+
+#include <error-description.h>
+#include <password-exception.h>
+
+#include <dpl/fstream_accessors.h>
+
+#include <openssl/sha.h>
+
+namespace AuthPasswd {
+
+NoPassword::NoPassword(IStream&)
+{
+}
+
+void NoPassword::Serialize(IStream &stream) const
+{
+       Serialization::Serialize(stream, static_cast<unsigned int>(PasswordType::NONE));
+}
+
+bool NoPassword::match(const std::string &pass) const
+{
+       return pass.empty();
+}
+
+SHA256Password::SHA256Password(IStream &stream)
+{
+       Deserialization::Deserialize(stream, m_hash);
+}
+
+SHA256Password::SHA256Password(const std::string &password) : m_hash(hash(password))
+{
+}
+
+SHA256Password::SHA256Password(const RawHash &paramHash) : m_hash(paramHash)
+{
+}
+
+void SHA256Password::Serialize(IStream &stream) const
+{
+       Serialization::Serialize(stream, static_cast<unsigned int>(PasswordType::SHA256));
+       Serialization::Serialize(stream, m_hash);
+}
+
+bool SHA256Password::match(const std::string &password) const
+{
+       return m_hash == hash(password);
+}
+
+RawHash SHA256Password::hash(const std::string &password)
+{
+       RawHash result(SHA256_DIGEST_LENGTH);
+       SHA256_CTX context;
+       SHA256_Init(&context);
+       SHA256_Update(&context, reinterpret_cast<const unsigned char *>(password.c_str()),
+                                 password.size());
+       SHA256_Final(result.data(), &context);
+       return result;
+}
+
+template <>
+void Deserialization::Deserialize(IStream &stream, IPasswordPtr &ptr)
+{
+       unsigned int algorithm;
+       Deserialization::Deserialize(stream, algorithm);
+
+       switch (algorithm) {
+       case (unsigned int)IPassword::PasswordType::NONE:
+               ptr.reset(new NoPassword());
+               break;
+
+       case (unsigned int)IPassword::PasswordType::SHA256:
+               ptr.reset(new SHA256Password(stream));
+               break;
+
+       default:
+               Throw(PasswordException::FStreamReadError);
+       }
+}
+
+} //namespace AuthPasswd
diff --git a/src/server/plugin/generic-backend/ipassword.h b/src/server/plugin/generic-backend/ipassword.h
new file mode 100644 (file)
index 0000000..21dcd33
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ *  Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  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
+ */
+#ifndef _IPASSWORD_H_
+#define _IPASSWORD_H_
+
+#include <string>
+#include <memory>
+#include <limits>
+
+#include <time.h>
+
+#include <dpl/serialization.h>
+
+namespace AuthPasswd {
+
+constexpr time_t PASSWORD_INFINITE_EXPIRATION_TIME = std::numeric_limits<time_t>::max();
+
+struct IPassword;
+using RawHash = std::vector<unsigned char>;
+using IPasswordPtr = std::shared_ptr<IPassword>;
+using PasswordList = std::list<IPasswordPtr>;
+
+struct IPassword: public ISerializable {
+
+       enum class PasswordType : unsigned int {
+               NONE = 0,
+               SHA256 = 1,
+       };
+
+       virtual bool match(const std::string &password) const = 0;
+};
+
+class NoPassword: public IPassword {
+public:
+       NoPassword() = default;
+       NoPassword(IStream &);
+
+       void Serialize(IStream &stream) const;
+       bool match(const std::string &pass) const;
+};
+
+class SHA256Password: public IPassword {
+public:
+       SHA256Password(IStream &stream);
+       SHA256Password(const std::string &password);
+       SHA256Password(const RawHash &paramHash);
+
+       void Serialize(IStream &stream) const;
+       bool match(const std::string &password) const;
+
+private:
+       static RawHash hash(const std::string &password);
+
+       RawHash m_hash;
+};
+
+template <>
+void Deserialization::Deserialize(IStream &stream, IPasswordPtr &ptr);
+
+} //namespace AuthPasswd
+
+#endif
index 22a30f054d1a6d5c1b5b068ff571ff8e5daa84b8..4e6d1a1e0fe3078448987c6a742af85b4425f594 100644 (file)
@@ -34,8 +34,6 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
-#include <openssl/sha.h>
-
 #include <dpl/log/log.h>
 #include <dpl/fstream_accessors.h>
 
@@ -66,74 +64,6 @@ const unsigned int CURRENT_FILE_VERSION = 4;
 } // namespace anonymous
 
 namespace AuthPasswd {
-
-class NoPassword: public IPassword {
-public:
-       NoPassword(IStream &) {}
-       NoPassword() {}
-
-       void Serialize(IStream &stream) const {
-               Serialization::Serialize(stream, static_cast<unsigned int>(PasswordType::NONE));
-       }
-
-       bool match(const std::string &pass) const {
-               return pass.empty();
-       }
-};
-
-class SHA256Password: public IPassword {
-public:
-       SHA256Password(IStream &stream) {
-               Deserialization::Deserialize(stream, m_hash);
-       }
-
-       SHA256Password(const std::string &password) : m_hash(hash(password)) {}
-
-       SHA256Password(const RawHash &paramHash) : m_hash(paramHash) {}
-
-       void Serialize(IStream &stream) const {
-               Serialization::Serialize(stream, static_cast<unsigned int>(PasswordType::SHA256));
-               Serialization::Serialize(stream, m_hash);
-       }
-
-       bool match(const std::string &password) const {
-               return m_hash == hash(password);
-       }
-
-private:
-       RawHash m_hash;
-
-       static RawHash hash(const std::string &password) {
-               RawHash result(SHA256_DIGEST_LENGTH);
-               SHA256_CTX context;
-               SHA256_Init(&context);
-               SHA256_Update(&context, reinterpret_cast<const unsigned char *>(password.c_str()),
-                                         password.size());
-               SHA256_Final(result.data(), &context);
-               return result;
-       }
-};
-
-template <>
-void Deserialization::Deserialize(IStream &stream, IPasswordPtr &ptr)
-{
-       unsigned int algorithm;
-       Deserialization::Deserialize(stream, algorithm);
-
-       switch (algorithm) {
-       case (unsigned int)IPassword::PasswordType::NONE:
-               ptr.reset(new NoPassword());
-               break;
-
-       case (unsigned int)IPassword::PasswordType::SHA256:
-               ptr.reset(new SHA256Password(stream));
-               break;
-
-       default:
-               Throw(PasswordException::FStreamReadError);
-       }
-}
-
 namespace SWBackend {
 
 PasswordFile::PasswordFile(unsigned int user) :
@@ -479,7 +409,7 @@ void PasswordFile::setPassword(unsigned int passwdType, const std::string &passw
        }
 }
 
-bool PasswordFile::checkPassword(unsigned int passwdType, const std::string &password) const
+bool PasswordFile::checkPassword(unsigned int passwdType, const std::string &password)
 {
        if (passwdType != AUTH_PWD_NORMAL)
                return false;
index 15f496b36fb3aec5b584d43713dd94e7bab72ddc..f954531459023304aa2c7b2f3c5cf62d135395df 100644 (file)
 #include <generic-backend/ipassword-file.h>
 
 namespace AuthPasswd {
-
-struct IPassword: public ISerializable {
-       typedef std::vector<unsigned char> RawHash;
-
-       enum class PasswordType : unsigned int {
-               NONE = 0,
-               SHA256 = 1,
-       };
-
-       virtual bool match(const std::string &password) const = 0;
-};
-
-using IPasswordPtr = std::shared_ptr<IPassword>;
-using PasswordList = std::list<IPasswordPtr>;
-
 namespace SWBackend {
 
 class PasswordFile : public IPasswordFile {
@@ -73,7 +58,7 @@ public:
 
        void setPassword(unsigned int passwdType, const std::string &password) override;
        bool checkPassword(unsigned int passwdType,
-                                          const std::string &password) const override;
+                                          const std::string &password) override;
 
        bool isPasswordActive(unsigned int passwdType) const override;
 
index 9289b66b5eb7df03577f08be3a531eb8a008cbf5..d67e3ac5c568cf599e61d44c5dc021ad8ee64d14 100644 (file)
@@ -52,7 +52,7 @@ void PasswordFile::setPassword(unsigned int, const std::string &)
 }
 
 bool PasswordFile::checkPassword(unsigned int,
-                                                                const std::string &) const
+                                                                const std::string &)
 {
        std::runtime_error("TZ-Backend is not implemented.");
        return false;
index 16cc04bfca447ff77d39d88911e1c5ee91685c42..11bdc0d72851178b27446840c25427eadf134cff 100644 (file)
@@ -37,7 +37,7 @@ public:
 
        void setPassword(unsigned int passwdType, const std::string &password) override;
        bool checkPassword(unsigned int passwdType,
-                                          const std::string &password) const override;
+                                          const std::string &password) override;
 
        void setMaxHistorySize(unsigned int history) override;
        unsigned int getMaxHistorySize() const override;
index 9e401e5b57150ffd341fb2fc83fb0e93dfca95e2..70ab2bdc0726124c1343a4ca2dbb7ef9225db1fc 100644 (file)
@@ -29,6 +29,7 @@ PKG_CHECK_MODULES(${TARGET_TEST}_DEP REQUIRED klay)
 
 INCLUDE_DIRECTORIES(SYSTEM ${${TARGET_TEST}_DEP_INCLUDE_DIRS}
                                                   ${INCLUDE_PATH}
+                                                  ${COMMON_PATH}/include
                                                   ${SERVER_PATH}/service/include
                                                   ${DPL_PATH}/core/include
                                                   ${DPL_PATH}/log/include
@@ -47,7 +48,7 @@ SET_TARGET_PROPERTIES(${TARGET_TEST} PROPERTIES COMPILE_FLAGS "-fPIE")
 SET_TARGET_PROPERTIES(${TARGET_TEST} PROPERTIES LINK_FLAGS "-pie")
 
 INSTALL(TARGETS ${TARGET_TEST}
-               DESTINATION bin
+               DESTINATION ${BIN_DIR}
                PERMISSIONS OWNER_READ
                                        OWNER_WRITE
                                        OWNER_EXECUTE