Add flag checking if password is set. Correct history behaviour
authorLukasz Kostyra <l.kostyra@partner.samsung.com>
Tue, 3 Dec 2013 14:49:11 +0000 (15:49 +0100)
committerBartlomiej Grzelewski <b.grzelewski@samsung.com>
Thu, 6 Feb 2014 16:13:24 +0000 (17:13 +0100)
[Issue#]        N/A
[Bug]           N/A
[Cause]         N/A
[Solution]      N/A
[Verification]  Build, install, run tests. Should pass as they passed earlier.

Change-Id: I1ab45c23564e0592c8f5912c03a96dc4cb146ead

src/server/service/password-file.cpp
src/server/service/password-file.h
src/server/service/password-manager.cpp

index 3146e60..bd7254b 100644 (file)
@@ -69,7 +69,8 @@ namespace SecurityServer
     }
 
     PasswordFile::PasswordFile(): m_maxAttempt(PASSWORD_INFINITE_ATTEMPT_COUNT), m_historySize(0),
-                                  m_expireTime(PASSWORD_INFINITE_EXPIRATION_TIME), m_attempt(0)
+                                  m_expireTime(PASSWORD_INFINITE_EXPIRATION_TIME),
+                                  m_passwordActive(false), m_attempt(0)
     {
         // check if data directory exists
         // if not create it
@@ -158,10 +159,14 @@ namespace SecurityServer
     {
         PasswordFileBuffer pwdBuffer;
 
+        LogError("Saving max_att: " << m_maxAttempt << ", history_size: " << m_historySize <<
+                 ", m_expireTime: " << m_expireTime << ", isActive: " << m_passwordActive);
+
         //serialize password attributes
         Serialization::Serialize(pwdBuffer, m_maxAttempt);
         Serialization::Serialize(pwdBuffer, m_historySize);
         Serialization::Serialize(pwdBuffer, m_expireTime);
+        Serialization::Serialize(pwdBuffer, m_passwordActive);
         Serialization::Serialize(pwdBuffer, m_passwords);
 
         pwdBuffer.Save(DATA_DIR + "/" + PASSWORD_FILE);
@@ -178,7 +183,11 @@ namespace SecurityServer
         Deserialization::Deserialize(pwdFile, m_maxAttempt);
         Deserialization::Deserialize(pwdFile, m_historySize);
         Deserialization::Deserialize(pwdFile, m_expireTime);
+        Deserialization::Deserialize(pwdFile, m_passwordActive);
         Deserialization::Deserialize(pwdFile, m_passwords);
+
+        LogError("Received max_att: " << m_maxAttempt << ", history_size: " << m_historySize <<
+                 ", m_expireTime: " << m_expireTime << ", isActive: " << m_passwordActive);
     }
 
     void PasswordFile::writeAttemptToFile() const
@@ -207,9 +216,14 @@ namespace SecurityServer
         }
     }
 
+    void PasswordFile::activatePassword()
+    {
+        m_passwordActive = true;
+    }
+
     bool PasswordFile::isPasswordActive() const
     {
-        return !(m_passwords.empty());
+        return m_passwordActive;
     }
 
     void PasswordFile::setHistory(unsigned int history)
@@ -258,7 +272,9 @@ namespace SecurityServer
 
         LogSecureDebug("PwdCount: " << m_passwords.size() << ", PwdMaxHistory: " << getHistorySize());
 
-        if(std::find_if(m_passwords.begin(), m_passwords.end(),
+        auto history_beginning = (m_passwords.begin())++;
+
+        if(std::find_if(history_beginning, m_passwords.end(),
                         [&hashedPwd](const Password& pwd) { return (pwd.m_password == hashedPwd); })
                 != m_passwords.end()) {
             LogSecureDebug("Passwords match!");
@@ -331,6 +347,11 @@ namespace SecurityServer
         return (diff.count() < RETRY_TIMEOUT);
     }
 
+    bool PasswordFile::isHistoryActive() const
+    {
+        return (m_historySize != 0);
+    }
+
     //hashPassword is also used in Password struct constructor, that's why it's static. Moreover
     //it is assumed that incorrect input password was checked earlier.
     PasswordFile::RawHash PasswordFile::hashPassword(const std::string &password)
index 0386db9..a122108 100644 (file)
@@ -46,6 +46,8 @@ namespace SecurityServer
 
         void setPassword(const std::string &password);
         bool checkPassword(const std::string &password) const;
+
+        void activatePassword();
         bool isPasswordActive() const;
 
         void setHistory(unsigned int history);
@@ -68,6 +70,8 @@ namespace SecurityServer
         bool checkIfAttemptsExceeded() const;
         bool isIgnorePeriod() const;
 
+        bool isHistoryActive() const;
+
     private:
         typedef std::vector<unsigned char> RawHash;
         typedef std::chrono::duration<double> TimeDiff;
@@ -102,6 +106,7 @@ namespace SecurityServer
         unsigned int m_maxAttempt;
         unsigned int m_historySize;
         time_t m_expireTime;
+        bool m_passwordActive;
 
         //attempt file data
         unsigned int m_attempt;
index 6224bbe..853513e 100644 (file)
@@ -155,7 +155,7 @@ namespace SecurityServer
         //check delivered currentPassword
         //when m_passwordActive flag is true, currentPassword shouldn't be empty
         if (currentPassword.empty() && m_pwdFile.isPasswordActive()) {
-            LogError("Password is already set.");
+            LogError("Password is already set. History count: " << m_pwdFile.getHistorySize());
             return SECURITY_SERVER_API_ERROR_PASSWORD_EXIST;
         }
 
@@ -183,8 +183,8 @@ namespace SecurityServer
             return SECURITY_SERVER_API_ERROR_PASSWORD_EXPIRED;
         }
 
-        //check history
-        if (m_pwdFile.isPasswordActive()) {
+        //check history, however only if history is active
+        if (m_pwdFile.isPasswordActive() && m_pwdFile.isHistoryActive()) {
             if (m_pwdFile.isPasswordReused(newPassword)) {
                 LogError("Password reused.");
                 return SECURITY_SERVER_API_ERROR_PASSWORD_REUSED;
@@ -198,6 +198,7 @@ namespace SecurityServer
 
         //setting password
         m_pwdFile.setPassword(newPassword);
+        m_pwdFile.activatePassword();
         m_pwdFile.setMaxAttempt(receivedAttempts);
         m_pwdFile.setExpireTime(valid_secs);
         m_pwdFile.writeMemoryToFile();
@@ -243,6 +244,7 @@ namespace SecurityServer
             return SECURITY_SERVER_API_ERROR_INPUT_PARAM;
 
         m_pwdFile.setPassword(newPassword);
+        m_pwdFile.activatePassword();
         m_pwdFile.setMaxAttempt(receivedAttempts);
         m_pwdFile.setExpireTime(valid_secs);
         m_pwdFile.writeMemoryToFile();