mirror of
https://github.com/OpenNebula/one.git
synced 2024-12-23 17:33:56 +03:00
feature #1879: New function to check object names.
This commit is contained in:
parent
0407963026
commit
5dc50875d4
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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() )
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user