sftp_server.h
Go to the documentation of this file.
1 /**
2  * @file sftp_server.h
3  * @brief SFTP 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 _SFTP_SERVER_H
32 #define _SFTP_SERVER_H
33 
34 //Dependencies
35 #include "ssh/ssh_server.h"
36 #include "sftp/sftp_common.h"
37 #include "fs_port.h"
38 
39 //SFTP server support
40 #ifndef SFTP_SERVER_SUPPORT
41  #define SFTP_SERVER_SUPPORT DISABLED
42 #elif (SFTP_SERVER_SUPPORT != ENABLED && SFTP_SERVER_SUPPORT != DISABLED)
43  #error SFTP_SERVER_SUPPORT parameter is not valid
44 #endif
45 
46 //Stack size required to run the SFTP server
47 #ifndef SFTP_SERVER_STACK_SIZE
48  #define SFTP_SERVER_STACK_SIZE 650
49 #elif (SFTP_SERVER_STACK_SIZE < 1)
50  #error SFTP_SERVER_STACK_SIZE parameter is not valid
51 #endif
52 
53 //Priority at which the SFTP server should run
54 #ifndef SFTP_SERVER_PRIORITY
55  #define SFTP_SERVER_PRIORITY OS_TASK_PRIORITY_NORMAL
56 #endif
57 
58 //Maximum number of simultaneous SFTP sessions
59 #ifndef SFTP_SERVER_MAX_SESSIONS
60  #define SFTP_SERVER_MAX_SESSIONS 10
61 #elif (SFTP_SERVER_MAX_SESSIONS < 1)
62  #error SFTP_SERVER_MAX_SESSIONS parameter is not valid
63 #endif
64 
65 //SFTP server tick interval
66 #ifndef SFTP_SERVER_TICK_INTERVAL
67  #define SFTP_SERVER_TICK_INTERVAL 1000
68 #elif (SFTP_SERVER_TICK_INTERVAL < 100)
69  #error SFTP_SERVER_TICK_INTERVAL parameter is not valid
70 #endif
71 
72 //Minimum SFTP protocol version that can be negotiated
73 #ifndef SFTP_SERVER_MIN_VERSION
74  #define SFTP_SERVER_MIN_VERSION 1
75 #elif (SFTP_SERVER_MIN_VERSION < 1)
76  #error SFTP_SERVER_MIN_VERSION parameter is not valid
77 #endif
78 
79 //Maximum SFTP protocol version that can be negotiated
80 #ifndef SFTP_SERVER_MAX_VERSION
81  #define SFTP_SERVER_MAX_VERSION 3
82 #elif (SFTP_SERVER_MAX_VERSION > 3 || SFTP_SERVER_MAX_VERSION < SFTP_CLIENT_MIN_VERSION)
83  #error SFTP_SERVER_MAX_VERSION parameter is not valid
84 #endif
85 
86 //Size of buffer used for input/output operations
87 #ifndef SFTP_SERVER_BUFFER_SIZE
88  #define SFTP_SERVER_BUFFER_SIZE 1024
89 #elif (SFTP_SERVER_BUFFER_SIZE < 128)
90  #error SFTP_SERVER_BUFFER_SIZE parameter is not valid
91 #endif
92 
93 //Maximum length of root directory
94 #ifndef SFTP_SERVER_MAX_ROOT_DIR_LEN
95  #define SFTP_SERVER_MAX_ROOT_DIR_LEN 63
96 #elif (SFTP_SERVER_MAX_ROOT_DIR_LEN < 7)
97  #error SFTP_SERVER_MAX_ROOT_DIR_LEN parameter is not valid
98 #endif
99 
100 //Maximum length of home directory
101 #ifndef SFTP_SERVER_MAX_HOME_DIR_LEN
102  #define SFTP_SERVER_MAX_HOME_DIR_LEN 63
103 #elif (SFTP_SERVER_MAX_HOME_DIR_LEN < 7)
104  #error SFTP_SERVER_MAX_HOME_DIR_LEN parameter is not valid
105 #endif
106 
107 //Maximum path length
108 #ifndef SFTP_SERVER_MAX_PATH_LEN
109  #define SFTP_SERVER_MAX_PATH_LEN 255
110 #elif (SFTP_SERVER_MAX_PATH_LEN < 7)
111  #error SFTP_SERVER_MAX_PATH_LEN parameter is not valid
112 #endif
113 
114 //Forward declaration of SftpServerContext structure
115 struct _SftpServerContext;
116 #define SftpServerContext struct _SftpServerContext
117 
118 //Forward declaration of SftpServerSession structure
119 struct _SftpServerSession;
120 #define SftpServerSession struct _SftpServerSession
121 
122 //C++ guard
123 #ifdef __cplusplus
124 extern "C" {
125 #endif
126 
127 
128 /**
129  * @brief Access status
130  **/
131 
132 typedef enum
133 {
137 
138 
139 /**
140  * @brief File permissions
141  **/
142 
143 typedef enum
144 {
147  SFTP_FILE_PERM_WRITE = 0x04
149 
150 
151 /**
152  * @brief SFTP session state
153  **/
154 
155 typedef enum
156 {
163 
164 
165 /**
166  * @brief User verification callback function
167  **/
168 
170  const char_t *user);
171 
172 
173 /**
174  * @brief Callback used to retrieve file permissions
175  **/
176 
178  const char_t *user, const char_t *path);
179 
180 
181 /**
182  * @brief File or directory object
183  **/
184 
185 typedef struct
186 {
187  SftpFileType type; ///<File type
188  SftpServerSession *session; ///<Pointer to the SFTP session
189  char_t path[SFTP_SERVER_MAX_PATH_LEN + 1]; ///<Path name
190  uint32_t handle; ///<Opaque value that identifies the file
191  uint64_t size; ///<Size of the file
192  uint64_t offset; ///<Offset within the file
193  FsFile *file; ///<File pointer
194  FsDir *dir; ///<Directory pointer
196 
197 
198 /**
199  * @brief SFTP server settings
200  **/
201 
202 typedef struct
203 {
204  OsTaskParameters task; ///<Task parameters
205  SshServerContext *sshServerContext; ///<SSH server context
206  uint_t numSessions; ///<Maximum number of SFTP sessions
207  SftpServerSession *sessions; ///<SFTP sessions
208  uint_t numFileObjects; ///<Maximum number of file objects
209  SftpFileObject *fileObjects; ///<File objects
210  const char_t *rootDir; ///<Root directory
211  SftpServerCheckUserCallback checkUserCallback; ///<User verification callback function
212  SftpServerGetFilePermCallback getFilePermCallback; ///<Callback used to retrieve file permissions
214 
215 
216 /**
217  * @brief SFTP session
218  **/
219 
221 {
222  SftpServerSessionState state; ///<Session state
223  SftpVersion version; ///<SFTP protocol version
224  SftpServerContext *context; ///<SFTP server context
225  SshChannel *channel; ///<Underlying SSH channel
226  char_t rootDir[SFTP_SERVER_MAX_ROOT_DIR_LEN + 1]; ///<Root directory
227  char_t homeDir[SFTP_SERVER_MAX_HOME_DIR_LEN + 1]; ///<Home directory
228  uint32_t requestId; ///<Request identifier
229  error_t requestStatus; ///<Status of the request
230  FsFile *file; ///<File pointer
231  size_t dataLen; ///<Length of the data payload
232  uint8_t buffer[SFTP_SERVER_BUFFER_SIZE]; ///<Memory buffer for input/output operations
233  size_t bufferPos; ///<Current position in the buffer
234  size_t bufferLen; ///<Actual length of the buffer, in bytes
235  size_t totalLen;
236  uint32_t handle; ///<File or directory handle
237 };
238 
239 
240 /**
241  * @brief SFTP server context
242  **/
243 
245 {
246  SshServerContext *sshServerContext; ///<SSH server context
247  uint_t numSessions; ///<Maximum number of SFTP sessions
248  SftpServerSession *sessions; ///<SFTP sessions
249  uint_t numFileObjects; ///<Maximum number of file objects
250  SftpFileObject *fileObjects; ///<File objects
251  char_t rootDir[SFTP_SERVER_MAX_ROOT_DIR_LEN + 1]; ///<Root directory
252  SftpServerCheckUserCallback checkUserCallback; ///<User verification callback function
253  SftpServerGetFilePermCallback getFilePermCallback; ///<Callback used to retrieve file permissions
254  bool_t running; ///<Operational state of the FTP server
255  bool_t stop; ///<Stop request
256  OsEvent event; ///<Event object used to poll the channels
257  OsTaskParameters taskParams; ///<Task parameters
258  OsTaskId taskId; ///<Task identifier
259  SshChannelEventDesc eventDesc[SFTP_SERVER_MAX_SESSIONS]; ///<The events the application is interested in
261 };
262 
263 
264 //SFTP server related functions
266 
268  const SftpServerSettings *settings);
269 
272 
273 error_t sftpServerSetRootDir(SftpServerSession *session, const char_t *rootDir);
274 error_t sftpServerSetHomeDir(SftpServerSession *session, const char_t *homeDir);
275 
276 void sftpServerTask(void *param);
277 
278 void sftpServerDeinit(SftpServerContext *context);
279 
280 //C++ guard
281 #ifdef __cplusplus
282 }
283 #endif
284 
285 #endif
uint_t numSessions
Maximum number of SFTP sessions.
Definition: sftp_server.h:206
char_t path[SFTP_SERVER_MAX_PATH_LEN+1]
Path name.
Definition: sftp_server.h:260
SftpFileType
File types.
Definition: sftp_common.h:188
size_t dataLen
Length of the data payload.
Definition: sftp_server.h:231
int bool_t
Definition: compiler_port.h:53
#define SFTP_SERVER_BUFFER_SIZE
Definition: sftp_server.h:88
uint_t numSessions
Maximum number of SFTP sessions.
Definition: sftp_server.h:247
error_t sftpServerSetRootDir(SftpServerSession *session, const char_t *rootDir)
Set user's root directory.
Definition: sftp_server.c:297
SFtpFilePerm
File permissions.
Definition: sftp_server.h:144
OsTaskParameters task
Task parameters.
Definition: sftp_server.h:204
SftpServerSession * sessions
SFTP sessions.
Definition: sftp_server.h:248
SftpServerCheckUserCallback checkUserCallback
User verification callback function.
Definition: sftp_server.h:211
FsFile * file
File pointer.
Definition: sftp_server.h:193
Event object.
@ SFTP_SERVER_SESSION_STATE_RECEIVING_DATA
Definition: sftp_server.h:160
SftpAccessStatus(* SftpServerCheckUserCallback)(SftpServerSession *session, const char_t *user)
User verification callback function.
Definition: sftp_server.h:169
FsDir * dir
Directory pointer.
Definition: sftp_server.h:194
@ SFTP_ACCESS_DENIED
Definition: sftp_server.h:134
SftpServerSession * session
Pointer to the SFTP session.
Definition: sftp_server.h:188
SshServerContext * sshServerContext
SSH server context.
Definition: sftp_server.h:205
error_t sftpServerStop(SftpServerContext *context)
Stop SFTP server.
Definition: sftp_server.c:242
char_t rootDir[SFTP_SERVER_MAX_ROOT_DIR_LEN+1]
Root directory.
Definition: sftp_server.h:251
OsEvent event
Event object used to poll the channels.
Definition: sftp_server.h:256
SFTP server context.
Definition: sftp_server.h:245
Directory descriptor.
Definition: fs_port_posix.h:60
size_t bufferLen
Actual length of the buffer, in bytes.
Definition: sftp_server.h:234
SFTP server settings.
Definition: sftp_server.h:203
FsFile * file
File pointer.
Definition: sftp_server.h:230
@ SFTP_FILE_PERM_READ
Definition: sftp_server.h:146
size_t bufferPos
Current position in the buffer.
Definition: sftp_server.h:233
OsTaskParameters taskParams
Task parameters.
Definition: sftp_server.h:257
error_t
Error codes.
Definition: error.h:43
SftpFileType type
File type.
Definition: sftp_server.h:187
#define SFTP_SERVER_MAX_PATH_LEN
Definition: sftp_server.h:109
const char_t * rootDir
Root directory.
Definition: sftp_server.h:210
uint8_t buffer[SFTP_SERVER_BUFFER_SIZE]
Memory buffer for input/output operations.
Definition: sftp_server.h:232
uint64_t offset
Offset within the file.
Definition: sftp_server.h:192
SftpFileObject * fileObjects
File objects.
Definition: sftp_server.h:250
OsTaskId taskId
Task identifier.
Definition: sftp_server.h:258
#define SFTP_SERVER_MAX_ROOT_DIR_LEN
Definition: sftp_server.h:95
SftpServerSession * sessions
SFTP sessions.
Definition: sftp_server.h:207
SftpAccessStatus
Access status.
Definition: sftp_server.h:133
SftpVersion version
SFTP protocol version.
Definition: sftp_server.h:223
SFTP session.
Definition: sftp_server.h:221
SSH server context.
Definition: ssh_server.h:115
bool_t running
Operational state of the FTP server.
Definition: sftp_server.h:254
uint32_t requestId
Request identifier.
Definition: sftp_server.h:228
Task parameters.
bool_t stop
Stop request.
Definition: sftp_server.h:255
void sftpServerTask(void *param)
SFTP server task.
Definition: sftp_server.c:360
@ SFTP_SERVER_SESSION_STATE_SENDING_DATA
Definition: sftp_server.h:161
File or directory object.
Definition: sftp_server.h:186
uint32_t handle
File or directory handle.
Definition: sftp_server.h:236
error_t sftpServerStart(SftpServerContext *context)
Start SFTP server.
Definition: sftp_server.c:183
File system abstraction layer.
char_t rootDir[SFTP_SERVER_MAX_ROOT_DIR_LEN+1]
Root directory.
Definition: sftp_server.h:226
SftpServerSessionState state
Session state.
Definition: sftp_server.h:222
void sftpServerDeinit(SftpServerContext *context)
Release SFTP server context.
Definition: sftp_server.c:457
error_t requestStatus
Status of the request.
Definition: sftp_server.h:229
@ SFTP_SERVER_SESSION_STATE_CLOSED
Definition: sftp_server.h:157
char char_t
Definition: compiler_port.h:48
SftpServerContext * context
SFTP server context.
Definition: sftp_server.h:224
uint_t numFileObjects
Maximum number of file objects.
Definition: sftp_server.h:249
#define SFTP_SERVER_MAX_SESSIONS
Definition: sftp_server.h:60
uint_t(* SftpServerGetFilePermCallback)(SftpServerSession *session, const char_t *user, const char_t *path)
Callback used to retrieve file permissions.
Definition: sftp_server.h:177
Structure describing channel events.
Definition: ssh.h:1551
SftpServerSessionState
SFTP session state.
Definition: sftp_server.h:156
uint64_t size
Size of the file.
Definition: sftp_server.h:191
SftpServerCheckUserCallback checkUserCallback
User verification callback function.
Definition: sftp_server.h:252
SftpFileObject * fileObjects
File objects.
Definition: sftp_server.h:209
SSH server.
@ SFTP_FILE_PERM_LIST
Definition: sftp_server.h:145
SshChannel * channel
Underlying SSH channel.
Definition: sftp_server.h:225
error_t sftpServerSetHomeDir(SftpServerSession *session, const char_t *homeDir)
Set user's home directory.
Definition: sftp_server.c:331
SshServerContext * sshServerContext
SSH server context.
Definition: sftp_server.h:246
void sftpServerGetDefaultSettings(SftpServerSettings *settings)
Initialize settings with default values.
Definition: sftp_server.c:50
@ SFTP_ACCESS_ALLOWED
Definition: sftp_server.h:135
@ SFTP_SERVER_SESSION_STATE_RECEIVING
Definition: sftp_server.h:158
Definitions common to SFTP client and server.
SftpVersion
SFTP protocol version.
Definition: sftp_common.h:118
SftpServerGetFilePermCallback getFilePermCallback
Callback used to retrieve file permissions.
Definition: sftp_server.h:253
@ SFTP_FILE_PERM_WRITE
Definition: sftp_server.h:147
thread_t * OsTaskId
Task identifier.
SftpServerGetFilePermCallback getFilePermCallback
Callback used to retrieve file permissions.
Definition: sftp_server.h:212
#define SftpServerSession
Definition: sftp_server.h:120
unsigned int uint_t
Definition: compiler_port.h:50
#define SftpServerContext
Definition: sftp_server.h:116
uint32_t handle
Opaque value that identifies the file.
Definition: sftp_server.h:190
error_t sftpServerInit(SftpServerContext *context, const SftpServerSettings *settings)
Initialize SFTP server context.
Definition: sftp_server.c:85
SshChannelEventDesc eventDesc[SFTP_SERVER_MAX_SESSIONS]
The events the application is interested in.
Definition: sftp_server.h:259
uint_t numFileObjects
Maximum number of file objects.
Definition: sftp_server.h:208
@ SFTP_SERVER_SESSION_STATE_SENDING
Definition: sftp_server.h:159
#define SshChannel
Definition: ssh.h:878
#define SFTP_SERVER_MAX_HOME_DIR_LEN
Definition: sftp_server.h:102
void FsFile
File descriptor.
Definition: fs_port_fatfs.h:60
char_t homeDir[SFTP_SERVER_MAX_HOME_DIR_LEN+1]
Home directory.
Definition: sftp_server.h:227