Files
m241Neelima/components/webcli-4.9/target/src/wrn/wm/common/wmUserLib.c
T
2026-07-30 11:19:44 +05:30

382 lines
8.0 KiB
C

/* wmUserLib.c - WindManage user access library */
/*
* Copyright (c) 2002, 2007, 2009, 2010, 2012 Wind River Systems, Inc.
*
* The right to copy, distribute, modify or otherwise make use
* of this software may be licensed only pursuant to the terms
* of an applicable Wind River license agreement.
*/
/*
modification history
--------------------
02e,22nov12,f_f move the userInfo defination to the header file
(REQ: WIND00364534)
01f,17may12,h_l replace NO_STRING with NULL_STRING (WIND00032212)
01e,07may10,m_z eliminate compile warning
01d,15mar10,m_z VxWorks 64bit support (WIND00193649)
01c,09mar09,m_z Eliminate compile warning
02b,09mar09,m_z Eliminate compile warning
(WIND00157339)
02,24feb07,msa Correct the apigen comments so they match the actual routines
and correct any apigen errors that may occur.
01,17may02,rs updated refgen comments
*/
/*
DESCRIPTION:
This module implements multi user access routines.
INCLUDE FILES:
wm.h
wmb.h
*/
#include "wm.h"
#include "wmb.h"
#ifdef __ENABLE_WMB_USERS__
/* passwords should be stored in encrypted form */
#ifndef RCC_ENCRYPT_FUNC
# define RCC_ENCRYPT_FUNC(password, salt) password
#endif
userInfo userBase[MAX_USERS_K] =
{
{DEFAULT_LOGIN_K, DEFAULT_PASSWORD_K, 10},
{"guest", "guest", 0}
};
LOCAL void USER_GetSalt
(
sbyte salt[2]
)
{
sbyte buffer[32];
#ifndef _WRS_CONFIG_LP64
ubyte4 timeNow;
#else
ubyte8 timeNow;
#endif
sbyte4 len;
sbyte4 offset;
sbyte4 seed = 0;
timeNow = wmosTimeGet();
ASCTIME((ubyte4)timeNow, buffer);
len = (sbyte4)STRLEN(buffer) - 1;
offset = (int)(timeNow % len);
while (1)
{
seed = buffer[offset];
if (ISALNUM(seed))
break;
++offset;
offset = offset % len;
}
salt[0] = (sbyte) seed;
offset = seed % len;
while (1)
{
seed = buffer[offset];
if (ISALNUM(seed))
break;
++offset;
offset = offset % len;
}
salt[1] = (sbyte) seed;
}
LOCAL userInfo * USER_GetUser
(
sbyte * pLogin
)
{
sbyte2 index;
userInfo *pUser = userBase;
for (index = 0; index < MAX_USERS_K; index++, pUser++)
{
if (0 == STRCMP(pUser->login, pLogin))
return pUser;
}
return NULL;
}
#if 0
LOCAL userInfo * USER_GetPasswordOwner
(
sbyte * pPassword
)
{
sbyte2 index;
sbyte salt[2];
sbyte * pEncrypted = NULL;
userInfo * pUser = userBase;
USER_GetSalt(salt);
pEncrypted = RCC_ENCRYPT_FUNC(pPassword, salt);
for (index = 0; index < MAX_USERS_K; index++, pUser++)
{
if (NULL_STRING(pUser->password))
continue;
if (0 == STRCMP(pUser->password, pEncrypted))
return pUser;
}
return NULL;
}
#endif
/*-----------------------------------------------------------------------*/
LOCAL userInfo * USER_NewUser
(
void
)
{
sbyte2 index;
userInfo *pUser = userBase;
for (index = 0; index < MAX_USERS_K; index++, pUser++)
{
if (NULL_STRING(pUser->login))
return pUser;
}
return NULL;
}
/**************************************************************************
*
* wmUserValidate - Validate (authenticate?) a WMB user.
*
* This routine validates a user <pLogin>:<pPassword> .
*
* RETURNS: TRUE is <pUser> is a valid WMB user. FALSE if not.
*
*/
BOOL wmUserValidate
(
OS_SOCKET_T sock,
sbyte * pLogin,
sbyte * pPassword,
Access * pAccess
)
{
sbyte * pEncrypted = NULL;
sbyte salt[2];
userInfo * pUser;
#ifdef __USE_PASSWORD_ONLY__
if (NULL != (pUser = USER_GetPasswordOwner(pPassword)))
{
*pAccess = pUser->accessLevel;
return TRUE;
}
else
{
*pAccess = 0;
return FALSE;
}
#endif
if (NULL == (pUser = USER_GetUser(pLogin)))
return FALSE;
*pAccess = pUser->accessLevel;
USER_GetSalt(salt);
pEncrypted = RCC_ENCRYPT_FUNC(pPassword, salt);
return (0 == STRCMP(pUser->password, pEncrypted));
}
/**************************************************************************
*
* wmUserAdd - Add a WMB user.
*
* This routine adds a user <pLogin>:<pPassword>, with <pAccess> level.
*
* RETURNS: OK, or ERROR if the username already exists, the specified
* access level is invalid, or if the user database is full.
*
*/
STATUS wmUserAdd
(
sbyte * pLogin,
sbyte * pAccess
)
{
Access accessLevel;
userInfo * pUser;
/* check for pre-existing account */
if (NULL != (pUser = USER_GetUser(pLogin)))
{
printf("User: %s already exists.", pLogin);
return wmError(ERROR_GENERAL, "User already exists");
}
if (NULL == pAccess)
pAccess = DEFAULT_ACCESS_K;
if (OK != wmConvertStrTypeTo(pAccess, &accessLevel, wmAccessInfoGet()))
return wmError(ERROR_GENERAL_ILLEGAL_VALUE, "invalid access");
if (NULL == (pUser = USER_NewUser()))
return wmError(ERROR_GENERAL_MAXED, "user database is full");
STRCPY(pUser->login, pLogin);
pUser->accessLevel = accessLevel;
return OK;
}
/**************************************************************************
*
* wmUserDelete - Delete a WMB user.
*
* This routine deletes user <pLogin> from the WMB user database.
*
* RETURNS: OK, or ERROR if the username does not exist.
*
*/
STATUS wmUserDelete
(
sbyte * pLogin
)
{
userInfo * pUser;
if (NULL == (pUser = USER_GetUser(pLogin)))
return wmError(ERROR_GENERAL, "invalid user");
MEMSET(pUser->login, 0, sizeof(pUser->login));
MEMSET(pUser->password, 0, sizeof(pUser->password));
pUser->accessLevel = 0;
return OK;
}
/**************************************************************************
*
* wmUserChangePassword - Change a WMB user's password.
*
* This routine changes the password for user <pLogin>.
*
* RETURNS: OK, or ERROR if the username does not exist.
*
*/
STATUS wmUserChangePassword
(
sbyte * pLogin,
sbyte * pPassword
)
{
sbyte seed[2];
userInfo * pUser;
if (NULL == (pUser = USER_GetUser(pLogin)))
return wmError(ERROR_GENERAL, "invalid user");
USER_GetSalt(seed);
STRCPY(pUser->password,RCC_ENCRYPT_FUNC(pPassword, seed));
return OK;
}
/**************************************************************************
*
* wmUserChangeAccess - Change a WMB user's access level.
*
* This routine changes the access level for user <pLogin> to <pAccess>.
*
* RETURNS: OK, or ERROR if the username does not exist.
*
*/
STATUS wmUserChangeAccess
(
sbyte * pLogin,
sbyte * pAccess
)
{
userInfo * pUser = USER_GetUser(pLogin);
Access accessLevel;
STATUS status;
if (NULL == pUser)
return wmError(ERROR_GENERAL_INVALID_USER, "invalid user");
status = wmConvertStrTypeTo(pAccess, &accessLevel, wmAccessInfoGet());
if (OK != status)
return status;
pUser->accessLevel = accessLevel;
return OK;
}
/*-----------------------------------------------------------------------*/
/**************************************************************************
*
* wmUserShowUsers - Shows all WMB users.
*
* This routine lists all WMB users and WMB access level. This is intended for
* use ONLY as a debugging feature.
*
* RETURNS: N/A.
*
*/
void wmUserShowUsers
(
void
)
{
sbyte4 index;
sbyte access[30];
userInfo * pUser;
pUser = userBase;
for (index = 0; index < MAX_USERS_K; index++, pUser++)
{
if (NULL_STRING(pUser->login))
continue;
wmAccessToStr(pUser->accessLevel, access);
printf ("User: %20s Access: %s", pUser->login, access);
printf ("\n");
}
}
#endif /* __ENABLE_WMB_USERS__ */