-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsftpclient.c
3075 lines (2866 loc) · 93.5 KB
/
sftpclient.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
/*
* This file is part of the Green End SFTP Server.
* Copyright (C) 2007, 2010, 2011, 2014, 2016 Richard Kettlewell
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#define CLIENT 1
#include "sftpclient.h"
#include "utils.h"
#include "xfns.h"
#include "send.h"
#include "globals.h"
#include "types.h"
#include "alloc.h"
#include "parse.h"
#include "sftp.h"
#include "debug.h"
#include "thread.h"
#include "stat.h"
#include "charset.h"
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <stdarg.h>
#include <sys/wait.h>
#include <time.h>
#include <fcntl.h>
#include <libgen.h>
#include <assert.h>
#include <sys/ioctl.h>
#include <locale.h>
#include <sys/time.h>
#include <langinfo.h>
#include <sys/socket.h>
#include <netdb.h>
#include <signal.h>
#include <limits.h>
#include <termios.h>
#if HAVE_READLINE
# include <readline/readline.h>
# include <readline/history.h>
#endif
// Command options
#define CMD_RAW 0x0001
// Command table
struct command {
// Command name
const char *name;
// Supported options
unsigned options;
// Minimum/maximum number of arguments
int minargs, maxargs;
// Handler function
int (*handler)(int ac, char **av, unsigned options);
// Argument descriptions
const char *args;
// Command description
const char *help;
};
struct client_handle {
size_t len;
char *data;
};
static int sftpin;
static struct allocator allocator;
static struct sftpjob fakejob;
static struct worker fakeworker;
static char *cwd;
static const char *inputpath;
static int inputline;
static int progress_indicators = 1;
static int terminal_width;
static int textmode;
static char *newline;
static char *vendorname, *servername, *serverversion, *serverversions;
static uint64_t serverbuild;
static int stoponerror;
static int echo;
static uint32_t attrmask;
static const char *rename_extension;
static const char *hardlink_extension;
static const char *statvfs_extension;
const struct sftpprotocol *protocol = &sftp_v3;
const char sendtype[] = "request";
/* Command line */
static size_t buffersize = 32768;
static int nrequests = 16;
static const char *subsystem;
static const char *program;
static const char *program_debugpath;
static const char *program_config;
static const char *batchfile;
static int sshversion;
static int compress;
static const char *sshconf;
static const char *sshoptions[1024];
static int nsshoptions;
static int sshverbose;
static int sftpversion = 6;
static int quirk_reverse_symlink;
static int forceversion;
static char *sftp_realpath(const char *path);
enum {
OPT_QUIRK_REVERSE_SYMLINK = 256,
OPT_STOP_ON_ERROR,
OPT_NO_STOP_ON_ERROR,
OPT_PROGRESS,
OPT_NO_PROGRESS,
OPT_ECHO,
OPT_FIX_SIGPIPE,
OPT_FORCE_VERSION,
OPT_PROGRAM_DEBUG_PATH,
OPT_PROGRAM_CONFIG,
};
static const struct option options[] = {
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
{"dropbear", no_argument, 0, 'r'},
{"buffer", required_argument, 0, 'B'},
{"batch", required_argument, 0, 'b'},
{"program", required_argument, 0, 'P'},
{"program-config", required_argument, 0, OPT_PROGRAM_CONFIG},
{"requests", required_argument, 0, 'R'},
{"subsystem", required_argument, 0, 's'},
{"sftp-version", required_argument, 0, 'S'},
{"quirk-reverse-symlink", no_argument, 0, OPT_QUIRK_REVERSE_SYMLINK},
{"stop-on-error", no_argument, 0, OPT_STOP_ON_ERROR},
{"no-stop-on-error", no_argument, 0, OPT_NO_STOP_ON_ERROR},
{"progress", no_argument, 0, OPT_PROGRESS},
{"no-progress", no_argument, 0, OPT_NO_PROGRESS},
{"echo", no_argument, 0, OPT_ECHO},
{"fix-sigpipe", no_argument, 0, OPT_FIX_SIGPIPE},
{"force-version", required_argument, 0, OPT_FORCE_VERSION},
{"debug", no_argument, 0, 'd'},
{"debug-path", required_argument, 0, 'D'},
{"program-debug-path", required_argument, 0, OPT_PROGRAM_DEBUG_PATH},
{"host", required_argument, 0, 'H'},
{"port", required_argument, 0, 'p'},
{"ipv4", no_argument, 0, '4'},
{"ipv6", no_argument, 0, '6'},
{"1", no_argument, 0, '1'},
{"2", no_argument, 0, '2'},
{"C", no_argument, 0, 'C'},
{"F", required_argument, 0, 'F'},
{"o", required_argument, 0, 'o'},
{"v", no_argument, 0, 'v'},
{0, 0, 0, 0}};
/* display usage message and terminate */
static void attribute((noreturn)) help(void) {
sftp_xprintf("Usage:\n"
" sftpclient [OPTIONS] [USER@]HOST\n"
"\n"
"Quick and dirty SFTP client, not intended for production use.\n"
"\n");
sftp_xprintf(
"Options:\n"
" --help, -h Display usage message\n"
" --version, -V Display version number\n"
" -r, --dropbear Use dbclient instead of ssh\n"
" -B, --buffer BYTES Select buffer size (default 8192)\n"
" -b, --batch PATH Read batch file\n"
" -P, --program PATH Execute program as SFTP server\n"
" -R, --requests COUNT Maximum outstanding requests (default 8)\n"
" -s, --subsystem NAME Remote subsystem name\n"
" -S, --sftp-version VER Protocol version to request (default 3)\n"
" --echo Echo commands\n"
" --quirk-openssh Server gets SSH_FXP_SYMLINK backwards\n"
"Options passed to SSH:\n"
" -1, -2 Select protocol version\n"
" -C Enable compression\n"
" -F PATH Use alternative config file\n"
" -o OPTION Pass option to client\n"
" -v Raise logging level\n");
exit(0);
}
/* display version number and terminate */
static void attribute((noreturn)) version(void) {
sftp_xprintf("sftp client version %s\n", VERSION);
exit(0);
}
/* Utilities */
static int attribute((format(printf, 1, 2))) error(const char *fmt, ...) {
va_list ap;
if(inputpath)
fprintf(stderr, "%s:%d ", inputpath, inputline);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
fflush(stderr);
return -1;
}
static int status(void) {
uint32_t status;
char *msg;
/* Cope with half-parsed responses */
fakejob.ptr = fakejob.data + 5;
fakejob.left = fakejob.len - 5;
cpcheck(sftp_parse_uint32(&fakejob, &status));
cpcheck(sftp_parse_string(&fakejob, &msg, 0));
if(status) {
error("%s (%s)", msg, status_to_string(status));
return -1;
} else
return 0;
}
/* Get a response. Picks out the type and ID. */
static uint8_t getresponse(int expected, uint32_t expected_id,
const char *what) {
uint32_t len;
uint8_t type;
if(sftp_xread(sftpin, &len, sizeof len))
sftp_fatal("unexpected EOF from server while reading %s response length",
what);
free(fakejob.data); /* free last job */
fakejob.len = ntohl(len);
fakejob.data = sftp_xmalloc(fakejob.len);
if(sftp_xread(sftpin, fakejob.data, fakejob.len))
sftp_fatal("unexpected EOF from server while reading %s response data",
what);
if(sftp_debugging) {
D(("%s response:", what));
sftp_debug_hexdump(fakejob.data, fakejob.len);
}
fakejob.left = fakejob.len;
fakejob.ptr = fakejob.data;
cpcheck(sftp_parse_uint8(&fakejob, &type));
if(type != SSH_FXP_VERSION) {
cpcheck(sftp_parse_uint32(&fakejob, &fakejob.id));
if(expected_id && fakejob.id != expected_id)
sftp_fatal("wrong ID in response to %s (want %" PRIu32 " got %" PRIu32
" type was %d)",
what, expected_id, fakejob.id, type);
}
if(expected > 0 && type != expected) {
if(type == SSH_FXP_STATUS)
status();
else
sftp_fatal("expected %s response %d got %d", what, expected, type);
}
return type;
}
/* Split a command line */
static int split(char *line, char **av) {
char *arg;
int ac = 0;
while(*line) {
if(isspace((unsigned char)*line)) {
++line;
continue;
}
if(*line == '"') {
arg = av[ac++] = line++;
while(*line && *line != '"') {
if(*line == '\\' && line[1])
++line;
*arg++ = *line++;
}
if(!*line) {
error("unterminated string");
return -1;
}
*arg++ = 0;
line++;
} else {
av[ac++] = line;
while(*line && !isspace((unsigned char)*line))
++line;
if(*line)
*line++ = 0;
}
}
av[ac] = 0;
return ac;
}
static uint32_t newid(void) {
static uint32_t latestid;
do {
++latestid;
} while(!latestid);
return latestid;
}
/* Find path to current directory */
static char *remote_cwd(void) {
if(!cwd) {
if(!(cwd = sftp_realpath(".")))
exit(1);
cwd = sftp_xstrdup(cwd);
}
return cwd;
}
static void progress(const char *path, uint64_t sofar, uint64_t total) {
if(progress_indicators) {
if(!total)
sftp_xprintf("\r%*s\r", terminal_width, "");
else if(total == (uint64_t)-1)
sftp_xprintf("\r%.60s: %12" PRIu64 "b", path, sofar);
else
sftp_xprintf("\r%.60s: %12" PRIu64 "b %3d%%", path, sofar,
(int)(100 * sofar / total));
if(fflush(stdout) < 0)
sftp_fatal("error writing to stdout: %s", strerror(errno));
}
}
/* SFTP operation stubs */
static int sftp_init(void) {
uint32_t version, u32;
uint16_t u16;
/* Send SSH_FXP_INIT */
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, SSH_FXP_INIT);
sftp_send_uint32(&fakeworker, sftpversion);
sftp_send_end(&fakeworker);
/* Parse the version reponse */
if(getresponse(SSH_FXP_VERSION, 0, "SSH_FXP_INIT") != SSH_FXP_VERSION)
return -1;
cpcheck(sftp_parse_uint32(&fakejob, &version));
switch(version) {
case 3:
protocol = &sftp_v3;
break;
case 4:
protocol = &sftp_v4;
break;
case 5:
protocol = &sftp_v5;
break;
case 6:
protocol = &sftp_v6;
break;
default:
return error("server wanted protocol version %" PRIu32, version);
}
attrmask = protocol->attrmask;
/* Extension data */
while(fakejob.left) {
char *xname, *xdata;
size_t xdatalen;
cpcheck(sftp_parse_string(&fakejob, &xname, 0));
cpcheck(sftp_parse_string(&fakejob, &xdata, &xdatalen));
D(("server sent extension '%s'", xname));
/* TODO table-driven extension parsing */
if(!strcmp(xname, "newline")) {
free(newline);
newline = sftp_xstrdup(xdata);
if(!*newline)
return error("cannot cope with empty newline sequence");
/* TODO check newline sequence doesn't contain repeats */
} else if(!strcmp(xname, "vendor-id")) {
struct sftpjob xjob;
char *vn, *sn, *sv;
xjob.a = &allocator;
xjob.ptr = (void *)xdata;
xjob.left = xdatalen;
cpcheck(sftp_parse_string(&xjob, &vn, 0));
cpcheck(sftp_parse_string(&xjob, &sn, 0));
cpcheck(sftp_parse_string(&xjob, &sv, 0));
cpcheck(sftp_parse_uint64(&xjob, &serverbuild));
free(vendorname);
vendorname = sftp_xstrdup(vn);
free(servername);
servername = sftp_xstrdup(sn);
free(serverversion);
serverversion = sftp_xstrdup(sv);
} else if(!strcmp(xname, "versions")) {
free(serverversions);
serverversions = sftp_xstrdup(xdata);
} else if(!strcmp(xname, "[email protected]")) {
/* See commentary in v3.c */
if(!strcmp(xdata, "targetpath-linkpath"))
quirk_reverse_symlink = 1;
else if(!strcmp(xdata, "linkpath-targetpath"))
quirk_reverse_symlink = 0;
else
error("unknown %s value '%s'", xname, xdata);
} else if(!strcmp(xname, "supported")) {
struct sftpjob xjob;
xjob.a = &allocator;
xjob.ptr = (void *)xdata;
xjob.left = xdatalen;
cpcheck(sftp_parse_uint32(&xjob, &attrmask));
cpcheck(sftp_parse_uint32(&xjob, &u32)); /* supported-attribute-bits */
cpcheck(sftp_parse_uint32(&xjob, &u32)); /* supported-open-flags */
cpcheck(sftp_parse_uint32(&xjob, &u32)); /* supported-access-mask */
cpcheck(sftp_parse_uint32(&xjob, &u32)); /* max-read-size */
while(xjob.left)
cpcheck(sftp_parse_string(&xjob, 0, 0)); /* extension-names */
} else if(!strcmp(xname, "supported2")) {
struct sftpjob xjob;
xjob.a = &allocator;
xjob.ptr = (void *)xdata;
xjob.left = xdatalen;
cpcheck(
sftp_parse_uint32(&xjob, &attrmask)); /* supported-attribute-mask */
assert(!(attrmask & SSH_FILEXFER_ATTR_CTIME));
cpcheck(sftp_parse_uint32(&xjob, &u32)); /* supported-attribute-bits */
cpcheck(sftp_parse_uint32(&xjob, &u32)); /* supported-open-flags */
cpcheck(sftp_parse_uint32(&xjob, &u32)); /* supported-access-mask */
cpcheck(sftp_parse_uint32(&xjob, &u32)); /* max-read-size */
cpcheck(sftp_parse_uint16(&xjob, &u16)); /* supported-open-block-vector */
cpcheck(sftp_parse_uint16(&xjob, &u16)); /* supported-block-vector */
cpcheck(sftp_parse_uint32(&xjob, &u32)); /* attrib-extension-count */
while(u32 > 0) {
cpcheck(sftp_parse_string(&xjob, 0, 0)); /* attrib-extension-names */
--u32;
}
cpcheck(sftp_parse_uint32(&xjob, &u32)); /* extension-count */
while(u32 > 0) {
cpcheck(sftp_parse_string(&xjob, 0, 0)); /* extension-names */
--u32;
}
} else if(!strcmp(xname, "[email protected]") &&
!strcmp(xdata, "1")) {
rename_extension = "[email protected]";
} else if(!strcmp(xname, "[email protected]") && !rename_extension) {
rename_extension = "[email protected]";
} else if(!strcmp(xname, "[email protected]") && !strcmp(xdata, "1")) {
hardlink_extension = "[email protected]";
} else if(!strcmp(xname, "[email protected]") && !strcmp(xdata, "2")) {
statvfs_extension = "[email protected]";
}
}
/* Make sure outbound translation will actually work */
if(buffersize < strlen(newline))
buffersize = strlen(newline);
return 0;
}
static char *sftp_fullpath(struct sftpjob *job, const char *path,
unsigned options) {
char *fullpath;
if(path[0] == '/' || (options & CMD_RAW)) {
fullpath = sftp_alloc(job->a, strlen(path) + 1);
strcpy(fullpath, path);
} else {
remote_cwd();
fullpath = sftp_alloc(job->a, strlen(cwd) + strlen(path) + 2);
strcpy(fullpath, cwd);
strcat(fullpath, "/");
strcat(fullpath, path);
}
return fullpath;
}
static char *sftp_realpath(const char *path) {
char *resolved;
uint32_t u32, id;
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, SSH_FXP_REALPATH);
sftp_send_uint32(&fakeworker, id = newid());
sftp_send_path(&fakejob, &fakeworker, path);
sftp_send_end(&fakeworker);
if(getresponse(SSH_FXP_NAME, id, "SSH_FXP_REALPATH") != SSH_FXP_NAME)
return 0;
cpcheck(sftp_parse_uint32(&fakejob, &u32));
if(u32 != 1)
sftp_fatal("wrong count in SSH_FXP_REALPATH reply");
cpcheck(sftp_parse_path(&fakejob, &resolved));
return resolved;
}
static char *sftp_realpath_v6(const char *path, int control_byte,
char **compose, struct sftpattr *attrs) {
char *resolved;
uint32_t u32, id;
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, SSH_FXP_REALPATH);
sftp_send_uint32(&fakeworker, id = newid());
sftp_send_path(&fakejob, &fakeworker, path);
if(control_byte >= 0) {
sftp_send_uint8(&fakeworker, control_byte);
if(compose)
while(*compose)
sftp_send_path(&fakejob, &fakeworker, *compose++);
}
sftp_send_end(&fakeworker);
if(getresponse(SSH_FXP_NAME, id, "SSH_FXP_REALPATH") != SSH_FXP_NAME)
return 0;
cpcheck(sftp_parse_uint32(&fakejob, &u32));
if(u32 != 1)
sftp_fatal("wrong count in SSH_FXP_REALPATH reply");
cpcheck(sftp_parse_path(&fakejob, &resolved));
cpcheck(protocol->parseattrs(&fakejob, attrs));
return resolved;
}
static int sftp_stat(const char *path, struct sftpattr *attrs, uint8_t type) {
uint32_t id;
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, type);
sftp_send_uint32(&fakeworker, id = newid());
sftp_send_path(&fakejob, &fakeworker, path);
if(protocol->version > 3)
sftp_send_uint32(&fakeworker, 0xFFFFFFFF);
sftp_send_end(&fakeworker);
if(getresponse(SSH_FXP_ATTRS, id, "SSH_FXP_*STAT") != SSH_FXP_ATTRS)
return -1;
cpcheck(protocol->parseattrs(&fakejob, attrs));
attrs->name = path;
return 0;
}
static int sftp_fstat(const struct client_handle *hp, struct sftpattr *attrs) {
uint32_t id;
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, SSH_FXP_FSTAT);
sftp_send_uint32(&fakeworker, id = newid());
sftp_send_bytes(&fakeworker, hp->data, hp->len);
if(protocol->version > 3)
sftp_send_uint32(&fakeworker, 0xFFFFFFFF);
sftp_send_end(&fakeworker);
if(getresponse(SSH_FXP_ATTRS, id, "SSH_FXP_FSTAT") != SSH_FXP_ATTRS)
return -1;
cpcheck(protocol->parseattrs(&fakejob, attrs));
return 0;
}
static int sftp_opendir(const char *path, struct client_handle *hp) {
uint32_t id;
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, SSH_FXP_OPENDIR);
sftp_send_uint32(&fakeworker, id = newid());
sftp_send_path(&fakejob, &fakeworker, path);
sftp_send_end(&fakeworker);
if(getresponse(SSH_FXP_HANDLE, id, "SSH_FXP_OPENDIR") != SSH_FXP_HANDLE)
return -1;
cpcheck(sftp_parse_string(&fakejob, &hp->data, &hp->len));
return 0;
}
static int sftp_readdir(const struct client_handle *hp,
struct sftpattr **attrsp, size_t *nattrsp) {
uint32_t id, n;
struct sftpattr *attrs;
char *name, *longname = 0;
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, SSH_FXP_READDIR);
sftp_send_uint32(&fakeworker, id = newid());
sftp_send_bytes(&fakeworker, hp->data, hp->len);
sftp_send_end(&fakeworker);
switch(getresponse(-1, id, "SSH_FXP_READDIR")) {
case SSH_FXP_NAME:
cpcheck(sftp_parse_uint32(&fakejob, &n));
if(n > SIZE_MAX / sizeof(struct sftpattr))
sftp_fatal("too many attributes in SSH_FXP_READDIR response");
attrs = sftp_alloc(fakejob.a, n * sizeof(struct sftpattr));
if(nattrsp)
*nattrsp = n;
if(attrsp)
*attrsp = attrs;
while(n > 0) {
cpcheck(sftp_parse_path(&fakejob, &name));
// all variants require relative paths in responses to SSH_FXP_READDIR
if(name[0] == '/')
sftp_fatal("absolute path in SSH_FXP_READDIR response (%s)", name);
if(protocol->version <= 3)
cpcheck(sftp_parse_path(&fakejob, &longname));
cpcheck(protocol->parseattrs(&fakejob, attrs));
attrs->name = name;
attrs->longname = longname;
++attrs;
--n;
}
return 0;
case SSH_FXP_STATUS:
cpcheck(sftp_parse_uint32(&fakejob, &n));
if(n == SSH_FX_EOF) {
if(nattrsp)
*nattrsp = 0;
if(attrsp)
*attrsp = 0;
return 0;
}
status();
return -1;
default:
sftp_fatal("bogus response to SSH_FXP_READDIR");
}
}
static int sftp_close(const struct client_handle *hp) {
uint32_t id;
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, SSH_FXP_CLOSE);
sftp_send_uint32(&fakeworker, id = newid());
sftp_send_bytes(&fakeworker, hp->data, hp->len);
sftp_send_end(&fakeworker);
getresponse(SSH_FXP_STATUS, id, "SSH_FXP_CLOSE");
return status();
}
static int sftp_setstat(const char *path, const struct sftpattr *attrs) {
uint32_t id;
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, SSH_FXP_SETSTAT);
sftp_send_uint32(&fakeworker, id = newid());
sftp_send_path(&fakejob, &fakeworker, path);
protocol->sendattrs(&fakejob, attrs);
sftp_send_end(&fakeworker);
getresponse(SSH_FXP_STATUS, id, "SSH_FXP_SETSTAT");
return status();
}
static int sftp_fsetstat(const struct client_handle *hp,
const struct sftpattr *attrs) {
uint32_t id;
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, SSH_FXP_FSETSTAT);
sftp_send_uint32(&fakeworker, id = newid());
sftp_send_bytes(&fakeworker, hp->data, hp->len);
protocol->sendattrs(&fakejob, attrs);
sftp_send_end(&fakeworker);
getresponse(SSH_FXP_STATUS, id, "SSH_FXP_FSETSTAT");
return status();
}
static int sftp_rmdir(const char *path) {
uint32_t id;
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, SSH_FXP_RMDIR);
sftp_send_uint32(&fakeworker, id = newid());
sftp_send_path(&fakejob, &fakeworker, path);
sftp_send_end(&fakeworker);
getresponse(SSH_FXP_STATUS, id, "SSH_FXP_RMDIR");
return status();
}
static int sftp_remove(const char *path) {
uint32_t id;
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, SSH_FXP_REMOVE);
sftp_send_uint32(&fakeworker, id = newid());
sftp_send_path(&fakejob, &fakeworker, path);
sftp_send_end(&fakeworker);
getresponse(SSH_FXP_STATUS, id, "SSH_FXP_REMOVE");
return status();
}
static int sftp_rename(const char *oldpath, const char *newpath,
unsigned flags) {
uint32_t id;
/* In v3/4 atomic is assumed, overwrite and native are not available */
if(protocol->version <= 4 && (flags & (uint32_t)~SSH_FXF_RENAME_ATOMIC) != 0)
return error("cannot emulate rename flags %#x in protocol %d", flags,
protocol->version);
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, SSH_FXP_RENAME);
sftp_send_uint32(&fakeworker, id = newid());
sftp_send_path(&fakejob, &fakeworker, oldpath);
sftp_send_path(&fakejob, &fakeworker, newpath);
if(protocol->version >= 5)
sftp_send_uint32(&fakeworker, flags);
sftp_send_end(&fakeworker);
getresponse(SSH_FXP_STATUS, id, "SSH_FXP_RENAME");
return status();
}
static int sftp_prename(const char *oldpath, const char *newpath) {
uint32_t id;
if(!rename_extension)
return error("no posix-rename extension found");
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, SSH_FXP_EXTENDED);
sftp_send_uint32(&fakeworker, id = newid());
sftp_send_string(&fakeworker, rename_extension);
sftp_send_path(&fakejob, &fakeworker, oldpath);
sftp_send_path(&fakejob, &fakeworker, newpath);
sftp_send_end(&fakeworker);
getresponse(SSH_FXP_STATUS, id, rename_extension);
return status();
}
static int sftp_hardlink(const char *targetpath, const char *linkpath) {
uint32_t id;
if(!hardlink_extension)
return error("no hardlink extension found");
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, SSH_FXP_EXTENDED);
sftp_send_uint32(&fakeworker, id = newid());
sftp_send_string(&fakeworker, hardlink_extension);
sftp_send_path(&fakejob, &fakeworker, targetpath);
sftp_send_path(&fakejob, &fakeworker, linkpath);
sftp_send_end(&fakeworker);
getresponse(SSH_FXP_STATUS, id, hardlink_extension);
return status();
}
static int sftp_link(const char *targetpath, const char *linkpath,
int sftp_send_symlink) {
uint32_t id;
if(protocol->version < 6 && !sftp_send_symlink) {
if(hardlink_extension)
return sftp_hardlink(targetpath, linkpath);
return error("hard links not supported in protocol %" PRIu32,
protocol->version);
}
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker,
(protocol->version >= 6 ? SSH_FXP_LINK : SSH_FXP_SYMLINK));
sftp_send_uint32(&fakeworker, id = newid());
if(quirk_reverse_symlink) {
/* OpenSSH server gets SSH_FXP_SYMLINK args back to front
* - see http://bugzilla.mindrot.org/show_bug.cgi?id=861 */
sftp_send_path(&fakejob, &fakeworker, targetpath);
sftp_send_path(&fakejob, &fakeworker, linkpath);
} else {
sftp_send_path(&fakejob, &fakeworker, linkpath);
sftp_send_path(&fakejob, &fakeworker, targetpath);
}
if(protocol->version >= 6)
sftp_send_uint8(&fakeworker, !!sftp_send_symlink);
sftp_send_end(&fakeworker);
getresponse(SSH_FXP_STATUS, id, "SSH_FXP_*LINK");
return status();
}
static int sftp_open(const char *path, uint32_t desired_access, uint32_t flags,
const struct sftpattr *attrs, struct client_handle *hp) {
uint32_t id, pflags = 0;
remote_cwd();
if(protocol->version <= 4) {
/* We must translate the v5/6 style parameters back down to v3/4 */
if(desired_access & ACE4_READ_DATA)
pflags |= SSH_FXF_READ;
if(desired_access & ACE4_WRITE_DATA)
pflags |= SSH_FXF_WRITE;
switch(flags & SSH_FXF_ACCESS_DISPOSITION) {
case SSH_FXF_CREATE_NEW:
pflags |= SSH_FXF_CREAT | SSH_FXF_EXCL;
break;
case SSH_FXF_CREATE_TRUNCATE:
pflags |= SSH_FXF_CREAT | SSH_FXF_TRUNC;
break;
case SSH_FXF_OPEN_OR_CREATE:
pflags |= SSH_FXF_CREAT;
break;
case SSH_FXF_OPEN_EXISTING:
break;
case SSH_FXF_TRUNCATE_EXISTING:
pflags |= SSH_FXF_TRUNC;
break;
default:
return error("unknown SSH_FXF_ACCESS_DISPOSITION %#" PRIx32, flags);
}
if(flags & (SSH_FXF_APPEND_DATA | SSH_FXF_APPEND_DATA_ATOMIC))
pflags |= SSH_FXF_APPEND;
if(flags & SSH_FXF_TEXT_MODE) {
if(protocol->version < 4)
return error("SSH_FXF_TEXT_MODE cannot be emulated in protocol %d",
protocol->version);
else
pflags |= SSH_FXF_TEXT;
}
if(flags & (uint32_t) ~(SSH_FXF_ACCESS_DISPOSITION | SSH_FXF_APPEND_DATA |
SSH_FXF_APPEND_DATA_ATOMIC | SSH_FXF_TEXT_MODE))
return error("future SSH_FXP_OPEN flags (%#" PRIx32
") cannot be emulated in protocol %d",
flags, protocol->version);
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, SSH_FXP_OPEN);
sftp_send_uint32(&fakeworker, id = newid());
sftp_send_path(&fakejob, &fakeworker, path);
sftp_send_uint32(&fakeworker, pflags);
protocol->sendattrs(&fakejob, attrs);
sftp_send_end(&fakeworker);
} else {
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, SSH_FXP_OPEN);
sftp_send_uint32(&fakeworker, id = newid());
sftp_send_path(&fakejob, &fakeworker, path);
sftp_send_uint32(&fakeworker, desired_access);
sftp_send_uint32(&fakeworker, flags);
protocol->sendattrs(&fakejob, attrs);
sftp_send_end(&fakeworker);
}
if(getresponse(SSH_FXP_HANDLE, id, "SSH_FXP_OPEN") != SSH_FXP_HANDLE)
return -1;
cpcheck(sftp_parse_string(&fakejob, &hp->data, &hp->len));
return 0;
}
struct space_available {
uint64_t bytes_on_device;
uint64_t unused_bytes_on_device;
uint64_t bytes_available_to_user;
uint64_t unused_bytes_available_to_user;
uint32_t bytes_per_allocation_unit;
};
static int sftp_space_available(const char *path, struct space_available *as) {
uint32_t id;
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, SSH_FXP_EXTENDED);
sftp_send_uint32(&fakeworker, id = newid());
sftp_send_string(&fakeworker, "space-available");
sftp_send_path(&fakejob, &fakeworker, path);
sftp_send_end(&fakeworker);
if(getresponse(SSH_FXP_EXTENDED_REPLY, id, "space-available") !=
SSH_FXP_EXTENDED_REPLY)
return -1;
cpcheck(sftp_parse_uint64(&fakejob, &as->bytes_on_device));
cpcheck(sftp_parse_uint64(&fakejob, &as->unused_bytes_on_device));
cpcheck(sftp_parse_uint64(&fakejob, &as->bytes_available_to_user));
cpcheck(sftp_parse_uint64(&fakejob, &as->unused_bytes_available_to_user));
cpcheck(sftp_parse_uint32(&fakejob, &as->bytes_per_allocation_unit));
return 0;
}
static int sftp_mkdir(const char *path, mode_t mode) {
struct sftpattr attrs;
uint32_t id;
if(mode == (mode_t)-1)
attrs.valid = 0;
else {
attrs.valid = SSH_FILEXFER_ATTR_PERMISSIONS;
attrs.permissions = mode;
}
attrs.type = SSH_FILEXFER_TYPE_DIRECTORY;
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, SSH_FXP_MKDIR);
sftp_send_uint32(&fakeworker, id = newid());
sftp_send_path(&fakejob, &fakeworker, path);
protocol->sendattrs(&fakejob, &attrs);
sftp_send_end(&fakeworker);
getresponse(SSH_FXP_STATUS, id, "SSH_FXP_MKDIR");
return status();
}
static char *sftp_readlink(const char *path) {
char *resolved;
uint32_t u32, id;
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, SSH_FXP_READLINK);
sftp_send_uint32(&fakeworker, id = newid());
sftp_send_path(&fakejob, &fakeworker, path);
sftp_send_end(&fakeworker);
if(getresponse(SSH_FXP_NAME, id, "SSH_FXP_READLINK") != SSH_FXP_NAME)
return 0;
cpcheck(sftp_parse_uint32(&fakejob, &u32));
if(u32 != 1)
sftp_fatal("wrong count in SSH_FXP_READLINK reply");
cpcheck(sftp_parse_path(&fakejob, &resolved));
return resolved;
}
static int sftp_text_seek(const struct client_handle *hp, uint64_t line) {
uint32_t id;
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, SSH_FXP_EXTENDED);
sftp_send_uint32(&fakeworker, id = newid());
sftp_send_string(&fakeworker, "text-seek");
sftp_send_bytes(&fakeworker, hp->data, hp->len);
sftp_send_uint64(&fakeworker, line);
sftp_send_end(&fakeworker);
getresponse(SSH_FXP_STATUS, id, "text-seek");
return status();
}
struct statvfs_reply {
uint64_t bsize;
uint64_t frsize;
uint64_t blocks;
uint64_t bfree;
uint64_t bavail;
uint64_t files;
uint64_t ffree;
uint64_t favail;
uint64_t fsid;
uint64_t flags;
uint64_t namemax;
};
static int sftp_statvfs(const char *path, struct statvfs_reply *sr) {
uint32_t id;
if(!statvfs_extension)
return error("no statvfs extension found");
sftp_send_begin(&fakeworker);
sftp_send_uint8(&fakeworker, SSH_FXP_EXTENDED);
sftp_send_uint32(&fakeworker, id = newid());
sftp_send_string(&fakeworker, statvfs_extension);
sftp_send_path(&fakejob, &fakeworker, path);
sftp_send_end(&fakeworker);
getresponse(SSH_FXP_EXTENDED_REPLY, id, statvfs_extension);
sftp_memset(sr, 0,
sizeof *sr); // workaround overenthusiastic warning with LTO
cpcheck(sftp_parse_uint64(&fakejob, &sr->bsize));
cpcheck(sftp_parse_uint64(&fakejob, &sr->frsize));
cpcheck(sftp_parse_uint64(&fakejob, &sr->blocks));
cpcheck(sftp_parse_uint64(&fakejob, &sr->bfree));
cpcheck(sftp_parse_uint64(&fakejob, &sr->bavail));
cpcheck(sftp_parse_uint64(&fakejob, &sr->files));
cpcheck(sftp_parse_uint64(&fakejob, &sr->ffree));
cpcheck(sftp_parse_uint64(&fakejob, &sr->favail));
cpcheck(sftp_parse_uint64(&fakejob, &sr->fsid));
cpcheck(sftp_parse_uint64(&fakejob, &sr->flags));
cpcheck(sftp_parse_uint64(&fakejob, &sr->namemax));
return 0;
}
/* Command line operations */
static int cmd_pwd(int attribute((unused)) ac, char attribute((unused)) * *av,
unsigned attribute((unused)) options) {
sftp_xprintf("%s\n", remote_cwd());
return 0;
}
static int cmd_cd(int attribute((unused)) ac, char **av,
unsigned attribute((unused)) options) {
const char *newcwd;
struct sftpattr attrs;
remote_cwd();
if(protocol->version >= 6) {
/* We can do this more efficiently */
newcwd = sftp_realpath_v6(cwd, SSH_FXP_REALPATH_STAT_ALWAYS, av, &attrs);
if(!newcwd)
return -1;
} else {
if(av[0][0] == '/')
newcwd = sftp_realpath(av[0]);
else {
char *full = sftp_alloc(fakejob.a, strlen(cwd) + strlen(av[0]) + 2);