-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathsftpclient.c
More file actions
1766 lines (1497 loc) · 53.5 KB
/
sftpclient.c
File metadata and controls
1766 lines (1497 loc) · 53.5 KB
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
/* sftpclient.c
*
* Copyright (C) 2014-2026 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 WOLFSSH_TEST_CLIENT
#ifdef WOLFSSL_USER_SETTINGS
#include <wolfssl/wolfcrypt/settings.h>
#else
#include <wolfssl/options.h>
#endif
#include <wolfssh/ssh.h>
#include <wolfssh/internal.h>
#include <wolfssh/wolfsftp.h>
#include <wolfssh/test.h>
#include <wolfssh/port.h>
#include <wolfssl/wolfcrypt/ecc.h>
#include <wolfssl/wolfcrypt/coding.h>
#include "examples/sftpclient/sftpclient.h"
#include "examples/client/common.h"
#if !defined(USE_WINDOWS_API) && !defined(MICROCHIP_PIC32) && \
!defined(WOLFSSH_ZEPHYR)
#include <termios.h>
#endif
#ifdef WOLFSSH_CERTS
#include <wolfssl/wolfcrypt/asn.h>
#endif
#if defined(WOLFSSH_SFTP) && !defined(NO_WOLFSSH_CLIENT)
/* static so that signal handler can access and interrupt get/put */
static WOLFSSH* ssh = NULL;
static char* workingDir;
#define fin stdin
#define fout stdout
#define MAX_CMD_SZ 7
#define AUTOPILOT_OFF 0
#define AUTOPILOT_GET 1
#define AUTOPILOT_PUT 2
#ifdef WOLFSSH_STATIC_MEMORY
#include <wolfssl/wolfcrypt/memory.h>
typedef WOLFSSL_HEAP_HINT SFTPC_HEAP_HINT;
/* This static buffer is tuned for building with SFTP only. The static
* buffer size is calculated by multiplying the pairs of sizeList items
* and distList items and summing (32*50 + 128*100 + ...) and adding
* the sum of the distList values times the sizeof wc_Memory (rounded up
* to a word, 24). This total was 268kb plus change, rounded up to 269. */
#ifndef SFTPC_STATIC_SIZES
#define SFTPC_STATIC_SIZES 64,128,384,800,3120,8400,17552,33104,131072
#endif
#ifndef SFTPC_STATIC_DISTS
#define SFTPC_STATIC_DISTS 60,100,4,6,5,2,1,2,1
#endif
#ifndef SFTPC_STATIC_LISTSZ
#define SFTPC_STATIC_LISTSZ 9
#endif
#ifndef SFTPC_STATIC_BUFSZ
#define SFTPC_STATIC_BUFSZ (269*1024)
#endif
static const word32 static_sizeList[] = {SFTPC_STATIC_SIZES};
static const word32 static_distList[] = {SFTPC_STATIC_DISTS};
static byte static_buffer[SFTPC_STATIC_BUFSZ];
#else /* WOLFSSH_STATIC_MEMORY */
typedef void SFTPC_HEAP_HINT;
#endif /* WOLFSSH_STATIC_MEMORY */
static void err_msg(const char* s)
{
printf("%s\n", s);
}
#ifndef WOLFSSH_NO_TIMESTAMP
static char currentFile[WOLFSSH_MAX_FILENAME + 1] = "";
static word32 startTime;
#define TIMEOUT_VALUE 120
word32 current_time(int);
#ifdef USE_WINDOWS_API
#include <time.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
word32 current_time(int reset)
{
static int init = 0;
static LARGE_INTEGER freq;
LARGE_INTEGER count;
(void)reset;
if (!init) {
QueryPerformanceFrequency(&freq);
init = 1;
}
QueryPerformanceCounter(&count);
return (word32)(count.QuadPart / freq.QuadPart);
}
#else
#include <sys/time.h>
/* return number of seconds*/
word32 current_time(int reset)
{
struct timeval tv;
(void)reset;
gettimeofday(&tv, 0);
return (word32)tv.tv_sec;
}
#endif /* USE_WINDOWS_API */
#endif /* !WOLFSSH_NO_TIMESTAMP */
static void myStatusCb(WOLFSSH* sshIn, word32* bytes, char* name)
{
/* Variables declared at function scope per wolfSSL coding standards.
* Modern compilers optimize variable access regardless of declaration
* placement, so there is no performance impact. */
word32 currentTime;
#ifndef WOLFSSH_NO_TIMESTAMP
static word32 lastOutputTime = 0;
static word32 lastPrintedBytes[2] = {0, 0};
word32 elapsedTime;
#endif
char buf[80];
word64 longBytes = ((word64)bytes[1] << 32) | bytes[0];
#ifndef WOLFSSH_NO_TIMESTAMP
currentTime = current_time(0);
if (currentTime == lastOutputTime) {
if (bytes[0] != lastPrintedBytes[0] || bytes[1] != lastPrintedBytes[1]) {
/* Progress made in the same second — throttle but track latest */
lastPrintedBytes[0] = bytes[0];
lastPrintedBytes[1] = bytes[1];
return;
}
/* bytes unchanged: EOF final call — fall through to print */
}
else {
lastOutputTime = currentTime;
}
lastPrintedBytes[0] = bytes[0];
lastPrintedBytes[1] = bytes[1];
if (WSTRNCMP(currentFile, name, WSTRLEN(name)) != 0) {
startTime = current_time(1);
lastOutputTime = 0; /* Reset timer for new file transfer */
lastPrintedBytes[0] = 0;
lastPrintedBytes[1] = 0;
WMEMSET(currentFile, 0, WOLFSSH_MAX_FILENAME);
WSTRNCPY(currentFile, name, WOLFSSH_MAX_FILENAME);
}
elapsedTime = currentTime - startTime;
WSNPRINTF(buf, sizeof(buf), "Processed %8llu\t bytes in %d seconds\r",
(unsigned long long)longBytes, elapsedTime);
#ifndef WOLFSSH_NO_SFTP_TIMEOUT
if (elapsedTime > TIMEOUT_VALUE) {
WSNPRINTF(buf, sizeof(buf), "\nProcess timed out at %d seconds, "
"stopping\r", elapsedTime);
WMEMSET(currentFile, 0, WOLFSSH_MAX_FILENAME);
wolfSSH_SFTP_Interrupt(ssh);
}
#endif
#else
WSNPRINTF(buf, sizeof(buf), "Processed %8llu\t bytes \r",
(unsigned long long)longBytes);
(void)currentTime;
#endif
WFPUTS(buf, fout);
(void)name;
(void)sshIn;
}
static int NonBlockSSH_connect(void)
{
int ret;
int error;
SOCKET_T sockfd;
int select_ret = 0;
ret = wolfSSH_SFTP_connect(ssh);
error = wolfSSH_get_error(ssh);
sockfd = (SOCKET_T)wolfSSH_get_fd(ssh);
while (ret != WS_SUCCESS &&
(error == WS_WANT_READ || error == WS_WANT_WRITE ||
error == WS_REKEYING || error == WS_AUTH_PENDING))
{
if (error == WS_WANT_READ)
printf("... client would read block\n");
else if (error == WS_WANT_WRITE)
printf("... client would write block\n");
select_ret = tcp_select(sockfd, 1);
if (select_ret == WS_SELECT_RECV_READY ||
select_ret == WS_SELECT_ERROR_READY ||
error == WS_WANT_WRITE)
{
ret = wolfSSH_SFTP_connect(ssh);
error = wolfSSH_get_error(ssh);
#ifdef WOLFSSH_TEST_BLOCK
if (select_ret == WS_SELECT_RECV_READY && error == WS_WANT_READ) {
/* The socket has data to be read but the non blocking test
* code returned want read. Loop over wolfSSH_SFTP_connect here
* until we get the data off the socket that select indicated
* was available. */
while (error == WS_WANT_READ) {
ret = wolfSSH_SFTP_connect(ssh);
error = wolfSSH_get_error(ssh);
}
}
#endif
}
else if (select_ret == WS_SELECT_TIMEOUT)
error = WS_WANT_READ;
else
error = WS_FATAL_ERROR;
}
return ret;
}
#ifndef WS_NO_SIGNAL
/* for command reget and reput to handle saving offset after interrupt during
* get and put */
#include <signal.h>
static byte interrupt = 0;
static void sig_handler(const int sig)
{
(void)sig;
interrupt = 1;
wolfSSH_SFTP_Interrupt(ssh);
}
#endif /* WS_NO_SIGNAL */
/* cleans up absolute path */
static void clean_path(char* path)
{
int i;
long sz;
byte found;
if (path == NULL) {
return;
}
sz = (long)WSTRLEN(path);
/* remove any double '/' chars */
for (i = 0; i < sz; i++) {
if (path[i] == '/' && path[i+1] == '/') {
WMEMMOVE(path + i, path + i + 1, sz - i);
sz -= 1;
i--;
}
}
/* remove any trailing '/' chars */
sz = (long)WSTRLEN(path);
for (i = (int)sz - 1; i > 0; i--) {
if (path[i] == '/') {
path[i] = '\0';
}
else {
break;
}
}
/* go through path until no cases are found */
do {
int prIdx = 0; /* begin of cut */
int enIdx = 0; /* end of cut */
sz = (long)WSTRLEN(path);
found = 0;
for (i = 0; i < sz; i++) {
if (path[i] == '/') {
int z;
/* if next two chars are .. then delete */
if (path[i+1] == '.' && path[i+2] == '.') {
enIdx = i + 3;
/* start at one char before / and retrace path */
for (z = i - 1; z > 0; z--) {
if (path[z] == '/') {
prIdx = z;
break;
}
}
/* cut out .. and previous */
WMEMMOVE(path + prIdx, path + enIdx, sz - enIdx);
path[sz - (enIdx - prIdx)] = '\0';
if (enIdx == sz) {
path[prIdx] = '\0';
}
/* case of at / */
if (WSTRLEN(path) == 0) {
path[0] = '/';
path[1] = '\0';
}
found = 1;
break;
}
}
}
} while (found);
}
#define WS_MAX_EXAMPLE_RW 1024
static void ShowCommands(void)
{
printf("\n\nCommands :\n");
printf("\tcd <string> change directory\n");
printf("\tchmod <mode> <path> change mode\n");
printf("\tget <remote file> <local file> pulls file(s) from server\n");
printf("\tlcd <path> change local directory\n");
printf("\tlls list local directory\n");
printf("\tls list current directory\n");
printf("\tmkdir <dir name> creates new directory on server\n");
printf("\tput <local file> <remote file> push file(s) to server\n");
printf("\tpwd list current path\n");
printf("\tquit exit\n");
printf("\trename <old> <new> renames remote file\n");
printf("\treget <remote file> <local file> resume pulling file\n");
printf("\treput <remote file> <local file> resume pushing file\n");
printf("\t<crtl + c> interrupt get/put cmd\n");
}
static void ShowUsage(void)
{
printf("wolfsftp %s linked with wolfSSL %s\n", LIBWOLFSSH_VERSION_STRING,
LIBWOLFSSL_VERSION_STRING);
printf(" -? display this help and exit\n");
printf(" -h <host> host to connect to, default %s\n", wolfSshIp);
printf(" -p <num> port to connect on, default %d\n", wolfSshPort);
printf(" -u <username> username to authenticate as (REQUIRED)\n");
printf(" -P <password> password for username, prompted if omitted\n");
printf(" -d <path> set the default local path\n");
printf(" -N use non blocking sockets\n");
printf(" -e use ECC user authentication\n");
/*printf(" -E use ECC server authentication\n");*/
printf(" -l <filename> local filename\n");
printf(" -r <filename> remote filename\n");
printf(" -g put local filename as remote filename\n");
printf(" -G get remote filename as local filename\n");
printf(" -i <filename> filename for the user's private key\n");
#ifdef WOLFSSH_CERTS
printf(" -J <filename> filename for DER certificate to use\n");
printf(" Certificate example : client -u orange \\\n");
printf(" -J orange-cert.der -i orange-key.der\n");
printf(" -A <filename> filename for DER CA certificate to verify host\n");
printf(" -X Ignore IP checks on peer vs peer certificate\n");
#endif
ShowCommands();
}
/* returns 0 on success */
static INLINE int SFTP_FPUTS(func_args* args, const char* msg)
{
int ret;
if (args && args->sftp_cb)
ret = args->sftp_cb(msg, NULL, 0);
else
ret = WFPUTS(msg, fout);
return ret;
}
/* returns pointer on success, NULL on failure */
static INLINE char* SFTP_FGETS(func_args* args, char* msg, int msgSz)
{
char* ret = NULL;
WMEMSET(msg, 0, msgSz);
if (args && args->sftp_cb) {
if (args->sftp_cb(NULL, msg, msgSz) == 0)
ret = msg;
}
else
ret = WFGETS(msg, msgSz, fin);
return ret;
}
/* main loop for handling commands */
static int doCmds(func_args* args)
{
byte quit = 0;
int ret = WS_SUCCESS, err;
byte resume = 0;
int i;
do {
char msg[WOLFSSH_MAX_FILENAME * 2];
char* pt;
if (wolfSSH_get_error(ssh) == WS_SOCKET_ERROR_E) {
if (SFTP_FPUTS(args, "peer disconnected\n") < 0) {
err_msg("fputs error");
return -1;
}
return WS_SOCKET_ERROR_E;
}
if (SFTP_FPUTS(args, "wolfSSH sftp> ") < 0) {
err_msg("fputs error");
return -1;
}
WFFLUSH(stdout);
WMEMSET(msg, 0, sizeof(msg));
if (SFTP_FGETS(args, msg, sizeof(msg) - 1) == NULL) {
err_msg("fgets error");
return -1;
}
msg[WOLFSSH_MAX_FILENAME * 2 - 1] = '\0';
if ((pt = WSTRNSTR(msg, "mkdir", sizeof(msg))) != NULL) {
WS_SFTP_FILEATRB atrb;
int sz;
char* f = NULL;
pt += sizeof("mkdir");
sz = (int)WSTRLEN(pt);
if (sz > 0 && pt[sz - 1] == '\n') pt[sz - 1] = '\0';
if (pt[0] != '/') {
int maxSz = (int)WSTRLEN(workingDir) + sz + 2;
f = (char*)WMALLOC(maxSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (f == NULL)
return WS_MEMORY_E;
f[0] = '\0';
WSTRNCAT(f, workingDir, maxSz);
WSTRNCAT(f, "/", maxSz);
WSTRNCAT(f, pt, maxSz);
pt = f;
}
do {
err = WS_SUCCESS;
if ((ret = wolfSSH_SFTP_MKDIR(ssh, pt, &atrb)) != WS_SUCCESS) {
err = wolfSSH_get_error(ssh);
if (ret == WS_PERMISSIONS) {
if (SFTP_FPUTS(args, "Insufficient permissions\n") < 0) {
err_msg("fputs error");
return -1;
}
}
else if (err != WS_WANT_READ && err != WS_WANT_WRITE) {
if (SFTP_FPUTS(args, "Error writing directory\n") < 0) {
err_msg("fputs error");
return -1;
}
}
}
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE)
&& ret != WS_SUCCESS);
WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER);
continue;
}
if (WSTRNSTR(msg, "reget", MAX_CMD_SZ) != NULL) {
resume = 1;
}
if ((pt = WSTRNSTR(msg, "get", MAX_CMD_SZ)) != NULL) {
int sz;
char* f = NULL;
char* to = NULL;
pt += sizeof("get");
sz = (int)WSTRLEN(pt);
if (sz > 0 && pt[sz - 1] == '\n') pt[sz - 1] = '\0';
/* search for space delimiter */
to = pt;
for (i = 0; i < sz; i++) {
to++;
if (pt[i] == ' ') {
pt[i] = '\0';
break;
}
}
/* check if local file path listed */
if (WSTRLEN(to) <= 0) {
to = pt;
/* if local path not listed follow path till at the tail */
for (i = 0; i < sz; i++) {
if (pt[i] == '/') {
to = pt + i + 1;
}
}
}
if (pt[0] != '/') {
int maxSz = (int)(WSTRLEN(workingDir) + sz + 2);
f = (char*)WMALLOC(maxSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (f == NULL) {
err_msg("Error malloc'ing");
return -1;
}
f[0] = '\0';
WSTRNCAT(f, workingDir, maxSz);
if (WSTRLEN(workingDir) > 1) {
WSTRNCAT(f, "/", maxSz);
}
WSTRNCAT(f, pt, maxSz);
pt = f;
}
{
char buf[WOLFSSH_MAX_FILENAME * 3];
if (resume) {
WSNPRINTF(buf, sizeof(buf), "resuming %s to %s\n", pt, to);
}
else {
WSNPRINTF(buf, sizeof(buf), "fetching %s to %s\n", pt, to);
}
if (SFTP_FPUTS(args, buf) < 0) {
err_msg("fputs error");
return -1;
}
}
do {
while (ret == WS_REKEYING || ssh->error == WS_REKEYING) {
ret = wolfSSH_worker(ssh, NULL);
if (ret != WS_SUCCESS && ret == WS_FATAL_ERROR) {
ret = wolfSSH_get_error(ssh);
}
}
ret = wolfSSH_SFTP_Get(ssh, pt, to, resume, &myStatusCb);
if (ret != WS_SUCCESS && ret == WS_FATAL_ERROR) {
ret = wolfSSH_get_error(ssh);
}
} while (ret == WS_WANT_READ || ret == WS_WANT_WRITE ||
ret == WS_CHAN_RXD || ret == WS_REKEYING);
#ifndef WOLFSSH_NO_TIMESTAMP
WMEMSET(currentFile, 0, WOLFSSH_MAX_FILENAME);
#endif
if (ret != WS_SUCCESS) {
if (wolfSSH_get_error(ssh) == WS_SFTP_NOT_FILE_E) {
if (SFTP_FPUTS(args, "Not a regular file\n") < 0) {
err_msg("fputs error");
return -1;
}
}
if (SFTP_FPUTS(args, "Error getting file\n") < 0) {
err_msg("fputs error");
return -1;
}
}
else {
if (SFTP_FPUTS(args, "\n") < 0) { /* new line after status output */
err_msg("fputs error");
return -1;
}
}
resume = 0;
WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER);
continue;
}
if (WSTRNSTR(msg, "reput", MAX_CMD_SZ) != NULL) {
resume = 1;
}
if ((pt = WSTRNSTR(msg, "put", MAX_CMD_SZ)) != NULL) {
int sz;
char* f = NULL;
char* to = NULL;
pt += sizeof("put");
sz = (int)WSTRLEN(pt);
if (sz > 0 && pt[sz - 1] == '\n') pt[sz - 1] = '\0';
to = pt;
for (i = 0; i < sz; i++) {
to++;
if (pt[i] == ' ') {
pt[i] = '\0';
break;
}
}
/* check if local file path listed */
if (WSTRLEN(to) <= 0) {
to = pt;
/* if local path not listed follow path till at the tail */
for (i = 0; i < sz; i++) {
if (pt[i] == '/') {
to = pt + i + 1;
}
}
}
if (to[0] != '/') {
int maxSz = (int)WSTRLEN(workingDir) + sz + 2;
f = (char*)WMALLOC(maxSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (f == NULL) {
err_msg("Error malloc'ing");
return -1;
}
f[0] = '\0';
WSTRNCAT(f, workingDir, maxSz);
if (WSTRLEN(workingDir) > 1) {
WSTRNCAT(f, "/", maxSz);
}
WSTRNCAT(f, to, maxSz);
to = f;
}
{
char buf[WOLFSSH_MAX_FILENAME * 3];
if (resume) {
WSNPRINTF(buf, sizeof(buf), "resuming %s to %s\n", pt, to);
}
else {
WSNPRINTF(buf, sizeof(buf), "pushing %s to %s\n", pt, to);
}
if (SFTP_FPUTS(args, buf) < 0) {
err_msg("fputs error");
return -1;
}
}
do {
while (ret == WS_REKEYING || ssh->error == WS_REKEYING) {
ret = wolfSSH_worker(ssh, NULL);
if (ret != WS_SUCCESS && ret == WS_FATAL_ERROR) {
ret = wolfSSH_get_error(ssh);
}
}
ret = wolfSSH_SFTP_Put(ssh, pt, to, resume, &myStatusCb);
if (ret != WS_SUCCESS && ret == WS_FATAL_ERROR) {
ret = wolfSSH_get_error(ssh);
}
} while (ret == WS_WANT_READ || ret == WS_WANT_WRITE ||
ret == WS_CHAN_RXD || ret == WS_REKEYING);
#ifndef WOLFSSH_NO_TIMESTAMP
WMEMSET(currentFile, 0, WOLFSSH_MAX_FILENAME);
#endif
if (ret != WS_SUCCESS) {
if (wolfSSH_get_error(ssh) == WS_SFTP_NOT_FILE_E) {
if (SFTP_FPUTS(args, "Not a regular file\n") < 0) {
err_msg("fputs error");
return -1;
}
}
if (SFTP_FPUTS(args, "Error pushing file\n") < 0) {
err_msg("fputs error");
return -1;
}
}
else {
if (SFTP_FPUTS(args, "\n") < 0) { /* new line after status output */
err_msg("fputs error");
return -1;
}
}
resume = 0;
WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER);
continue;
}
/* lcd must be checked before cd so that WSTRNSTR
* does not match "cd" inside "lcd" */
#ifndef WOLFSSH_FATFS /* WCHDIR not yet defined for FatFS */
if ((pt = WSTRNSTR(msg, "lcd", MAX_CMD_SZ)) != NULL) {
int sz;
pt += sizeof("lcd");
sz = (int)WSTRLEN(pt);
if (sz > 0 && pt[sz - 1] == '\n') {
pt[sz - 1] = '\0';
}
if (WCHDIR(ssh->fs, pt) != 0) {
if (SFTP_FPUTS(args,
"Error changing local directory\n") < 0) {
err_msg("fputs error");
return -1;
}
}
continue;
}
#endif /* !WOLFSSH_FATFS */
if ((pt = WSTRNSTR(msg, "cd", MAX_CMD_SZ)) != NULL) {
WS_SFTP_FILEATRB atrb;
int sz;
char* f = NULL;
pt += sizeof("cd");
sz = (int)WSTRLEN(pt);
if (sz > 0 && pt[sz - 1] == '\n') pt[sz - 1] = '\0';
if (pt[0] != '/') {
int maxSz = (int)WSTRLEN(workingDir) + sz + 2;
f = (char*)WMALLOC(maxSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (f == NULL) {
err_msg("Error malloc'ing");
return -1;
}
f[0] = '\0';
WSTRNCAT(f, workingDir, maxSz);
if (WSTRLEN(workingDir) > 1) {
WSTRNCAT(f, "/", maxSz);
}
WSTRNCAT(f, pt, maxSz);
pt = f;
}
/* check directory is valid */
do {
while (ret == WS_REKEYING || ssh->error == WS_REKEYING) {
ret = wolfSSH_worker(ssh, NULL);
if (ret != WS_SUCCESS && ret == WS_FATAL_ERROR) {
ret = wolfSSH_get_error(ssh);
}
}
ret = wolfSSH_SFTP_STAT(ssh, pt, &atrb);
err = wolfSSH_get_error(ssh);
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE ||
err == WS_REKEYING) && ret != WS_SUCCESS);
if (ret != WS_SUCCESS) {
if (SFTP_FPUTS(args, "Error changing directory\n") < 0) {
err_msg("fputs error");
return -1;
}
}
if (ret == WS_SUCCESS) {
sz = (int)WSTRLEN(pt);
WFREE(workingDir, NULL, DYNAMIC_TYPE_TMP_BUFFER);
workingDir = (char*)WMALLOC(sz + 1, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (workingDir == NULL) {
err_msg("Error malloc'ing");
return -1;
}
WMEMCPY(workingDir, pt, sz);
workingDir[sz] = '\0';
clean_path(workingDir);
}
WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER);
continue;
}
if ((pt = WSTRNSTR(msg, "chmod", MAX_CMD_SZ)) != NULL) {
word32 sz, idx;
char* f = NULL;
char mode[WOLFSSH_MAX_OCTET_LEN];
pt += sizeof("chmod");
sz = (word32)WSTRLEN(pt);
if (sz > 0 && pt[sz - 1] == '\n') pt[sz - 1] = '\0';
/* advance pointer to first location of non space character */
for (idx = 0; idx < sz && pt[0] == ' '; idx++, pt++);
sz = (word32)WSTRLEN(pt);
/* get mode */
sz = (sz < WOLFSSH_MAX_OCTET_LEN - 1)? sz :
WOLFSSH_MAX_OCTET_LEN -1;
WMEMCPY(mode, pt, sz);
mode[WOLFSSH_MAX_OCTET_LEN - 1] = '\0';
for (idx = 0; idx < sz; idx++) {
if (mode[idx] == ' ') {
mode[idx] = '\0';
break;
}
}
if (idx == 0) {
printf("error with getting mode\r\n");
continue;
}
pt += (word32)WSTRLEN(mode);
sz = (word32)WSTRLEN(pt);
for (idx = 0; idx < sz && pt[0] == ' '; idx++, pt++);
if (pt[0] != '/') {
int maxSz = (int)WSTRLEN(workingDir) + sz + 2;
f = (char*)WMALLOC(maxSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (f == NULL) {
err_msg("Error malloc'ing");
return -1;
}
f[0] = '\0';
WSTRNCAT(f, workingDir, maxSz);
if (WSTRLEN(workingDir) > 1) {
WSTRNCAT(f, "/", maxSz);
}
WSTRNCAT(f, pt, maxSz);
pt = f;
}
/* update permissions */
do {
while (ret == WS_REKEYING || ssh->error == WS_REKEYING) {
ret = wolfSSH_worker(ssh, NULL);
if (ret != WS_SUCCESS && ret == WS_FATAL_ERROR) {
ret = wolfSSH_get_error(ssh);
}
}
ret = wolfSSH_SFTP_CHMOD(ssh, pt, mode);
err = wolfSSH_get_error(ssh);
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE ||
err == WS_REKEYING) && ret != WS_SUCCESS);
if (ret != WS_SUCCESS) {
if (SFTP_FPUTS(args, "Unable to change permissions of ") < 0) {
err_msg("fputs error");
return -1;
}
if (SFTP_FPUTS(args, pt) < 0) {
err_msg("fputs error");
return -1;
}
if (SFTP_FPUTS(args, "\n") < 0) {
err_msg("fputs error");
return -1;
}
}
WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER);
continue;
}
if ((pt = WSTRNSTR(msg, "rmdir", MAX_CMD_SZ)) != NULL) {
int sz;
char* f = NULL;
pt += sizeof("rmdir");
sz = (int)WSTRLEN(pt);
if (sz > 0 && pt[sz - 1] == '\n') pt[sz - 1] = '\0';
if (pt[0] != '/') {
int maxSz = (int)WSTRLEN(workingDir) + sz + 2;
f = (char*)WMALLOC(maxSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (f == NULL) {
err_msg("Error malloc'ing");
return -1;
}
f[0] = '\0';
WSTRNCAT(f, workingDir, maxSz);
if (WSTRLEN(workingDir) > 1) {
WSTRNCAT(f, "/", maxSz);
}
WSTRNCAT(f, pt, maxSz);
pt = f;
}
do {
while (ret == WS_REKEYING || ssh->error == WS_REKEYING) {
ret = wolfSSH_worker(ssh, NULL);
if (ret != WS_SUCCESS && ret == WS_FATAL_ERROR) {
ret = wolfSSH_get_error(ssh);
}
}
ret = wolfSSH_SFTP_RMDIR(ssh, pt);
err = wolfSSH_get_error(ssh);
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE ||
err == WS_REKEYING) && ret != WS_SUCCESS);
if (ret != WS_SUCCESS) {
if (ret == WS_PERMISSIONS) {
if (SFTP_FPUTS(args, "Insufficient permissions\n") < 0) {
err_msg("fputs error");
return -1;
}
}
else {
if (SFTP_FPUTS(args, "Error writing directory\n") < 0) {
err_msg("fputs error");
return -1;
}
}
}
WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER);
continue;
}
if ((pt = WSTRNSTR(msg, "rm", MAX_CMD_SZ)) != NULL) {
int sz;
char* f = NULL;
pt += sizeof("rm");
sz = (int)WSTRLEN(pt);
if (sz > 0 && pt[sz - 1] == '\n') pt[sz - 1] = '\0';
if (pt[0] != '/') {
int maxSz = (int)WSTRLEN(workingDir) + sz + 2;
f = (char*)WMALLOC(maxSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
f[0] = '\0';
WSTRNCAT(f, workingDir, maxSz);
if (WSTRLEN(workingDir) > 1) {
WSTRNCAT(f, "/", maxSz);
}
WSTRNCAT(f, pt, maxSz);
pt = f;
}
do {
while (ret == WS_REKEYING || ssh->error == WS_REKEYING) {
ret = wolfSSH_worker(ssh, NULL);
if (ret != WS_SUCCESS && ret == WS_FATAL_ERROR) {
ret = wolfSSH_get_error(ssh);
}
}
ret = wolfSSH_SFTP_Remove(ssh, pt);
err = wolfSSH_get_error(ssh);
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE ||
err == WS_REKEYING) && ret != WS_SUCCESS);
if (ret != WS_SUCCESS) {
if (ret == WS_PERMISSIONS) {
if (SFTP_FPUTS(args, "Insufficient permissions\n") < 0) {