cli_user.c

来自「命令行在嵌入式系统的实现」· C语言 代码 · 共 1,020 行 · 第 1/3 页

C
1,020
字号
/********************************************************************/
_U32 CLI_CreatUserLink(_VOID)
{
    _U32 i;
    /* 增加调试用户*/
    CLI_UserInfoCfg(&(m_sUserTable[0]),
                    g_csz_CFG_CLI_DBG_USER_NAME, g_csz_CFG_CLI_DBG_USER_PASSWORD,
                    CT_AL_DEBUG, USER_FOR_CLI, ML_CHS);

    /* 增加超级用户*/
    CLI_UserInfoCfg(&(m_sUserTable[1]),
                    g_csz_CFG_CLI_SUPER_USER_NAME, g_csz_CFG_CLI_SUPER_USER_PASSWORD,
                    CT_AL_SUPER, USER_FOR_CLI, ML_ENG);

    /* 增加普通管理员用户*/
    CLI_UserInfoCfg(&(m_sUserTable[2]),
                    g_csz_CFG_CLI_ADMIN_USER_NAME, g_csz_CFG_CLI_ADMIN_USER_PASSWORD,
                    CT_AL_ADMIN, USER_FOR_CLI, ML_ENG);

    for (i = 3; i< MAX_USER_NUM; i++)
    {
        CLI_UserInfoCfg(&m_sUserTable[i],
                        "", "", 0, USER_NOT_USED,  ML_GetDefaultLang());
    }

    return G_SUCCESS ;
}

/********************************************************************/
/*      函数名称    :CLI_AddTermUser                                */
/*      函数功能    :增加一个用户                                   */
/*      输入参数    :_S8 *szUserName 用户名                          */
/*                   _S8 *szPassword 用户口令                        */
/*                   _U32 ulLevel    用户权限                        */
/*      输出参数    :无                                             */
/*      返回值      :成功、失败                                     */
/*      调用函数    :                                               */
/*      被调函数    :该函数只能被命令行调用                         */
/********************************************************************/
_U32 CLI_AddTermUser(const _S8 *szUserName, const _S8 *szPassword, _U8 ucLevel, _U8 ucUserType, _U8 ucLan)
{
    _U32 i;
    PTerm_Data_S pTermStruct;
    _U8 ucCurLevel;

    if (EOS_StrLen(szUserName) >= USERNAME_LEN
     || EOS_StrLen(szPassword) >= PASSWORD_LEN)
    {
        MT_ERRLOG(0);
        return G_FAILURE;
    }

    /* 语种ID的合法性检查 */
    if( ucLan >= ML_GetLangNum() )
    {
        MT_ERRLOG(0);
        return G_FAILURE;
    }

    if((pTermStruct = CLI_GetCurrentTaskData()) == G_NULL)
    {
        MT_ERRLOG(0);
        return G_FAILURE;
    }

    ucCurLevel = pTermStruct->ucUserLevel;

    if (ucCurLevel < ucLevel)
    {
        IO_Print(CLI_USER_HAS_NO_RIGHT);
        return G_FAILURE;
    }

    for (i = 0; i < MAX_USER_NUM; i++)
    {
        if (!EOS_StriCmp(m_sUserTable[i].szUserName, szUserName)
         && m_sUserTable[i].ucUserType != USER_NOT_USED)
        {
            IO_Print(CLI_USER_HAS_EXISTED);
            return G_FAILURE;
        }
    }
    /*调试用户为固定用户,不能被替换*/
    for (i = 1; i < MAX_USER_NUM; i++)
    {
        if (m_sUserTable[i].ucUserType == USER_NOT_USED)
            break;
    }
    if (i == MAX_USER_NUM)
    {
        IO_Print(CLI_USER_TABLE_FULL);
        return G_FAILURE;
    }
    CLI_UserInfoCfg(&m_sUserTable[i],
             szUserName, szPassword, ucLevel, ucUserType, ucLan);

    #if CLI_DBASE_SUPPORT
    /* 是否包含RDB模块 */
    if( G_YES == g_ul_INCLUDE_MODULE_CLI_RDB )
    {
        /* 函数指针是否注册 */
        if( G_NULL == g_pfn_CFG_CLI_RDB_RecordDirectInsert )
        {
            MT_ERRLOG(0);
            return G_FAILURE;
        }

        if (g_pfn_CFG_CLI_RDB_RecordDirectInsert(m_ulCliUserHandle, m_usUserTable, i, &m_sUserTable[i])!= G_SUCCESS)
        {
            m_sUserTable[i].ucUserType = USER_NOT_USED;
            return G_FAILURE;
        }
    }
    #endif

    return G_SUCCESS ;

}

/********************************************************************/
/*      函数名称    :CLI_DelTermUser                                */
/*      函数功能    :删除一个用户                                   */
/*      输入参数    :_S8 *szUserName 删除的用户名                    */
/*      输出参数    :无                                             */
/*      返回值      :成功、失败                                     */
/*      调用函数    :                                               */
/*      被调函数    :                                               */
/********************************************************************/
_U32 CLI_DelTermUser(const _S8 *szUserName)
{
    _U32 i;
    _U8 ucTmp;
    PTerm_Data_S pTermStruct;
    _U8 ucLevel;

    if((pTermStruct = CLI_GetCurrentTaskData()) == G_NULL)
    {
        MT_ERRLOG(0);
        return G_FAILURE;
    }
    ucLevel = pTermStruct->ucUserLevel;

    for (i = 0; i < MAX_USER_NUM; i++)
    {
        if (!EOS_StriCmp(m_sUserTable[i].szUserName, szUserName)
         && m_sUserTable[i].ucUserType != USER_NOT_USED)
        {
            if (m_sUserTable[i].ulLevel >= CT_AL_SUPER
             || ucLevel < m_sUserTable[i].ulLevel)
            {
                IO_Print(CLI_USER_HAS_NO_RIGHT);
                return G_FAILURE;
            }
            else if (m_sUserTable[i].ulTermId != 0)
            {
                IO_Print(CLI_USER_DEL_ONLINE);
                return G_FAILURE;
            }
            else
                break;
        }
    }
    if (i == MAX_USER_NUM)
    {
        IO_Print(CLI_USER_NOT_EXISTED);
        return G_FAILURE;
    }
    ucTmp = m_sUserTable[i].ucUserType;
    m_sUserTable[i].ucUserType = USER_NOT_USED;

    #if CLI_DBASE_SUPPORT
    /* 是否包含RDB模块 */
    if( G_YES == g_ul_INCLUDE_MODULE_CLI_RDB )
    {
        /* 函数指针是否注册 */
        if( G_NULL == g_pfn_CFG_CLI_RDB_RecordDirectDelete )
        {
            MT_ERRLOG(0);
            return G_FAILURE;
        }

        if (g_pfn_CFG_CLI_RDB_RecordDirectDelete( m_ulCliUserHandle, m_usUserTable, i) != G_SUCCESS)
        {
            m_sUserTable[i].ucUserType = ucTmp;
            MT_ERRLOG(0);
            return G_FAILURE;
        }
    }
    #endif

    return G_SUCCESS;
}

/********************************************************************/
/*      函数名称    :CLI_LoadTermUser                               */
/*      函数功能    :从flash中装载所有的用户信息到用户数据表        */
/*      输入参数    :无                                             */
/*      输出参数    :无                                             */
/*      返回值      :成功、失败                                     */
/*      调用函数    :                                               */
/*      被调函数    :                                               */
/********************************************************************/
_U32 CLI_LoadTermUser(_VOID)
{
    _U8 ucType;
    _U32 i;
    /*将用户数据读到用户信息表*/
    #if CLI_DBASE_SUPPORT
    /* 是否包含RDB模块 */
    if( G_YES == g_ul_INCLUDE_MODULE_CLI_RDB )
    {
        if (G_SUCCESS != CLI_UserRead())
        {
            CLI_CreatUserLink();
            CLI_UserWrite();
        }
    }
    else
    {
        CLI_CreatUserLink();
    }
    #else
        CLI_CreatUserLink();
    #endif

    for (i = 0; i < MAX_USER_NUM; i++)
    {
        ucType = m_sUserTable[i].ucUserType;
        if (ucType > USER_NOT_USED)
        {
            return G_FAILURE;
        }
    }
    return G_SUCCESS;
}

/********************************************************************/
/*      函数名称    :CLI_GetUserByName                              */
/*      函数功能    :取得某一用户的信息                             */
/*      输入参数    :_S8 *szUserName 用户名                         */
/*      输出参数    :无                                             */
/*      返回值      :PTermUserItem: 取得的用户信息指针              */
/*      调用函数    :                                               */
/*      被调函数    :                                               */
/********************************************************************/
PTermUserItem CLI_GetUserByName(const _S8 *szUserName)
{
    _U32 i;

    CLI_ASSURE_OR_NULL( szUserName != G_NULL );

    for (i = 0; i < MAX_USER_NUM; i++)
    {
        if (!EOS_StriCmp(m_sUserTable[i].szUserName, szUserName)
         && m_sUserTable[i].ucUserType != USER_NOT_USED)
        {
            return &m_sUserTable[i];
        }
    }
    return G_NULL ;
}

/********************************************************************/
/*      函数名称    :CLI_GetPasswordByName                          */
/*      函数功能    :取得某一用户的口令                             */
/*      输入参数    :_S8 *szUserName 用户名                          */
/*      输出参数    :_S8 *szPassword用户口令                         */
/*      返回值      :成功、失败                                     */
/*      调用函数    :                                               */
/*      被调函数    :                                               */
/********************************************************************/
_U32 CLI_GetPasswordByName(_S8 *szUserName, _S8 *szPassword)
{
    PTermUserItem pTermUserItem ;

    CLI_ASSURE_OR_FAIL(( szUserName != G_NULL ) && ( szPassword != G_NULL ));

    pTermUserItem = CLI_GetUserByName(szUserName) ;
    if (G_NULL == pTermUserItem)
        return G_FAILURE ;
    EOS_MemCopy(szPassword, pTermUserItem->szPassword, PASSWORD_LEN) ;
    return G_SUCCESS ;
}
/********************************************************************/
/*      函数名称    :CLI_CheckUserPassword                          */
/*      函数功能    :取得某一用户的口令                             */
/*      输入参数    :_S8 *szUserName 用户名                         */
/*      输出参数    :_S8 *szPassword用户口令                        */
/*      返回值      :成功、失败                                     */
/*      调用函数    :                                               */
/*      被调函数    :                                               */
/********************************************************************/
_U32 CLI_CheckUserPassword(const _S8 *szUserName, const _S8 *szPassword)
{
    PTermUserItem pTermUserItem ;
    _S8 szPswd[PASSWORD_LEN];

    CLI_ASSURE_OR_FAIL(( szUserName != G_NULL ) && ( szPassword != G_NULL ));

    pTermUserItem = CLI_GetUserByName(szUserName) ;
    if (G_NULL == pTermUserItem)
        return G_FAILURE ;
    CLI_Encrypt(szPswd, szPassword, 0);
    if (EOS_MemCmp(pTermUserItem->szPassword, szPswd, PASSWORD_LEN))
    {
        return G_FAILURE;
    }

    return G_SUCCESS ;
}

/********************************************************************/
/*      函数名称    :CLI_GetUserLevel                               */
/*      函数功能    :取得某一用户的权限                             */
/*      输入参数    :_S8 *szUserName 用户名                         */
/*                   _U32 *ulLevel用户权限                          */
/*      输出参数    :_U32 *ulLevel用户权限                          */
/*      返回值      :成功、失败                                     */
/*      调用函数    :                                               */
/*      被调函数    :                                               */
/********************************************************************/
_U32 CLI_GetUserLevel(const _S8 *szUserName, _U32 *ulLevel)
{
    PTermUserItem pTermUserItem ;

    CLI_ASSURE_OR_FAIL(( szUserName != G_NULL ) && ( ulLevel != G_NULL ));

    pTermUserItem = CLI_GetUserByName(szUserName) ;
    if (G_NULL == pTermUserItem)
        return G_FAILURE ;

    *ulLevel = pTermUserItem->ulLevel ;
    return G_SUCCESS ;
}

/********************************************************************/
/*      函数名称    :CLI_SetUserName                                */
/*      函数功能    :设定某一用户的口令                             */
/*      输入参数    :ulIndex: 用户数据索引                          */
/*                   _S8 *szUserName 用户名                         */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?