Add new constants to password service. Change infinite expiration time.
authorLukasz Kostyra <l.kostyra@partner.samsung.com>
Mon, 2 Dec 2013 14:16:11 +0000 (15:16 +0100)
committerBartlomiej Grzelewski <b.grzelewski@samsung.com>
Thu, 6 Feb 2014 16:13:24 +0000 (17:13 +0100)
[Issue#]        SSDWSSP-700
[Feature/Bug]   Add PASSWORD_INFINITE_EXPIRATION_TIME and PASSWORD_INFINITE_ATTEMPT_COUNT constant
                to password service. Change infinite expiration time.
[Cause]         Infinite expiration time and infinite attempt count were inputed directly to
                variables in multiple places. Other infinite expiration time
[Solution]      Constants were added for simplier switching between different infinite expiration
                times and infinite attempt  counts.
[Verification]  Build, install, run tests. All should pass, except tests that treated infinite
                expiration time as 0 value.

Change-Id: I49877154e97f57ac444dbc37924bb571f4fa3abe

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

index 5c219ac..4f03c9f 100644 (file)
@@ -58,8 +58,11 @@ const size_t COOKIE_SIZE = 20;
 
 const size_t MAX_PASSWORD_LEN = 32;
 const unsigned int MAX_PASSWORD_HISTORY = 50;
+const unsigned int PASSWORD_INFINITE_EXPIRATION_DAYS = 0;
+const time_t PASSWORD_INFINITE_EXPIRATION_TIME = 0xFFFFFFFF;
+const unsigned int PASSWORD_INFINITE_ATTEMPT_COUNT = 0;
 
-const int SECURITY_SERVER_MAX_OBJ_NAME         = 30;
+const int SECURITY_SERVER_MAX_OBJ_NAME = 30;
 
 } // namespace SecurityServer
 
index b9383ad..b663972 100644 (file)
@@ -26,6 +26,7 @@
 #define _SECURITY_SERVER_PROTOCOLS_
 
 #include <cstddef>
+#include <time.h>
 
 namespace SecurityServer {
 
@@ -76,6 +77,9 @@ enum class PasswordHdrs
 
 extern const size_t MAX_PASSWORD_LEN;
 extern const unsigned int MAX_PASSWORD_HISTORY;
+extern const unsigned int PASSWORD_INFINITE_EXPIRATION_DAYS;
+extern const time_t PASSWORD_INFINITE_EXPIRATION_TIME;
+extern const unsigned int PASSWORD_INFINITE_ATTEMPT_COUNT;
 
 extern const int SECURITY_SERVER_MAX_OBJ_NAME;
 
index 0cef500..e6b8c44 100644 (file)
@@ -34,6 +34,7 @@
 #include <dpl/log/log.h>
 
 #include <security-server.h>
+#include <protocols.h>
 #include <password-exception.h>
 #include <password-file-buffer.h>
 
@@ -67,7 +68,8 @@ namespace SecurityServer
         Serialization::Serialize(stream, m_password);
     }
 
-    PasswordFile::PasswordFile(): m_maxAttempt(0), m_historySize(0),  m_expireTime(0), m_attempt(0)
+    PasswordFile::PasswordFile(): m_maxAttempt(PASSWORD_INFINITE_ATTEMPT_COUNT), m_historySize(0),
+                                  m_expireTime(PASSWORD_INFINITE_EXPIRATION_TIME), m_attempt(0)
     {
         // check if data directory exists
         // if not create it
@@ -302,7 +304,7 @@ namespace SecurityServer
 
     time_t PasswordFile::getExpireTimeLeft() const
     {
-        if(m_expireTime > 0)
+        if(m_expireTime != PASSWORD_INFINITE_EXPIRATION_TIME)
             return (m_expireTime - time(NULL));
         else
             return m_expireTime;
@@ -311,7 +313,12 @@ namespace SecurityServer
     bool PasswordFile::checkExpiration() const
     {
         //return true if expired, else false
-        return ((m_expireTime != 0) && (time(NULL) > m_expireTime));
+        return ((m_expireTime != PASSWORD_INFINITE_EXPIRATION_TIME) && (time(NULL) > m_expireTime));
+    }
+
+    bool PasswordFile::checkIfAttemptsExceeded() const
+    {
+        return ((m_maxAttempt != PASSWORD_INFINITE_ATTEMPT_COUNT) && (m_attempt >= m_maxAttempt));
     }
 
     bool PasswordFile::isIgnorePeriod() const
index 216b9ab..0386db9 100644 (file)
@@ -65,6 +65,7 @@ namespace SecurityServer
         bool isPasswordReused(const std::string &password) const;
 
         bool checkExpiration() const;
+        bool checkIfAttemptsExceeded() const;
         bool isIgnorePeriod() const;
 
     private:
index 23a4b14..be7fc60 100644 (file)
 namespace {
     bool calculateExpiredTime(unsigned int receivedDays, unsigned int &validSecs)
     {
-        validSecs = 0;
+        validSecs = SecurityServer::PASSWORD_INFINITE_EXPIRATION_TIME;
 
-        if(receivedDays == 0)
+        //when receivedDays means infinite expiration, return default validSecs value.
+        if(receivedDays == SecurityServer::PASSWORD_INFINITE_EXPIRATION_DAYS)
             return true;
 
         time_t curTime = time(NULL);
@@ -54,10 +55,6 @@ namespace {
             validSecs = (curTime + (receivedDays * 86400));
             return true;
         }
-
-        //when receivedDays equal to zero, it means infinite password valid time
-        //if receivedDays is 0 return true, else return false (that is, an error)
-        return false;
     }
 } //namespace
 
@@ -104,7 +101,7 @@ namespace SecurityServer
         maxAttempt = m_pwdFile.getMaxAttempt();
         expirationTime = m_pwdFile.getExpireTimeLeft();
 
-        if ((maxAttempt != 0) && (currentAttempt >= maxAttempt)) {
+        if (m_pwdFile.checkIfAttemptsExceeded()) {
             LogError("Too many tries.");
             return SECURITY_SERVER_API_ERROR_PASSWORD_MAX_ATTEMPTS_EXCEEDED;
         }
@@ -163,8 +160,7 @@ namespace SecurityServer
         }
 
         // check attempt
-        unsigned int maxAttempt = m_pwdFile.getMaxAttempt();
-        if ((maxAttempt != 0) && (m_pwdFile.getAttempt() >= maxAttempt)) {
+        if (m_pwdFile.checkIfAttemptsExceeded()) {
             LogError("Too many attempts.");
             return SECURITY_SERVER_API_ERROR_PASSWORD_MAX_ATTEMPTS_EXCEEDED;
         }