-
Notifications
You must be signed in to change notification settings - Fork 1
/
shell.c
192 lines (181 loc) · 5.14 KB
/
shell.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* @file shell.c
* @brief Implementation of the shell object
* Import part of linenoise(https://github.com/antirez/linenoise)
* @author Eggcar
*/
/**
* MIT License
*
* Copyright (c) 2020 Eggcar(eggcar at qq.com or eggcar.luan at gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "shell.h"
#include "console_codes.h"
#include "ec_api.h"
#include "ecshell_common.h"
#include "ecshell_exec.h"
#include "ecshell_exec_def.h"
#include "exceptions.h"
#include "readline.h"
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
int user_authentication(char *uname, size_t ulen, char *passwd, size_t plen)
{
// Test only...
if (strcmp(uname, passwd) == 0) {
return 0;
}
else {
return -1;
}
}
void display_welcome(ecshell_t *sh)
{
return;
}
ecshell_t *ecshell_new(int32_t i_fd, int32_t o_fd, shell_type_t type, uint32_t timeout)
{
ecshell_t *sh = NULL;
if ((i_fd < 0) || (o_fd < 0)) {
goto exit;
}
sh = sh_malloc(sizeof(ecshell_t));
if (sh == NULL) {
goto exit;
}
memset(sh, 0, sizeof(ecshell_t));
sh->stdin_fd = i_fd;
sh->stdout_fd = o_fd;
sh->echo_mode = 1;
sh->shell_status = e_SHELLSTAT_WaitUserLogin;
sh->shell_prompt[0] = '\0';
sh->cmd_len = 0;
sh->cmd_cursor = 0;
sh->history_head = 0;
sh->history_tail = 0;
sh->history_used = 0;
sh->history_offset = 0;
sh->timeout_ms = timeout;
exit:
return sh;
}
void ecshell_free(ecshell_t *sh)
{
sh_free(sh);
}
int shell_run(ecshell_t *sh)
{
int err;
char outbuff[256];
char *_user_name;
char *_password;
size_t _user_name_len, _password_len;
ecshell_env_t exec_env;
if (sh == NULL) {
err = -EINVAL;
goto exit;
}
if ((sh->stdin_fd < 0) || (sh->stdout_fd < 0)) {
err = -EBADF;
goto exit;
}
for (;;) {
switch (sh->shell_status) {
case e_SHELLSTAT_WaitUserLogin:
sh->prompt_len = sprintf(sh->shell_prompt, "User Login:");
err = linenoiseEdit(sh);
write(sh->stdout_fd, "\r\n", 2);
if (err > 0) {
_user_name_len = sh->cmd_len;
_user_name = sh_malloc(_user_name_len + 1);
memset(_user_name, 0, _user_name_len + 1);
if (_user_name == NULL) {
err = -ENOMEM;
goto exit;
}
strncpy(_user_name, sh->cmd_line, _user_name_len);
_user_name[_user_name_len] = '\0';
sh->shell_status = e_SHELLSTAT_WaitUserAuthen;
sh->echo_mask_mode = 1;
}
break;
case e_SHELLSTAT_WaitUserAuthen:
sh->prompt_len = sprintf(sh->shell_prompt, "Password:");
err = linenoiseEdit(sh);
sh->echo_mask_mode = 0;
write(sh->stdout_fd, "\r\n", 2);
if (err > 0) {
_password_len = sh->cmd_len;
_password = sh_malloc(sh->cmd_len + 1);
memset(_password, 0, _password_len + 1);
if (_password == NULL) {
// Clear memory, prevent information leakage.
memset(_user_name, 0, _user_name_len);
sh_free(_user_name);
err = -ENOMEM;
goto exit;
}
strncpy(_password, sh->cmd_line, _password_len);
// Clear buffered password as soon as possible.
memset(sh->cmd_line, 0, SHELL_LINE_MAXLEN);
// Now we have username and password, check it.
if (user_authentication(_user_name, _user_name_len, _password, _password_len) == 0) {
sh->shell_status = e_SHELLSTAT_NormalCMDLine;
display_welcome(sh);
}
else {
sh->shell_status = e_SHELLSTAT_WaitUserLogin;
}
// Keep user name and free password.
memset(_password, 0, _password_len);
sh_free(_password);
}
else {
memset(_user_name, 0, _user_name_len);
sh_free(_user_name);
sh->shell_status = e_SHELLSTAT_WaitUserLogin;
}
break;
case e_SHELLSTAT_NormalCMDLine:
sh->prompt_len = sprintf(sh->shell_prompt, "%s@ecshell>", _user_name);
err = linenoiseEdit(sh);
write(sh->stdout_fd, "\r\n", 2);
if (err > 0) {
linenoiseHistoryAdd(sh, sh->cmd_line);
memcpy(&exec_env, sh, sizeof(exec_env));
sh->shell_status = e_SHELLSTAT_UserProgramIO;
ecshell_exec_by_line(sh->cmd_line, &exec_env);
sh->shell_status = e_SHELLSTAT_NormalCMDLine;
}
break;
case e_SHELLSTAT_RecvTelnetIAC:
// Not implemented yet. May be moved to my modified linenoise processing method later.
break;
case e_SHELLSTAT_UserProgramIO:
default:
break;
}
}
exit:
return err;
}