mirror of
https://github.com/NevermindZZT/letter-shell.git
synced 2025-01-01 09:58:41 +08:00
新增 安全用户组件
This commit is contained in:
parent
dc0e4915fd
commit
c823f6403d
@ -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
|
||||
|
@ -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);
|
@ -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`组件,我们可以定义一个函数,使用这个函数得到用户的密码,一般的,我们可以根据芯片的ID,mac地址,甚至可以向服务器做请求以获取密码,提高安全性
|
||||
|
||||
- 定义获取用户密码函数(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命令行输入用户名和密码即可
|
||||
|
24
extensions/shell_enhance/shell_secure_user.c
Normal file
24
extensions/shell_enhance/shell_secure_user.c
Normal 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;
|
||||
}
|
58
extensions/shell_enhance/shell_secure_user.h
Normal file
58
extensions/shell_enhance/shell_secure_user.h
Normal 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
|
@ -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 =
|
||||
|
Loading…
x
Reference in New Issue
Block a user