kfsw-services/src/ftp/ftp_protocol.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <stdbool.h> | ||
| 3 | #include <stddef.h> | ||
| 4 | #include <stdint.h> | ||
| 5 | #include <string.h> | ||
| 6 | |||
| 7 | #include <zephyr/fs/fs.h> | ||
| 8 | #include <zephyr/sys/byteorder.h> | ||
| 9 | #include <zephyr/sys/util.h> | ||
| 10 | |||
| 11 | #include <kfsw/platform/storage.h> | ||
| 12 | |||
| 13 | #include "ftp_internal.h" | ||
| 14 | |||
| 15 | 16 | int kfsw_ftp_protocol_encode(uint8_t *buffer, size_t capacity, | |
| 16 | const struct kfsw_ftp_message *message, size_t *encoded_size) | ||
| 17 | { | ||
| 18 | size_t size; | ||
| 19 | |||
| 20 | 16 | if ((buffer == NULL) || (message == NULL) || (encoded_size == NULL) || | |
| 21 | 16 | (message->opcode < KFSW_FTP_OP_MKDIR_REQUEST) || | |
| 22 | 16 | (message->opcode > KFSW_FTP_OP_REMOVE_RESPONSE) || | |
| 23 | 12 | (message->path_size > KFSW_FTP_MAX_PATH_SIZE) || | |
| 24 | 12 | (message->data_size > KFSW_FTP_CHUNK_SIZE) || | |
| 25 | 12 | ((message->path_size != 0U) && (message->path == NULL)) || | |
| 26 | 12 | ((message->data_size != 0U) && (message->data == NULL))) { | |
| 27 | 4 | return -EINVAL; | |
| 28 | } | ||
| 29 | |||
| 30 | 12 | size = KFSW_FTP_PROTOCOL_HEADER_SIZE + message->path_size + message->data_size; | |
| 31 | 12 | if (size > capacity) { | |
| 32 | ✗ | return -EMSGSIZE; | |
| 33 | } | ||
| 34 | |||
| 35 | 12 | buffer[0] = KFSW_FTP_PROTOCOL_VERSION; | |
| 36 | 12 | buffer[1] = message->opcode; | |
| 37 | 12 | buffer[2] = message->flags; | |
| 38 | 12 | buffer[3] = message->status; | |
| 39 | 12 | sys_put_be32(message->request_id, &buffer[4]); | |
| 40 | 12 | sys_put_be32(message->offset, &buffer[8]); | |
| 41 | 12 | sys_put_be32(message->total_size, &buffer[12]); | |
| 42 | 12 | sys_put_be32(message->crc32, &buffer[16]); | |
| 43 | 12 | sys_put_be16(message->path_size, &buffer[20]); | |
| 44 | 12 | sys_put_be16(message->data_size, &buffer[22]); | |
| 45 | 12 | if (message->path_size != 0U) { | |
| 46 | ✗ | memcpy(&buffer[KFSW_FTP_PROTOCOL_HEADER_SIZE], message->path, message->path_size); | |
| 47 | } | ||
| 48 | 12 | if (message->data_size != 0U) { | |
| 49 | 4 | memcpy(&buffer[KFSW_FTP_PROTOCOL_HEADER_SIZE + message->path_size], message->data, | |
| 50 | 4 | message->data_size); | |
| 51 | } | ||
| 52 | 12 | *encoded_size = size; | |
| 53 | 12 | return 0; | |
| 54 | } | ||
| 55 | |||
| 56 | 36 | int kfsw_ftp_protocol_decode(const uint8_t *buffer, size_t size, struct kfsw_ftp_message *message) | |
| 57 | { | ||
| 58 | size_t expected_size; | ||
| 59 | |||
| 60 | 36 | if ((buffer == NULL) || (message == NULL) || (size < KFSW_FTP_PROTOCOL_HEADER_SIZE)) { | |
| 61 | 4 | return -EMSGSIZE; | |
| 62 | } | ||
| 63 | 32 | if (buffer[0] != KFSW_FTP_PROTOCOL_VERSION) { | |
| 64 | 4 | return -EPROTONOSUPPORT; | |
| 65 | } | ||
| 66 | 28 | if ((buffer[1] < KFSW_FTP_OP_MKDIR_REQUEST) || (buffer[1] > KFSW_FTP_OP_REMOVE_RESPONSE)) { | |
| 67 | 4 | return -ENOTSUP; | |
| 68 | } | ||
| 69 | |||
| 70 | 24 | memset(message, 0, sizeof(*message)); | |
| 71 | 24 | message->opcode = buffer[1]; | |
| 72 | 24 | message->flags = buffer[2]; | |
| 73 | 24 | message->status = buffer[3]; | |
| 74 | 24 | message->request_id = sys_get_be32(&buffer[4]); | |
| 75 | 24 | message->offset = sys_get_be32(&buffer[8]); | |
| 76 | 24 | message->total_size = sys_get_be32(&buffer[12]); | |
| 77 | 24 | message->crc32 = sys_get_be32(&buffer[16]); | |
| 78 | 24 | message->path_size = sys_get_be16(&buffer[20]); | |
| 79 | 24 | message->data_size = sys_get_be16(&buffer[22]); | |
| 80 | 24 | if ((message->path_size > KFSW_FTP_MAX_PATH_SIZE) || | |
| 81 | 20 | (message->data_size > KFSW_FTP_CHUNK_SIZE)) { | |
| 82 | 4 | return -EMSGSIZE; | |
| 83 | } | ||
| 84 | |||
| 85 | 20 | expected_size = KFSW_FTP_PROTOCOL_HEADER_SIZE + message->path_size + message->data_size; | |
| 86 | 20 | if (expected_size != size) { | |
| 87 | 4 | return -EMSGSIZE; | |
| 88 | } | ||
| 89 | 16 | message->path = &buffer[KFSW_FTP_PROTOCOL_HEADER_SIZE]; | |
| 90 | 16 | message->data = &buffer[KFSW_FTP_PROTOCOL_HEADER_SIZE + message->path_size]; | |
| 91 | 20 | if ((message->path_size != 0U) && | |
| 92 | 4 | (memchr(message->path, '\0', message->path_size) != NULL)) { | |
| 93 | 4 | return -EBADMSG; | |
| 94 | } | ||
| 95 | 12 | return 0; | |
| 96 | } | ||
| 97 | |||
| 98 | 96 | int kfsw_ftp_copy_message_path(const struct kfsw_ftp_message *message, char *path, size_t path_size) | |
| 99 | { | ||
| 100 | 96 | if ((message == NULL) || (path == NULL) || (message->path_size == 0U) || | |
| 101 | 86 | (message->path_size >= path_size)) { | |
| 102 | 10 | return -EINVAL; | |
| 103 | } | ||
| 104 | 86 | memcpy(path, message->path, message->path_size); | |
| 105 | 86 | path[message->path_size] = '\0'; | |
| 106 | 86 | return 0; | |
| 107 | } | ||
| 108 | |||
| 109 | static const struct { | ||
| 110 | const char *prefix; | ||
| 111 | const char *root; | ||
| 112 | bool writable; | ||
| 113 | } extra_roots[] = { | ||
| 114 | {KFSW_FTP_HK_PATH, KFSW_FTP_READONLY_ROOT, false}, | ||
| 115 | {KFSW_FTP_BOOT_PATH, "/kfsw/boot", false}, | ||
| 116 | #if CONFIG_KFSW_STORAGE_TMP | ||
| 117 | {KFSW_FTP_TMP_PATH, KFSW_STORAGE_TMP_MOUNT_POINT, true}, | ||
| 118 | #endif | ||
| 119 | }; | ||
| 120 | |||
| 121 | 644 | static int extra_root(const char *virtual_path) | |
| 122 | { | ||
| 123 | 644 | const char *relative = virtual_path; | |
| 124 | |||
| 125 | 644 | if (virtual_path == NULL) { | |
| 126 | 4 | return -1; | |
| 127 | } | ||
| 128 | 640 | if (relative[0] == '/') { | |
| 129 | 498 | relative++; | |
| 130 | } | ||
| 131 | 2062 | for (size_t i = 0U; i < ARRAY_SIZE(extra_roots); i++) { | |
| 132 | 1516 | size_t size = strlen(extra_roots[i].prefix); | |
| 133 | |||
| 134 | 1516 | if ((strncmp(relative, extra_roots[i].prefix, size) == 0) && | |
| 135 | 102 | ((relative[size] == '\0') || (relative[size] == '/'))) { | |
| 136 | 94 | return (int)i; | |
| 137 | } | ||
| 138 | } | ||
| 139 | 546 | return -1; | |
| 140 | } | ||
| 141 | |||
| 142 | 168 | bool kfsw_ftp_path_is_read_only(const char *virtual_path) | |
| 143 | { | ||
| 144 | 168 | const int selected = extra_root(virtual_path); | |
| 145 | |||
| 146 | 168 | return (selected >= 0) && !extra_roots[selected].writable; | |
| 147 | } | ||
| 148 | |||
| 149 | 10 | bool kfsw_ftp_list_roots(kfsw_ftp_list_visitor_t visitor, void *context) | |
| 150 | { | ||
| 151 | 38 | for (size_t i = 0U; i < ARRAY_SIZE(extra_roots); i++) { | |
| 152 | 28 | const struct kfsw_ftp_entry entry = { | |
| 153 | 28 | .name = extra_roots[i].prefix, | |
| 154 | .type = KFSW_FTP_ENTRY_DIRECTORY, | ||
| 155 | }; | ||
| 156 | struct fs_dirent mounted; | ||
| 157 | |||
| 158 | 28 | if (fs_stat(extra_roots[i].root, &mounted) != 0) { | |
| 159 | 12 | continue; | |
| 160 | } | ||
| 161 | 16 | if (!visitor(&entry, context)) { | |
| 162 | ✗ | return false; | |
| 163 | } | ||
| 164 | } | ||
| 165 | 10 | return true; | |
| 166 | } | ||
| 167 | |||
| 168 | 118 | int kfsw_ftp_resolve_write_path(const char *virtual_path, char *resolved, size_t resolved_size) | |
| 169 | { | ||
| 170 | /* Checked here so shell and remote requests are both refused. */ | ||
| 171 | 118 | if (kfsw_ftp_path_is_read_only(virtual_path)) { | |
| 172 | 20 | return -EROFS; | |
| 173 | } | ||
| 174 | 98 | return kfsw_ftp_resolve_path(virtual_path, false, resolved, resolved_size); | |
| 175 | } | ||
| 176 | |||
| 177 | 484 | int kfsw_ftp_resolve_path(const char *virtual_path, bool allow_root, char *resolved, | |
| 178 | size_t resolved_size) | ||
| 179 | { | ||
| 180 | const char *relative; | ||
| 181 | 484 | const char *root = KFSW_FTP_ROOT_PATH; | |
| 182 | 484 | size_t component_size = 0U; | |
| 183 | size_t relative_size; | ||
| 184 | 484 | size_t root_size = sizeof(KFSW_FTP_ROOT_PATH) - 1U; | |
| 185 | int selected; | ||
| 186 | |||
| 187 | 484 | if ((virtual_path == NULL) || (resolved == NULL)) { | |
| 188 | 4 | return -EINVAL; | |
| 189 | } | ||
| 190 | 480 | relative_size = strnlen(virtual_path, KFSW_FTP_MAX_PATH_SIZE + 1U); | |
| 191 | 480 | if (relative_size > KFSW_FTP_MAX_PATH_SIZE) { | |
| 192 | 4 | return -ENAMETOOLONG; | |
| 193 | } | ||
| 194 | 476 | relative = virtual_path; | |
| 195 | 476 | if ((relative_size != 0U) && (relative[0] == '/')) { | |
| 196 | 368 | relative++; | |
| 197 | 368 | relative_size--; | |
| 198 | } | ||
| 199 | 476 | selected = extra_root(virtual_path); | |
| 200 | 476 | if (selected >= 0) { | |
| 201 | 40 | const size_t selector = strlen(extra_roots[selected].prefix); | |
| 202 | |||
| 203 | 40 | root = extra_roots[selected].root; | |
| 204 | 40 | root_size = strlen(root); | |
| 205 | 40 | relative += selector; | |
| 206 | 40 | relative_size -= selector; | |
| 207 | 40 | if ((relative_size != 0U) && (relative[0] == '/')) { | |
| 208 | 32 | relative++; | |
| 209 | 32 | relative_size--; | |
| 210 | } | ||
| 211 | } | ||
| 212 | 476 | if (relative_size == 0U) { | |
| 213 | 52 | if (!allow_root || (resolved_size <= root_size)) { | |
| 214 | 30 | return -EINVAL; | |
| 215 | } | ||
| 216 | 22 | memcpy(resolved, root, root_size + 1U); | |
| 217 | 22 | return 0; | |
| 218 | } | ||
| 219 | 424 | if (resolved_size <= root_size + 1U + relative_size) { | |
| 220 | ✗ | return -ENAMETOOLONG; | |
| 221 | } | ||
| 222 | |||
| 223 | 4306 | for (size_t index = 0U; index <= relative_size; index++) { | |
| 224 | 3940 | const bool at_end = index == relative_size; | |
| 225 | 3940 | const unsigned char character = at_end ? '/' : (unsigned char)relative[index]; | |
| 226 | |||
| 227 | 3940 | if (character == '/') { | |
| 228 | 464 | const size_t component_start = index - component_size; | |
| 229 | |||
| 230 | 464 | if ((component_size == 0U) || | |
| 231 | 456 | ((component_size == 1U) && (relative[component_start] == '.')) || | |
| 232 | 38 | ((component_size == 2U) && (relative[component_start] == '.') && | |
| 233 | 38 | (relative[component_start + 1U] == '.'))) { | |
| 234 | 50 | return -EINVAL; | |
| 235 | } | ||
| 236 | 414 | component_size = 0U; | |
| 237 | 414 | continue; | |
| 238 | } | ||
| 239 | 3476 | if ((character == '\\') || (character < 0x20U) || (character == 0x7fU)) { | |
| 240 | 8 | return -EINVAL; | |
| 241 | } | ||
| 242 | 3468 | component_size++; | |
| 243 | } | ||
| 244 | |||
| 245 | 366 | memcpy(resolved, root, root_size); | |
| 246 | 366 | resolved[root_size] = '/'; | |
| 247 | 366 | memcpy(&resolved[root_size + 1U], relative, relative_size); | |
| 248 | 366 | resolved[root_size + 1U + relative_size] = '\0'; | |
| 249 | 366 | return 0; | |
| 250 | } | ||
| 251 | |||
| 252 | 86 | int kfsw_ftp_validate_path(const char *path, bool allow_root) | |
| 253 | { | ||
| 254 | char resolved[KFSW_FTP_FULL_PATH_SIZE]; | ||
| 255 | |||
| 256 | 86 | return kfsw_ftp_resolve_path(path, allow_root, resolved, sizeof(resolved)); | |
| 257 | } | ||
| 258 | |||
| 259 | 104 | int kfsw_ftp_wire_status_to_errno(uint8_t status) | |
| 260 | { | ||
| 261 | 104 | switch (status) { | |
| 262 | 70 | case KFSW_FTP_STATUS_OK: | |
| 263 | 70 | return 0; | |
| 264 | ✗ | case KFSW_FTP_STATUS_INVALID_REQUEST: | |
| 265 | ✗ | return -EBADMSG; | |
| 266 | ✗ | case KFSW_FTP_STATUS_INVALID_PATH: | |
| 267 | ✗ | return -EINVAL; | |
| 268 | 6 | case KFSW_FTP_STATUS_NOT_FOUND: | |
| 269 | 6 | return -ENOENT; | |
| 270 | 6 | case KFSW_FTP_STATUS_ALREADY_EXISTS: | |
| 271 | 6 | return -EEXIST; | |
| 272 | 4 | case KFSW_FTP_STATUS_NO_SPACE: | |
| 273 | 4 | return -ENOSPC; | |
| 274 | ✗ | case KFSW_FTP_STATUS_INTEGRITY_ERROR: | |
| 275 | ✗ | return -EILSEQ; | |
| 276 | ✗ | case KFSW_FTP_STATUS_BUSY: | |
| 277 | ✗ | return -EBUSY; | |
| 278 | ✗ | case KFSW_FTP_STATUS_UNSUPPORTED: | |
| 279 | ✗ | return -ENOTSUP; | |
| 280 | ✗ | case KFSW_FTP_STATUS_TIMEOUT: | |
| 281 | ✗ | return -ETIMEDOUT; | |
| 282 | ✗ | case KFSW_FTP_STATUS_CONNECTION_ERROR: | |
| 283 | ✗ | return -ECONNREFUSED; | |
| 284 | ✗ | case KFSW_FTP_STATUS_NOT_DIRECTORY: | |
| 285 | ✗ | return -ENOTDIR; | |
| 286 | 6 | case KFSW_FTP_STATUS_READ_ONLY: | |
| 287 | 6 | return -EROFS; | |
| 288 | 2 | case KFSW_FTP_STATUS_NOT_EMPTY: | |
| 289 | 2 | return -ENOTEMPTY; | |
| 290 | 10 | case KFSW_FTP_STATUS_IO_ERROR: | |
| 291 | default: | ||
| 292 | 10 | return -EIO; | |
| 293 | } | ||
| 294 | } | ||
| 295 | |||
| 296 | 68 | uint8_t kfsw_ftp_errno_to_wire_status(int error) | |
| 297 | { | ||
| 298 | 68 | const int positive_error = (error < 0) ? -error : error; | |
| 299 | |||
| 300 | 68 | switch (positive_error) { | |
| 301 | 22 | case 0: | |
| 302 | 22 | return KFSW_FTP_STATUS_OK; | |
| 303 | 12 | case EINVAL: | |
| 304 | case ENAMETOOLONG: | ||
| 305 | 12 | return KFSW_FTP_STATUS_INVALID_PATH; | |
| 306 | 8 | case ENOENT: | |
| 307 | 8 | return KFSW_FTP_STATUS_NOT_FOUND; | |
| 308 | 2 | case EEXIST: | |
| 309 | 2 | return KFSW_FTP_STATUS_ALREADY_EXISTS; | |
| 310 | 18 | case EROFS: | |
| 311 | 18 | return KFSW_FTP_STATUS_READ_ONLY; | |
| 312 | 2 | case ENOTEMPTY: | |
| 313 | 2 | return KFSW_FTP_STATUS_NOT_EMPTY; | |
| 314 | ✗ | case ENOSPC: | |
| 315 | ✗ | return KFSW_FTP_STATUS_NO_SPACE; | |
| 316 | ✗ | case EILSEQ: | |
| 317 | case EBADMSG: | ||
| 318 | ✗ | return KFSW_FTP_STATUS_INTEGRITY_ERROR; | |
| 319 | ✗ | case EBUSY: | |
| 320 | ✗ | return KFSW_FTP_STATUS_BUSY; | |
| 321 | ✗ | case ENOTSUP: | |
| 322 | case EPROTONOSUPPORT: | ||
| 323 | ✗ | return KFSW_FTP_STATUS_UNSUPPORTED; | |
| 324 | 4 | case ETIMEDOUT: | |
| 325 | 4 | return KFSW_FTP_STATUS_TIMEOUT; | |
| 326 | ✗ | case ECONNREFUSED: | |
| 327 | case ECONNRESET: | ||
| 328 | ✗ | return KFSW_FTP_STATUS_CONNECTION_ERROR; | |
| 329 | ✗ | case EISDIR: | |
| 330 | case ENOTDIR: | ||
| 331 | ✗ | return KFSW_FTP_STATUS_NOT_DIRECTORY; | |
| 332 | ✗ | default: | |
| 333 | ✗ | return KFSW_FTP_STATUS_IO_ERROR; | |
| 334 | } | ||
| 335 | } | ||
| 336 |