forked from AnttiTakala/SSH2DOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sftp.c
998 lines (926 loc) · 25.1 KB
/
sftp.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
/*
* sftp.c: SFTP generic client code.
*
* Taken from the PuTTY source.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "channel.h"
#include "int64.h"
#include "macros.h"
#include "sftp.h"
#include "ssh.h"
#include "transprt.h"
#include "common.h"
#if defined (MEMWATCH)
#include "memwatch.h"
#endif
/* external structures, variables */
extern Packet pktin; /* incoming SSH packet */
/* local variables */
struct sftp_packet {
unsigned char *data;
unsigned long length, maxlen;
unsigned long savedpos;
int type;
};
static const char *fxp_error_message;
static int fxp_errtype;
static unsigned pendlen = 0; /* we have unused SFTP bytes */
static char *pendbuf = NULL; /* buffer for unused SFTP bytes */
/* ----------------------------------------------------------------------
* SFTP packet construction functions.
*/
static void sftp_pkt_ensure(struct sftp_packet *pkt, int length)
{
if (pkt->maxlen < length) {
pkt->maxlen = length + 256;
if((pkt->data = realloc(pkt->data, pkt->maxlen)) == NULL)
fatal("Memory allocation error. %s: %d", __FILE__, __LINE__);
}
}
static void sftp_pkt_adddata(struct sftp_packet *pkt, void *data, int len)
{
pkt->length += len;
sftp_pkt_ensure(pkt, pkt->length);
memcpy(pkt->data + pkt->length - len, data, len);
}
static void sftp_pkt_addbyte(struct sftp_packet *pkt, unsigned char byte)
{
sftp_pkt_adddata(pkt, &byte, 1);
}
static struct sftp_packet *sftp_pkt_init(int pkt_type)
{
struct sftp_packet *pkt;
if((pkt = malloc(sizeof(struct sftp_packet))) == NULL)
fatal("Memory allocation error. %s: %d", __FILE__, __LINE__);
pkt->data = NULL;
pkt->savedpos = -1;
pkt->length = 0;
pkt->maxlen = 0;
sftp_pkt_addbyte(pkt, (unsigned char) pkt_type);
return pkt;
}
static void sftp_pkt_adduint32(struct sftp_packet *pkt,
unsigned long value)
{
unsigned char x[4];
PUT_32BIT_MSB_FIRST(x, value);
sftp_pkt_adddata(pkt, x, 4);
}
static void sftp_pkt_adduint64(struct sftp_packet *pkt, uint64 value)
{
unsigned char x[8];
PUT_32BIT_MSB_FIRST(x, value.hi);
PUT_32BIT_MSB_FIRST(x + 4, value.lo);
sftp_pkt_adddata(pkt, x, 8);
}
static void sftp_pkt_addstring_start(struct sftp_packet *pkt)
{
sftp_pkt_adduint32(pkt, 0);
pkt->savedpos = pkt->length;
}
static void sftp_pkt_addstring_str(struct sftp_packet *pkt, char *data)
{
sftp_pkt_adddata(pkt, data, strlen(data));
PUT_32BIT_MSB_FIRST(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
}
static void sftp_pkt_addstring_data(struct sftp_packet *pkt,
char *data, int len)
{
sftp_pkt_adddata(pkt, data, len);
PUT_32BIT_MSB_FIRST(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
}
static void sftp_pkt_addstring(struct sftp_packet *pkt, char *data)
{
sftp_pkt_addstring_start(pkt);
sftp_pkt_addstring_str(pkt, data);
}
static void sftp_pkt_addattrs(struct sftp_packet *pkt, struct fxp_attrs attrs)
{
sftp_pkt_adduint32(pkt, attrs.flags);
if (attrs.flags & SSH_FILEXFER_ATTR_SIZE) {
sftp_pkt_adduint32(pkt, attrs.size.hi);
sftp_pkt_adduint32(pkt, attrs.size.lo);
}
if (attrs.flags & SSH_FILEXFER_ATTR_UIDGID) {
sftp_pkt_adduint32(pkt, attrs.uid);
sftp_pkt_adduint32(pkt, attrs.gid);
}
if (attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
sftp_pkt_adduint32(pkt, attrs.permissions);
}
if (attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME) {
sftp_pkt_adduint32(pkt, attrs.atime);
sftp_pkt_adduint32(pkt, attrs.mtime);
}
if (attrs.flags & SSH_FILEXFER_ATTR_EXTENDED) {
/*
* We currently don't support sending any extended
* attributes.
*/
}
}
/* ----------------------------------------------------------------------
* SFTP packet decode functions.
*/
static unsigned char sftp_pkt_getbyte(struct sftp_packet *pkt)
{
unsigned char value;
if (pkt->length - pkt->savedpos < 1)
return 0; /* arrgh, no way to decline (FIXME?) */
value = (unsigned char) pkt->data[pkt->savedpos];
pkt->savedpos++;
return value;
}
static unsigned long sftp_pkt_getuint32(struct sftp_packet *pkt)
{
unsigned long value;
if (pkt->length - pkt->savedpos < 4)
return 0; /* arrgh, no way to decline (FIXME?) */
value = GET_32BIT_MSB_FIRST(pkt->data + pkt->savedpos);
pkt->savedpos += 4;
return value;
}
static void sftp_pkt_getstring(struct sftp_packet *pkt,
char **p, int *length)
{
*p = NULL;
if (pkt->length - pkt->savedpos < 4)
return;
*length = GET_32BIT_MSB_FIRST(pkt->data + pkt->savedpos);
pkt->savedpos += 4;
if (pkt->length - pkt->savedpos < *length)
return;
*p = pkt->data + pkt->savedpos;
pkt->savedpos += *length;
}
static struct fxp_attrs sftp_pkt_getattrs(struct sftp_packet *pkt)
{
struct fxp_attrs ret;
ret.flags = sftp_pkt_getuint32(pkt);
if (ret.flags & SSH_FILEXFER_ATTR_SIZE) {
unsigned long hi, lo;
hi = sftp_pkt_getuint32(pkt);
lo = sftp_pkt_getuint32(pkt);
ret.size = uint64_make(hi, lo);
}
if (ret.flags & SSH_FILEXFER_ATTR_UIDGID) {
ret.uid = sftp_pkt_getuint32(pkt);
ret.gid = sftp_pkt_getuint32(pkt);
}
if (ret.flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
ret.permissions = sftp_pkt_getuint32(pkt);
}
if (ret.flags & SSH_FILEXFER_ATTR_ACMODTIME) {
ret.atime = sftp_pkt_getuint32(pkt);
ret.mtime = sftp_pkt_getuint32(pkt);
}
if (ret.flags & SSH_FILEXFER_ATTR_EXTENDED) {
int count;
count = sftp_pkt_getuint32(pkt);
while (count--) {
char *str;
int len;
/*
* We should try to analyse these, if we ever find one
* we recognise.
*/
sftp_pkt_getstring(pkt, &str, &len);
sftp_pkt_getstring(pkt, &str, &len);
}
}
return ret;
}
static void sftp_pkt_free(struct sftp_packet *pkt)
{
if (pkt->data)
free(pkt->data);
free(pkt);
}
/* ----------------------------------------------------------------------
* Send and receive packet functions.
*/
static void sftp_send(struct sftp_packet *pkt)
{
unsigned char x[4];
PUT_32BIT_MSB_FIRST(x, pkt->length);
SSH2_Channel_Send(x, 4);
SSH2_Channel_Send(pkt->data, pkt->length);
sftp_pkt_free(pkt);
}
/*
* Get data from the SSH layer to the SFTP layer.
* Return NULL if connecton is closed or error occured
*/
static int sftp_recvdata(char *buf, int len)
{
/*
* If we have enough pending data, no problem. However,
* if the SFTP layer needs more than we have, we must
* get enough data with blocking from the SSH layer
*/
restart:
if (pendlen >= len) { /* we have enough? */
memcpy(buf, pendbuf, len);
memmove(pendbuf, pendbuf + len, pendlen - len);
pendlen -= len;
if(pendlen == 0){ /* if we have no more, free the buffer */
free(pendbuf);
pendbuf = NULL;
} else {
if((pendbuf = realloc(pendbuf, pendlen)) == NULL){
fatal("Memory allocation error. %s: %d", __FILE__, __LINE__);
}
}
return len;
} else { /* we must wait for more input from the SSH layer */
if(SSH2_Channel_Read(SSH_MSG_CHANNEL_DATA))
return 0;
pktin.length -= 9;
pendlen += pktin.length;
if((pendbuf = (pendbuf ? realloc(pendbuf, pendlen) :
malloc(pendlen))) == NULL)
fatal("Memory allocation error. %s: %d", __FILE__, __LINE__);
memcpy(pendbuf + pendlen - pktin.length, pktin.ptr + 4, pktin.length);
goto restart;
}
}
struct sftp_packet *sftp_recv(void)
{
struct sftp_packet *pkt;
unsigned char x[4];
if (!sftp_recvdata(x, 4))
return NULL;
if((pkt = malloc(sizeof(struct sftp_packet))) == NULL)
fatal("Memory allocation error. %s: %d", __FILE__, __LINE__);
pkt->savedpos = 0;
pkt->length = pkt->maxlen = GET_32BIT_MSB_FIRST(x);
if((pkt->data = malloc(pkt->length)) == NULL)
fatal("Memory allocation error. %s: %d", __FILE__, __LINE__);
if (!sftp_recvdata(pkt->data, pkt->length)) {
sftp_pkt_free(pkt);
return NULL;
}
pkt->type = sftp_pkt_getbyte(pkt);
return pkt;
}
/* ----------------------------------------------------------------------
* String handling routines.
*/
static char *mkstr(char *s, int len)
{
char *p;
if((p = (char *)malloc(len + 1)) == NULL)
fatal("Memory allocation error. %s: %d", __FILE__, __LINE__);
memcpy(p, s, len);
p[len] = '\0';
return p;
}
/* ----------------------------------------------------------------------
* SFTP primitives.
*/
/*
* Deal with (and free) an FXP_STATUS packet. Return 1 if
* SSH_FX_OK, 0 if SSH_FX_EOF, and -1 for anything else (error).
* Also place the status into fxp_errtype.
*/
static int fxp_got_status(struct sftp_packet *pktin)
{
static const char *const messages[] = {
/* SSH_FX_OK. The only time we will display a _message_ for this
* is if we were expecting something other than FXP_STATUS on
* success, so this is actually an error message! */
"unexpected OK response",
"end of file",
"no such file or directory",
"permission denied",
"failure",
"bad message",
"no connection",
"connection lost",
"operation unsupported",
};
if (pktin->type != SSH_FXP_STATUS) {
fxp_error_message = "expected FXP_STATUS packet";
fxp_errtype = -1;
} else {
fxp_errtype = sftp_pkt_getuint32(pktin);
if (fxp_errtype < 0 ||
fxp_errtype >= sizeof(messages) / sizeof(*messages))
fxp_error_message = "unknown error code";
else
fxp_error_message = messages[fxp_errtype];
}
if (fxp_errtype == SSH_FX_OK)
return 1;
else if (fxp_errtype == SSH_FX_EOF)
return 0;
else
return -1;
}
static void fxp_internal_error(char *msg)
{
fxp_error_message = msg;
fxp_errtype = -1;
}
const char *fxp_error(void)
{
return fxp_error_message;
}
int fxp_error_type(void)
{
return fxp_errtype;
}
/*
* Perform exchange of init/version packets. Return 0 on failure.
*/
int fxp_init(void)
{
struct sftp_packet *pktout, *pktin;
int remotever;
pktout = sftp_pkt_init(SSH_FXP_INIT);
sftp_pkt_adduint32(pktout, SFTP_PROTO_VERSION);
sftp_send(pktout);
pktin = sftp_recv();
if (!pktin) {
fxp_internal_error("could not connect");
return 0;
}
if (pktin->type != SSH_FXP_VERSION) {
fxp_internal_error("did not receive FXP_VERSION");
sftp_pkt_free(pktin);
return 0;
}
remotever = sftp_pkt_getuint32(pktin);
if (remotever > SFTP_PROTO_VERSION) {
fxp_internal_error
("remote protocol is more advanced than we support");
sftp_pkt_free(pktin);
return 0;
}
/*
* In principle, this packet might also contain extension-
* string pairs. We should work through them and look for any
* we recognise. In practice we don't currently do so because
* we know we don't recognise _any_.
*/
sftp_pkt_free(pktin);
return 1;
}
/*
* Canonify a pathname.
*/
char *fxp_realpath(char *path)
{
struct sftp_packet *pktin, *pktout;
int id;
pktout = sftp_pkt_init(SSH_FXP_REALPATH);
sftp_pkt_adduint32(pktout, 0x123); /* request id */
sftp_pkt_addstring_start(pktout);
sftp_pkt_addstring_str(pktout, path);
sftp_send(pktout);
pktin = sftp_recv();
if (!pktin) {
fxp_internal_error("did not receive a valid SFTP packet\n");
return NULL;
}
id = sftp_pkt_getuint32(pktin);
if (id != 0x123) {
fxp_internal_error("request ID mismatch\n");
sftp_pkt_free(pktin);
return NULL;
}
if (pktin->type == SSH_FXP_NAME) {
int count;
char *path;
int len;
count = sftp_pkt_getuint32(pktin);
if (count != 1) {
fxp_internal_error("REALPATH returned name count != 1\n");
sftp_pkt_free(pktin);
return NULL;
}
sftp_pkt_getstring(pktin, &path, &len);
if (!path) {
fxp_internal_error("REALPATH returned malformed FXP_NAME\n");
sftp_pkt_free(pktin);
return NULL;
}
path = mkstr(path, len);
sftp_pkt_free(pktin);
return path;
} else {
fxp_got_status(pktin);
sftp_pkt_free(pktin);
return NULL;
}
}
/*
* Open a file.
*/
struct fxp_handle *fxp_open(char *path, int type)
{
struct sftp_packet *pktin, *pktout;
int id;
pktout = sftp_pkt_init(SSH_FXP_OPEN);
sftp_pkt_adduint32(pktout, 0x567); /* request id */
sftp_pkt_addstring(pktout, path);
sftp_pkt_adduint32(pktout, type);
sftp_pkt_adduint32(pktout, 0); /* (FIXME) empty ATTRS structure */
sftp_send(pktout);
pktin = sftp_recv();
if (!pktin) {
fxp_internal_error("did not receive a valid SFTP packet\n");
return NULL;
}
id = sftp_pkt_getuint32(pktin);
if (id != 0x567) {
fxp_internal_error("request ID mismatch\n");
sftp_pkt_free(pktin);
return NULL;
}
if (pktin->type == SSH_FXP_HANDLE) {
char *hstring;
struct fxp_handle *handle;
int len;
sftp_pkt_getstring(pktin, &hstring, &len);
if (!hstring) {
fxp_internal_error("OPEN returned malformed FXP_HANDLE\n");
sftp_pkt_free(pktin);
return NULL;
}
if((handle = malloc(sizeof(struct fxp_handle))) == NULL)
fatal("Memory allocation error. %s: %d", __FILE__, __LINE__);
handle->hstring = mkstr(hstring, len);
handle->hlen = len;
sftp_pkt_free(pktin);
return handle;
} else {
fxp_got_status(pktin);
sftp_pkt_free(pktin);
return NULL;
}
}
/*
* Open a directory.
*/
struct fxp_handle *fxp_opendir(char *path)
{
struct sftp_packet *pktin, *pktout;
int id;
pktout = sftp_pkt_init(SSH_FXP_OPENDIR);
sftp_pkt_adduint32(pktout, 0x456); /* request id */
sftp_pkt_addstring(pktout, path);
sftp_send(pktout);
pktin = sftp_recv();
if (!pktin) {
fxp_internal_error("did not receive a valid SFTP packet\n");
return NULL;
}
id = sftp_pkt_getuint32(pktin);
if (id != 0x456) {
fxp_internal_error("request ID mismatch\n");
sftp_pkt_free(pktin);
return NULL;
}
if (pktin->type == SSH_FXP_HANDLE) {
char *hstring;
struct fxp_handle *handle;
int len;
sftp_pkt_getstring(pktin, &hstring, &len);
if (!hstring) {
fxp_internal_error("OPENDIR returned malformed FXP_HANDLE\n");
sftp_pkt_free(pktin);
return NULL;
}
if((handle = malloc(sizeof(struct fxp_handle))) == NULL)
fatal("Memory allocation error. %s: %d", __FILE__, __LINE__);
handle->hstring = mkstr(hstring, len);
handle->hlen = len;
sftp_pkt_free(pktin);
return handle;
} else {
fxp_got_status(pktin);
sftp_pkt_free(pktin);
return NULL;
}
}
/*
* Close a file/dir.
*/
void fxp_close(struct fxp_handle *handle)
{
struct sftp_packet *pktin, *pktout;
int id;
pktout = sftp_pkt_init(SSH_FXP_CLOSE);
sftp_pkt_adduint32(pktout, 0x789); /* request id */
sftp_pkt_addstring_start(pktout);
sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
sftp_send(pktout);
pktin = sftp_recv();
if (!pktin) {
fxp_internal_error("did not receive a valid SFTP packet\n");
return;
}
id = sftp_pkt_getuint32(pktin);
if (id != 0x789) {
fxp_internal_error("request ID mismatch\n");
sftp_pkt_free(pktin);
return;
}
fxp_got_status(pktin);
sftp_pkt_free(pktin);
free(handle->hstring);
free(handle);
}
int fxp_mkdir(char *path)
{
struct sftp_packet *pktin, *pktout;
int id;
pktout = sftp_pkt_init(SSH_FXP_MKDIR);
sftp_pkt_adduint32(pktout, 0x234); /* request id */
sftp_pkt_addstring(pktout, path);
sftp_pkt_adduint32(pktout, 0); /* (FIXME) empty ATTRS structure */
sftp_send(pktout);
pktin = sftp_recv();
if (!pktin) {
fxp_internal_error("did not receive a valid SFTP packet\n");
return 0;
}
id = sftp_pkt_getuint32(pktin);
if (id != 0x234) {
fxp_internal_error("request ID mismatch\n");
sftp_pkt_free(pktin);
return 0;
}
id = fxp_got_status(pktin);
sftp_pkt_free(pktin);
if (id != 1) {
return 0;
}
return 1;
}
int fxp_rmdir(char *path)
{
struct sftp_packet *pktin, *pktout;
int id;
pktout = sftp_pkt_init(SSH_FXP_RMDIR);
sftp_pkt_adduint32(pktout, 0x345); /* request id */
sftp_pkt_addstring(pktout, path);
sftp_send(pktout);
pktin = sftp_recv();
if (!pktin) {
fxp_internal_error("did not receive a valid SFTP packet\n");
return 0;
}
id = sftp_pkt_getuint32(pktin);
if (id != 0x345) {
fxp_internal_error("request ID mismatch\n");
sftp_pkt_free(pktin);
return 0;
}
id = fxp_got_status(pktin);
sftp_pkt_free(pktin);
if (id != 1) {
return 0;
}
return 1;
}
int fxp_remove(char *fname)
{
struct sftp_packet *pktin, *pktout;
int id;
pktout = sftp_pkt_init(SSH_FXP_REMOVE);
sftp_pkt_adduint32(pktout, 0x678); /* request id */
sftp_pkt_addstring(pktout, fname);
sftp_send(pktout);
pktin = sftp_recv();
if (!pktin) {
fxp_internal_error("did not receive a valid SFTP packet\n");
return 0;
}
id = sftp_pkt_getuint32(pktin);
if (id != 0x678) {
fxp_internal_error("request ID mismatch\n");
sftp_pkt_free(pktin);
return 0;
}
id = fxp_got_status(pktin);
sftp_pkt_free(pktin);
if (id != 1) {
return 0;
}
return 1;
}
int fxp_rename(char *srcfname, char *dstfname)
{
struct sftp_packet *pktin, *pktout;
int id;
pktout = sftp_pkt_init(SSH_FXP_RENAME);
sftp_pkt_adduint32(pktout, 0x678); /* request id */
sftp_pkt_addstring(pktout, srcfname);
sftp_pkt_addstring(pktout, dstfname);
sftp_send(pktout);
pktin = sftp_recv();
if (!pktin) {
fxp_internal_error("did not receive a valid SFTP packet\n");
return 0;
}
id = sftp_pkt_getuint32(pktin);
if (id != 0x678) {
fxp_internal_error("request ID mismatch\n");
sftp_pkt_free(pktin);
return 0;
}
id = fxp_got_status(pktin);
sftp_pkt_free(pktin);
if (id != 1) {
return 0;
}
return 1;
}
/*
* Retrieve the attributes of a file. We have fxp_stat which works
* on filenames, and fxp_fstat which works on open file handles.
*/
int fxp_stat(char *fname, struct fxp_attrs *attrs)
{
struct sftp_packet *pktin, *pktout;
int id;
pktout = sftp_pkt_init(SSH_FXP_STAT);
sftp_pkt_adduint32(pktout, 0x678); /* request id */
sftp_pkt_addstring(pktout, fname);
sftp_send(pktout);
pktin = sftp_recv();
if (!pktin) {
fxp_internal_error("did not receive a valid SFTP packet\n");
return 0;
}
id = sftp_pkt_getuint32(pktin);
if (id != 0x678) {
fxp_internal_error("request ID mismatch\n");
sftp_pkt_free(pktin);
return 0;
}
if (pktin->type == SSH_FXP_ATTRS) {
*attrs = sftp_pkt_getattrs(pktin);
sftp_pkt_free(pktin);
return 1;
} else {
fxp_got_status(pktin);
sftp_pkt_free(pktin);
return 0;
}
}
int fxp_fstat(struct fxp_handle *handle, struct fxp_attrs *attrs)
{
struct sftp_packet *pktin, *pktout;
int id;
pktout = sftp_pkt_init(SSH_FXP_FSTAT);
sftp_pkt_adduint32(pktout, 0x678); /* request id */
sftp_pkt_addstring_start(pktout);
sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
sftp_send(pktout);
pktin = sftp_recv();
if (!pktin) {
fxp_internal_error("did not receive a valid SFTP packet\n");
return 0;
}
id = sftp_pkt_getuint32(pktin);
if (id != 0x678) {
fxp_internal_error("request ID mismatch\n");
sftp_pkt_free(pktin);
return 0;
}
if (pktin->type == SSH_FXP_ATTRS) {
*attrs = sftp_pkt_getattrs(pktin);
sftp_pkt_free(pktin);
return 1;
} else {
fxp_got_status(pktin);
sftp_pkt_free(pktin);
return 0;
}
}
/*
* Set the attributes of a file.
*/
int fxp_setstat(char *fname, struct fxp_attrs attrs)
{
struct sftp_packet *pktin, *pktout;
int id;
pktout = sftp_pkt_init(SSH_FXP_SETSTAT);
sftp_pkt_adduint32(pktout, 0x678); /* request id */
sftp_pkt_addstring(pktout, fname);
sftp_pkt_addattrs(pktout, attrs);
sftp_send(pktout);
pktin = sftp_recv();
if (!pktin) {
fxp_internal_error("did not receive a valid SFTP packet\n");
return 0;
}
id = sftp_pkt_getuint32(pktin);
if (id != 0x678) {
fxp_internal_error("request ID mismatch\n");
sftp_pkt_free(pktin);
return 0;
}
id = fxp_got_status(pktin);
sftp_pkt_free(pktin);
if (id != 1) {
return 0;
}
return 1;
}
int fxp_fsetstat(struct fxp_handle *handle, struct fxp_attrs attrs)
{
struct sftp_packet *pktin, *pktout;
int id;
pktout = sftp_pkt_init(SSH_FXP_FSETSTAT);
sftp_pkt_adduint32(pktout, 0x678); /* request id */
sftp_pkt_addstring_start(pktout);
sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
sftp_pkt_addattrs(pktout, attrs);
sftp_send(pktout);
pktin = sftp_recv();
if (!pktin) {
fxp_internal_error("did not receive a valid SFTP packet\n");
return 0;
}
id = sftp_pkt_getuint32(pktin);
if (id != 0x678) {
fxp_internal_error("request ID mismatch\n");
sftp_pkt_free(pktin);
return 0;
}
id = fxp_got_status(pktin);
sftp_pkt_free(pktin);
if (id != 1) {
return 0;
}
return 1;
}
/*
* Read from a file. Returns the number of bytes read, or -1 on an
* error, or possibly 0 if EOF. (I'm not entirely sure whether it
* will return 0 on EOF, or return -1 and store SSH_FX_EOF in the
* error indicator. It might even depend on the SFTP server.)
*/
int fxp_read(struct fxp_handle *handle, char *buffer, uint64 offset,
int len)
{
struct sftp_packet *pktin, *pktout;
int id;
pktout = sftp_pkt_init(SSH_FXP_READ);
sftp_pkt_adduint32(pktout, 0xBCD); /* request id */
sftp_pkt_addstring_start(pktout);
sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
sftp_pkt_adduint64(pktout, offset);
sftp_pkt_adduint32(pktout, len);
sftp_send(pktout);
pktin = sftp_recv();
if (!pktin) {
fxp_internal_error("did not receive a valid SFTP packet\n");
return -1;
}
id = sftp_pkt_getuint32(pktin);
if (id != 0xBCD) {
fxp_internal_error("request ID mismatch");
sftp_pkt_free(pktin);
return -1;
}
if (pktin->type == SSH_FXP_DATA) {
char *str;
int rlen;
sftp_pkt_getstring(pktin, &str, &rlen);
if (rlen > len || rlen < 0) {
fxp_internal_error("READ returned more bytes than requested");
sftp_pkt_free(pktin);
return -1;
}
memcpy(buffer, str, rlen);
sftp_pkt_free(pktin);
return rlen;
} else {
fxp_got_status(pktin);
sftp_pkt_free(pktin);
return -1;
}
}
/*
* Read from a directory.
*/
struct fxp_names *fxp_readdir(struct fxp_handle *handle)
{
struct sftp_packet *pktin, *pktout;
int id;
pktout = sftp_pkt_init(SSH_FXP_READDIR);
sftp_pkt_adduint32(pktout, 0xABC); /* request id */
sftp_pkt_addstring_start(pktout);
sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
sftp_send(pktout);
pktin = sftp_recv();
if (!pktin) {
fxp_internal_error("did not receive a valid SFTP packet\n");
return NULL;
}
id = sftp_pkt_getuint32(pktin);
if (id != 0xABC) {
fxp_internal_error("request ID mismatch\n");
sftp_pkt_free(pktin);
return NULL;
}
if (pktin->type == SSH_FXP_NAME) {
struct fxp_names *ret;
int i;
if((ret = malloc(sizeof(struct fxp_names))) == NULL)
fatal("Memory allocation error. %s: %d", __FILE__, __LINE__);
ret->nnames = sftp_pkt_getuint32(pktin);
if((ret->names = malloc(ret->nnames * sizeof(struct fxp_name))) == NULL)
fatal("Memory allocation error. %s: %d", __FILE__, __LINE__);
for (i = 0; i < ret->nnames; i++) {
char *str;
int len;
sftp_pkt_getstring(pktin, &str, &len);
ret->names[i].filename = mkstr(str, len);
sftp_pkt_getstring(pktin, &str, &len);
ret->names[i].longname = mkstr(str, len);
ret->names[i].attrs = sftp_pkt_getattrs(pktin);
}
sftp_pkt_free(pktin);
return ret;
} else {
fxp_got_status(pktin);
sftp_pkt_free(pktin);
return NULL;
}
}
/*
* Write to a file. Returns 0 on error, 1 on OK.
*/
int fxp_write(struct fxp_handle *handle, char *buffer, uint64 offset,
int len)
{
struct sftp_packet *pktin, *pktout;
int id;
pktout = sftp_pkt_init(SSH_FXP_WRITE);
sftp_pkt_adduint32(pktout, 0xDCB); /* request id */
sftp_pkt_addstring_start(pktout);
sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
sftp_pkt_adduint64(pktout, offset);
sftp_pkt_addstring_start(pktout);
sftp_pkt_addstring_data(pktout, buffer, len);
sftp_send(pktout);
pktin = sftp_recv();
if (!pktin) {
fxp_internal_error("did not receive a valid SFTP packet\n");
return 0;
}
id = sftp_pkt_getuint32(pktin);
if (id != 0xDCB) {
fxp_internal_error("request ID mismatch\n");
sftp_pkt_free(pktin);
return 0;
}
fxp_got_status(pktin);
sftp_pkt_free(pktin);
return fxp_errtype == SSH_FX_OK;
}
/*
* Free up an fxp_names structure.
*/
void fxp_free_names(struct fxp_names *names)
{
int i;
for (i = 0; i < names->nnames; i++) {
free(names->names[i].filename);
free(names->names[i].longname);
}
free(names->names);
free(names);
}
/*
* Duplicate an fxp_name structure.
*/
struct fxp_name *fxp_dup_name(struct fxp_name *name)
{
struct fxp_name *ret;
if((ret = malloc(sizeof(struct fxp_name))) == NULL)
fatal("Memory allocation error. %s: %d", __FILE__, __LINE__);
ret->filename = strdup(name->filename);
ret->longname = strdup(name->longname);
ret->attrs = name->attrs; /* structure copy */
return ret;
}
/*
* Free up an fxp_name structure.
*/
void fxp_free_name(struct fxp_name *name)
{
free(name->filename);
free(name->longname);
free(name);
}