1
0
mirror of https://github.com/NevermindZZT/letter-shell.git synced 2025-01-01 09:58:41 +08:00

新增 安全用户组件

This commit is contained in:
Letter 2022-03-12 21:37:49 +08:00
parent dc0e4915fd
commit c823f6403d
6 changed files with 123 additions and 3 deletions

View File

@ -13,6 +13,7 @@ add_executable(LetterShell
../../extensions/telnet/telnetd.c
../../extensions/shell_enhance/shell_passthrough.c
../../extensions/shell_enhance/shell_cmd_group.c
../../extensions/shell_enhance/shell_secure_user.c
../../extensions/game/game.c
../../extensions/game/2048/2048.c
../../extensions/game/pushbox/pushbox.c
@ -20,6 +21,7 @@ add_executable(LetterShell
target_include_directories(LetterShell PUBLIC
"${PROJECT_BINARY_DIR}"
./
../../src
../../extensions/fs_support
../../extensions/cpp_support

View File

@ -12,6 +12,7 @@
#include "shell.h"
#include "shell_fs.h"
#include "shell_passthrough.h"
#include "shell_secure_user.h"
#include "log.h"
#include "telnetd.h"
#include <stdio.h>
@ -259,3 +260,9 @@ int shellRetValChange(int value)
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC),
changeRetVal, shellRetValChange, change shell return vallue);
char *shellSecureUserHandlerTest(const char *name)
{
return (char *)name;
}
SHELL_EXPORT_SECURE_USER(SHELL_CMD_PERMISSION(0xFF), secure, shellSecureUserHandlerTest, secure user test);

View File

@ -10,6 +10,7 @@
- [组件](#组件)
- [shell_cmd_group](#shell_cmd_group)
- [shell_passthrough](#shell_passthrough)
- [shell_secure_user](#shell_secure_user)
## 简介
@ -23,6 +24,7 @@
| ----------------- | -------------- | --------------------------------------- |
| shell_cmd_group | 提供命令组功能 | shell_cmd_group.c shell_cmd_group.h |
| shell_passthrough | 提供透传功能 | shell_passthrough.c shell_passthrough.h |
| shell_secure_user | 安全用户功能 | shell_secure_user.c shell_secure_user.h |
### shell_cmd_group
@ -133,9 +135,36 @@
- 单次调用
某些情况下,使用`passthrough`模式时,我们可能只需要单数据的透传,此时可以不进入`passthrough`命令行,直接调用命令带上透传的数据即可
某些情况下,使用`passthrough`模式时,我们可能只需要单数据的透传,此时可以不进入`passthrough`命令行,直接调用命令带上透传的数据即可
```sh
letter:/mnt/f/Github/letter shell/demo/x86-gcc$ passTest "hello world"
passthrough mode test, data: hello world, len: 11
```
### shell_secure_user
`shell_secure_user`组件是对shell用户的一个补充`letter shell`我们可以通过定义不同的用户和分配命令权限约束使用者可以执行的命令可以将高权限的用户定义密码但是shell用户的默认实现只支持固定的密码在编译时就已经确定安全性不高
使用`shell_secure_user`组件我们可以定义一个函数使用这个函数得到用户的密码一般的我们可以根据芯片的IDmac地址甚至可以向服务器做请求以获取密码提高安全性
- 定义获取用户密码函数(handler)
示例函数直接返回用户名作为密码,实际使用时可以通过任何方式计算或者获取密码,注意,此处返回的密码字符串,请自行分配内存保存
```c
char *shellSecureUserHandlerTest(const char *name)
{
return (char *)name;
}
```
- 定义用户
```c
SHELL_EXPORT_SECURE_USER(SHELL_CMD_PERMISSION(0xFF), secure, shellSecureUserHandlerTest, secure user test);
```
- 调用
使用`shell_secure_user`定义的用户和shell默认用户调用方法完全一致只需要在shell命令行输入用户名和密码即可

View File

@ -0,0 +1,24 @@
/**
* @file shell_secure_user.c
* @author Letter (nevermindzzt@gmail.com)
* @brief shell secure user
* @version 0.1
* @date 2022-03-12
*
* @copyright Copyright (c) 2022 Letter
*
*/
#include "shell_secure_user.h"
extern void shellSetUser(Shell *shell, const ShellCommand *user);
ShellCommand secureUser;
int shellSecureUser(Shell *shell, const char *name, int attr, ShellSecureUserGetPassword handler)
{
secureUser.attr.value = attr | SHELL_CMD_TYPE(SHELL_TYPE_USER);
secureUser.data.user.name = name;
secureUser.data.user.password = handler(name);
shellSetUser(shell, &secureUser);
return 0;
}

View File

@ -0,0 +1,58 @@
/**
* @file shell_secure_user.h
* @author Letter (nevermindzzt@gmail.com)
* @brief shell secure user
* @version 0.1
* @date 2022-03-12
*
* @copyright Copyright (c) 2022 Letter
*
*/
#ifndef __SHELL_SECURE_USER_H__
#define __SHELL_SECURE_USER_H__
#include "shell.h"
/**
* @brief shell secure user
*
* @param name
*
* @return char*
*/
typedef char* (*ShellSecureUserGetPassword)(const char *name);
/**
* @brief shell secure user
*
*/
#define SHELL_SECURE_USER_FUNC_NAME(_name) agency##_name
/**
* @brief shell secure user
*
* @param _name
* @param _attr
* @param _handler
*/
#define SHELL_SECURE_USER_FUNC(_name, _attr, _handler) \
void SHELL_SECURE_USER_FUNC_NAME(_name)(int p1, int p2) \
{ shellSecureUser(shellGetCurrent(), #_name, _attr, _handler); }
/**
* @brief shell secure user
*
* @param _attr
* @param _name
* @param _handler
* @param _desc
*/
#define SHELL_EXPORT_SECURE_USER(_attr, _name, _handler, _desc) \
SHELL_SECURE_USER_FUNC(_name, _attr, _handler) \
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN)|SHELL_CMD_DISABLE_RETURN, \
_name, SHELL_SECURE_USER_FUNC_NAME(_name), _desc)
int shellSecureUser(Shell *shell, const char *name, int attr, ShellSecureUserGetPassword handler);
#endif

View File

@ -153,7 +153,7 @@ static void shellAdd(Shell *shell);
static void shellWritePrompt(Shell *shell, unsigned char newline);
static void shellWriteReturnValue(Shell *shell, int value);
static int shellShowVar(Shell *shell, ShellCommand *command);
static void shellSetUser(Shell *shell, const ShellCommand *user);
void shellSetUser(Shell *shell, const ShellCommand *user);
ShellCommand* shellSeekCommand(Shell *shell,
const char *cmd,
ShellCommand *base,
@ -1249,7 +1249,7 @@ static void shellCheckPassword(Shell *shell)
* @param shell shell对象
* @param user
*/
static void shellSetUser(Shell *shell, const ShellCommand *user)
void shellSetUser(Shell *shell, const ShellCommand *user)
{
shell->info.user = user;
shell->status.isChecked =