From 5dc50875d468093c71c548fab6e286b1d926cde4 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 10 Apr 2013 12:56:06 +0200 Subject: [PATCH] feature #1879: New function to check object names. --- include/PoolObjectSQL.h | 24 ++++++++++++++++++++++ include/User.h | 17 ++++------------ src/pool/PoolObjectSQL.cc | 42 +++++++++++++++++++++++++++++++++++++++ src/um/User.cc | 31 ----------------------------- src/um/UserPool.cc | 2 +- 5 files changed, 71 insertions(+), 45 deletions(-) diff --git a/include/PoolObjectSQL.h b/include/PoolObjectSQL.h index a8e0ccf26f..00a469a2b2 100644 --- a/include/PoolObjectSQL.h +++ b/include/PoolObjectSQL.h @@ -135,6 +135,26 @@ public: return obj_type; }; + /** + * Check if the object name contains invalid characters or exceed the max. + * length. By Default these are not allowed "&|:\;/'#{}()$ + * @param obj_name for this object + * @param extra_chars aditional invalid characters to test + * @param error_str describing the error + * @return true if the name is valid + */ + static bool name_is_valid(const string& obj_name, const string& extra_chars, + string& error_str); + + /** + * Check if the object name is valid, no extra characters needed to be + * tested. + */ + static bool name_is_valid(const string& obj_name, string& error_str) + { + return name_is_valid(obj_name, "", error_str); + } + const string& get_name() const { return name; @@ -634,6 +654,10 @@ protected: Template * obj_template; private: + /** + * Characters that can not be in a name + */ + static const string INVALID_NAME_CHARS; /** * The PoolSQL, friend to easily manipulate its Objects diff --git a/include/User.h b/include/User.h index e108fb0d78..eb4836d739 100644 --- a/include/User.h +++ b/include/User.h @@ -93,15 +93,6 @@ public: invalidate_session(); }; - /** - * Checks if a name is valid, i.e. it is not empty and does not - * contain invalid characters. - * @param uname Name to be checked - * @param error_str Returns the error reason, if any - * @return true if the string is valid - */ - static bool name_is_valid(const string& uname, string& error_str); - /** * Checks if a password is valid, i.e. it is not empty and does not * contain invalid characters. @@ -123,7 +114,7 @@ public: int rc = 0; if (pass_is_valid(passwd, error_str)) - { + { password = passwd; invalidate_session(); } @@ -311,9 +302,9 @@ protected: // Constructor // ************************************************************************* - User(int id, - int _gid, - const string& _uname, + User(int id, + int _gid, + const string& _uname, const string& _gname, const string& _password, const string& _auth_driver, diff --git a/src/pool/PoolObjectSQL.cc b/src/pool/PoolObjectSQL.cc index 23d7188e57..40a76d69e4 100644 --- a/src/pool/PoolObjectSQL.cc +++ b/src/pool/PoolObjectSQL.cc @@ -20,6 +20,8 @@ #include "Nebula.h" #include "Clusterable.h" +const string PoolObjectSQL::INVALID_NAME_CHARS = "&|:\\\";/'#{}()$"; + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -349,3 +351,43 @@ void PoolObjectSQL::set_umask(int umask) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ + +bool PoolObjectSQL::name_is_valid(const string& obj_name, + const string& extra_chars, + string& error_str) +{ + size_t pos; + + if ( obj_name.empty() ) + { + error_str = "Invalid NAME, it cannot be empty"; + return false; + } + + if (extra_chars.empty()) + { + pos = obj_name.find_first_of(INVALID_NAME_CHARS); + } + else + { + string invalid_chars = INVALID_NAME_CHARS + extra_chars; + pos = obj_name.find_first_of(invalid_chars); + } + + if ( pos != string::npos ) + { + ostringstream oss; + oss << "Invalid NAME, char '" << obj_name.at(pos) << "' is not allowed"; + + error_str = oss.str(); + return false; + } + + if ( obj_name.length() > 128 ) + { + error_str = "Invalid NAME, max length is 128 chars"; + return false; + } + + return true; +} diff --git a/src/um/User.cc b/src/um/User.cc index 51857cb0ae..40fe011183 100644 --- a/src/um/User.cc +++ b/src/um/User.cc @@ -254,37 +254,6 @@ int User::split_secret(const string secret, string& user, string& pass) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -bool User::name_is_valid(const string& uname, string& error_str) -{ - if ( uname.empty() ) - { - error_str = "Invalid NAME, it cannot be empty"; - return false; - } - - size_t pos = uname.find_first_of(INVALID_NAME_CHARS); - - if ( pos != string::npos ) - { - ostringstream oss; - oss << "Invalid NAME, character '" << uname.at(pos) << "' is not allowed"; - - error_str = oss.str(); - return false; - } - - if ( uname.length() > 128 ) - { - error_str = "Invalid NAME, max length is 128 chars"; - return false; - } - - return true; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - bool User::pass_is_valid(const string& pass, string& error_str) { if ( pass.empty() ) diff --git a/src/um/UserPool.cc b/src/um/UserPool.cc index 076e0a04d9..ab01115cb4 100644 --- a/src/um/UserPool.cc +++ b/src/um/UserPool.cc @@ -268,7 +268,7 @@ int UserPool::allocate ( goto error_pass; } - if ( !User::name_is_valid(uname, error_str) ) + if (!PoolObjectSQL::name_is_valid(uname,User::INVALID_NAME_CHARS,error_str)) { goto error_name; }