user_channel: Rewriting ipmiUserSetUserName API

Rewriting ipmiUserSetUserName API

Tested:
Verified using ipmitool commands.
Command: ipmitool user set name 4 user4
Response:    //Success
Command: ipmitool user set password 4 asdf1234
Response: Set User Password command successful (user 4)
Command: ipmitool user priv 4 0x03 1
Response: Set Privilege Level command successful (user 4)
Command: ipmitool user set name 14 user13asdfghkjlqwert
Response: Username is too long (> 16 bytes)
Command: ipmitool -I lanplus -C 3 -p 623 -U root -P <password> -H
         <BMC-IP> user set name 8 WIJGueNKd
Response:                       //Success
Command: ipmitool user list 1 //User list for channel 1
1   root             false   true       true       ADMINISTRATOR
2   user2            true    false      false      USER
3   user3            true    false      false      NO ACCESS
4   user4            true    false      false      OPERATOR
5   WIJGueNK         true    false      false      NO ACCESS
6   WIJGueNKb        true    false      false      NO ACCESS
7   WIJGueNKc        true    false      false      NO ACCESS
8   WIJGueNKd        true    false      false      NO ACCESS
9                    true    false      false      NO ACCESS
10                   true    false      false      NO ACCESS

Signed-off-by: jayaprakash Mutyala <mutyalax.jayaprakash@intel.com>
Change-Id: I41c091f6d9aaf54326295d1e80e16db521b2e23d
diff --git a/user_channel/user_mgmt.cpp b/user_channel/user_mgmt.cpp
index 14978a1..e1e21c9 100644
--- a/user_channel/user_mgmt.cpp
+++ b/user_channel/user_mgmt.cpp
@@ -554,14 +554,13 @@
     }
 }
 
-bool UserAccess::isValidUserName(const char* userNameInChar)
+bool UserAccess::isValidUserName(const std::string& userName)
 {
-    if (!userNameInChar)
+    if (userName.empty())
     {
-        log<level::ERR>("null ptr");
+        log<level::ERR>("userName is empty");
         return false;
     }
-    std::string userName(userNameInChar, 0, ipmiMaxUserName);
     if (!std::regex_match(userName.c_str(),
                           std::regex("[a-zA-z_][a-zA-Z_0-9]*")))
     {
@@ -964,7 +963,7 @@
     return false;
 }
 
-Cc UserAccess::setUserName(const uint8_t userId, const char* userNameInChar)
+Cc UserAccess::setUserName(const uint8_t userId, const std::string& userName)
 {
     if (!isValidUserId(userId))
     {
@@ -976,15 +975,15 @@
     std::string oldUser;
     getUserName(userId, oldUser);
 
-    std::string newUser(userNameInChar, 0, ipmiMaxUserName);
-    if (oldUser == newUser)
+    if (oldUser == userName)
     {
         // requesting to set the same user name, return success.
         return ccSuccess;
     }
-    bool validUser = isValidUserName(userNameInChar);
+
+    bool validUser = isValidUserName(userName);
     UserInfo* userInfo = getUserInfo(userId);
-    if (newUser.empty() && !oldUser.empty())
+    if (userName.empty() && !oldUser.empty())
     {
         // Delete existing user
         std::string userPath = std::string(userObjBasePath) + "/" + oldUser;
@@ -1004,7 +1003,7 @@
         }
         deleteUserIndex(userId);
     }
-    else if (oldUser.empty() && !newUser.empty() && validUser)
+    else if (oldUser.empty() && !userName.empty() && validUser)
     {
         try
         {
@@ -1016,7 +1015,7 @@
             auto method = bus.new_method_call(
                 getUserServiceName().c_str(), userMgrObjBasePath,
                 userMgrInterface, createUserMethod);
-            method.append(newUser.c_str(), availableGroups, "", false);
+            method.append(userName.c_str(), availableGroups, "", false);
             auto reply = bus.call(method);
         }
         catch (const sdbusplus::exception::SdBusError& e)
@@ -1026,10 +1025,13 @@
                               entry("PATH=%s", userMgrObjBasePath));
             return ccUnspecifiedError;
         }
-        std::memcpy(userInfo->userName, userNameInChar, ipmiMaxUserName);
+
+        std::memset(userInfo->userName, 0, sizeof(userInfo->userName));
+        std::memcpy(userInfo->userName,
+                    static_cast<const void*>(userName.data()), userName.size());
         userInfo->userInSystem = true;
     }
-    else if (oldUser != newUser && validUser)
+    else if (oldUser != userName && validUser)
     {
         try
         {
@@ -1037,7 +1039,7 @@
             auto method = bus.new_method_call(
                 getUserServiceName().c_str(), userMgrObjBasePath,
                 userMgrInterface, renameUserMethod);
-            method.append(oldUser.c_str(), newUser.c_str());
+            method.append(oldUser.c_str(), userName.c_str());
             auto reply = bus.call(method);
         }
         catch (const sdbusplus::exception::SdBusError& e)
@@ -1051,8 +1053,12 @@
                   static_cast<uint8_t*>(userInfo->userName) +
                       sizeof(userInfo->userName),
                   0);
-        std::memcpy(userInfo->userName, userNameInChar, ipmiMaxUserName);
-        ipmiRenameUserEntryPassword(oldUser, newUser);
+
+        std::memset(userInfo->userName, 0, sizeof(userInfo->userName));
+        std::memcpy(userInfo->userName,
+                    static_cast<const void*>(userName.data()), userName.size());
+
+        ipmiRenameUserEntryPassword(oldUser, userName);
         userInfo->userInSystem = true;
     }
     else if (!validUser)