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-2022 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.2.0
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 //CodeWarrior or Win32 compiler?
203 #if defined(__CWCC__) || defined(_WIN32)
204  #pragma pack(push, 1)
205 #endif
206 
207 
208 /**
209  * @brief SFTP packet header
210  **/
211 
212 typedef __start_packed struct
213 {
214  uint32_t length; //0-3
215  uint8_t type; //4
216  uint8_t payload[]; //5
218 
219 
220 /**
221  * @brief SSH_FXP_DATA packet header
222  **/
223 
224 typedef __start_packed struct
225 {
226  uint32_t id; //0-3
227  uint32_t dataLen; //4-7
228  uint8_t data[]; //8
230 
231 
232 //CodeWarrior or Win32 compiler?
233 #if defined(__CWCC__) || defined(_WIN32)
234  #pragma pack(pop)
235 #endif
236 
237 
238 /**
239  * @brief File attributes
240  **/
241 
242 typedef struct
243 {
244  uint32_t flags;
246  uint64_t size;
247  uint32_t uid;
248  uint32_t gid;
249  uint32_t permissions;
252  uint32_t bits;
253 } SftpFileAttrs;
254 
255 
256 /**
257  * @brief Name structure
258  **/
259 
260 typedef struct
261 {
265 } SftpName;
266 
267 
268 //SFTP related functions
270  uint8_t *p, size_t *written);
271 
273  const SftpFileAttrs *attributes, char_t *p, size_t *written);
274 
276  const SftpFileAttrs *attributes, uint8_t *p, size_t *written);
277 
279  size_t length, size_t *consumed);
280 
282  const uint8_t *data, size_t length, size_t *consumed);
283 
284 SftpFileType sftpConvertPermToFileType(uint32_t permissions);
286 
287 //C++ guard
288 #ifdef __cplusplus
289 }
290 #endif
291 
292 #endif
uint32_t bits
Definition: sftp_common.h:252
uint32_t permissions
Definition: sftp_common.h:249
@ 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:216
SftpPacketType
SFTP packet types.
Definition: sftp_common.h:134
uint64_t size
Definition: sftp_common.h:246
@ 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:298
@ SFTP_VERSION_4
Definition: sftp_common.h:123
@ SSH_FXP_SETSTAT
Definition: sftp_common.h:143
uint32_t flags
Definition: sftp_common.h:244
@ 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
SshString longname
Definition: sftp_common.h:263
@ SSH_FXP_STATUS
Definition: sftp_common.h:155
SftpFileType type
Definition: sftp_common.h:245
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:175
@ 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:215
@ 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:247
SftpFileType sftpConvertPermToFileType(uint32_t permissions)
Extract file type from permission bits.
Definition: sftp_common.c:648
char_t filename[]
Definition: tftp_common.h:91
error_t
Error codes.
Definition: error.h:43
@ SSH_FXP_RENAME
Definition: sftp_common.h:152
@ SSH_FXP_OPENDIR
Definition: sftp_common.h:145
@ SSH_FXP_RMDIR
Definition: sftp_common.h:149
@ SSH_FXP_DATA
Definition: sftp_common.h:157
uint32_t id
Definition: sftp_common.h:226
@ SSH_FILEXFER_TYPE_SPECIAL
Definition: sftp_common.h:193
@ SSH_FX_FAILURE
Definition: sftp_common.h:175
__start_packed struct _Ipv4Header __end_packed
@ 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:248
@ 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:227
@ SSH_FX_CONNECTION_LOST
Definition: sftp_common.h:178
DateTime mtime
Definition: sftp_common.h:251
@ 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:446
char char_t
Definition: compiler_port.h:43
Name structure.
Definition: sftp_common.h:261
@ 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
@ 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:362
@ 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:228
SftpVersion
SFTP protocol version.
Definition: sftp_common.h:118
__start_packed struct @1 SftpFxpDataHeader
SSH_FXP_DATA packet header.
@ 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:250
uint32_t sftpConvertFileTypeToPerm(SftpFileType type)
Convert file type to permission bits.
Definition: sftp_common.c:700
@ SSH_FXP_EXTENDED_REPLY
Definition: sftp_common.h:161
File attributes.
Definition: sftp_common.h:243
@ SFTP_VERSION_0
Definition: sftp_common.h:119
Secure Shell (SSH)
SshString filename
Definition: sftp_common.h:262
uint32_t uid
Definition: sftp_common.h:247
@ SSH_FXP_STAT
Definition: sftp_common.h:151
uint32_t length
Definition: sftp_common.h:214
SftpFileAttrs attributes
Definition: sftp_common.h:264
__start_packed struct @0 SftpPacketHeader
SFTP packet header.
@ SSH_FX_EOF
Definition: sftp_common.h:172
@ SSH_FX_NO_SUCH_FILE
Definition: sftp_common.h:173