-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathsftpserver.c
More file actions
1869 lines (1618 loc) · 51.3 KB
/
sftpserver.c
File metadata and controls
1869 lines (1618 loc) · 51.3 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
/*
* sftpserver.c - server based function for the sftp protocol
*
* This file is part of the SSH Library
*
* Copyright (c) 2005 Aris Adamantiadis
* Copyright (c) 2022 Zeyu Sheng <shengzeyu19_98@163.com>
*
* The SSH Library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at your
* option) any later version.
*
* The SSH Library 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 Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the SSH Library; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*/
#include "config.h"
#ifndef _WIN32
#include <netinet/in.h>
#include <arpa/inet.h>
#include <dirent.h>
#include <sys/statvfs.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif /* HAVE_SYS_TIME_H */
#ifdef HAVE_SYS_UTIME_H
#include <sys/utime.h>
#endif /* HAVE_SYS_UTIME_H */
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include "libssh/libssh.h"
#include "libssh/sftp.h"
#include "libssh/sftp_priv.h"
#include "libssh/sftpserver.h"
#include "libssh/ssh2.h"
#include "libssh/priv.h"
#include "libssh/buffer.h"
#include "libssh/misc.h"
#define SFTP_HANDLES 256
#define MAX_ENTRIES_NUM_IN_PACKET 50
#define MAX_LONG_NAME_LEN 350
static sftp_client_message
sftp_make_client_message(sftp_session sftp, sftp_packet packet)
{
ssh_session session = sftp->session;
sftp_client_message msg = NULL;
ssh_buffer payload = NULL;
int rc;
int version;
msg = calloc(1, sizeof(struct sftp_client_message_struct));
if (msg == NULL) {
ssh_set_error_oom(session);
return NULL;
}
payload = packet->payload;
msg->type = packet->type;
msg->sftp = sftp;
/* take a copy of the whole packet */
msg->complete_message = ssh_buffer_new();
if (msg->complete_message == NULL) {
ssh_set_error_oom(session);
goto error;
}
rc = ssh_buffer_add_data(msg->complete_message,
ssh_buffer_get(payload),
ssh_buffer_get_len(payload));
if (rc < 0) {
goto error;
}
if (msg->type != SSH_FXP_INIT) {
rc = ssh_buffer_get_u32(payload, &msg->id);
if (rc != sizeof(uint32_t)) {
goto error;
}
}
switch (msg->type) {
case SSH_FXP_INIT:
rc = ssh_buffer_unpack(payload,
"d",
&version);
if (rc != SSH_OK) {
printf("unpack init failed!\n");
goto error;
}
version = ntohl(version);
sftp->client_version = version;
break;
case SSH_FXP_CLOSE:
case SSH_FXP_READDIR:
msg->handle = ssh_buffer_get_ssh_string(payload);
if (msg->handle == NULL) {
goto error;
}
break;
case SSH_FXP_READ:
rc = ssh_buffer_unpack(payload,
"Sqd",
&msg->handle,
&msg->offset,
&msg->len);
if (rc != SSH_OK) {
goto error;
}
break;
case SSH_FXP_WRITE:
rc = ssh_buffer_unpack(payload,
"SqS",
&msg->handle,
&msg->offset,
&msg->data);
if (rc != SSH_OK) {
goto error;
}
break;
case SSH_FXP_REMOVE:
case SSH_FXP_RMDIR:
case SSH_FXP_OPENDIR:
case SSH_FXP_READLINK:
case SSH_FXP_REALPATH:
rc = ssh_buffer_unpack(payload,
"s",
&msg->filename);
if (rc != SSH_OK) {
goto error;
}
break;
case SSH_FXP_RENAME:
case SSH_FXP_SYMLINK:
rc = ssh_buffer_unpack(payload,
"sS",
&msg->filename,
&msg->data);
if (rc != SSH_OK) {
goto error;
}
break;
case SSH_FXP_MKDIR:
case SSH_FXP_SETSTAT:
rc = ssh_buffer_unpack(payload,
"s",
&msg->filename);
if (rc != SSH_OK) {
goto error;
}
msg->attr = sftp_parse_attr(sftp, payload, 0);
if (msg->attr == NULL) {
goto error;
}
break;
case SSH_FXP_FSETSTAT:
msg->handle = ssh_buffer_get_ssh_string(payload);
if (msg->handle == NULL) {
goto error;
}
msg->attr = sftp_parse_attr(sftp, payload, 0);
if (msg->attr == NULL) {
goto error;
}
break;
case SSH_FXP_LSTAT:
case SSH_FXP_STAT:
rc = ssh_buffer_unpack(payload,
"s",
&msg->filename);
if (rc != SSH_OK) {
goto error;
}
if (sftp->version > 3) {
ssh_buffer_unpack(payload, "d", &msg->flags);
}
break;
case SSH_FXP_OPEN:
rc = ssh_buffer_unpack(payload,
"sd",
&msg->filename,
&msg->flags);
if (rc != SSH_OK) {
goto error;
}
msg->attr = sftp_parse_attr(sftp, payload, 0);
if (msg->attr == NULL) {
goto error;
}
break;
case SSH_FXP_FSTAT:
rc = ssh_buffer_unpack(payload,
"S",
&msg->handle);
if (rc != SSH_OK) {
goto error;
}
break;
case SSH_FXP_EXTENDED:
rc = ssh_buffer_unpack(payload,
"s",
&msg->submessage);
if (rc != SSH_OK) {
goto error;
}
if (strcmp(msg->submessage, "hardlink@openssh.com") == 0 ||
strcmp(msg->submessage, "posix-rename@openssh.com") == 0) {
rc = ssh_buffer_unpack(payload,
"sS",
&msg->filename,
&msg->data);
if (rc != SSH_OK) {
goto error;
}
} else if (strcmp(msg->submessage, "statvfs@openssh.com") == 0 ){
rc = ssh_buffer_unpack(payload,
"s",
&msg->filename);
if (rc != SSH_OK) {
goto error;
}
}
break;
default:
ssh_set_error(sftp->session, SSH_FATAL,
"Received unhandled sftp message %d", msg->type);
goto error;
}
return msg;
error:
sftp_client_message_free(msg);
return NULL;
}
sftp_client_message sftp_get_client_message(sftp_session sftp)
{
sftp_packet packet = NULL;
packet = sftp_packet_read(sftp);
if (packet == NULL) {
return NULL;
}
return sftp_make_client_message(sftp, packet);
}
/**
* @brief Get the client message from a sftp packet.
*
* @param sftp The sftp session handle.
*
* @return The pointer to the generated sftp client message.
*/
static sftp_client_message
sftp_get_client_message_from_packet(sftp_session sftp)
{
sftp_packet packet = NULL;
packet = sftp->read_packet;
if (packet == NULL) {
return NULL;
}
return sftp_make_client_message(sftp, packet);
}
/* Send an sftp client message. Can be used in case of proxying */
int sftp_send_client_message(sftp_session sftp, sftp_client_message msg)
{
return sftp_packet_write(sftp, msg->type, msg->complete_message);
}
uint8_t sftp_client_message_get_type(sftp_client_message msg)
{
return msg->type;
}
const char *sftp_client_message_get_filename(sftp_client_message msg)
{
return msg->filename;
}
void
sftp_client_message_set_filename(sftp_client_message msg, const char *newname)
{
free(msg->filename);
msg->filename = strdup(newname);
}
const char *sftp_client_message_get_data(sftp_client_message msg)
{
if (msg->str_data == NULL)
msg->str_data = ssh_string_to_char(msg->data);
return msg->str_data;
}
uint32_t sftp_client_message_get_flags(sftp_client_message msg)
{
return msg->flags;
}
const char *sftp_client_message_get_submessage(sftp_client_message msg)
{
return msg->submessage;
}
void sftp_client_message_free(sftp_client_message msg)
{
if (msg == NULL) {
return;
}
SAFE_FREE(msg->filename);
SAFE_FREE(msg->submessage);
SSH_STRING_FREE(msg->data);
SSH_STRING_FREE(msg->handle);
sftp_attributes_free(msg->attr);
SSH_BUFFER_FREE(msg->complete_message);
SAFE_FREE(msg->str_data);
ZERO_STRUCTP(msg);
SAFE_FREE(msg);
}
int
sftp_reply_name(sftp_client_message msg, const char *name, sftp_attributes attr)
{
ssh_buffer out = NULL;
ssh_string file = NULL;
out = ssh_buffer_new();
if (out == NULL) {
return -1;
}
file = ssh_string_from_char(name);
if (file == NULL) {
SSH_BUFFER_FREE(out);
return -1;
}
SSH_LOG(SSH_LOG_PROTOCOL, "Sending name %s", ssh_string_get_char(file));
if (ssh_buffer_add_u32(out, msg->id) < 0 ||
ssh_buffer_add_u32(out, htonl(1)) < 0 ||
ssh_buffer_add_ssh_string(out, file) < 0 ||
ssh_buffer_add_ssh_string(out, file) < 0 || /* The protocol is broken here between 3 & 4 */
buffer_add_attributes(out, attr) < 0 ||
sftp_packet_write(msg->sftp, SSH_FXP_NAME, out) < 0) {
SSH_BUFFER_FREE(out);
SSH_STRING_FREE(file);
return -1;
}
SSH_BUFFER_FREE(out);
SSH_STRING_FREE(file);
return 0;
}
int sftp_reply_handle(sftp_client_message msg, ssh_string handle)
{
ssh_buffer out;
out = ssh_buffer_new();
if (out == NULL) {
return -1;
}
ssh_log_hexdump("Sending handle:",
(const unsigned char *)ssh_string_get_char(handle),
ssh_string_len(handle));
if (ssh_buffer_add_u32(out, msg->id) < 0 ||
ssh_buffer_add_ssh_string(out, handle) < 0 ||
sftp_packet_write(msg->sftp, SSH_FXP_HANDLE, out) < 0) {
SSH_BUFFER_FREE(out);
return -1;
}
SSH_BUFFER_FREE(out);
return 0;
}
int sftp_reply_attr(sftp_client_message msg, sftp_attributes attr)
{
ssh_buffer out;
out = ssh_buffer_new();
if (out == NULL) {
return -1;
}
SSH_LOG(SSH_LOG_PROTOCOL, "Sending attr");
if (ssh_buffer_add_u32(out, msg->id) < 0 ||
buffer_add_attributes(out, attr) < 0 ||
sftp_packet_write(msg->sftp, SSH_FXP_ATTRS, out) < 0) {
SSH_BUFFER_FREE(out);
return -1;
}
SSH_BUFFER_FREE(out);
return 0;
}
int
sftp_reply_names_add(sftp_client_message msg, const char *file,
const char *longname, sftp_attributes attr)
{
ssh_string name = NULL;
name = ssh_string_from_char(file);
if (name == NULL) {
return -1;
}
if (msg->attrbuf == NULL) {
msg->attrbuf = ssh_buffer_new();
if (msg->attrbuf == NULL) {
SSH_STRING_FREE(name);
return -1;
}
}
if (ssh_buffer_add_ssh_string(msg->attrbuf, name) < 0) {
SSH_STRING_FREE(name);
return -1;
}
SSH_STRING_FREE(name);
name = ssh_string_from_char(longname);
if (name == NULL) {
return -1;
}
if (ssh_buffer_add_ssh_string(msg->attrbuf, name) < 0 ||
buffer_add_attributes(msg->attrbuf, attr) < 0) {
SSH_STRING_FREE(name);
return -1;
}
SSH_STRING_FREE(name);
msg->attr_num++;
return 0;
}
int sftp_reply_names(sftp_client_message msg)
{
ssh_buffer out;
out = ssh_buffer_new();
if (out == NULL) {
SSH_BUFFER_FREE(msg->attrbuf);
return -1;
}
SSH_LOG(SSH_LOG_PROTOCOL, "Sending %d names", msg->attr_num);
if (ssh_buffer_add_u32(out, msg->id) < 0 ||
ssh_buffer_add_u32(out, htonl(msg->attr_num)) < 0 ||
ssh_buffer_add_data(out, ssh_buffer_get(msg->attrbuf),
ssh_buffer_get_len(msg->attrbuf)) < 0 ||
sftp_packet_write(msg->sftp, SSH_FXP_NAME, out) < 0) {
SSH_BUFFER_FREE(out);
SSH_BUFFER_FREE(msg->attrbuf);
return -1;
}
SSH_BUFFER_FREE(out);
SSH_BUFFER_FREE(msg->attrbuf);
msg->attr_num = 0;
msg->attrbuf = NULL;
return 0;
}
int
sftp_reply_status(sftp_client_message msg, uint32_t status, const char *message)
{
ssh_buffer out = NULL;
ssh_string s = NULL;
out = ssh_buffer_new();
if (out == NULL) {
return -1;
}
s = ssh_string_from_char(message ? message : "");
if (s == NULL) {
SSH_BUFFER_FREE(out);
return -1;
}
SSH_LOG(SSH_LOG_PROTOCOL, "Sending status %d, message: %s", status,
ssh_string_get_char(s));
if (ssh_buffer_add_u32(out, msg->id) < 0 ||
ssh_buffer_add_u32(out, htonl(status)) < 0 ||
ssh_buffer_add_ssh_string(out, s) < 0 ||
ssh_buffer_add_u32(out, 0) < 0 || /* language string */
sftp_packet_write(msg->sftp, SSH_FXP_STATUS, out) < 0) {
SSH_BUFFER_FREE(out);
SSH_STRING_FREE(s);
return -1;
}
SSH_BUFFER_FREE(out);
SSH_STRING_FREE(s);
return 0;
}
int sftp_reply_data(sftp_client_message msg, const void *data, int len)
{
ssh_buffer out;
out = ssh_buffer_new();
if (out == NULL) {
return -1;
}
SSH_LOG(SSH_LOG_PROTOCOL, "Sending data, length: %d", len);
if (ssh_buffer_add_u32(out, msg->id) < 0 ||
ssh_buffer_add_u32(out, ntohl(len)) < 0 ||
ssh_buffer_add_data(out, data, len) < 0 ||
sftp_packet_write(msg->sftp, SSH_FXP_DATA, out) < 0) {
SSH_BUFFER_FREE(out);
return -1;
}
SSH_BUFFER_FREE(out);
return 0;
}
/**
* @brief Handle the statvfs request, return information the mounted file system.
*
* @param msg The sftp client message.
*
* @param st The statvfs state of target file.
*
* @return 0 on success, < 0 on error with ssh and sftp error set.
*
* @see sftp_get_error()
*/
static int
sftp_reply_statvfs(sftp_client_message msg, sftp_statvfs_t st)
{
int ret = 0;
ssh_buffer out;
out = ssh_buffer_new();
if (out == NULL) {
return -1;
}
SSH_LOG(SSH_LOG_PROTOCOL, "Sending statvfs reply");
if (ssh_buffer_add_u32(out, msg->id) < 0 ||
ssh_buffer_add_u64(out, ntohll(st->f_bsize)) < 0 ||
ssh_buffer_add_u64(out, ntohll(st->f_frsize)) < 0 ||
ssh_buffer_add_u64(out, ntohll(st->f_blocks)) < 0 ||
ssh_buffer_add_u64(out, ntohll(st->f_bfree)) < 0 ||
ssh_buffer_add_u64(out, ntohll(st->f_bavail)) < 0 ||
ssh_buffer_add_u64(out, ntohll(st->f_files)) < 0 ||
ssh_buffer_add_u64(out, ntohll(st->f_ffree)) < 0 ||
ssh_buffer_add_u64(out, ntohll(st->f_favail)) < 0 ||
ssh_buffer_add_u64(out, ntohll(st->f_fsid)) < 0 ||
ssh_buffer_add_u64(out, ntohll(st->f_flag)) < 0 ||
ssh_buffer_add_u64(out, ntohll(st->f_namemax)) < 0 ||
sftp_packet_write(msg->sftp, SSH_FXP_EXTENDED_REPLY, out) < 0) {
ret = -1;
}
SSH_BUFFER_FREE(out);
return ret;
}
int sftp_reply_version(sftp_client_message client_msg)
{
sftp_session sftp = client_msg->sftp;
ssh_session session = sftp->session;
int version;
ssh_buffer reply;
int rc;
SSH_LOG(SSH_LOG_PROTOCOL, "Sending version packet");
version = sftp->client_version;
reply = ssh_buffer_new();
if (reply == NULL) {
ssh_set_error_oom(session);
return -1;
}
rc = ssh_buffer_pack(reply, "dssssss",
LIBSFTP_VERSION,
"posix-rename@openssh.com",
"1",
"hardlink@openssh.com",
"1",
"statvfs@openssh.com",
"2");
if (rc != SSH_OK) {
ssh_set_error_oom(session);
SSH_BUFFER_FREE(reply);
return -1;
}
rc = sftp_packet_write(sftp, SSH_FXP_VERSION, reply);
if (rc < 0) {
SSH_BUFFER_FREE(reply);
return -1;
}
SSH_BUFFER_FREE(reply);
SSH_LOG(SSH_LOG_PROTOCOL, "Server version sent");
if (version > LIBSFTP_VERSION) {
sftp->version = LIBSFTP_VERSION;
} else {
sftp->version = version;
}
return SSH_OK;
}
/*
* This function will return you a new handle to give the client.
* the function accepts an info that can be retrieved later with
* the handle. Care is given that a corrupted handle won't give a
* valid info (or worse).
*/
ssh_string sftp_handle_alloc(sftp_session sftp, void *info)
{
ssh_string ret = NULL;
uint32_t val;
uint32_t i;
if (sftp->handles == NULL) {
sftp->handles = calloc(SFTP_HANDLES, sizeof(void *));
if (sftp->handles == NULL) {
return NULL;
}
}
for (i = 0; i < SFTP_HANDLES; i++) {
if (sftp->handles[i] == NULL) {
break;
}
}
if (i == SFTP_HANDLES) {
return NULL; /* no handle available */
}
val = i;
ret = ssh_string_new(4);
if (ret == NULL) {
return NULL;
}
memcpy(ssh_string_data(ret), &val, sizeof(uint32_t));
sftp->handles[i] = info;
return ret;
}
void *sftp_handle(sftp_session sftp, ssh_string handle)
{
uint32_t val;
if (sftp->handles == NULL) {
return NULL;
}
if (ssh_string_len(handle) != sizeof(uint32_t)) {
return NULL;
}
memcpy(&val, ssh_string_data(handle), sizeof(uint32_t));
if (val >= SFTP_HANDLES) {
return NULL;
}
return sftp->handles[val];
}
void sftp_handle_remove(sftp_session sftp, void *handle)
{
int i;
for (i = 0; i < SFTP_HANDLES; i++) {
if (sftp->handles[i] == handle) {
sftp->handles[i] = NULL;
break;
}
}
}
/* Default SFTP handlers */
static const char *
ssh_str_error(int u_errno)
{
switch (u_errno) {
case SSH_FX_NO_SUCH_FILE:
return "No such file";
case SSH_FX_PERMISSION_DENIED:
return "Permission denied";
case SSH_FX_BAD_MESSAGE:
return "Bad message";
case SSH_FX_OP_UNSUPPORTED:
return "Operation not supported";
default:
return "Operation failed";
}
}
static int
unix_errno_to_ssh_stat(int u_errno)
{
int ret = SSH_OK;
switch (u_errno) {
case 0:
break;
case ENOENT:
case ENOTDIR:
case EBADF:
case ELOOP:
ret = SSH_FX_NO_SUCH_FILE;
break;
case EPERM:
case EACCES:
case EFAULT:
ret = SSH_FX_PERMISSION_DENIED;
break;
case ENAMETOOLONG:
case EINVAL:
ret = SSH_FX_BAD_MESSAGE;
break;
case ENOSYS:
ret = SSH_FX_OP_UNSUPPORTED;
break;
default:
ret = SSH_FX_FAILURE;
break;
}
return ret;
}
static void
stat_to_filexfer_attrib(const struct stat *z_st, struct sftp_attributes_struct *z_attr)
{
z_attr->flags = 0 | (uint32_t)SSH_FILEXFER_ATTR_SIZE;
z_attr->size = z_st->st_size;
z_attr->flags |= (uint32_t)SSH_FILEXFER_ATTR_UIDGID;
z_attr->uid = z_st->st_uid;
z_attr->gid = z_st->st_gid;
z_attr->flags |= (uint32_t)SSH_FILEXFER_ATTR_PERMISSIONS;
z_attr->permissions = z_st->st_mode;
z_attr->flags |= (uint32_t)SSH_FILEXFER_ATTR_ACMODTIME;
z_attr->atime = z_st->st_atime;
z_attr->mtime = z_st->st_mtime;
}
static void
clear_filexfer_attrib(struct sftp_attributes_struct *z_attr)
{
z_attr->flags = 0;
z_attr->size = 0;
z_attr->uid = 0;
z_attr->gid = 0;
z_attr->permissions = 0;
z_attr->atime = 0;
z_attr->mtime = 0;
}
#ifndef _WIN32
/* internal */
enum sftp_handle_type
{
SFTP_NULL_HANDLE,
SFTP_DIR_HANDLE,
SFTP_FILE_HANDLE
};
struct sftp_handle
{
enum sftp_handle_type type;
int fd;
DIR *dirp;
char *name;
};
SSH_SFTP_CALLBACK(process_unsupposed);
SSH_SFTP_CALLBACK(process_open);
SSH_SFTP_CALLBACK(process_read);
SSH_SFTP_CALLBACK(process_write);
SSH_SFTP_CALLBACK(process_close);
SSH_SFTP_CALLBACK(process_opendir);
SSH_SFTP_CALLBACK(process_readdir);
SSH_SFTP_CALLBACK(process_rmdir);
SSH_SFTP_CALLBACK(process_realpath);
SSH_SFTP_CALLBACK(process_mkdir);
SSH_SFTP_CALLBACK(process_lstat);
SSH_SFTP_CALLBACK(process_stat);
SSH_SFTP_CALLBACK(process_readlink);
SSH_SFTP_CALLBACK(process_symlink);
SSH_SFTP_CALLBACK(process_remove);
SSH_SFTP_CALLBACK(process_extended_statvfs);
SSH_SFTP_CALLBACK(process_setstat);
const struct sftp_message_handler message_handlers[] = {
{"open", NULL, SSH_FXP_OPEN, process_open},
{"close", NULL, SSH_FXP_CLOSE, process_close},
{"read", NULL, SSH_FXP_READ, process_read},
{"write", NULL, SSH_FXP_WRITE, process_write},
{"lstat", NULL, SSH_FXP_LSTAT, process_lstat},
{"fstat", NULL, SSH_FXP_FSTAT, process_unsupposed},
{"setstat", NULL, SSH_FXP_SETSTAT, process_setstat},
{"fsetstat", NULL, SSH_FXP_FSETSTAT, process_unsupposed},
{"opendir", NULL, SSH_FXP_OPENDIR, process_opendir},
{"readdir", NULL, SSH_FXP_READDIR, process_readdir},
{"remove", NULL, SSH_FXP_REMOVE, process_remove},
{"mkdir", NULL, SSH_FXP_MKDIR, process_mkdir},
{"rmdir", NULL, SSH_FXP_RMDIR, process_rmdir},
{"realpath", NULL, SSH_FXP_REALPATH, process_realpath},
{"stat", NULL, SSH_FXP_STAT, process_stat},
{"rename", NULL, SSH_FXP_RENAME, process_unsupposed},
{"readlink", NULL, SSH_FXP_READLINK, process_readlink},
{"symlink", NULL, SSH_FXP_SYMLINK, process_symlink},
{"init", NULL, SSH_FXP_INIT, sftp_reply_version},
{NULL, NULL, 0, NULL},
};
const struct sftp_message_handler extended_handlers[] = {
/* here are some extended type handlers */
{"statvfs", "statvfs@openssh.com", 0, process_extended_statvfs},
{NULL, NULL, 0, NULL},
};
static int
process_open(sftp_client_message client_msg)
{
const char *filename = sftp_client_message_get_filename(client_msg);
uint32_t msg_flag = sftp_client_message_get_flags(client_msg);
uint32_t mode = client_msg->attr->permissions;
ssh_string handle_s = NULL;
struct sftp_handle *h = NULL;
int file_flag;
int fd = -1;
int status;
SSH_LOG(SSH_LOG_PROTOCOL, "Processing open: filename %s, mode=0%o" PRIu32,
filename, mode);
if (((msg_flag & (uint32_t)SSH_FXF_READ) == SSH_FXF_READ) &&
((msg_flag & (uint32_t)SSH_FXF_WRITE) == SSH_FXF_WRITE)) {
file_flag = O_RDWR; // file must exist
if ((msg_flag & (uint32_t)SSH_FXF_CREAT) == SSH_FXF_CREAT)
file_flag |= O_CREAT;
} else if ((msg_flag & (uint32_t)SSH_FXF_WRITE) == SSH_FXF_WRITE) {
file_flag = O_WRONLY;
if ((msg_flag & (uint32_t)SSH_FXF_APPEND) == SSH_FXF_APPEND)
file_flag |= O_APPEND;
if ((msg_flag & (uint32_t)SSH_FXF_CREAT) == SSH_FXF_CREAT)
file_flag |= O_CREAT;
} else if ((msg_flag & (uint32_t)SSH_FXF_READ) == SSH_FXF_READ) {
file_flag = O_RDONLY;
} else {
SSH_LOG(SSH_LOG_PROTOCOL, "undefined message flag: %" PRIu32, msg_flag);
sftp_reply_status(client_msg, SSH_FX_FAILURE, "Flag error");
return SSH_ERROR;
}
fd = open(filename, file_flag, mode);
if (fd == -1) {
int saved_errno = errno;
SSH_LOG(SSH_LOG_PROTOCOL, "error open file with error: %s",
strerror(saved_errno));
status = unix_errno_to_ssh_stat(saved_errno);
sftp_reply_status(client_msg, status, "Write error");
return SSH_ERROR;
}
h = calloc(1, sizeof (struct sftp_handle));
if (h == NULL) {
close(fd);
SSH_LOG(SSH_LOG_PROTOCOL, "failed to allocate a new handle");
sftp_reply_status(client_msg, SSH_FX_FAILURE,
"Failed to allocate new handle");
return SSH_ERROR;
}
h->fd = fd;
h->type = SFTP_FILE_HANDLE;
handle_s = sftp_handle_alloc(client_msg->sftp, h);
if (handle_s != NULL) {
sftp_reply_handle(client_msg, handle_s);
ssh_string_free(handle_s);
} else {
free(h);
close(fd);
SSH_LOG(SSH_LOG_PROTOCOL, "Failed to allocate handle");
sftp_reply_status(client_msg, SSH_FX_FAILURE,
"Failed to allocate handle");
}
return SSH_OK;
}
static int
process_read(sftp_client_message client_msg)
{
sftp_session sftp = client_msg->sftp;
ssh_string handle = client_msg->handle;
struct sftp_handle *h = NULL;
ssize_t readn = 0;
int fd = -1;
char *buffer = NULL;
off_t off;
ssh_log_hexdump("Processing read: handle:",
(const unsigned char *)ssh_string_get_char(handle),
ssh_string_len(handle));
h = sftp_handle(sftp, handle);
if (h != NULL && h->type == SFTP_FILE_HANDLE) {
fd = h->fd;
}
if (fd < 0) {
sftp_reply_status(client_msg, SSH_FX_INVALID_HANDLE, NULL);
SSH_LOG(SSH_LOG_PROTOCOL, "invalid fd (%d) received from handle", fd);
return SSH_ERROR;
}
off = lseek(fd, client_msg->offset, SEEK_SET);
if (off == -1) {
sftp_reply_status(client_msg, SSH_FX_FAILURE, NULL);
SSH_LOG(SSH_LOG_PROTOCOL,
"error seeking file fd: %d at offset: %" PRIu64,
fd, client_msg->offset);
return SSH_ERROR;
}
buffer = malloc(client_msg->len);
if (buffer == NULL) {
ssh_set_error_oom(sftp->session);
sftp_reply_status(client_msg, SSH_FX_FAILURE, NULL);
SSH_LOG(SSH_LOG_PROTOCOL, "Failed to allocate memory for read data");
return SSH_ERROR;
}
readn = ssh_readn(fd, buffer, client_msg->len);
if (readn < 0) {
sftp_reply_status(client_msg, SSH_FX_FAILURE, NULL);
SSH_LOG(SSH_LOG_PROTOCOL, "read file error!");
free(buffer);
return SSH_ERROR;
} else if (readn > 0) {
sftp_reply_data(client_msg, buffer, readn);
} else {
sftp_reply_status(client_msg, SSH_FX_EOF, NULL);
}
free(buffer);
return SSH_OK;
}
static int
process_write(sftp_client_message client_msg)
{
sftp_session sftp = client_msg->sftp;
ssh_string handle = client_msg->handle;