cli_user.c
来自「命令行在嵌入式系统的实现」· C语言 代码 · 共 1,020 行 · 第 1/3 页
C
1,020 行
/************************************************************************
Copyright 200X - 200X+1.
filename : CLI_User.c
description : 本文件实现命令行操作用户的管理,提供用户的增删、修改、保存与查询
author : Woodhead
modification : Woodhead create 2004-12-07
************************************************************************/
#include "Ros.h"
#include "Cli_Private.inc"
#include "Cli_User.inc"
#include "ML.h"
#include "Sys_Ml.h"
#include "Cli_MlPriv.h"
#include "Config\Cli_ConfigGlbVar.h"
#include "cm_sock.h"
#if CLI_DBASE_SUPPORT
#include "Rdb.h"
#endif
/* 终端用户表 (规定第一个表即m_sUserTable[0]用于存储调试用户信息)*/
STermUserItem m_sUserTable[MAX_USER_NUM];
#if CLI_DBASE_SUPPORT
RDB_HANDLE m_ulCliUserHandle = RDB_INVALID_HANDLE;
RDB_TABLE_GID m_usUserTable = RDB_INVALID_TABLE_GID;
RDB_TABLE_GID m_usHostNameTable = RDB_INVALID_TABLE_GID;
RDB_TABLE_GID m_usClientACLTable = RDB_INVALID_TABLE_GID;
#endif /* CLI_DBASE_SUPPORT */
/* 用户在数据库的表创建*/
_U32 CLI_RDB_DataInit(_VOID)
{
#if CLI_DBASE_SUPPORT
_U32 ulRet = G_SUCCESS;
_S8 szTbCreatStr[] =
RDB_DIRECT_TABLE(Cliusers, yes, 16)
"( valid bool 1,"
" usertype u8 1, "
" userstat u8 1, "
" language u8 1, "
" username string 16,"
" password u8 16,"
" pswdtime u32 1, "
" termid u32 1, "
" level u32 1, "
" validflag u8 1, "
" reserved u8 3 )";
_S8 szHostTableStr[] =
RDB_DIRECT_TABLE(CliHostName, yes, 1)
"( valid bool 1, "
" hostname string 35)";
_S8 szACLTableStr[] =
RDB_DIRECT_TABLE(CliTelnetACL, yes, 10)
"( valid bool 1,"
" ucReserv u8 3,"
" ulIpAddr u32 1,"
" ulIpMask u32 1,"
" ulState u32 1)";
/* 是否包含RDB模块 */
if( G_YES != g_ul_INCLUDE_MODULE_CLI_RDB )
{
return G_SUCCESS;
}
/* 函数指针是否注册 */
if( G_NULL == g_pfn_CFG_CLI_RDB_TableCreate )
{
MT_ERRLOG(0);
return G_FAILURE;
}
ulRet = g_pfn_CFG_CLI_RDB_TableCreate((const _S8 *)szTbCreatStr, &m_usUserTable, sizeof(STermUserItem));
ulRet += g_pfn_CFG_CLI_RDB_TableCreate((const _S8 *)szACLTableStr, &m_usClientACLTable, sizeof(ST_ACL));
ulRet += g_pfn_CFG_CLI_RDB_TableCreate((const _S8 *)szHostTableStr, &m_usHostNameTable, sizeof(HostName_S));
return ulRet;
#else
return G_SUCCESS;
#endif
}
/********************************************************************/
/* 函数名称 :CLI_UserWrite */
/* 函数功能 :存储新创建的用户链到数据库 */
/* 输入参数 :无 */
/* 输出参数 :无 */
/* 返回值 :成功、失败 */
/* 调用函数 : */
/* 被调函数 : */
/********************************************************************/
#if CLI_DBASE_SUPPORT
_U32 CLI_UserWrite()
{
_U32 i;
_U32 ulRet = G_SUCCESS;
/* 是否包含RDB模块 */
if( G_YES != g_ul_INCLUDE_MODULE_CLI_RDB )
{
return G_SUCCESS;
}
/* 函数指针是否注册 */
if( ( G_NULL == g_pfn_CFG_CLI_RDB_TableClear )
|| ( G_NULL == g_pfn_CFG_CLI_RDB_RecordDirectInsert )
)
{
MT_ERRLOG(0);
return G_FAILURE;
}
if (g_pfn_CFG_CLI_RDB_TableClear(m_ulCliUserHandle, m_usUserTable) != G_SUCCESS)
{
MT_ERRLOG(0);
return G_FAILURE;
}
for (i = 0; i < MAX_USER_NUM; i++)
{
if (m_sUserTable[i].ucUserType == USER_NOT_USED)
{
continue;
}
if (RDB_SUCCESS != g_pfn_CFG_CLI_RDB_RecordDirectInsert(m_ulCliUserHandle, m_usUserTable, i,
&m_sUserTable[i]))
{
MT_ERRLOG(0);
ulRet = G_FAILURE;
}
}
return ulRet;
}
#endif
/********************************************************************/
/* 函数名称 :CLI_UserRead */
/* 函数功能 :从数据库中读取用户列表 */
/* 输入参数 :无 */
/* 输出参数 :无 */
/* 返回值 :成功、失败 */
/* 调用函数 : */
/* 被调函数 : */
/********************************************************************/
#if CLI_DBASE_SUPPORT
_U32 CLI_UserRead()
{
_U32 i;
/* 是否包含RDB模块 */
if( G_YES != g_ul_INCLUDE_MODULE_CLI_RDB )
{
return G_SUCCESS;
}
/* 函数指针是否注册 */
if( G_NULL == g_pfn_CFG_CLI_RDB_RecordDirectQuery )
{
MT_ERRLOG(0);
return G_FAILURE;
}
for (i = 0; i < MAX_USER_NUM; i++)
{
if (RDB_SUCCESS == g_pfn_CFG_CLI_RDB_RecordDirectQuery(m_ulCliUserHandle, m_usUserTable, i,
G_FALSE, &m_sUserTable[i]))
{
m_sUserTable[i].ucUserStatus = TERM_SLEEPING;
m_sUserTable[i].ulTermId = 0;
}
else if(i < 2) /* 如果两个固定用户恢复失败,则认为整个恢复失败 */
{
return G_FAILURE;
}
}
/* 判断调试用户恢复是否正确,如果调试用户名和密码被改写,则任务数据库有误 */
if (G_SUCCESS != CLI_CheckUserPassword(g_csz_CFG_CLI_DBG_USER_NAME, g_csz_CFG_CLI_DBG_USER_PASSWORD))
{
MT_ERRLOG(0);
return G_FAILURE;
}
return G_SUCCESS;
}
#endif
/********************************************************************/
/* 函数名称 :CLI_UserTableInit */
/* 函数功能 :初始化终端用户表 */
/* 输入参数 :无 */
/* 输出参数 :无 */
/* 返回值 :成功、失败 */
/* 调用函数 : */
/* 被调函数 : */
/********************************************************************/
_U32 CLI_UserTableInit(_VOID)
{
int i;
EOS_MemSet(m_sUserTable, 0, sizeof(m_sUserTable));
for (i = 0; i< MAX_USER_NUM; i++)
{
CLI_UserInfoCfg(&m_sUserTable[i],
"", "", 0, USER_NOT_USED, ML_GetDefaultLang());
}
#if CLI_DBASE_SUPPORT
/* 是否包含RDB模块 */
if( G_YES == g_ul_INCLUDE_MODULE_CLI_RDB )
{
/* 函数指针是否注册 */
if( G_NULL == g_pfn_CFG_CLI_RDB_Open )
{
MT_ERRLOG(0);
return G_FAILURE;
}
if (G_SUCCESS != g_pfn_CFG_CLI_RDB_Open(&m_ulCliUserHandle))
return G_FAILURE;
}
#endif /* CLI_DBASE_SUPPORT */
return G_SUCCESS;
}
/********************************************************************/
/* 函数名称 :CLI_Encrypt */
/* 函数功能 :密码加密 */
/* 输入参数 :szSrc :原密码 */
/* ucType :类型澹(0:加密 1:解密) */
/* 输出参数 :szDst :加密结果 */
/* 返回值 :成功、失败 */
/* 调用函数 : */
/* 被调函数 : */
/********************************************************************/
_U32 CLI_Encrypt(_S8 szDst[PASSWORD_LEN], const _S8 *szSrc, _U8 ucType)
{
_U8 ucMod[2] = {123, 6};
_S32 lStrlen, i;
CLI_ASSURE_OR_FAIL(szSrc != NULL && szDst != NULL);
EOS_MemSet(szDst, 0, PASSWORD_LEN);
if (ucType == 1)
{
ucMod[1] = (_U8)(256 - ucMod[1]);
ucMod[0] = (_U8)(256 - ucMod[0]);
}
if (EOS_StrLen(szSrc) == 0)
{
*szDst = 0;
return G_SUCCESS;
}
lStrlen = (_S32)EOS_StrLen(szSrc) - 1;
for (i = 0; i <= (_S32)(lStrlen - i); i++)
{
if (i == lStrlen - i)
{
*(szDst + i) = (_S8)(*(szSrc + i) + ucMod[i%2]);
}
else
{
*(szDst + i) = (_S8)(*(szSrc + lStrlen - i) + ucMod[i%2]);
*(szDst + lStrlen - i) = (_S8)(*(szSrc + i) + ucMod[i%2]);
}
}
*(szDst + lStrlen + 1) = '\0';
return G_SUCCESS;
}
/*********************************************************************/
/* 函数名称 : CLI_UserInfoCfg() */
/* 函数功能 : 用户数据配置 */
/* 输入参数 : */
/* 输出参数 : */
/* 创建者 : */
/* 修改记录 : */
/*********************************************************************/
_U32 CLI_UserInfoCfg(PTermUserItem pUserItem, const _S8 *szName,
const _S8 *szPassowrd, _U8 ucLevel, _U8 ucUserType, _U8 ucLan)
{
ST_TIME sTime;
/* 语种ID的合法性检查 */
if( ucLan >= ML_GetLangNum() )
{
MT_ERRLOG(0);
return G_FAILURE;
}
if (EOS_GetTime(&sTime) != G_SUCCESS)
{
sTime.ucYear = 0x03;
sTime.ucMonth = 0x05;
sTime.ucDay = 0x01;
}
EOS_StrCpy(pUserItem->szUserName, szName);
if ((CLI_Encrypt(pUserItem->szPassword, szPassowrd, 0)) != G_SUCCESS)
{
return G_FAILURE;
}
_AffirmStrBeLower(pUserItem->szUserName);
pUserItem->ulLevel = (_U32)ucLevel;
pUserItem->ucInvadedFlag = EN_USER_NOT_INVADED;
pUserItem->ucUserType = ucUserType;
pUserItem->ulTermId = 0;
pUserItem->ucUserStatus = TERM_SLEEPING;
pUserItem->ulPwdRenewTime = USER_MAKE_DAY(sTime.ucYear, sTime.ucMonth, sTime.ucDay);
pUserItem->ucLanguage = ucLan;
if (ucUserType != USER_NOT_USED)
pUserItem->ucValid = G_TRUE;
else
pUserItem->ucValid = G_FALSE;
return G_SUCCESS;
}
/********************************************************************/
/* 函数名称 :CLI_CreatBasicUser */
/* 函数功能 :建立基本终端用户 */
/* 输入参数 :无 */
/* 输出参数 :无 */
/* 返回值 :成功、失败 */
/* 调用函数 : */
/* 被调函数 : */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?