From 2e40fd0de07eb5a66c3760f2f0f746912b43eaf0 Mon Sep 17 00:00:00 2001 From: luoliangyi <87842688@qq.com> Date: Mon, 12 Dec 2022 17:05:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E8=B4=A6=E5=8F=B7=E8=A1=A8?= =?UTF-8?q?=E6=9D=A5=E6=B3=A8=E5=86=8C=E8=B4=A6=E5=8F=B7=EF=BC=8C=E8=80=8C?= =?UTF-8?q?=E4=B8=8D=E6=98=AF=E7=9B=B4=E6=8E=A5=E4=BD=BF=E7=94=A8mysql?= =?UTF-8?q?=E7=9A=84=E8=B4=A6=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/HGPdtToolDb/HGPdtToolDbImpl.cpp | 146 ++++++++++++++++------------- db/HGPdtToolDb/HGPdtToolDbImpl.hpp | 3 + 2 files changed, 82 insertions(+), 67 deletions(-) diff --git a/db/HGPdtToolDb/HGPdtToolDbImpl.cpp b/db/HGPdtToolDb/HGPdtToolDbImpl.cpp index 2257135..7851272 100644 --- a/db/HGPdtToolDb/HGPdtToolDbImpl.cpp +++ b/db/HGPdtToolDb/HGPdtToolDbImpl.cpp @@ -117,28 +117,36 @@ HGResult HGPdtToolDbUserMgrImpl::Create(const HGChar* host, HGUShort port, const return HGBASE_ERR_FAIL; } + if (NULL == userName || 0 == *userName || NULL == pwd || 0 == *pwd) + { + return HGBASE_ERR_INVALIDARG; + } + MYSQL* sql = mysql_init(NULL); if (NULL == sql) { return HGBASE_ERR_FAIL; } - if (NULL == mysql_real_connect(sql, host, userName, pwd, NULL, port, NULL, 0)) + if (NULL == mysql_real_connect(sql, host, "huago", "huago", "huago_production_tool", port, NULL, 0)) { + // 连接错误 mysql_close(sql); return HGBASE_ERR_FAIL; } - int ret = mysql_query(sql, "create database if not exists huago_production_tool;"); - if (0 != ret) + std::string password = GetPassword(sql, userName); + if (password.empty()) { + // 用户不存在 mysql_close(sql); return HGBASE_ERR_FAIL; } - ret = mysql_select_db(sql, "huago_production_tool"); - if (0 != ret) + std::string authString = GetAuthString(sql, pwd); + if (password != authString) { + // 密码不正确 mysql_close(sql); return HGBASE_ERR_FAIL; } @@ -229,7 +237,7 @@ HGResult HGPdtToolDbUserMgrImpl::GetUserList(HGChar** userNameList, HGUInt maxLe } char sqlCmd[1024] = {0}; - sprintf(sqlCmd, "select user, host from mysql.user;"); + sprintf(sqlCmd, "select user from user_list;"); int ret = mysql_query(m_sql, sqlCmd); if (0 != ret) { @@ -251,14 +259,11 @@ HGResult HGPdtToolDbUserMgrImpl::GetUserList(HGChar** userNameList, HGUInt maxLe break; } - assert(NULL != row[0] && NULL != row[1]); - if (0 == strcmp(row[1], "%")) - { - userNameList[*count] = (HGChar*)malloc(strlen(row[0]) + 1); - assert(NULL != userNameList[*count]); - strcpy(userNameList[*count], row[0]); - ++(*count); - } + assert(NULL != row && NULL != row[0]); + userNameList[*count] = (HGChar*)malloc(strlen(row[0]) + 1); + assert(NULL != userNameList[*count]); + strcpy(userNameList[*count], row[0]); + ++(*count); } mysql_free_result(sqlRes); @@ -305,20 +310,9 @@ HGResult HGPdtToolDbUserMgrImpl::CreateUser(const HGChar* userName, const HGChar } char sqlCmd[1024] = {0}; - sprintf(sqlCmd, "create user '%s'@'%%' identified by '%s';", userName, defPwd); + sprintf(sqlCmd, "insert into user_list (user, password) values ('%s', password('%s'));", userName, defPwd); int ret = mysql_query(m_sql, sqlCmd); - if (0 != ret) - { - return HGBASE_ERR_FAIL; - } - - sprintf(sqlCmd, "grant create, insert, select, update on huago_production_tool.* to '%s'@'%%';", userName); - ret = mysql_query(m_sql, sqlCmd); - assert(0 == ret); - sprintf(sqlCmd, "grant select on mysql.user to '%s'@'%%';", userName); - ret = mysql_query(m_sql, sqlCmd); - assert(0 == ret); - return HGBASE_ERR_OK; + return (0 == ret) ? HGBASE_ERR_OK : HGBASE_ERR_FAIL; } HGResult HGPdtToolDbUserMgrImpl::DestroyUser(const HGChar* userName) @@ -349,7 +343,7 @@ HGResult HGPdtToolDbUserMgrImpl::DestroyUser(const HGChar* userName) sprintf(sqlCmd, "drop table %s_config;", userName); mysql_query(m_sql, sqlCmd); - sprintf(sqlCmd, "drop user '%s'@'%%';", userName); + sprintf(sqlCmd, "delete from user_list where user = '%s';", userName); int ret = mysql_query(m_sql, sqlCmd); return (0 == ret) ? HGBASE_ERR_OK : HGBASE_ERR_FAIL; } @@ -374,7 +368,7 @@ HGResult HGPdtToolDbUserMgrImpl::SetPassword(const HGChar* userName, const HGCha } char sqlCmd[1024] = { 0 }; - sprintf(sqlCmd, "set password for '%s'@'%%' = password('%s');", userName, pwd); + sprintf(sqlCmd, "update user_list set password = password('%s') where user = '%s';", pwd, userName); int ret = mysql_query(m_sql, sqlCmd); return (0 == ret) ? HGBASE_ERR_OK : HGBASE_ERR_FAIL; } @@ -391,49 +385,19 @@ HGResult HGPdtToolDbUserMgrImpl::ModifyPassword(const HGChar* oldPwd, const HGCh return HGBASE_ERR_INVALIDARG; } - std::string oldAuthStr; - char sqlCmd[1024] = { 0 }; - sprintf(sqlCmd, "select authentication_string from mysql.user where user = '%s' and host = '%%';", m_userName.c_str()); - int ret = mysql_query(m_sql, sqlCmd); - if (0 == ret) - { - MYSQL_RES* sqlRes = mysql_use_result(m_sql); - if (NULL != sqlRes) - { - MYSQL_ROW row = mysql_fetch_row(sqlRes); - assert(NULL != row[0]); - oldAuthStr = row[0]; - mysql_free_result(sqlRes); - } - } + std::string password = GetPassword(m_sql, m_userName.c_str()); + assert(!password.empty()); - if (oldAuthStr.empty()) - { - return HGBASE_ERR_ACCESSDENIED; - } - - std::string oldAuthStr2; - sprintf(sqlCmd, "select password('%s');", oldPwd); - ret = mysql_query(m_sql, sqlCmd); - if (0 == ret) - { - MYSQL_RES* sqlRes = mysql_use_result(m_sql); - if (NULL != sqlRes) - { - MYSQL_ROW row = mysql_fetch_row(sqlRes); - assert(NULL != row[0]); - oldAuthStr2 = row[0]; - mysql_free_result(sqlRes); - } - } - - if (0 != strcmp(oldAuthStr.c_str(), oldAuthStr2.c_str())) + std::string authString = GetAuthString(m_sql, oldPwd); + if (password != authString) { + // 旧密码错误 return HGBASE_ERR_FAIL; } - sprintf(sqlCmd, "set password for '%s'@'%%' = password('%s');", m_userName.c_str(), newPwd); - ret = mysql_query(m_sql, sqlCmd); + char sqlCmd[1024] = {0}; + sprintf(sqlCmd, "update user_list set password = password('%s') where user = '%s';", newPwd, m_userName.c_str()); + int ret = mysql_query(m_sql, sqlCmd); return (0 == ret) ? HGBASE_ERR_OK : HGBASE_ERR_FAIL; } @@ -730,6 +694,54 @@ const HGChar* HGPdtToolDbUserMgrImpl::GetEntryNameCnStr(HGUInt entryName) return NULL; } +std::string HGPdtToolDbUserMgrImpl::GetPassword(MYSQL* sql, const HGChar* userName) +{ + assert(NULL != sql); + assert(NULL != userName && 0 != *userName); + + std::string password; + char sqlCmd[1024] = { 0 }; + sprintf(sqlCmd, "select password from user_list where user = '%s';", userName); + int ret = mysql_query(sql, sqlCmd); + if (0 == ret) + { + MYSQL_RES* sqlRes = mysql_use_result(sql); + if (NULL != sqlRes) + { + MYSQL_ROW row = mysql_fetch_row(sqlRes); + assert(NULL != row && NULL != row[0]); + password = row[0]; + mysql_free_result(sqlRes); + } + } + + return password; +} + +std::string HGPdtToolDbUserMgrImpl::GetAuthString(MYSQL* sql, const HGChar* pwd) +{ + assert(NULL != sql); + assert(NULL != pwd && 0 != *pwd); + + std::string authString; + char sqlCmd[1024] = {0}; + sprintf(sqlCmd, "select password('%s');", pwd); + int ret = mysql_query(sql, sqlCmd); + if (0 == ret) + { + MYSQL_RES* sqlRes = mysql_use_result(sql); + if (NULL != sqlRes) + { + MYSQL_ROW row = mysql_fetch_row(sqlRes); + assert(NULL != row && NULL != row[0]); + authString = row[0]; + mysql_free_result(sqlRes); + } + } + + return authString; +} + int HGPdtToolDbUserMgrImpl::CreateUserConfigTable(MYSQL* sql, const HGChar* user) { assert(NULL != sql); diff --git a/db/HGPdtToolDb/HGPdtToolDbImpl.hpp b/db/HGPdtToolDb/HGPdtToolDbImpl.hpp index ce8bd9d..247d29b 100644 --- a/db/HGPdtToolDb/HGPdtToolDbImpl.hpp +++ b/db/HGPdtToolDb/HGPdtToolDbImpl.hpp @@ -33,6 +33,9 @@ private: void RemoveDevice(class HGPdtToolDbDeviceImpl* deviceImpl); static const HGChar* GetEntryNameCnStr(HGUInt entryName); + static std::string GetPassword(MYSQL* sql, const HGChar* userName); + static std::string GetAuthString(MYSQL* sql, const HGChar* pwd); + int CreateUserConfigTable(MYSQL* sql, const HGChar *user); int CreateMainTestTable(MYSQL *sql); int CreateInitInspTestTable(MYSQL* sql);