-
Notifications
You must be signed in to change notification settings - Fork 89
/
wolfsftp.c
9110 lines (7816 loc) · 277 KB
/
wolfsftp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* wolfsftp.c
*
* Copyright (C) 2014-2024 wolfSSL Inc.
*
* This file is part of wolfSSH.
*
* wolfSSH is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wolfSSH. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _CRT_SECURE_NO_WARNINGS
#include <wolfssh/wolfsftp.h>
#ifdef WOLFSSH_SFTP
#include <wolfssh/internal.h>
#include <wolfssh/log.h>
#ifdef NO_INLINE
#include <wolfssh/misc.h>
#else
#define WOLFSSH_MISC_INCLUDED
#if defined(WOLFSSL_NUCLEUS)
#include "src/wolfssh_misc.c"
#else
#include "src/misc.c"
#endif
#endif
/* for XGMTIME if defined */
#include <wolfssl/wolfcrypt/wc_port.h>
/* enum for bit field with an ID of each of the state structures */
enum WS_SFTP_STATE_ID {
STATE_ID_ALL = 0, /* default to select all */
STATE_ID_LSTAT = 0x01,
STATE_ID_OPEN = 0x02,
STATE_ID_GET = 0x04,
STATE_ID_SEND_READ = 0x08,
STATE_ID_CLOSE = 0x10,
STATE_ID_GET_HANDLE = 0x20,
STATE_ID_NAME = 0x40,
STATE_ID_SEND = 0x80,
STATE_ID_LS = 0x100,
STATE_ID_READDIR = 0x200,
STATE_ID_PUT = 0x0400,
STATE_ID_SEND_WRITE = 0x0800,
STATE_ID_RM = 0x1000,
STATE_ID_MKDIR = 0x2000,
STATE_ID_RMDIR = 0x4000,
STATE_ID_RENAME = 0x8000,
STATE_ID_RECV = 0x10000,
STATE_ID_CHMOD = 0x20000,
STATE_ID_SETATR = 0x40000,
STATE_ID_RECV_INIT = 0x80000,
};
enum WS_SFTP_CHMOD_STATE_ID {
STATE_CHMOD_GET,
STATE_CHMOD_SEND
};
enum WS_SFTP_SETATR_STATE_ID {
STATE_SET_ATR_INIT,
STATE_SET_ATR_SEND,
STATE_SET_ATR_GET,
STATE_SET_ATR_STATUS
};
enum WS_SFTP_RMDIR_STATE_ID {
STATE_RMDIR_SEND,
STATE_RMDIR_GET,
STATE_RMDIR_STATUS
};
enum WS_SFTP_MKDIR_STATE_ID {
STATE_MKDIR_SEND,
STATE_MKDIR_GET,
STATE_MKDIR_STATUS
};
enum WS_SFTP_RM_STATE_ID {
STATE_RM_LSTAT,
STATE_RM_SEND,
STATE_RM_GET,
STATE_RM_DOSTATUS
};
enum WS_SFTP_NAME_STATE_ID {
SFTP_NAME_GETHEADER_PACKET,
SFTP_NAME_DO_STATUS,
SFTP_NAME_GET_PACKET
};
enum WS_SFTP_REAL_STATE_ID {
SFTP_REAL_SEND_PACKET,
SFTP_REAL_GET_PACKET
};
enum WS_SFTP_RECV_STATE_ID {
STATE_RECV_READ,
STATE_RECV_DO,
STATE_RECV_SEND
};
enum WS_SFTP_READDIR_STATE_ID {
STATE_READDIR_SEND,
STATE_READDIR_NAME
};
enum WS_SFTP_SEND_STATE_ID {
SFTP_BUILD_PACKET,
SFTP_SEND_PACKET
};
enum WS_SFTP_LS_STATE_ID {
STATE_LS_REALPATH,
STATE_LS_OPENDIR,
STATE_LS_GETHANDLE,
STATE_LS_READDIR,
STATE_LS_CLOSE
};
enum WS_SFTP_LSTAT_STATE_ID {
STATE_LSTAT_INIT,
STATE_LSTAT_SEND_TYPE_REQ,
STATE_LSTAT_GET_HEADER,
STATE_LSTAT_CHECK_REQ_ID,
STATE_LSTAT_PARSE_REPLY,
STATE_LSTAT_CLEANUP
};
/* This structure is to help with nonblocking and keeping track of state.
* If adding any read/writes use the wolfSSH_SFTP_buffer_read/send functions */
typedef struct WS_SFTP_BUFFER {
byte* data;
word32 sz;
word32 idx;
} WS_SFTP_BUFFER;
typedef struct WS_SFTP_CHMOD_STATE {
enum WS_SFTP_CHMOD_STATE_ID state;
WS_SFTP_FILEATRB atr;
} WS_SFTP_CHMOD_STATE;
typedef struct WS_SFTP_SETATR_STATE {
enum WS_SFTP_SETATR_STATE_ID state;
WS_SFTP_BUFFER buffer;
word32 reqId;
} WS_SFTP_SETATR_STATE;
typedef struct WS_SFTP_LSTAT_STATE {
enum WS_SFTP_LSTAT_STATE_ID state;
word32 reqId;
word32 dirSz;
WS_SFTP_BUFFER buffer;
byte type;
} WS_SFTP_LSTAT_STATE;
enum WS_SFTP_OPEN_STATE_ID {
STATE_OPEN_INIT,
STATE_OPEN_SEND,
STATE_OPEN_GETHANDLE,
STATE_OPEN_CLEANUP
};
typedef struct WS_SFTP_OPEN_STATE {
enum WS_SFTP_OPEN_STATE_ID state;
WS_SFTP_BUFFER buffer;
} WS_SFTP_OPEN_STATE;
/* similar to open state, could refactor */
typedef struct WS_SFTP_NAME_STATE {
enum WS_SFTP_NAME_STATE_ID state;
WS_SFTP_BUFFER buffer;
} WS_SFTP_NAME_STATE;
/* similar to open state, could refactor */
typedef struct WS_SFTP_SEND_STATE {
enum WS_SFTP_SEND_STATE_ID state;
WS_SFTP_BUFFER buffer;
} WS_SFTP_SEND_STATE;
/* similar to open state, could refactor */
typedef struct WS_SFTP_READDIR_STATE {
enum WS_SFTP_READDIR_STATE_ID state;
WS_SFTP_BUFFER buffer;
} WS_SFTP_READDIR_STATE;
/* similar to open state, could refactor */
typedef struct WS_SFTP_RM_STATE {
enum WS_SFTP_RM_STATE_ID state;
WS_SFTP_BUFFER buffer;
word32 reqId;
} WS_SFTP_RM_STATE;
/* similar to open state, could refactor */
typedef struct WS_SFTP_MKDIR_STATE {
enum WS_SFTP_MKDIR_STATE_ID state;
WS_SFTP_BUFFER buffer;
word32 reqId;
} WS_SFTP_MKDIR_STATE;
/* similar to open state, could refactor */
typedef struct WS_SFTP_RMDIR_STATE {
enum WS_SFTP_RMDIR_STATE_ID state;
WS_SFTP_BUFFER buffer;
word32 reqId;
} WS_SFTP_RMDIR_STATE;
typedef struct WS_SFTP_RECV_STATE {
enum WS_SFTP_RECV_STATE_ID state;
WS_SFTP_BUFFER buffer;
byte type;
byte toSend;
int reqId;
} WS_SFTP_RECV_STATE;
typedef struct WS_SFTP_LS_STATE {
enum WS_SFTP_LS_STATE_ID state;
byte handle[WOLFSSH_MAX_HANDLE];
int sz;
WS_SFTPNAME* name;
} WS_SFTP_LS_STATE;
typedef struct WS_SFTP_RECV_INIT_STATE {
WS_SFTP_BUFFER buffer;
word32 extSz;
} WS_SFTP_RECV_INIT_STATE;
enum WS_SFTP_GET_STATE_ID {
STATE_GET_INIT,
STATE_GET_LSTAT,
STATE_GET_OPEN_REMOTE,
STATE_GET_LOOKUP_OFFSET,
STATE_GET_OPEN_LOCAL,
STATE_GET_READ,
STATE_GET_CLOSE_LOCAL,
STATE_GET_CLOSE_REMOTE,
STATE_GET_CLEANUP
};
typedef struct WS_SFTP_GET_STATE {
enum WS_SFTP_GET_STATE_ID state;
WS_SFTP_FILEATRB attrib;
#ifndef USE_WINDOWS_API
WFILE* fl;
#ifdef WOLFSSL_NUCLEUS
int fd; /* file descriptor, in the case of Nucleus fp points to fd */
#endif
#else
HANDLE fileHandle;
OVERLAPPED offset;
#endif
word32 gOfst[2];
word32 handleSz;
byte handle[WOLFSSH_MAX_HANDLE];
byte r[WOLFSSH_MAX_SFTP_RW];
} WS_SFTP_GET_STATE;
enum WS_SFTP_PUT_STATE_ID {
STATE_PUT_INIT,
STATE_PUT_LOOKUP_OFFSET,
STATE_PUT_OPEN_REMOTE,
STATE_PUT_OPEN_LOCAL,
STATE_PUT_WRITE,
STATE_PUT_CLOSE_LOCAL,
STATE_PUT_CLOSE_REMOTE,
STATE_PUT_CLEANUP
};
typedef struct WS_SFTP_PUT_STATE {
enum WS_SFTP_PUT_STATE_ID state;
#ifndef USE_WINDOWS_API
WFILE* fl;
#ifdef WOLFSSL_NUCLEUS
int fd; /* file descriptor, in the case of Nucleus fp points to fd */
#endif
#else
HANDLE fileHandle;
OVERLAPPED offset;
#endif
word32 pOfst[2];
word32 handleSz;
int rSz;
byte handle[WOLFSSH_MAX_HANDLE];
byte r[WOLFSSH_MAX_SFTP_RW];
} WS_SFTP_PUT_STATE;
enum WS_SFTP_SEND_READ_STATE_ID {
STATE_SEND_READ_INIT,
STATE_SEND_READ_SEND_REQ,
STATE_SEND_READ_GET_HEADER,
STATE_SEND_READ_CHECK_REQ_ID,
STATE_SEND_READ_FTP_DATA,
STATE_SEND_READ_REMAINDER,
STATE_SEND_READ_FTP_STATUS,
STATE_SEND_READ_CLEANUP
};
typedef struct WS_SFTP_SEND_READ_STATE {
enum WS_SFTP_SEND_READ_STATE_ID state;
word32 reqId;
WS_SFTP_BUFFER buffer;
word32 recvSz;
byte type;
} WS_SFTP_SEND_READ_STATE;
enum WS_SFTP_SEND_WRITE_STATE_ID {
STATE_SEND_WRITE_INIT,
STATE_SEND_WRITE_SEND_HEADER,
STATE_SEND_WRITE_SEND_BODY,
STATE_SEND_WRITE_GET_HEADER,
STATE_SEND_WRITE_READ_STATUS,
STATE_SEND_WRITE_DO_STATUS,
STATE_SEND_WRITE_CLEANUP
};
typedef struct WS_SFTP_SEND_WRITE_STATE {
enum WS_SFTP_SEND_WRITE_STATE_ID state;
word32 reqId;
WS_SFTP_BUFFER buffer;
int maxSz;
int sentSz;
} WS_SFTP_SEND_WRITE_STATE;
enum WS_SFTP_CLOSE_STATE_ID {
STATE_CLOSE_INIT,
STATE_CLOSE_SEND,
STATE_CLOSE_GET_HEADER,
STATE_CLOSE_DO_STATUS,
STATE_CLOSE_CLEANUP
};
typedef struct WS_SFTP_CLOSE_STATE {
enum WS_SFTP_CLOSE_STATE_ID state;
word32 reqId;
WS_SFTP_BUFFER buffer;
} WS_SFTP_CLOSE_STATE;
enum WS_SFTP_GET_HANDLE_STATE_ID {
STATE_GET_HANDLE_INIT,
STATE_GET_HANDLE_GET_HEADER,
STATE_GET_HANDLE_DO_STATUS,
STATE_GET_HANDLE_CHECK_REQ_ID,
STATE_GET_HANDLE_READ,
STATE_GET_HANDLE_CLEANUP
};
typedef struct WS_SFTP_GET_HANDLE_STATE {
enum WS_SFTP_GET_HANDLE_STATE_ID state;
WS_SFTP_BUFFER buffer;
word32 reqId;
word32 bufSz;
byte buf[WOLFSSH_MAX_HANDLE + UINT32_SZ];
} WS_SFTP_GET_HANDLE_STATE;
enum WS_SFTP_RENAME_STATE_ID {
STATE_RENAME_INIT,
STATE_RENAME_GET_STAT,
STATE_RENAME_SEND,
STATE_RENAME_GET_HEADER,
STATE_RENAME_READ_STATUS,
STATE_RENAME_DO_STATUS,
STATE_RENAME_CLEANUP
};
typedef struct WS_SFTP_RENAME_STATE {
enum WS_SFTP_RENAME_STATE_ID state;
WS_SFTP_FILEATRB atrb;
WS_SFTP_BUFFER buffer;
int maxSz;
word32 reqId;
} WS_SFTP_RENAME_STATE;
static int SendPacketType(WOLFSSH* ssh, byte type, byte* buf, word32 bufSz);
static int SFTP_ParseAtributes_buffer(WOLFSSH* ssh, WS_SFTP_FILEATRB* atr,
byte* buf, word32* idx, word32 maxIdx);
static WS_SFTPNAME* wolfSSH_SFTPNAME_new(void* heap);
static int SFTP_CreateLongName(WS_SFTPNAME* name);
/* A few errors are OK to get. They are a notice rather that a fault.
* return TRUE if ssh->error is one of the following: */
static INLINE int NoticeError(WOLFSSH* ssh)
{
return (ssh->error == WS_WANT_READ ||
ssh->error == WS_WANT_WRITE ||
ssh->error == WS_CHAN_RXD ||
ssh->error == WS_REKEYING);
}
static byte* wolfSSH_SFTP_buffer_data(WS_SFTP_BUFFER* buffer)
{
byte* ret = NULL;
if (buffer != NULL) {
ret = buffer->data;
}
return ret;
}
/* sets the size of internal buffer, can not set to larger size than internal
* buffer (the buffer should be recreated in that case)
* returns a negative value on fail, WS_SUCCESS on success
*/
static int wolfSSH_SFTP_buffer_set_size(WS_SFTP_BUFFER* buffer, word32 sz)
{
if (buffer == NULL || sz > buffer->sz) {
WLOG(WS_LOG_SFTP, "Error setting size, buffer null or increase in sz");
return WS_BAD_ARGUMENT;
}
buffer->sz = sz;
return WS_SUCCESS;
}
/* returns the size of the buffer */
static word32 wolfSSH_SFTP_buffer_size(WS_SFTP_BUFFER* buffer)
{
word32 ret = 0;
if (buffer != NULL) {
ret = buffer->sz;
}
return ret;
}
/* sets the current idx into the buffer */
static void wolfSSH_SFTP_buffer_seek(WS_SFTP_BUFFER* buffer,
word32 start, word32 ofst)
{
if (buffer != NULL) {
buffer->idx = start + ofst;
}
}
/* c32toa function and advance idx */
static void wolfSSH_SFTP_buffer_c32toa(WS_SFTP_BUFFER* buffer,
word32 value)
{
if (buffer != NULL) {
c32toa(value, buffer->data + buffer->idx);
buffer->idx += UINT32_SZ;
}
}
/* returns WS_SUCCESS on success */
static int wolfSSH_SFTP_buffer_ato32(WS_SFTP_BUFFER* buffer, word32* out)
{
if (buffer == NULL || out == NULL ||
buffer->idx + UINT32_SZ > buffer->sz) {
return WS_BAD_ARGUMENT;
}
ato32(buffer->data + buffer->idx, out);
buffer->idx += UINT32_SZ;
return WS_SUCCESS;
}
/* getter function for current buffer idx */
static word32 wolfSSH_SFTP_buffer_idx(WS_SFTP_BUFFER* buffer)
{
word32 ret = 0;
if (buffer != NULL) {
ret = buffer->idx;
}
return ret;
}
/* rewinds reading the buffer, resetting it idx value to 0 */
static void wolfSSH_SFTP_buffer_rewind(WS_SFTP_BUFFER* buffer)
{
if (buffer != NULL)
buffer->idx = 0;
}
/* try to send the rest of the buffer (buffer.sz - buffer.idx)
* increments idx with amount sent */
static int wolfSSH_SFTP_buffer_send(WOLFSSH* ssh, WS_SFTP_BUFFER* buffer)
{
int ret = WS_SUCCESS;
if (buffer == NULL) {
return WS_BAD_ARGUMENT;
}
if (buffer->idx > buffer->sz) {
return WS_BUFFER_E;
}
if (buffer->idx < buffer->sz) {
ret = wolfSSH_stream_send(ssh, buffer->data + buffer->idx,
buffer->sz - buffer->idx);
if (ret > 0) {
buffer->idx += ret;
}
WLOG(WS_LOG_SFTP, "SFTP buffer sent %d / %d bytes", buffer->idx,
buffer->sz);
}
return ret;
}
/* returns the amount read on success */
static int wolfSSH_SFTP_buffer_read(WOLFSSH* ssh, WS_SFTP_BUFFER* buffer,
int readSz)
{
int ret;
if (buffer == NULL || ssh == NULL) {
return WS_FATAL_ERROR;
}
if (readSz == 0) {
return 0;
}
if (buffer->data == NULL) {
buffer->idx = 0;
buffer->sz = readSz;
buffer->data = (byte*)WMALLOC(buffer->sz, ssh->ctx->heap,
DYNTYPE_BUFFER);
if (buffer->data == NULL) {
return WS_MEMORY_E;
}
}
do {
ret = wolfSSH_stream_read(ssh, buffer->data + buffer->idx,
buffer->sz - buffer->idx);
if (ret < 0) {
return WS_FATAL_ERROR;
}
buffer->idx += (word32)ret;
WLOG(WS_LOG_SFTP, "SFTP buffer read %d / %d bytes", buffer->idx,
buffer->sz);
} while (buffer->idx < buffer->sz);
return buffer->sz;
}
static void wolfSSH_SFTP_buffer_free(WOLFSSH* ssh, WS_SFTP_BUFFER* buffer)
{
if (ssh != NULL && buffer != NULL) {
buffer->idx = 0;
buffer->sz = 0;
if (buffer->data != NULL) {
WFREE(buffer->data, ssh->ctx->heap, DYNTYPE_BUFFER);
buffer->data = NULL;
}
}
}
/* return WS_SUCCESS on success, creates a new buffer if one does not already
* exist */
static int wolfSSH_SFTP_buffer_create(WOLFSSH* ssh, WS_SFTP_BUFFER* buffer,
word32 sz)
{
if (ssh == NULL || buffer == NULL) {
return WS_BAD_ARGUMENT;
}
if (buffer->data == NULL ||
(buffer->data != NULL && buffer->sz != sz)) {
wolfSSH_SFTP_buffer_free(ssh, buffer);
buffer->data = (byte*)WMALLOC(sz, ssh->ctx->heap, DYNTYPE_BUFFER);
if (buffer->data == NULL)
return WS_MEMORY_E;
buffer->idx = 0;
buffer->sz = sz;
}
return WS_SUCCESS;
}
/* Used to clear and free all states. Should be when returning errors or
* success. Must be called when free'ing the SFTP. For now static since only
* used in wolfsftp.c
*
* Note: Most cases an error will free all states and a success will free
* specific state ID.
*/
static void wolfSSH_SFTP_ClearState(WOLFSSH* ssh, enum WS_SFTP_STATE_ID state)
{
if (ssh) {
if (state == 0)
state = (enum WS_SFTP_STATE_ID)~state; /* set all bits hot */
if (state & STATE_ID_GET) {
if (ssh->getState) {
WFREE(ssh->getState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
ssh->getState = NULL;
}
}
if (state & STATE_ID_LSTAT) {
if (ssh->lstatState) {
wolfSSH_SFTP_buffer_free(ssh, &ssh->lstatState->buffer);
WFREE(ssh->lstatState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
ssh->lstatState = NULL;
}
}
if (state & STATE_ID_OPEN) {
if (ssh->openState) {
wolfSSH_SFTP_buffer_free(ssh, &ssh->openState->buffer);
WFREE(ssh->openState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
ssh->openState = NULL;
}
}
if (state & STATE_ID_SEND_READ) {
if (ssh->sendReadState) {
wolfSSH_SFTP_buffer_free(ssh, &ssh->sendReadState->buffer);
WFREE(ssh->sendReadState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
ssh->sendReadState = NULL;
}
}
if (state & STATE_ID_CLOSE) {
if (ssh->closeState) {
wolfSSH_SFTP_buffer_free(ssh, &ssh->closeState->buffer);
WFREE(ssh->closeState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
ssh->closeState = NULL;
}
}
if (state & STATE_ID_GET_HANDLE) {
if (ssh->getHandleState) {
wolfSSH_SFTP_buffer_free(ssh, &ssh->getHandleState->buffer);
WFREE(ssh->getHandleState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
ssh->getHandleState = NULL;
}
}
if (state & STATE_ID_NAME) {
if (ssh->nameState) {
wolfSSH_SFTP_buffer_free(ssh, &ssh->nameState->buffer);
WFREE(ssh->nameState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
ssh->nameState = NULL;
}
}
if (state & STATE_ID_SEND) {
if (ssh->sendState) {
wolfSSH_SFTP_buffer_free(ssh, &ssh->sendState->buffer);
WFREE(ssh->sendState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
ssh->sendState = NULL;
}
}
if (state & STATE_ID_LS) {
if (ssh->lsState) {
WFREE(ssh->lsState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
ssh->lsState = NULL;
}
}
if (state & STATE_ID_READDIR) {
if (ssh->readDirState) {
wolfSSH_SFTP_buffer_free(ssh, &ssh->readDirState->buffer);
WFREE(ssh->readDirState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
ssh->readDirState = NULL;
}
}
if (state & STATE_ID_PUT) {
if (ssh->putState) {
WFREE(ssh->putState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
ssh->putState = NULL;
}
}
if (state & STATE_ID_SEND_WRITE) {
if (ssh->sendWriteState) {
wolfSSH_SFTP_buffer_free(ssh, &ssh->sendWriteState->buffer);
WFREE(ssh->sendWriteState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
ssh->sendWriteState = NULL;
}
}
if (state & STATE_ID_RM) {
if (ssh->rmState != NULL) {
wolfSSH_SFTP_buffer_free(ssh, &ssh->rmState->buffer);
WFREE(ssh->rmState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
ssh->rmState = NULL;
}
}
if (state & STATE_ID_MKDIR) {
if (ssh->mkdirState != NULL) {
wolfSSH_SFTP_buffer_free(ssh, &ssh->mkdirState->buffer);
WFREE(ssh->mkdirState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
ssh->mkdirState = NULL;
}
}
if (state & STATE_ID_RMDIR) {
if (ssh->rmdirState != NULL) {
wolfSSH_SFTP_buffer_free(ssh, &ssh->rmdirState->buffer);
WFREE(ssh->rmdirState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
ssh->rmdirState = NULL;
}
}
if (state & STATE_ID_RENAME) {
if (ssh->renameState != NULL) {
wolfSSH_SFTP_buffer_free(ssh, &ssh->renameState->buffer);
WFREE(ssh->renameState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
ssh->renameState = NULL;
}
}
if (state & STATE_ID_RECV) {
if (ssh->recvState != NULL) {
wolfSSH_SFTP_buffer_free(ssh, &ssh->recvState->buffer);
WFREE(ssh->recvState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
ssh->recvState = NULL;
}
}
if (state & STATE_ID_CHMOD) {
if (ssh->chmodState) {
WFREE(ssh->chmodState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
ssh->chmodState = NULL;
}
}
if (state & STATE_ID_SETATR) {
if (ssh->setatrState) {
wolfSSH_SFTP_buffer_free(ssh, &ssh->setatrState->buffer);
WFREE(ssh->setatrState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
ssh->setatrState = NULL;
}
}
if (state & STATE_ID_RECV_INIT) {
if (ssh->recvInitState) {
wolfSSH_SFTP_buffer_free(ssh, &ssh->recvInitState->buffer);
WFREE(ssh->recvInitState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
ssh->recvInitState = NULL;
}
}
}
}
/* Returns 1 if there is pending data to be sent and 0 if not */
int wolfSSH_SFTP_PendingSend(WOLFSSH* ssh)
{
int isSet = 0;
if (ssh) {
if (ssh->recvState != NULL && ssh->recvState->toSend)
isSet = 1;
}
return isSet;
}
/* Gets packet header information
* request Id, type, and size of type specific data
* return value is length of type specific data still on the wire to be read
*/
static int SFTP_GetHeader(WOLFSSH* ssh, word32* reqId, byte* type,
WS_SFTP_BUFFER* buffer)
{
int ret;
word32 len;
WLOG(WS_LOG_SFTP, "Entering SFTP_GetHeader()");
if (type == NULL || reqId == NULL || ssh == NULL) {
WLOG(WS_LOG_SFTP, "NULL argument error");
return WS_BAD_ARGUMENT;
}
ret = wolfSSH_SFTP_buffer_read(ssh, buffer, WOLFSSH_SFTP_HEADER);
if (ret < 0) {
return WS_FATAL_ERROR;
}
if (ret < WOLFSSH_SFTP_HEADER) {
WLOG(WS_LOG_SFTP, "Unable to read SFTP header");
return WS_FATAL_ERROR;
}
ato32(buffer->data, &len);
*type = buffer->data[LENGTH_SZ];
ato32(buffer->data + UINT32_SZ + MSG_ID_SZ, reqId);
wolfSSH_SFTP_buffer_free(ssh, buffer);
WLOG(WS_LOG_SFTP, "Leaving SFTP_GetHeader(), packet length = %d id = %d"
" type = %d", len - UINT32_SZ - MSG_ID_SZ, *reqId, *type);
return len - UINT32_SZ - MSG_ID_SZ;
}
/* Sets the packet header information to the front of buf. It is assumed that
* buf is at least WOLFSSH_SFTP_HEADER (9).
*
* reqId request ID to place in header of packet
* type type of SFTP packet i.e. WOLFSSH_FTP_OPEN
* len length of all data in packet
* buf buffer to place packet header at front of
*
* returns WS_SUCCESS on success
*/
static int SFTP_SetHeader(WOLFSSH* ssh, word32 reqId, byte type, word32 len,
byte* buf) {
c32toa(len + LENGTH_SZ + MSG_ID_SZ, buf);
buf[LENGTH_SZ] = type;
c32toa(reqId, buf + LENGTH_SZ + MSG_ID_SZ);
WOLFSSH_UNUSED(ssh);
return WS_SUCCESS;
}
static int SFTP_CreatePacket(WOLFSSH* ssh, byte type, byte* out, word32 outSz,
byte* data, word32 dataSz)
{
if (outSz < WOLFSSH_SFTP_HEADER + UINT32_SZ ||
(data == NULL && dataSz > 0) ||
out == NULL || ssh == NULL) {
return WS_BAD_ARGUMENT;
}
if (SFTP_SetHeader(ssh, ssh->reqId, type, outSz - WOLFSSH_SFTP_HEADER, out)
!= WS_SUCCESS) {
return WS_FATAL_ERROR;
}
c32toa(outSz - WOLFSSH_SFTP_HEADER - UINT32_SZ, out + WOLFSSH_SFTP_HEADER);
if (data) {
if (dataSz + UINT32_SZ + WOLFSSH_SFTP_HEADER > outSz) {
WLOG(WS_LOG_SFTP, "Data size was to large for packet buffer");
return WS_BUFFER_E;
}
WMEMCPY(out + UINT32_SZ + WOLFSSH_SFTP_HEADER, data, dataSz);
}
return WS_SUCCESS;
}
/* returns the size of buffer needed to hold attributes */
static int SFTP_AtributesSz(WOLFSSH* ssh, WS_SFTP_FILEATRB* atr)
{
word32 sz = 0;
WOLFSSH_UNUSED(ssh);
sz += UINT32_SZ; /* flag */
/* check if size attribute present */
if (atr->flags & WOLFSSH_FILEATRB_SIZE) {
sz += UINT32_SZ * 2;
}
/* check if uid and gid attribute present */
if (atr->flags & WOLFSSH_FILEATRB_UIDGID) {
sz += UINT32_SZ * 2;
}
/* check if permissions attribute present */
if (atr->flags & WOLFSSH_FILEATRB_PERM) {
sz += UINT32_SZ;
}
/* check if time attribute present */
if (atr->flags & WOLFSSH_FILEATRB_TIME) {
sz += UINT32_SZ * 2;
}
/* check if extended attributes are present */
if (atr->flags & WOLFSSH_FILEATRB_EXT) {
sz += UINT32_SZ;
/* @TODO handle extended attributes */
}
return sz;
}
/* set attributes in buffer
*
* returns WS_SUCCESS on success
*/
static int SFTP_SetAttributes(WOLFSSH* ssh, byte* buf, word32 bufSz,
WS_SFTP_FILEATRB* atr)
{
word32 idx = 0;
WOLFSSH_UNUSED(ssh);
WOLFSSH_UNUSED(bufSz);
/* get flags */
c32toa(atr->flags, buf); idx += UINT32_SZ;
/* check if size attribute present */
if (atr->flags & WOLFSSH_FILEATRB_SIZE) {
c32toa(atr->sz[1], buf + idx); idx += UINT32_SZ;
c32toa(atr->sz[0], buf + idx); idx += UINT32_SZ;
}
/* check if uid and gid attribute present */
if (atr->flags & WOLFSSH_FILEATRB_UIDGID) {
c32toa(atr->uid, buf + idx); idx += UINT32_SZ;
c32toa(atr->gid, buf + idx); idx += UINT32_SZ;
}
/* check if permissions attribute present */
if (atr->flags & WOLFSSH_FILEATRB_PERM) {
c32toa(atr->per, buf + idx); idx += UINT32_SZ;
}
/* check if time attribute present */
if (atr->flags & WOLFSSH_FILEATRB_TIME) {
c32toa(atr->atime, buf + idx); idx += UINT32_SZ;
c32toa(atr->mtime, buf + idx); idx += UINT32_SZ;
}
/* check if extended attributes are present */
if (atr->flags & WOLFSSH_FILEATRB_EXT) {
/* @TODO handle attribute extensions */
c32toa(atr->extCount, buf + idx);
}
return WS_SUCCESS;
}
static INLINE int SFTP_GetSz(byte* buf, word32* sz,
word32 lowerBound, word32 upperBound)
{
int ret = WS_SUCCESS;
if (buf == NULL || sz == NULL) {
ret = WS_BAD_ARGUMENT;
}
if (ret == WS_SUCCESS) {
word32 val;
ato32(buf, &val);
if (val < lowerBound || val > upperBound)
ret = WS_BUFFER_E;
else
*sz = val;
}
return ret;
}
#ifndef NO_WOLFSSH_SERVER
#ifndef WOLFSSH_USER_FILESYSTEM
static int SFTP_GetAttributes(void* fs, const char* fileName,
WS_SFTP_FILEATRB* atr, byte noFollow, void* heap);
static int SFTP_GetAttributes_Handle(WOLFSSH* ssh, byte* handle, int handleSz,
WS_SFTP_FILEATRB* atr);