PASSWORD_QUALITY implementation modification to follow DPM's definition 89/117889/9
authorDongsun Lee <ds73.lee@samsung.com>
Wed, 8 Mar 2017 03:24:08 +0000 (12:24 +0900)
committerDongsun Lee <ds73.lee@samsung.com>
Wed, 8 Mar 2017 09:53:15 +0000 (18:53 +0900)
NUMERIC : at least more than one numeric character should be included.
ALPHABETIC : at least more than one alphabet character should be included.
ALPHANUMERIC : at least more than one alphabet character and more than one numeric character
               should be included.

Change-Id: I17ddcdf3a872155a58ca56bfe6c82811d163ff2b
Signed-off-by: Dongsun Lee <ds73.lee@samsung.com>
src/common/policy.cpp
src/server/service/policy-file.cpp

index 05d2406de80d5320dcebf0283bb1f6fc5439660b..57a2ce46d0c31ff5b4f317390a34ceb958e8f1d8 100644 (file)
@@ -39,9 +39,9 @@ const char *NO_FORBIDDEND_PASSWORD = "";
 
 const std::string REGEX_QUALITY_UNSPECIFIED = "[.]*";
 const std::string REGEX_QUALITY_SOMETHING = ".+";
-const std::string REGEX_QUALITY_NUMERIC = "^[0-9]+$";
-const std::string REGEX_QUALITY_ALPHABETIC = "^[A-Za-z]+$";
-const std::string REGEX_QUALITY_ALPHANUMERIC = "^[A-Za-z0-9]+$";
+const std::string REGEX_QUALITY_NUMERIC = "[0-9]+";
+const std::string REGEX_QUALITY_ALPHABETIC = "[A-Za-z]+";
+const std::string REGEX_QUALITY_ALPHANUMERIC = "(?=.*[A-Za-z]+.*)(?=.*[0-9]+.*)";
 
 Policy::Policy() :
        flag(0),
index cc3bf8a8c43d84c2dcf7d8272902fe91afaabb48..a25343cba47420581c398f914d5b9f8c66acd72a 100644 (file)
@@ -25,7 +25,7 @@
 
 #include <fstream>
 #include <vector>
-#include <regex.h>
+#include <regex>
 
 #include <fcntl.h>
 #include <string.h>
@@ -323,12 +323,19 @@ bool PolicyFile::checkQualityType(const std::string &password) const
                return false;
        }
 
-       regex_t re;
-
-       if (regcomp(&re, pattern.c_str(), REG_EXTENDED | REG_NEWLINE) != 0)
+       try {
+               std::regex rx(pattern);
+               std::smatch match;
+               return std::regex_search(password, match, rx);
+       } catch (const std::regex_error& rerr) {
+               LogError("Fail to check quality due to invalid pattern: QualityType="
+                       << m_policy.qualityType << ", Pattern=" << pattern << ", error=" << rerr.code());
                return false;
-
-       return (regexec(&re, password.c_str(), 0, NULL, 0) == 0);
+       } catch (...) {
+               LogError("Fail to check quality with unknown reason : QualityType="
+                       << m_policy.qualityType << ", Pattern=" << pattern );
+               return false;
+       }
 }
 
 void PolicyFile::setQualityType(unsigned int qualityType)
@@ -342,8 +349,16 @@ bool PolicyFile::isValidPattern(const std::string &pattern) const
        if (pattern.empty())
                return true;
 
-       regex_t re;
-       return (regcomp(&re, pattern.c_str(), REG_EXTENDED | REG_NEWLINE) == 0);
+       try {
+               std::regex rx(pattern);
+               return true;
+       } catch (const std::regex_error& rerr) {
+               LogError("isValidPattern : invalid pattern. Pattern=" << pattern << ", error=" << rerr.code());
+               return false;
+       } catch (...) {
+               LogError("isValidPattern : unknown error. Pattern=" << pattern);
+               return false;
+       }
 }
 
 bool PolicyFile::checkPattern(const std::string &password) const
@@ -351,12 +366,18 @@ bool PolicyFile::checkPattern(const std::string &password) const
        if (m_policy.pattern.empty())
                return true;
 
-       regex_t re;
-
-       if (regcomp(&re, m_policy.pattern.c_str(), REG_EXTENDED | REG_NEWLINE) != 0)
+       try {
+               std::regex rx(m_policy.pattern);
+               std::smatch match;
+               return std::regex_search(password, match, rx);
+       } catch (const std::regex_error& rerr) {
+               LogError("Fail to check Pattern due to invalid pattern: Pattern=" << m_policy.pattern
+                       << ", error=" << rerr.code());
                return false;
-
-       return (regexec(&re, password.c_str(), 0, NULL, 0) == 0);
+       } catch (...) {
+               LogError("Fail to check Pattern with unknown reason: Pattern=" << m_policy.pattern);
+               return false;
+       }
 }
 
 void PolicyFile::setPattern(const std::string &pattern)