scp_server.h
Go to the documentation of this file.
1 /**
2  * @file scp_server.h
3  * @brief SCP 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 _SCP_SERVER_H
32 #define _SCP_SERVER_H
33 
34 //Dependencies
35 #include "ssh/ssh_server.h"
36 #include "scp/scp_common.h"
37 #include "fs_port.h"
38 
39 //SCP server support
40 #ifndef SCP_SERVER_SUPPORT
41  #define SCP_SERVER_SUPPORT DISABLED
42 #elif (SCP_SERVER_SUPPORT != ENABLED && SCP_SERVER_SUPPORT != DISABLED)
43  #error SCP_SERVER_SUPPORT parameter is not valid
44 #endif
45 
46 //Stack size required to run the SCP server
47 #ifndef SCP_SERVER_STACK_SIZE
48  #define SCP_SERVER_STACK_SIZE 650
49 #elif (SCP_SERVER_STACK_SIZE < 1)
50  #error SCP_SERVER_STACK_SIZE parameter is not valid
51 #endif
52 
53 //Priority at which the SCP server should run
54 #ifndef SCP_SERVER_PRIORITY
55  #define SCP_SERVER_PRIORITY OS_TASK_PRIORITY_NORMAL
56 #endif
57 
58 //Maximum number of simultaneous SCP sessions
59 #ifndef SCP_SERVER_MAX_SESSIONS
60  #define SCP_SERVER_MAX_SESSIONS 10
61 #elif (SCP_SERVER_MAX_SESSIONS < 1)
62  #error SCP_SERVER_MAX_SESSIONS parameter is not valid
63 #endif
64 
65 //SCP server tick interval
66 #ifndef SCP_SERVER_TICK_INTERVAL
67  #define SCP_SERVER_TICK_INTERVAL 1000
68 #elif (SCP_SERVER_TICK_INTERVAL < 100)
69  #error SCP_SERVER_TICK_INTERVAL parameter is not valid
70 #endif
71 
72 //Size of buffer used for input/output operations
73 #ifndef SCP_SERVER_BUFFER_SIZE
74  #define SCP_SERVER_BUFFER_SIZE 512
75 #elif (SCP_SERVER_BUFFER_SIZE < 128)
76  #error SCP_SERVER_BUFFER_SIZE parameter is not valid
77 #endif
78 
79 //Maximum length of root directory
80 #ifndef SCP_SERVER_MAX_ROOT_DIR_LEN
81  #define SCP_SERVER_MAX_ROOT_DIR_LEN 63
82 #elif (SCP_SERVER_MAX_ROOT_DIR_LEN < 7)
83  #error SCP_SERVER_MAX_ROOT_DIR_LEN parameter is not valid
84 #endif
85 
86 //Maximum length of home directory
87 #ifndef SCP_SERVER_MAX_HOME_DIR_LEN
88  #define SCP_SERVER_MAX_HOME_DIR_LEN 63
89 #elif (SCP_SERVER_MAX_HOME_DIR_LEN < 7)
90  #error SCP_SERVER_MAX_HOME_DIR_LEN parameter is not valid
91 #endif
92 
93 //Maximum path length
94 #ifndef SCP_SERVER_MAX_PATH_LEN
95  #define SCP_SERVER_MAX_PATH_LEN 255
96 #elif (SCP_SERVER_MAX_PATH_LEN < 7)
97  #error SCP_SERVER_MAX_PATH_LEN parameter is not valid
98 #endif
99 
100 //Maximum recursion depth
101 #ifndef SCP_SERVER_MAX_RECURSION_LEVEL
102  #define SCP_SERVER_MAX_RECURSION_LEVEL 4
103 #elif (SCP_SERVER_MAX_RECURSION_LEVEL < 1)
104  #error SCP_SERVER_MAX_RECURSION_LEVEL parameter is not valid
105 #endif
106 
107 //Forward declaration of ScpServerContext structure
108 struct _ScpServerContext;
109 #define ScpServerContext struct _ScpServerContext
110 
111 //Forward declaration of ScpServerSession structure
112 struct _ScpServerSession;
113 #define ScpServerSession struct _ScpServerSession
114 
115 //C++ guard
116 #ifdef __cplusplus
117 extern "C" {
118 #endif
119 
120 
121 /**
122  * @brief Access status
123  **/
124 
125 typedef enum
126 {
130 
131 
132 /**
133  * @brief File permissions
134  **/
135 
136 typedef enum
137 {
140  SCP_FILE_PERM_WRITE = 0x04
142 
143 
144 /**
145  * @brief SCP session state
146  **/
147 
148 typedef enum
149 {
166 
167 
168 /**
169  * @brief User verification callback function
170  **/
171 
173  const char_t *user);
174 
175 
176 /**
177  * @brief Callback used to retrieve file permissions
178  **/
179 
181  const char_t *user, const char_t *path);
182 
183 
184 /**
185  * @brief SCP server settings
186  **/
187 
188 typedef struct
189 {
190  OsTaskParameters task; ///<Task parameters
191  SshServerContext *sshServerContext; ///<SSH server context
192  uint_t numSessions; ///<Maximum number of SCP sessions
193  ScpServerSession *sessions; ///<SCP sessions
194  const char_t *rootDir; ///<Root directory
195  ScpServerCheckUserCallback checkUserCallback; ///<User verification callback function
196  ScpServerGetFilePermCallback getFilePermCallback; ///<Callback used to retrieve file permissions
198 
199 
200 /**
201  * @brief SCP session
202  **/
203 
205 {
206  ScpServerSessionState state; ///<Session state
207  ScpServerContext *context; ///<SCP server context
208  SshChannel *channel; ///<Underlying SSH channel
209  char_t rootDir[SCP_SERVER_MAX_ROOT_DIR_LEN + 1]; ///<Root directory
210  char_t homeDir[SCP_SERVER_MAX_HOME_DIR_LEN + 1]; ///<Home directory
211  char_t path[SCP_SERVER_MAX_PATH_LEN + 1]; ///<Path name
212  bool_t recursive; ///<Recursive copy
213  bool_t targetIsDir; ///<The target is a directory
214  uint_t dirLevel; ///<Current level of recursion
215  FsDir *dir[SCP_SERVER_MAX_RECURSION_LEVEL]; ///<Directory pointers
216  FsFile *file; ///<File pointer
217  uint32_t fileMode; ///<File access rights
218  uint64_t fileSize; ///<Size of the file, in bytes
219  uint64_t fileOffset; ///<Offset within the file
220  char_t buffer[SCP_SERVER_BUFFER_SIZE]; ///<Memory buffer for input/output operations
221  size_t bufferPos; ///<Current position in the buffer
222  size_t bufferLen; ///<Actual length of the buffer, in bytes
223  error_t statusCode; ///<Status code
224 };
225 
226 
227 /**
228  * @brief SCP server context
229  **/
230 
232 {
233  SshServerContext *sshServerContext; ///<SSH server context
234  uint_t numSessions; ///<Maximum number of SCP sessions
235  ScpServerSession *sessions; ///<SCP sessions
236  char_t rootDir[SCP_SERVER_MAX_ROOT_DIR_LEN + 1]; ///<Root directory
237  ScpServerCheckUserCallback checkUserCallback; ///<User verification callback function
238  ScpServerGetFilePermCallback getFilePermCallback; ///<Callback used to retrieve file permissions
239  bool_t running; ///<Operational state of the FTP server
240  bool_t stop; ///<Stop request
241  OsEvent event; ///<Event object used to poll the channels
242  OsTaskParameters taskParams; ///<Task parameters
243  OsTaskId taskId; ///<Task identifier
244  SshChannelEventDesc eventDesc[SCP_SERVER_MAX_SESSIONS]; ///<The events the application is interested in
245  char_t path[SCP_SERVER_MAX_PATH_LEN + 1]; ///<Path name
246 };
247 
248 
249 //SCP server related functions
251 
253  const ScpServerSettings *settings);
254 
257 
258 error_t scpServerSetRootDir(ScpServerSession *session, const char_t *rootDir);
259 error_t scpServerSetHomeDir(ScpServerSession *session, const char_t *homeDir);
260 
261 void scpServerTask(void *param);
262 
263 void scpServerDeinit(ScpServerContext *context);
264 
265 //C++ guard
266 #ifdef __cplusplus
267 }
268 #endif
269 
270 #endif
ScpServerSession * sessions
SCP sessions.
Definition: scp_server.h:235
int bool_t
Definition: compiler_port.h:53
char_t path[SCP_SERVER_MAX_PATH_LEN+1]
Path name.
Definition: scp_server.h:245
@ SCP_SERVER_SESSION_STATE_WRITE_STATUS
Definition: scp_server.h:155
SshServerContext * sshServerContext
SSH server context.
Definition: scp_server.h:191
uint_t(* ScpServerGetFilePermCallback)(ScpServerSession *session, const char_t *user, const char_t *path)
Callback used to retrieve file permissions.
Definition: scp_server.h:180
SshChannelEventDesc eventDesc[SCP_SERVER_MAX_SESSIONS]
The events the application is interested in.
Definition: scp_server.h:244
char_t buffer[SCP_SERVER_BUFFER_SIZE]
Memory buffer for input/output operations.
Definition: scp_server.h:220
@ SCP_ACCESS_DENIED
Definition: scp_server.h:127
Event object.
#define SCP_SERVER_BUFFER_SIZE
Definition: scp_server.h:74
@ SCP_SERVER_SESSION_STATE_WRITE_COMMAND
Definition: scp_server.h:152
#define SCP_SERVER_MAX_RECURSION_LEVEL
Definition: scp_server.h:102
ScpAccessStatus(* ScpServerCheckUserCallback)(ScpServerSession *session, const char_t *user)
User verification callback function.
Definition: scp_server.h:172
uint32_t fileMode
File access rights.
Definition: scp_server.h:217
ScpServerSessionState state
Session state.
Definition: scp_server.h:206
ScpFilePerm
File permissions.
Definition: scp_server.h:137
OsTaskId taskId
Task identifier.
Definition: scp_server.h:243
#define ScpServerSession
Definition: scp_server.h:113
@ SCP_SERVER_SESSION_STATE_WRITE_INIT
Definition: scp_server.h:151
Directory descriptor.
Definition: fs_port_posix.h:60
uint_t numSessions
Maximum number of SCP sessions.
Definition: scp_server.h:192
@ SCP_FILE_PERM_READ
Definition: scp_server.h:139
#define SCP_SERVER_MAX_SESSIONS
Definition: scp_server.h:60
uint64_t fileSize
Size of the file, in bytes.
Definition: scp_server.h:218
bool_t recursive
Recursive copy.
Definition: scp_server.h:212
#define SCP_SERVER_MAX_PATH_LEN
Definition: scp_server.h:95
ScpAccessStatus
Access status.
Definition: scp_server.h:126
bool_t running
Operational state of the FTP server.
Definition: scp_server.h:239
SshServerContext * sshServerContext
SSH server context.
Definition: scp_server.h:233
@ SCP_SERVER_SESSION_STATE_CLOSING
Definition: scp_server.h:164
SCP session.
Definition: scp_server.h:205
@ SCP_SERVER_SESSION_STATE_WRITE_ACK
Definition: scp_server.h:153
error_t
Error codes.
Definition: error.h:43
void scpServerDeinit(ScpServerContext *context)
Release SCP server context.
Definition: scp_server.c:438
FsFile * file
File pointer.
Definition: scp_server.h:216
@ SCP_SERVER_SESSION_STATE_READ_STATUS
Definition: scp_server.h:161
uint_t dirLevel
Current level of recursion.
Definition: scp_server.h:214
ScpServerContext * context
SCP server context.
Definition: scp_server.h:207
@ SCP_SERVER_SESSION_STATE_ERROR
Definition: scp_server.h:163
SSH server context.
Definition: ssh_server.h:115
error_t statusCode
Status code.
Definition: scp_server.h:223
#define ScpServerContext
Definition: scp_server.h:109
void scpServerTask(void *param)
SCP server task.
Definition: scp_server.c:341
char_t rootDir[SCP_SERVER_MAX_ROOT_DIR_LEN+1]
Root directory.
Definition: scp_server.h:236
#define SCP_SERVER_MAX_ROOT_DIR_LEN
Definition: scp_server.h:81
error_t scpServerSetHomeDir(ScpServerSession *session, const char_t *homeDir)
Set user's home directory.
Definition: scp_server.c:312
Task parameters.
uint64_t fileOffset
Offset within the file.
Definition: scp_server.h:219
SshChannel * channel
Underlying SSH channel.
Definition: scp_server.h:208
@ SCP_FILE_PERM_WRITE
Definition: scp_server.h:140
OsTaskParameters task
Task parameters.
Definition: scp_server.h:190
File system abstraction layer.
Definitions common to SCP client and server.
@ SCP_SERVER_SESSION_STATE_WRITE_FIN
Definition: scp_server.h:156
@ SCP_SERVER_SESSION_STATE_READ_COMMAND
Definition: scp_server.h:158
ScpServerGetFilePermCallback getFilePermCallback
Callback used to retrieve file permissions.
Definition: scp_server.h:196
size_t bufferLen
Actual length of the buffer, in bytes.
Definition: scp_server.h:222
error_t scpServerInit(ScpServerContext *context, const ScpServerSettings *settings)
Initialize SCP server context.
Definition: scp_server.c:81
SCP server settings.
Definition: scp_server.h:189
bool_t stop
Stop request.
Definition: scp_server.h:240
char char_t
Definition: compiler_port.h:48
@ SCP_SERVER_SESSION_STATE_CLOSED
Definition: scp_server.h:150
#define SCP_SERVER_MAX_HOME_DIR_LEN
Definition: scp_server.h:88
const char_t * rootDir
Root directory.
Definition: scp_server.h:194
void scpServerGetDefaultSettings(ScpServerSettings *settings)
Initialize settings with default values.
Definition: scp_server.c:50
Structure describing channel events.
Definition: ssh.h:1551
@ SCP_FILE_PERM_LIST
Definition: scp_server.h:138
@ SCP_SERVER_SESSION_STATE_READ_INIT
Definition: scp_server.h:157
SSH server.
bool_t targetIsDir
The target is a directory.
Definition: scp_server.h:213
ScpServerSessionState
SCP session state.
Definition: scp_server.h:149
char_t path[SCP_SERVER_MAX_PATH_LEN+1]
Path name.
Definition: scp_server.h:211
char_t rootDir[SCP_SERVER_MAX_ROOT_DIR_LEN+1]
Root directory.
Definition: scp_server.h:209
SCP server context.
Definition: scp_server.h:232
OsEvent event
Event object used to poll the channels.
Definition: scp_server.h:241
size_t bufferPos
Current position in the buffer.
Definition: scp_server.h:221
uint_t numSessions
Maximum number of SCP sessions.
Definition: scp_server.h:234
@ SCP_SERVER_SESSION_STATE_WRITE_DATA
Definition: scp_server.h:154
thread_t * OsTaskId
Task identifier.
OsTaskParameters taskParams
Task parameters.
Definition: scp_server.h:242
unsigned int uint_t
Definition: compiler_port.h:50
ScpServerSession * sessions
SCP sessions.
Definition: scp_server.h:193
ScpServerCheckUserCallback checkUserCallback
User verification callback function.
Definition: scp_server.h:237
char_t homeDir[SCP_SERVER_MAX_HOME_DIR_LEN+1]
Home directory.
Definition: scp_server.h:210
@ SCP_SERVER_SESSION_STATE_READ_ACK
Definition: scp_server.h:159
FsDir * dir[SCP_SERVER_MAX_RECURSION_LEVEL]
Directory pointers.
Definition: scp_server.h:215
ScpServerCheckUserCallback checkUserCallback
User verification callback function.
Definition: scp_server.h:195
error_t scpServerStop(ScpServerContext *context)
Stop SCP server.
Definition: scp_server.c:223
@ SCP_SERVER_SESSION_STATE_READ_FIN
Definition: scp_server.h:162
error_t scpServerSetRootDir(ScpServerSession *session, const char_t *rootDir)
Set user's root directory.
Definition: scp_server.c:278
@ SCP_SERVER_SESSION_STATE_READ_DATA
Definition: scp_server.h:160
#define SshChannel
Definition: ssh.h:878
error_t scpServerStart(ScpServerContext *context)
Start SCP server.
Definition: scp_server.c:164
void FsFile
File descriptor.
Definition: fs_port_fatfs.h:60
ScpServerGetFilePermCallback getFilePermCallback
Callback used to retrieve file permissions.
Definition: scp_server.h:238
@ SCP_ACCESS_ALLOWED
Definition: scp_server.h:128