sftp_common.h
Go to the documentation of this file.
1 /**
2  * @file sftp_common.h
3  * @brief Definitions common to SFTP client and 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_COMMON_H
32 #define _SFTP_COMMON_H
33 
34 //Dependencies
35 #include "ssh/ssh.h"
36 #include "date_time.h"
37 
38 //Time constant
39 #define SFTP_180_DAYS (180 * 86400)
40 
41 //File flags
42 #define SSH_FXF_READ 0x00000001
43 #define SSH_FXF_WRITE 0x00000002
44 #define SSH_FXF_APPEND 0x00000004
45 #define SSH_FXF_CREAT 0x00000008
46 #define SSH_FXF_TRUNC 0x00000010
47 #define SSH_FXF_EXCL 0x00000020
48 
49 //Valid attribute flags
50 #define SSH_FILEXFER_ATTR_SIZE 0x00000001
51 #define SSH_FILEXFER_ATTR_UIDGID 0x00000002
52 #define SSH_FILEXFER_ATTR_PERMISSIONS 0x00000004
53 #define SSH_FILEXFER_ATTR_ACMODTIME 0x00000008
54 #define SSH_FILEXFER_ATTR_ACCESSTIME 0x00000008
55 #define SSH_FILEXFER_ATTR_CREATETIME 0x00000010
56 #define SSH_FILEXFER_ATTR_MODIFYTIME 0x00000020
57 #define SSH_FILEXFER_ATTR_ACL 0x00000040
58 #define SSH_FILEXFER_ATTR_OWNERGROUP 0x00000080
59 #define SSH_FILEXFER_ATTR_SUBSECOND_TIMES 0x00000100
60 #define SSH_FILEXFER_ATTR_BITS 0x00000200
61 #define SSH_FILEXFER_ATTR_ALLOCATION_SIZE 0x00000400
62 #define SSH_FILEXFER_ATTR_TEXT_HINT 0x00000800
63 #define SSH_FILEXFER_ATTR_MIME_TYPE 0x00001000
64 #define SSH_FILEXFER_ATTR_LINK_COUNT 0x00002000
65 #define SSH_FILEXFER_ATTR_UNTRANLATED_NAME 0x00004000
66 #define SSH_FILEXFER_ATTR_EXTENDED 0x80000000
67 
68 //File attribute bits
69 #define SSH_FILEXFER_ATTR_FLAGS_READONLY 0x00000001
70 #define SSH_FILEXFER_ATTR_FLAGS_SYSTEM 0x00000002
71 #define SSH_FILEXFER_ATTR_FLAGS_HIDDEN 0x00000004
72 #define SSH_FILEXFER_ATTR_FLAGS_CASE_INSENSITIVE 0x00000008
73 #define SSH_FILEXFER_ATTR_FLAGS_ARCHIVE 0x00000010
74 #define SSH_FILEXFER_ATTR_FLAGS_ENCRYPTED 0x00000020
75 #define SSH_FILEXFER_ATTR_FLAGS_COMPRESSED 0x00000040
76 #define SSH_FILEXFER_ATTR_FLAGS_SPARSE 0x00000080
77 #define SSH_FILEXFER_ATTR_FLAGS_APPEND_ONLY 0x00000100
78 #define SSH_FILEXFER_ATTR_FLAGS_IMMUTABLE 0x00000200
79 #define SSH_FILEXFER_ATTR_FLAGS_SYNC 0x00000400
80 #define SSH_FILEXFER_ATTR_FLAGS_TRANSLATION_ERR 0x00000800
81 
82 //File permissions
83 #define SFTP_MODE_IXOTH 0x0001
84 #define SFTP_MODE_IWOTH 0x0002
85 #define SFTP_MODE_IROTH 0x0004
86 #define SFTP_MODE_IRWXO 0x0007
87 #define SFTP_MODE_IXGRP 0x0008
88 #define SFTP_MODE_IWGRP 0x0010
89 #define SFTP_MODE_IRGRP 0x0020
90 #define SFTP_MODE_IRWXG 0x0038
91 #define SFTP_MODE_IXUSR 0x0040
92 #define SFTP_MODE_IWUSR 0x0080
93 #define SFTP_MODE_IRUSR 0x0100
94 #define SFTP_MODE_IRWXU 0x01C0
95 #define SFTP_MODE_ISVTX 0x0200
96 #define SFTP_MODE_ISGID 0x0400
97 #define SFTP_MODE_ISUID 0x0800
98 #define SFTP_MODE_IFMT 0xF000
99 #define SFTP_MODE_IFIFO 0x1000
100 #define SFTP_MODE_IFCHR 0x2000
101 #define SFTP_MODE_IFDIR 0x4000
102 #define SFTP_MODE_IFBLK 0x6000
103 #define SFTP_MODE_IFREG 0x8000
104 #define SFTP_MODE_IFLNK 0xA000
105 #define SFTP_MODE_IFSOCK 0xC000
106 
107 //C++ guard
108 #ifdef __cplusplus
109 extern "C" {
110 #endif
111 
112 
113 /**
114  * @brief SFTP protocol version
115  **/
116 
117 typedef enum
118 {
125  SFTP_VERSION_6 = 6
127 
128 
129 /**
130  * @brief SFTP packet types
131  **/
132 
133 typedef enum
134 {
163 
164 
165 /**
166  * @brief Status codes
167  **/
168 
169 typedef enum
170 {
181 
182 
183 /**
184  * @brief File types
185  **/
186 
187 typedef enum
188 {
200 
201 
202 //CC-RX, CodeWarrior or Win32 compiler?
203 #if defined(__CCRX__)
204  #pragma pack
205 #elif defined(__CWCC__) || defined(_WIN32)
206  #pragma pack(push, 1)
207 #endif
208 
209 
210 /**
211  * @brief SFTP packet header
212  **/
213 
215 {
216  uint32_t length; //0-3
217  uint8_t type; //4
218  uint8_t payload[]; //5
220 
221 
222 /**
223  * @brief SSH_FXP_DATA packet header
224  **/
225 
226 typedef __packed_struct
227 {
228  uint32_t id; //0-3
229  uint32_t dataLen; //4-7
230  uint8_t data[]; //8
232 
233 
234 //CC-RX, CodeWarrior or Win32 compiler?
235 #if defined(__CCRX__)
236  #pragma unpack
237 #elif defined(__CWCC__) || defined(_WIN32)
238  #pragma pack(pop)
239 #endif
240 
241 
242 /**
243  * @brief File attributes
244  **/
245 
246 typedef struct
247 {
248  uint32_t flags;
250  uint64_t size;
251  uint32_t uid;
252  uint32_t gid;
253  uint32_t permissions;
256  uint32_t bits;
257 } SftpFileAttrs;
258 
259 
260 /**
261  * @brief Name structure
262  **/
263 
264 typedef struct
265 {
269 } SftpName;
270 
271 
272 //SFTP related functions
274  uint8_t *p, size_t *written);
275 
277  const SftpFileAttrs *attributes, char_t *p, size_t *written);
278 
280  const SftpFileAttrs *attributes, uint8_t *p, size_t *written);
281 
283  size_t length, size_t *consumed);
284 
286  const uint8_t *data, size_t length, size_t *consumed);
287 
288 SftpFileType sftpConvertPermToFileType(uint32_t permissions);
290 
291 //C++ guard
292 #ifdef __cplusplus
293 }
294 #endif
295 
296 #endif
uint32_t bits
Definition: sftp_common.h:256
uint32_t permissions
Definition: sftp_common.h:253
@ SSH_FXP_ATTRS
Definition: sftp_common.h:159
Date and time management.
@ SSH_FX_PERMISSION_DENIED
Definition: sftp_common.h:174
SftpFileType
File types.
Definition: sftp_common.h:188
@ SSH_FXP_FSETSTAT
Definition: sftp_common.h:144
uint8_t payload[]
Definition: sftp_common.h:218
SftpPacketType
SFTP packet types.
Definition: sftp_common.h:134
uint64_t size
Definition: sftp_common.h:250
@ SSH_FXP_OPEN
Definition: sftp_common.h:137
@ SSH_FX_BAD_MESSAGE
Definition: sftp_common.h:176
error_t sftpFormatName(SftpVersion version, const SftpName *name, uint8_t *p, size_t *written)
Format name structure.
Definition: sftp_common.c:50
uint8_t p
Definition: ndp.h:300
@ SFTP_VERSION_4
Definition: sftp_common.h:123
@ SSH_FXP_SETSTAT
Definition: sftp_common.h:143
uint32_t flags
Definition: sftp_common.h:248
@ SSH_FXP_REALPATH
Definition: sftp_common.h:150
@ SSH_FXP_EXTENDED
Definition: sftp_common.h:160
@ SSH_FILEXFER_TYPE_FIFO
Definition: sftp_common.h:198
@ SFTP_VERSION_3
Definition: sftp_common.h:122
uint8_t attributes[]
Definition: radius.h:88
SshString longname
Definition: sftp_common.h:267
@ SSH_FXP_STATUS
Definition: sftp_common.h:155
SftpFileType type
Definition: sftp_common.h:249
char_t name[]
@ SSH_FILEXFER_TYPE_CHAR_DEVICE
Definition: sftp_common.h:196
@ SSH_FXP_READLINK
Definition: sftp_common.h:153
uint8_t version
Definition: coap_common.h:177
@ SSH_FXP_WRITE
Definition: sftp_common.h:140
@ SSH_FX_NO_CONNECTION
Definition: sftp_common.h:177
@ SSH_FXP_HANDLE
Definition: sftp_common.h:156
uint8_t type
Definition: sftp_common.h:217
@ SFTP_VERSION_6
Definition: sftp_common.h:125
@ SSH_FXP_READDIR
Definition: sftp_common.h:146
@ SSH_FXP_VERSION
Definition: sftp_common.h:136
SftpStatusCode
Status codes.
Definition: sftp_common.h:170
@ SSH_FILEXFER_TYPE_REGULAR
Definition: sftp_common.h:190
error_t sftpFormatAttributes(SftpVersion version, const SftpFileAttrs *attributes, uint8_t *p, size_t *written)
Format file attributes.
Definition: sftp_common.c:255
SftpFileType sftpConvertPermToFileType(uint32_t permissions)
Extract file type from permission bits.
Definition: sftp_common.c:656
error_t
Error codes.
Definition: error.h:43
@ SSH_FXP_RENAME
Definition: sftp_common.h:152
@ SSH_FXP_OPENDIR
Definition: sftp_common.h:145
SftpPacketHeader
Definition: sftp_common.h:219
@ SSH_FXP_RMDIR
Definition: sftp_common.h:149
SftpFxpDataHeader
Definition: sftp_common.h:231
@ SSH_FXP_DATA
Definition: sftp_common.h:157
@ SSH_FILEXFER_TYPE_SPECIAL
Definition: sftp_common.h:193
@ SSH_FX_FAILURE
Definition: sftp_common.h:175
char_t filename[]
Definition: tftp_common.h:93
@ SSH_FILEXFER_TYPE_UNKNOWN
Definition: sftp_common.h:194
@ SSH_FXP_SYMLINK
Definition: sftp_common.h:154
Date and time representation.
Definition: date_time.h:47
@ SSH_FXP_LSTAT
Definition: sftp_common.h:141
uint32_t gid
Definition: sftp_common.h:252
uint8_t length
Definition: tcp.h:368
@ SSH_FX_OK
Definition: sftp_common.h:171
@ SSH_FX_OP_UNSUPPORTED
Definition: sftp_common.h:179
@ SSH_FXP_NAME
Definition: sftp_common.h:158
@ SSH_FXP_READ
Definition: sftp_common.h:139
@ SSH_FXP_MKDIR
Definition: sftp_common.h:148
String.
Definition: ssh_types.h:56
uint32_t dataLen
Definition: sftp_common.h:229
@ SSH_FX_CONNECTION_LOST
Definition: sftp_common.h:178
DateTime mtime
Definition: sftp_common.h:255
@ SSH_FXP_REMOVE
Definition: sftp_common.h:147
@ SFTP_VERSION_2
Definition: sftp_common.h:121
@ SFTP_VERSION_1
Definition: sftp_common.h:120
error_t sftpParseAttributes(SftpVersion version, SftpFileAttrs *attributes, const uint8_t *data, size_t length, size_t *consumed)
Parse file attributes.
Definition: sftp_common.c:454
char char_t
Definition: compiler_port.h:48
Name structure.
Definition: sftp_common.h:265
@ SSH_FILEXFER_TYPE_SOCKET
Definition: sftp_common.h:195
error_t sftpFormatLongFilename(const SshString *filename, const SftpFileAttrs *attributes, char_t *p, size_t *written)
Format long file name.
Definition: sftp_common.c:111
typedef __packed_struct
SFTP packet header.
Definition: sftp_common.h:215
@ SSH_FXP_CLOSE
Definition: sftp_common.h:138
@ SSH_FXP_FSTAT
Definition: sftp_common.h:142
@ SSH_FILEXFER_TYPE_INVALID
Definition: sftp_common.h:189
error_t sftpParseName(SftpVersion version, SftpName *name, const uint8_t *data, size_t length, size_t *consumed)
Parse name structure.
Definition: sftp_common.c:370
@ SSH_FILEXFER_TYPE_DIRECTORY
Definition: sftp_common.h:191
@ SSH_FXP_INIT
Definition: sftp_common.h:135
@ SFTP_VERSION_5
Definition: sftp_common.h:124
uint8_t data[]
Definition: sftp_common.h:230
SftpVersion
SFTP protocol version.
Definition: sftp_common.h:118
@ SSH_FILEXFER_TYPE_BLOCK_DEVICE
Definition: sftp_common.h:197
@ SSH_FILEXFER_TYPE_SYMLINK
Definition: sftp_common.h:192
DateTime atime
Definition: sftp_common.h:254
uint32_t sftpConvertFileTypeToPerm(SftpFileType type)
Convert file type to permission bits.
Definition: sftp_common.c:708
@ SSH_FXP_EXTENDED_REPLY
Definition: sftp_common.h:161
File attributes.
Definition: sftp_common.h:247
@ SFTP_VERSION_0
Definition: sftp_common.h:119
Secure Shell (SSH)
SshString filename
Definition: sftp_common.h:266
uint32_t uid
Definition: sftp_common.h:251
@ SSH_FXP_STAT
Definition: sftp_common.h:151
SftpFileAttrs attributes
Definition: sftp_common.h:268
@ SSH_FX_EOF
Definition: sftp_common.h:172
@ SSH_FX_NO_SUCH_FILE
Definition: sftp_common.h:173