|
@@ -20,31 +20,89 @@
|
|
|
#include "shellmatta.h"
|
|
|
#include "shellmatta_auth.h"
|
|
|
#include "shellmatta_history.h"
|
|
|
+#include "shellmatta_utils.h"
|
|
|
+#include "shellmatta_escape.h"
|
|
|
#include <stdint.h>
|
|
|
#include <stddef.h>
|
|
|
#include <string.h>
|
|
|
|
|
|
-static uint32_t checkFct(shellmatta_handle_t handle, char *username, char *password)
|
|
|
+shellmatta_retCode_t inputWrapper(shellmatta_instance_t *inst, char data, bool hide)
|
|
|
+{
|
|
|
+ shellmatta_retCode_t ret = SHELLMATTA_BUSY;
|
|
|
+
|
|
|
+ /** -# handle escape sequences */
|
|
|
+ if(inst->escapeCounter != 0u)
|
|
|
+ {
|
|
|
+ escape_handleSequence(inst, data);
|
|
|
+ }
|
|
|
+ else if (inst->delimiter == data)
|
|
|
+ {
|
|
|
+ ret = SHELLMATTA_OK;
|
|
|
+ }
|
|
|
+ /** -# ignore newline as first character (to be compatible to
|
|
|
+ * terminals sending newline after return */
|
|
|
+ else if((0u == inst->inputCount) && ('\n' == data))
|
|
|
+ {
|
|
|
+ /* do nothing */
|
|
|
+ }
|
|
|
+ /** -# check for backspace */
|
|
|
+ else if( ('\b' == data)
|
|
|
+ || ('\x7f' == data))
|
|
|
+ {
|
|
|
+ utils_removeChars(inst, 1u, true);
|
|
|
+ }
|
|
|
+ /** -# check for start of escape sequence */
|
|
|
+ else if('\x1b' == data)
|
|
|
+ {
|
|
|
+ inst->escapeCounter = 1u;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ utils_insertChars(inst, &data, 1u, hide);
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+static uint32_t getUserIdFromName(shellmatta_handle_t handle, char *username)
|
|
|
{
|
|
|
shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
|
|
|
uint32_t userId = 0u;
|
|
|
uint32_t i;
|
|
|
|
|
|
+ for (i = 0u; i < inst->userListLength; i++)
|
|
|
+ {
|
|
|
+ /** search for user */
|
|
|
+ if (0 == strcmp(inst->userList[i].username, username))
|
|
|
+ {
|
|
|
+ userId = inst->userList[i].userId;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return userId;
|
|
|
+}
|
|
|
+
|
|
|
+void checkFct(shellmatta_handle_t handle, uint32_t userId, char *password)
|
|
|
+{
|
|
|
+ shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
|
|
|
+ uint32_t approvedUserId = 0u;
|
|
|
+ uint32_t i;
|
|
|
+
|
|
|
if (NULL != inst->checkFct)
|
|
|
{
|
|
|
- userId = inst->checkFct(username, password);
|
|
|
+ approvedUserId = inst->checkFct(userId, password);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
for (i = 0u; i < inst->userListLength; i++)
|
|
|
{
|
|
|
/** search for user */
|
|
|
- if (0 == strcmp(inst->userList[i].username, username))
|
|
|
+ if (inst->userList[i].userId == userId)
|
|
|
{
|
|
|
/** check password */
|
|
|
if (0 == strcmp(inst->userList[i].password, password))
|
|
|
{
|
|
|
- userId = inst->userList[i].userId;
|
|
|
+ approvedUserId = inst->userList[i].userId;
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
@@ -52,16 +110,31 @@ static uint32_t checkFct(shellmatta_handle_t handle, char *username, char *passw
|
|
|
}
|
|
|
|
|
|
/** print login result */
|
|
|
- if (0 == userId)
|
|
|
+ if (0 == approvedUserId)
|
|
|
{
|
|
|
- shellmatta_write(handle, "username or password is wrong\n", 30);
|
|
|
+ shellmatta_write(handle, "username or password is wrong\r\n", 31);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- shellmatta_write(handle, "login successful\n", 17);
|
|
|
+ shellmatta_write(handle, "login successful\r\n", 18);
|
|
|
}
|
|
|
|
|
|
- return userId;
|
|
|
+ inst->userId = approvedUserId;
|
|
|
+ if (0 != userId)
|
|
|
+ {
|
|
|
+ for (i = 0u; i < inst->userListLength; i++)
|
|
|
+ {
|
|
|
+ if (inst->userList[i].userId == approvedUserId)
|
|
|
+ {
|
|
|
+ inst->userPointer = &inst->userList[i];
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ inst->userPointer = NULL;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -76,62 +149,95 @@ static shellmatta_retCode_t loginCmdFct(const shellmatta_handle_t handle, const
|
|
|
{
|
|
|
shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
|
|
|
shellmatta_retCode_t ret = SHELLMATTA_OK;
|
|
|
- uint32_t i;
|
|
|
char option;
|
|
|
char *argument;
|
|
|
uint32_t argLen;
|
|
|
char *username = NULL;
|
|
|
char *password = NULL;
|
|
|
|
|
|
- static const shellmatta_opt_long_t options[] =
|
|
|
+ static const shellmatta_opt_long_t options[] =
|
|
|
{
|
|
|
{"username" , 'u', SHELLMATTA_OPT_ARG_REQUIRED},
|
|
|
{"password" , 'p', SHELLMATTA_OPT_ARG_REQUIRED},
|
|
|
{NULL , '\0', SHELLMATTA_OPT_ARG_NONE}
|
|
|
};
|
|
|
|
|
|
- ret = shellmatta_opt_long(handle, options, &option, &argument, &argLen);
|
|
|
- while(SHELLMATTA_OK == ret)
|
|
|
+ switch(inst->loginState)
|
|
|
{
|
|
|
- switch(option)
|
|
|
- {
|
|
|
- case 'u':
|
|
|
- if(NULL != argument)
|
|
|
- {
|
|
|
- username = argument;
|
|
|
- }
|
|
|
- break;
|
|
|
- case 'p':
|
|
|
- if(NULL != argument)
|
|
|
- {
|
|
|
- password = argument;
|
|
|
- }
|
|
|
- break;
|
|
|
- default:
|
|
|
- shellmatta_printf(handle, "Unknown option: %c\r\n", option);
|
|
|
- break;
|
|
|
- }
|
|
|
- ret = shellmatta_opt_long(handle, options, &option, &argument, &argLen);
|
|
|
- }
|
|
|
|
|
|
- if ((NULL != username) || (NULL != password))
|
|
|
- {
|
|
|
- inst->userId = checkFct(handle, username, password);
|
|
|
- if (0 != inst->userId)
|
|
|
- {
|
|
|
- for (i = 0u; i < inst->userListLength; i++)
|
|
|
+ case SHELLMATTA_AUTH_IDLE:
|
|
|
+ ret = shellmatta_opt_long(handle, options, &option, &argument, &argLen);
|
|
|
+ while(SHELLMATTA_OK == ret)
|
|
|
{
|
|
|
- if (0 == strcmp(inst->userList[i].username, username))
|
|
|
+ switch(option)
|
|
|
{
|
|
|
- inst->userPointer = &inst->userList[i];
|
|
|
- break;
|
|
|
+ case 'u':
|
|
|
+ if(NULL != argument)
|
|
|
+ {
|
|
|
+ username = argument;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 'p':
|
|
|
+ if(NULL != argument)
|
|
|
+ {
|
|
|
+ password = argument;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ shellmatta_printf(handle, "Unknown option: %c\r\n", option);
|
|
|
+ break;
|
|
|
}
|
|
|
+ ret = shellmatta_opt_long(handle, options, &option, &argument, &argLen);
|
|
|
}
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- inst->userPointer = NULL;
|
|
|
- }
|
|
|
+
|
|
|
+ if ((NULL != username) && (NULL != password))
|
|
|
+ {
|
|
|
+ inst->tmpUserId = getUserIdFromName(handle, username);
|
|
|
+ checkFct(handle, inst->tmpUserId, password);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ inst->inputCount = 0u;
|
|
|
+ inst->cursor = 0u;
|
|
|
+ /** store pointer to username in buffer */
|
|
|
+ shellmatta_write(handle, "enter username:\r\n", 17);
|
|
|
+ inst->loginState = SHELLMATTA_AUTH_USERNAME;
|
|
|
+ return SHELLMATTA_CONTINUE;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case SHELLMATTA_AUTH_USERNAME:
|
|
|
+ (void)shellmatta_read(handle, &argument, &argLen);
|
|
|
+ if ((1 == argLen) && (SHELLMATTA_OK == inputWrapper(inst, argument[0], false)))
|
|
|
+ {
|
|
|
+ /** store user id */
|
|
|
+ inst->buffer[inst->inputCount] = '\0';
|
|
|
+ inst->tmpUserId = getUserIdFromName(handle, inst->buffer);
|
|
|
+
|
|
|
+ /** reinitialize input */
|
|
|
+ inst->inputCount = 0u;
|
|
|
+ inst->cursor = 0u;
|
|
|
+
|
|
|
+ shellmatta_write(handle, "\r\nenter password:\r\n", 19);
|
|
|
+ inst->loginState = SHELLMATTA_AUTH_PASSWORD;
|
|
|
+ }
|
|
|
+ ret = SHELLMATTA_CONTINUE;
|
|
|
+ break;
|
|
|
+ case SHELLMATTA_AUTH_PASSWORD:
|
|
|
+ (void)shellmatta_read(handle, &argument, &argLen);
|
|
|
+ if ((1 == argLen) && (SHELLMATTA_OK == inputWrapper(inst, argument[0], true)))
|
|
|
+ {
|
|
|
+ inst->buffer[inst->inputCount] = '\0';
|
|
|
+ shellmatta_write(handle, "\r\n", 2);
|
|
|
+ checkFct(handle, inst->tmpUserId, inst->buffer);
|
|
|
+ inst->loginState = SHELLMATTA_AUTH_IDLE;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ ret = SHELLMATTA_CONTINUE;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
}
|
|
|
|
|
|
(void)arguments;
|
|
@@ -166,7 +272,7 @@ static shellmatta_retCode_t logoutCmdFct(const shellmatta_handle_t handle, const
|
|
|
/** reset history buffer */
|
|
|
history_reset(inst);
|
|
|
|
|
|
- shellmatta_write(handle, "good bye\n", 9);
|
|
|
+ shellmatta_write(handle, "good bye\r\n", 10);
|
|
|
|
|
|
(void)arguments;
|
|
|
(void)length;
|
|
@@ -195,19 +301,21 @@ const shellmatta_cmd_t shellmatta_auth_logoutCmd = {"logout"
|
|
|
* #SHELLMATTA_USE_FAULT (param err)
|
|
|
*/
|
|
|
shellmatta_retCode_t shellmatta_auth_init(shellmatta_handle_t handle,
|
|
|
- shellmatta_user_t *userList,
|
|
|
+ shellmatta_auth_user_t *userList,
|
|
|
uint32_t userListLength,
|
|
|
- shellmatta_perm_t *permList,
|
|
|
+ shellmatta_auth_perm_t *permList,
|
|
|
uint32_t permListLength,
|
|
|
bool customLogin,
|
|
|
- shellmatta_check_t checkFct)
|
|
|
+ shellmatta_auth_check_t checkFct)
|
|
|
{
|
|
|
shellmatta_retCode_t ret = SHELLMATTA_OK;
|
|
|
shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
|
|
|
uint32_t i;
|
|
|
shellmatta_cmd_t *cmd;
|
|
|
|
|
|
+ inst->loginState = SHELLMATTA_AUTH_IDLE;
|
|
|
inst->userId = 0u;
|
|
|
+ inst->tmpUserId = 0u;
|
|
|
inst->userPointer = 0u;
|
|
|
inst->userList = userList;
|
|
|
inst->userListLength = userListLength;
|