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-2026 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.6.6
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 //Application specific context
113 #ifndef SHELL_SERVER_CONTEXT_PRIVATE
114  #define SHELL_SERVER_CONTEXT_PRIVATE
115 #endif
116 
117 //Application specific context
118 #ifndef SHELL_SERVER_SESSION_PRIVATE
119  #define SHELL_SERVER_SESSION_PRIVATE
120 #endif
121 
122 //Maximum length of multibyte escape sequences
123 #define SHELL_SERVER_MAX_ESC_SEQ_LEN 7
124 
125 //Forward declaration of ShellServerContext structure
126 struct _ShellServerContext;
127 #define ShellServerContext struct _ShellServerContext
128 
129 //Forward declaration of ShellServerSession structure
130 struct _ShellServerSession;
131 #define ShellServerSession struct _ShellServerSession
132 
133 //C++ guard
134 #ifdef __cplusplus
135 extern "C" {
136 #endif
137 
138 
139 /**
140  * @brief Access status
141  **/
142 
143 typedef enum
144 {
148 
149 
150 /**
151  * @brief Shell session state
152  **/
153 
154 typedef enum
155 {
161 
162 
163 /**
164  * @brief User verification callback function
165  **/
166 
168  const char_t *user);
169 
170 
171 /**
172  * @brief Command line processing callback function
173  **/
174 
176  char_t *commandLine);
177 
178 
179 /**
180  * @brief Session closing callback function
181  **/
182 
184  const char_t *user);
185 
186 
187 /**
188  * @brief Shell server settings
189  **/
190 
191 typedef struct
192 {
193  OsTaskParameters task[SHELL_SERVER_MAX_SESSIONS]; ///<Task parameters
194  SshServerContext *sshServerContext; ///<SSH server context
195  uint_t numSessions; ///<Maximum number of shell sessions
196  ShellServerSession *sessions; ///<Shell sessions
197  ShellServerCheckUserCallback checkUserCallback; ///<User verification callback function
198  ShellServerCommandLineCallback commandLineCallback; ///<Command line processing callback function
199  ShellServerCloseCallback closeCallback; ///<Session closing callback function
201 
202 
203 /**
204  * @brief Shell session
205  **/
206 
208 {
209  ShellServerSessionState state; ///<Session state
212  OsTaskParameters taskParams; ///<Task parameters
213  OsTaskId taskId; ///<Task identifier
214  ShellServerContext *context; ///<Shell server context
215  SshChannel *channel; ///<Underlying SSH channel
217  size_t promptLen; ///<Length of the shell prompt
218  char_t buffer[SHELL_SERVER_BUFFER_SIZE]; ///<Memory buffer for input/output operations
219  size_t bufferPos; ///<Current position in the buffer
220  size_t bufferLen; ///<Actual length of the buffer, in bytes
221 #if (SHELL_SERVER_HISTORY_SUPPORT == ENABLED)
222  char_t history[SHELL_SERVER_HISTORY_SIZE]; ///<Command history buffer
223  size_t historyLen; ///<Length of the command history buffer, in bytes
224  size_t historyPos; ///<Current position in the command history buffer
225 #endif
226  char_t backspaceCode; ///<Backspace key code
227  char_t deleteCode; ///<Delete key code
228  uint32_t termWidth; ///<Current terminal width (in characters)
229  uint32_t termHeight; ///<Current terminal height (in rows)
230  uint32_t newTermWidth; ///<New terminal width (in characters)
231  uint32_t newTermHeight; ///<New terminal height (in rows)
232  bool_t windowResize; ///<Window resize event
233  char_t escSeq[SHELL_SERVER_MAX_ESC_SEQ_LEN + 1]; ///<Multibyte escape sequence
234  size_t escSeqLen; ///<Length of the multibyte escape sequence
235  SHELL_SERVER_SESSION_PRIVATE ///<Application specific context
236 };
237 
238 
239 /**
240  * @brief shell server context
241  **/
242 
244 {
245  SshServerContext *sshServerContext; ///<SSH server context
246  uint_t numSessions; ///<Maximum number of shell sessions
247  ShellServerSession *sessions; ///<Shell sessions
248  ShellServerCheckUserCallback checkUserCallback; ///<User verification callback function
249  ShellServerCommandLineCallback commandLineCallback; ///<Command line processing callback function
250  ShellServerCloseCallback closeCallback; ///<Session closing callback function
251  bool_t running; ///<Operational state of the shell server
252  bool_t stop; ///<Stop request
253  OsEvent event; ///<Event object used to poll the channels
254  SshChannelEventDesc eventDesc[SHELL_SERVER_MAX_SESSIONS]; ///<The events the application is interested in
255  SHELL_SERVER_CONTEXT_PRIVATE ///<Application specific context
256 };
257 
258 
259 //Shell server related functions
261 
263  const ShellServerSettings *settings);
264 
266 
268  const char_t *banner);
269 
271  const char_t *prompt);
272 
274 
276  size_t length, size_t *written, uint_t flags);
277 
279  size_t size, size_t *received, uint_t flags);
280 
282  int32_t exitStatus);
283 
285  size_t size, size_t *length);
286 
288  const char_t *history, size_t length);
289 
291 
292 void shellServerTask(void *param);
293 
294 //C++ guard
295 #ifdef __cplusplus
296 }
297 #endif
298 
299 #endif
ShellServerSessionState
Shell session state.
Definition: shell_server.h:155
int bool_t
Definition: compiler_port.h:63
char_t escSeq[SHELL_SERVER_MAX_ESC_SEQ_LEN+1]
Multibyte escape sequence.
Definition: shell_server.h:233
char_t deleteCode
Delete key code.
Definition: shell_server.h:227
#define SHELL_SERVER_MAX_SESSIONS
Definition: shell_server.h:58
SshServerContext * sshServerContext
SSH server context.
Definition: shell_server.h:194
uint8_t data[]
Definition: ethernet.h:224
Event object.
ShellServerSessionState state
Session state.
Definition: shell_server.h:209
uint_t numSessions
Maximum number of shell sessions.
Definition: shell_server.h:246
#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:220
shell server context
Definition: shell_server.h:244
ShellServerCloseCallback closeCallback
Session closing callback function.
Definition: shell_server.h:199
#define SHELL_SERVER_CONTEXT_PRIVATE
Definition: shell_server.h:114
error_t shellServerInit(ShellServerContext *context, const ShellServerSettings *settings)
Initialize shell server context.
Definition: shell_server.c:86
Shell session.
Definition: shell_server.h:208
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:226
uint32_t termHeight
Current terminal height (in rows)
Definition: shell_server.h:229
ShellServerSession * sessions
Shell sessions.
Definition: shell_server.h:247
error_t shellServerSaveHistory(ShellServerSession *session, char_t *history, size_t size, size_t *length)
Save command history.
Definition: shell_server.c:394
error_t shellServerRestoreHistory(ShellServerSession *session, const char_t *history, size_t length)
Restore command history.
Definition: shell_server.c:443
@ SHELL_SERVER_SESSION_STATE_CLOSED
Definition: shell_server.h:156
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:198
char_t history[SHELL_SERVER_HISTORY_SIZE]
Command history buffer.
Definition: shell_server.h:222
uint_t numSessions
Maximum number of shell sessions.
Definition: shell_server.h:195
#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:216
ShellAccessStatus(* ShellServerCheckUserCallback)(ShellServerSession *session, const char_t *user)
User verification callback function.
Definition: shell_server.h:167
size_t historyPos
Current position in the command history buffer.
Definition: shell_server.h:224
@ SHELL_ACCESS_DENIED
Definition: shell_server.h:145
Shell server settings.
Definition: shell_server.h:192
SSH server context.
Definition: ssh_server.h:121
error_t(* ShellServerCommandLineCallback)(ShellServerSession *session, char_t *commandLine)
Command line processing callback function.
Definition: shell_server.h:175
ShellServerCheckUserCallback checkUserCallback
User verification callback function.
Definition: shell_server.h:248
#define SHELL_SERVER_SESSION_PRIVATE
Definition: shell_server.h:119
Task parameters.
@ SHELL_SERVER_SESSION_STATE_INIT
Definition: shell_server.h:157
#define SHELL_SERVER_MAX_ESC_SEQ_LEN
Definition: shell_server.h:123
SshChannel * channel
Underlying SSH channel.
Definition: shell_server.h:215
uint8_t length
Definition: tcp.h:375
#define SHELL_SERVER_MAX_PROMPT_LEN
Definition: shell_server.h:93
@ SHELL_SERVER_SESSION_STATE_OPEN
Definition: shell_server.h:158
@ SHELL_SERVER_SESSION_STATE_EXEC
Definition: shell_server.h:159
SshChannelEventDesc eventDesc[SHELL_SERVER_MAX_SESSIONS]
The events the application is interested in.
Definition: shell_server.h:254
void(* ShellServerCloseCallback)(ShellServerSession *session, const char_t *user)
Session closing callback function.
Definition: shell_server.h:183
SshServerContext * sshServerContext
SSH server context.
Definition: shell_server.h:245
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:523
bool_t windowResize
Window resize event.
Definition: shell_server.h:232
size_t historyLen
Length of the command history buffer, in bytes.
Definition: shell_server.h:223
char char_t
Definition: compiler_port.h:55
uint32_t newTermWidth
New terminal width (in characters)
Definition: shell_server.h:230
uint32_t newTermHeight
New terminal height (in rows)
Definition: shell_server.h:231
error_t shellServerSetExitStatus(ShellServerSession *session, int32_t exitStatus)
Set exit status.
Definition: shell_server.c:363
Structure describing channel events.
Definition: ssh.h:1620
bool_t stop
Stop request.
Definition: shell_server.h:252
OsTaskParameters taskParams
Task parameters.
Definition: shell_server.h:212
OsTaskId taskId
Task identifier.
Definition: shell_server.h:213
#define ShellServerSession
Definition: shell_server.h:131
ShellServerContext * context
Shell server context.
Definition: shell_server.h:214
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:250
ShellServerCheckUserCallback checkUserCallback
User verification callback function.
Definition: shell_server.h:197
uint32_t termWidth
Current terminal width (in characters)
Definition: shell_server.h:228
size_t escSeqLen
Length of the multibyte escape sequence.
Definition: shell_server.h:234
ShellServerCommandLineCallback commandLineCallback
Command line processing callback function.
Definition: shell_server.h:249
#define ShellServerContext
Definition: shell_server.h:127
ShellAccessStatus
Access status.
Definition: shell_server.h:144
bool_t running
Operational state of the shell server.
Definition: shell_server.h:251
thread_t * OsTaskId
Task identifier.
error_t shellServerClearHistory(ShellServerSession *session)
Clear command history.
Definition: shell_server.c:498
uint8_t flags
Definition: tcp.h:358
unsigned int uint_t
Definition: compiler_port.h:57
ShellServerSession * sessions
Shell sessions.
Definition: shell_server.h:196
size_t promptLen
Length of the shell prompt.
Definition: shell_server.h:217
size_t bufferPos
Current position in the buffer.
Definition: shell_server.h:219
@ SHELL_ACCESS_ALLOWED
Definition: shell_server.h:146
char_t buffer[SHELL_SERVER_BUFFER_SIZE]
Memory buffer for input/output operations.
Definition: shell_server.h:218
OsEvent event
Event object used to poll the channels.
Definition: shell_server.h:253
#define SshChannel
Definition: ssh.h:939