shell_server.h
Go to the documentation of this file.
1 /**
2  * @file shell_server.h
3  * @brief SSH secure shell server
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2019-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneSSH Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 #ifndef _SHELL_SERVER_H
32 #define _SHELL_SERVER_H
33 
34 //Dependencies
35 #include "ssh/ssh_server.h"
36 
37 //Shell server support
38 #ifndef SHELL_SERVER_SUPPORT
39  #define SHELL_SERVER_SUPPORT DISABLED
40 #elif (SHELL_SERVER_SUPPORT != ENABLED && SHELL_SERVER_SUPPORT != DISABLED)
41  #error SHELL_SERVER_SUPPORT parameter is not valid
42 #endif
43 
44 //Stack size required to run the shell server
45 #ifndef SHELL_SERVER_STACK_SIZE
46  #define SHELL_SERVER_STACK_SIZE 650
47 #elif (SHELL_SERVER_STACK_SIZE < 1)
48  #error SHELL_SERVER_STACK_SIZE parameter is not valid
49 #endif
50 
51 //Priority at which the shell server should run
52 #ifndef SHELL_SERVER_PRIORITY
53  #define SHELL_SERVER_PRIORITY OS_TASK_PRIORITY_NORMAL
54 #endif
55 
56 //Maximum number of simultaneous shell sessions
57 #ifndef SHELL_SERVER_MAX_SESSIONS
58  #define SHELL_SERVER_MAX_SESSIONS 10
59 #elif (SHELL_SERVER_MAX_SESSIONS < 1)
60  #error SHELL_SERVER_MAX_SESSIONS parameter is not valid
61 #endif
62 
63 //Shell server tick interval
64 #ifndef SHELL_SERVER_TICK_INTERVAL
65  #define SHELL_SERVER_TICK_INTERVAL 1000
66 #elif (SHELL_SERVER_TICK_INTERVAL < 100)
67  #error SHELL_SERVER_TICK_INTERVAL parameter is not valid
68 #endif
69 
70 //Size of buffer used for input/output operations
71 #ifndef SHELL_SERVER_BUFFER_SIZE
72  #define SHELL_SERVER_BUFFER_SIZE 256
73 #elif (SHELL_SERVER_BUFFER_SIZE < 128)
74  #error SHELL_SERVER_BUFFER_SIZE parameter is not valid
75 #endif
76 
77 //Command history support
78 #ifndef SHELL_SERVER_HISTORY_SUPPORT
79  #define SHELL_SERVER_HISTORY_SUPPORT ENABLED
80 #elif (SHELL_SERVER_HISTORY_SUPPORT != ENABLED && SHELL_SERVER_HISTORY_SUPPORT != DISABLED)
81  #error SHELL_SERVER_HISTORY_SUPPORT parameter is not valid
82 #endif
83 
84 //Size of command history buffer
85 #ifndef SHELL_SERVER_HISTORY_SIZE
86  #define SHELL_SERVER_HISTORY_SIZE 256
87 #elif (SHELL_SERVER_HISTORY_SIZE < 1)
88  #error SHELL_SERVER_HISTORY_SIZE parameter is not valid
89 #endif
90 
91 //Maximum length of shell prompt
92 #ifndef SHELL_SERVER_MAX_PROMPT_LEN
93  #define SHELL_SERVER_MAX_PROMPT_LEN 64
94 #elif (SHELL_SERVER_MAX_PROMPT_LEN < 1)
95  #error SHELL_SERVER_MAX_PROMPT_LEN parameter is not valid
96 #endif
97 
98 //Default terminal width (in characters)
99 #ifndef SHELL_SERVER_DEFAULT_TERM_WIDTH
100  #define SHELL_SERVER_DEFAULT_TERM_WIDTH 80
101 #elif (SHELL_SERVER_DEFAULT_TERM_WIDTH < 1)
102  #error SHELL_SERVER_DEFAULT_TERM_WIDTH parameter is not valid
103 #endif
104 
105 //Default terminal height (in row)
106 #ifndef SHELL_SERVER_DEFAULT_TERM_HEIGHT
107  #define SHELL_SERVER_DEFAULT_TERM_HEIGHT 60
108 #elif (SHELL_SERVER_DEFAULT_TERM_HEIGHT < 1)
109  #error SHELL_SERVER_DEFAULT_TERM_HEIGHT parameter is not valid
110 #endif
111 
112 //Maximum length of multibyte escape sequences
113 #define SHELL_SERVER_MAX_ESC_SEQ_LEN 7
114 
115 //Forward declaration of ShellServerContext structure
116 struct _ShellServerContext;
117 #define ShellServerContext struct _ShellServerContext
118 
119 //Forward declaration of ShellServerSession structure
120 struct _ShellServerSession;
121 #define ShellServerSession struct _ShellServerSession
122 
123 //C++ guard
124 #ifdef __cplusplus
125 extern "C" {
126 #endif
127 
128 
129 /**
130  * @brief Access status
131  **/
132 
133 typedef enum
134 {
138 
139 
140 /**
141  * @brief Shell session state
142  **/
143 
144 typedef enum
145 {
151 
152 
153 /**
154  * @brief User verification callback function
155  **/
156 
158  const char_t *user);
159 
160 
161 /**
162  * @brief Command line processing callback function
163  **/
164 
166  char_t *commandLine);
167 
168 
169 /**
170  * @brief Session closing callback function
171  **/
172 
174  const char_t *user);
175 
176 
177 /**
178  * @brief Shell server settings
179  **/
180 
181 typedef struct
182 {
183  OsTaskParameters task[SHELL_SERVER_MAX_SESSIONS]; ///<Task parameters
184  SshServerContext *sshServerContext; ///<SSH server context
185  uint_t numSessions; ///<Maximum number of shell sessions
186  ShellServerSession *sessions; ///<Shell sessions
187  ShellServerCheckUserCallback checkUserCallback; ///<User verification callback function
188  ShellServerCommandLineCallback commandLineCallback; ///<Command line processing callback function
189  ShellServerCloseCallback closeCallback; ///<Session closing callback function
191 
192 
193 /**
194  * @brief Shell session
195  **/
196 
198 {
199  ShellServerSessionState state; ///<Session state
202  OsTaskParameters taskParams; ///<Task parameters
203  OsTaskId taskId; ///<Task identifier
204  ShellServerContext *context; ///<Shell server context
205  SshChannel *channel; ///<Underlying SSH channel
207  size_t promptLen; ///<Length of the shell prompt
208  char_t buffer[SHELL_SERVER_BUFFER_SIZE]; ///<Memory buffer for input/output operations
209  size_t bufferPos; ///<Current position in the buffer
210  size_t bufferLen; ///<Actual length of the buffer, in bytes
211 #if (SHELL_SERVER_HISTORY_SUPPORT == ENABLED)
212  char_t history[SHELL_SERVER_HISTORY_SIZE]; ///<Command history buffer
213  size_t historyLen; ///<Length of the command history buffer, in bytes
214  size_t historyPos; ///<Current position in the command history buffer
215 #endif
216  char_t backspaceCode; ///<Backspace key code
217  char_t deleteCode; ///<Delete key code
218  uint32_t termWidth; ///<Current terminal width (in characters)
219  uint32_t termHeight; ///<Current terminal height (in rows)
220  uint32_t newTermWidth; ///<New terminal width (in characters)
221  uint32_t newTermHeight; ///<New terminal height (in rows)
222  bool_t windowResize; ///<Window resize event
223  char_t escSeq[SHELL_SERVER_MAX_ESC_SEQ_LEN + 1]; ///<Multibyte escape sequence
224  size_t escSeqLen; ///<Length of the multibyte escape sequence
225 #ifdef SHELL_SERVER_SESSION_PRIVATE_VARS
226  SHELL_SERVER_SESSION_PRIVATE_VARS ///<Application specific context
227 #endif
228 };
229 
230 
231 /**
232  * @brief shell server context
233  **/
234 
236 {
237  SshServerContext *sshServerContext; ///<SSH server context
238  uint_t numSessions; ///<Maximum number of shell sessions
239  ShellServerSession *sessions; ///<Shell sessions
240  ShellServerCheckUserCallback checkUserCallback; ///<User verification callback function
241  ShellServerCommandLineCallback commandLineCallback; ///<Command line processing callback function
242  ShellServerCloseCallback closeCallback; ///<Session closing callback function
243  bool_t running; ///<Operational state of the shell server
244  bool_t stop; ///<Stop request
245  OsEvent event; ///<Event object used to poll the channels
246  SshChannelEventDesc eventDesc[SHELL_SERVER_MAX_SESSIONS]; ///<The events the application is interested in
247 #ifdef SHELL_SERVER_CONTEXT_PRIVATE_VARS
248  SHELL_SERVER_CONTEXT_PRIVATE_VARS ///<Application specific context
249 #endif
250 };
251 
252 
253 //Shell server related functions
255 
257  const ShellServerSettings *settings);
258 
260 
262  const char_t *banner);
263 
265  const char_t *prompt);
266 
268 
270  size_t length, size_t *written, uint_t flags);
271 
273  size_t size, size_t *received, uint_t flags);
274 
276  size_t size, size_t *length);
277 
279  const char_t *history, size_t length);
280 
282 
283 void shellServerTask(void *param);
284 
285 //C++ guard
286 #ifdef __cplusplus
287 }
288 #endif
289 
290 #endif
ShellServerSessionState
Shell session state.
Definition: shell_server.h:145
int bool_t
Definition: compiler_port.h:53
char_t escSeq[SHELL_SERVER_MAX_ESC_SEQ_LEN+1]
Multibyte escape sequence.
Definition: shell_server.h:223
char_t deleteCode
Delete key code.
Definition: shell_server.h:217
#define SHELL_SERVER_MAX_SESSIONS
Definition: shell_server.h:58
SshServerContext * sshServerContext
SSH server context.
Definition: shell_server.h:184
uint8_t data[]
Definition: ethernet.h:222
Event object.
ShellServerSessionState state
Session state.
Definition: shell_server.h:199
uint_t numSessions
Maximum number of shell sessions.
Definition: shell_server.h:238
#define SHELL_SERVER_HISTORY_SIZE
Definition: shell_server.h:86
error_t shellServerWriteStream(ShellServerSession *session, const void *data, size_t length, size_t *written, uint_t flags)
Write to stdout stream.
Definition: shell_server.c:302
size_t bufferLen
Actual length of the buffer, in bytes.
Definition: shell_server.h:210
shell server context
Definition: shell_server.h:236
ShellServerCloseCallback closeCallback
Session closing callback function.
Definition: shell_server.h:189
error_t shellServerInit(ShellServerContext *context, const ShellServerSettings *settings)
Initialize shell server context.
Definition: shell_server.c:86
Shell session.
Definition: shell_server.h:198
error_t shellServerSetTimeout(ShellServerSession *session, systime_t timeout)
Set timeout for read/write operations.
Definition: shell_server.c:271
void shellServerGetDefaultSettings(ShellServerSettings *settings)
Initialize settings with default values.
Definition: shell_server.c:50
char_t backspaceCode
Backspace key code.
Definition: shell_server.h:216
uint32_t termHeight
Current terminal height (in rows)
Definition: shell_server.h:219
ShellServerSession * sessions
Shell sessions.
Definition: shell_server.h:239
error_t shellServerSaveHistory(ShellServerSession *session, char_t *history, size_t size, size_t *length)
Save command history.
Definition: shell_server.c:365
error_t shellServerRestoreHistory(ShellServerSession *session, const char_t *history, size_t length)
Restore command history.
Definition: shell_server.c:414
@ SHELL_SERVER_SESSION_STATE_CLOSED
Definition: shell_server.h:146
error_t shellServerSetBanner(ShellServerSession *session, const char_t *banner)
Set welcome banner.
Definition: shell_server.c:208
error_t
Error codes.
Definition: error.h:43
ShellServerCommandLineCallback commandLineCallback
Command line processing callback function.
Definition: shell_server.h:188
char_t history[SHELL_SERVER_HISTORY_SIZE]
Command history buffer.
Definition: shell_server.h:212
uint_t numSessions
Maximum number of shell sessions.
Definition: shell_server.h:185
#define SHELL_SERVER_BUFFER_SIZE
Definition: shell_server.h:72
error_t shellServerSetPrompt(ShellServerSession *session, const char_t *prompt)
Set shell prompt.
Definition: shell_server.c:243
char_t prompt[SHELL_SERVER_MAX_PROMPT_LEN+1]
Shell prompt.
Definition: shell_server.h:206
ShellAccessStatus(* ShellServerCheckUserCallback)(ShellServerSession *session, const char_t *user)
User verification callback function.
Definition: shell_server.h:157
size_t historyPos
Current position in the command history buffer.
Definition: shell_server.h:214
@ SHELL_ACCESS_DENIED
Definition: shell_server.h:135
Shell server settings.
Definition: shell_server.h:182
SSH server context.
Definition: ssh_server.h:115
error_t(* ShellServerCommandLineCallback)(ShellServerSession *session, char_t *commandLine)
Command line processing callback function.
Definition: shell_server.h:165
ShellServerCheckUserCallback checkUserCallback
User verification callback function.
Definition: shell_server.h:240
Task parameters.
@ SHELL_SERVER_SESSION_STATE_INIT
Definition: shell_server.h:147
#define SHELL_SERVER_MAX_ESC_SEQ_LEN
Definition: shell_server.h:113
SshChannel * channel
Underlying SSH channel.
Definition: shell_server.h:205
uint8_t length
Definition: tcp.h:368
#define SHELL_SERVER_MAX_PROMPT_LEN
Definition: shell_server.h:93
@ SHELL_SERVER_SESSION_STATE_OPEN
Definition: shell_server.h:148
@ SHELL_SERVER_SESSION_STATE_EXEC
Definition: shell_server.h:149
SshChannelEventDesc eventDesc[SHELL_SERVER_MAX_SESSIONS]
The events the application is interested in.
Definition: shell_server.h:246
void(* ShellServerCloseCallback)(ShellServerSession *session, const char_t *user)
Session closing callback function.
Definition: shell_server.h:173
SshServerContext * sshServerContext
SSH server context.
Definition: shell_server.h:237
error_t shellServerReadStream(ShellServerSession *session, void *data, size_t size, size_t *received, uint_t flags)
Read from stdin stream.
Definition: shell_server.c:334
uint32_t systime_t
System time.
void shellServerTask(void *param)
Shell server task.
Definition: shell_server.c:494
uint8_t flags
Definition: tcp.h:351
bool_t windowResize
Window resize event.
Definition: shell_server.h:222
size_t historyLen
Length of the command history buffer, in bytes.
Definition: shell_server.h:213
char char_t
Definition: compiler_port.h:48
uint32_t newTermWidth
New terminal width (in characters)
Definition: shell_server.h:220
uint32_t newTermHeight
New terminal height (in rows)
Definition: shell_server.h:221
Structure describing channel events.
Definition: ssh.h:1551
bool_t stop
Stop request.
Definition: shell_server.h:244
OsTaskParameters taskParams
Task parameters.
Definition: shell_server.h:202
OsTaskId taskId
Task identifier.
Definition: shell_server.h:203
#define ShellServerSession
Definition: shell_server.h:121
ShellServerContext * context
Shell server context.
Definition: shell_server.h:204
SSH server.
error_t shellServerStart(ShellServerContext *context)
Start shell server.
Definition: shell_server.c:157
ShellServerCloseCallback closeCallback
Session closing callback function.
Definition: shell_server.h:242
ShellServerCheckUserCallback checkUserCallback
User verification callback function.
Definition: shell_server.h:187
uint32_t termWidth
Current terminal width (in characters)
Definition: shell_server.h:218
size_t escSeqLen
Length of the multibyte escape sequence.
Definition: shell_server.h:224
ShellServerCommandLineCallback commandLineCallback
Command line processing callback function.
Definition: shell_server.h:241
#define ShellServerContext
Definition: shell_server.h:117
ShellAccessStatus
Access status.
Definition: shell_server.h:134
bool_t running
Operational state of the shell server.
Definition: shell_server.h:243
thread_t * OsTaskId
Task identifier.
error_t shellServerClearHistory(ShellServerSession *session)
Clear command history.
Definition: shell_server.c:469
unsigned int uint_t
Definition: compiler_port.h:50
ShellServerSession * sessions
Shell sessions.
Definition: shell_server.h:186
size_t promptLen
Length of the shell prompt.
Definition: shell_server.h:207
size_t bufferPos
Current position in the buffer.
Definition: shell_server.h:209
@ SHELL_ACCESS_ALLOWED
Definition: shell_server.h:136
char_t buffer[SHELL_SERVER_BUFFER_SIZE]
Memory buffer for input/output operations.
Definition: shell_server.h:208
OsEvent event
Event object used to poll the channels.
Definition: shell_server.h:245
#define SshChannel
Definition: ssh.h:878