aboutsummaryrefslogtreecommitdiff
path: root/libssh/sftp.c
blob: ff833e2fad30b6cbd5a0fcbb886391b4d18e15d3 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
/* scp.c contains the needed function to work with file transfer protocol over ssh*/
/* don't look further if you believe this is just FTP over some tunnel. It IS different */
/* This file contains code written by Nick Zitzmann */
/*
Copyright 2003-2005 Aris Adamantiadis

This file is part of the SSH Library

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. */

/** \defgroup ssh_sftp SFTP functions
 * \brief SFTP handling functions
 */
/** \addtogroup ssh_sftp
 * @{ */

#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "libssh/priv.h"
#include "libssh/ssh2.h"
#include "libssh/sftp.h"
#ifndef NO_SFTP
/* here how it works : sftp commands are channeled by the ssh sftp subsystem. */
/* every packet are sent/read using a SFTP_PACKET type structure. */
/* into these packets, most of the server answers are messages having an ID and */
/* having a message specific part. it is described by SFTP_MESSAGE */
/* when reading a message, the sftp system puts it into the queue, so the process having asked for it */
/* can fetch it, while continuing to read for other messages (it is inspecified in which order messages may */
/* be sent back to the client */


/* functions */
void sftp_enqueue(SFTP_SESSION *session, SFTP_MESSAGE *msg);
static void sftp_message_free(SFTP_MESSAGE *msg);

SFTP_SESSION *sftp_new(SSH_SESSION *session){
    SFTP_SESSION *sftp=malloc(sizeof(SFTP_SESSION));
    memset(sftp,0,sizeof(SFTP_SESSION));
    sftp->session=session;
    sftp->channel=channel_new(session);
    if(channel_open_session(sftp->channel)){
        channel_free(sftp->channel);
        free(sftp);
        return NULL;
    }
    if(channel_request_sftp(sftp->channel)){
        sftp_free(sftp);
        return NULL;
    }
    return sftp;
}

#ifndef NO_SERVER
SFTP_SESSION *sftp_server_new(SSH_SESSION *session, CHANNEL *chan){
    SFTP_SESSION *sftp=malloc(sizeof(SFTP_SESSION));
    memset(sftp,0,sizeof(SFTP_SESSION));
    sftp->session=session;
    sftp->channel=chan;
    return sftp;
}

int sftp_server_init(SFTP_SESSION *sftp){
    SFTP_PACKET *packet=sftp_packet_read(sftp);
    u32 version;
    BUFFER *reply;
    if(!packet)
        return -1;
    if(packet->type != SSH_FXP_INIT){
        ssh_set_error(sftp->session,SSH_FATAL,"Packet read of type %d instead of SSH_FXP_INIT",
                      packet->type);
        sftp_packet_free(packet);
        return -1;
    }
    ssh_say(2,"received SSH_FXP_INIT\n");
    buffer_get_u32(packet->payload,&version);
    version=ntohl(version);
    ssh_say(2,"client version %d\n");
    sftp->client_version=version;
    sftp_packet_free(packet);
    reply=buffer_new();
    buffer_add_u32(reply,ntohl(LIBSFTP_VERSION));
    if(sftp_packet_write(sftp,SSH_FXP_VERSION,reply)==-1){
        buffer_free(reply);
        return -1;
    }
    buffer_free(reply);
    ssh_say(2,"server version sent\n");
    if(version > LIBSFTP_VERSION)
        sftp->version=LIBSFTP_VERSION;
    else
        sftp->version=version;
    return 0;
}
#endif

void sftp_free(SFTP_SESSION *sftp){
    struct request_queue *ptr;
    channel_send_eof(sftp->channel);
    channel_free(sftp->channel);
    ptr=sftp->queue;
    while(ptr){
        struct request_queue *old;
        sftp_message_free(ptr->message);
        old=ptr->next;
        free(ptr);
        ptr=old;
    }
    memset(sftp,0,sizeof(*sftp));
    free(sftp);
}

int sftp_packet_write(SFTP_SESSION *sftp,u8 type, BUFFER *payload){
    u32 size;
    buffer_add_data_begin(payload,&type,sizeof(u8));
    size=htonl(buffer_get_len(payload));
    buffer_add_data_begin(payload,&size,sizeof(u32));
    size=channel_write(sftp->channel,buffer_get(payload),buffer_get_len(payload));
    if(size != buffer_get_len(payload)){
        ssh_say(1,"had to write %d bytes, wrote only %d\n",buffer_get_len(payload),size);
    }
    return size;
}

SFTP_PACKET *sftp_packet_read(SFTP_SESSION *sftp){
    SFTP_PACKET *packet=malloc(sizeof(SFTP_PACKET));
    u32 size;
    packet->sftp=sftp;
    packet->payload=buffer_new();
    if(channel_read(sftp->channel,packet->payload,4,0)<=0){
        buffer_free(packet->payload);
        free(packet);
        return NULL;
    }
    buffer_get_u32(packet->payload,&size);
    size=ntohl(size);
    if(channel_read(sftp->channel,packet->payload,1,0)<=0){
        buffer_free(packet->payload);
        free(packet);
        return NULL;
    }
    buffer_get_u8(packet->payload,&packet->type);
    if(size>1)
        if(channel_read(sftp->channel,packet->payload,size-1,0)<=0){
            buffer_free(packet->payload);
            free(packet);
            return NULL;
        }
    return packet;
}

static SFTP_MESSAGE *sftp_message_new(){
    SFTP_MESSAGE *msg=malloc(sizeof(SFTP_MESSAGE));
    memset(msg,0,sizeof(*msg));
    msg->payload=buffer_new();
    return msg;
}

static void sftp_message_free(SFTP_MESSAGE *msg){
    if(msg->payload)
        buffer_free(msg->payload);
    free(msg);
}

SFTP_MESSAGE *sftp_get_message(SFTP_PACKET *packet){
    SFTP_MESSAGE *msg=sftp_message_new();
    msg->sftp=packet->sftp;
    msg->packet_type=packet->type;
    if((packet->type!=SSH_FXP_STATUS)&&(packet->type!=SSH_FXP_HANDLE) &&
        (packet->type != SSH_FXP_DATA) && (packet->type != SSH_FXP_ATTRS)
    && (packet->type != SSH_FXP_NAME)){
        ssh_set_error(packet->sftp->session,SSH_FATAL,"get_message : unknown packet type %d\n",packet->type);
        sftp_message_free(msg);
        return NULL;
    }
    if(buffer_get_u32(packet->payload,&msg->id)!=sizeof(u32)){
        ssh_set_error(packet->sftp->session,SSH_FATAL,"invalid packet %d : no ID",packet->type);
        sftp_message_free(msg);
        return NULL;
    }
    ssh_say(2,"packet with id %d type %d\n",msg->id,msg->packet_type);
    buffer_add_data(msg->payload,buffer_get_rest(packet->payload),buffer_get_rest_len(packet->payload));
    return msg;
}

int sftp_read_and_dispatch(SFTP_SESSION *session){
    SFTP_PACKET *packet;
    SFTP_MESSAGE *message=NULL;
    packet=sftp_packet_read(session);
    if(!packet)
        return -1; /* something nasty happened reading the packet */
    message=sftp_get_message(packet);
    sftp_packet_free(packet);
    if(!message)
        return -1;
    sftp_enqueue(session,message);
    return 0;
}

void sftp_packet_free(SFTP_PACKET *packet){
    if(packet->payload)
        buffer_free(packet->payload);
    free(packet);
}

int sftp_init(SFTP_SESSION *sftp){
    SFTP_PACKET *packet;
    BUFFER *buffer=buffer_new();
    STRING *ext_name_s=NULL, *ext_data_s=NULL;
    char *ext_name,*ext_data;
    u32 version=htonl(LIBSFTP_VERSION);
    buffer_add_u32(buffer,version);
    sftp_packet_write(sftp,SSH_FXP_INIT,buffer);
    buffer_free(buffer);
    packet=sftp_packet_read(sftp);
    if(!packet)
        return -1;
    if(packet->type != SSH_FXP_VERSION){
        ssh_set_error(sftp->session,SSH_FATAL,"Received a %d messages instead of SSH_FXP_VERSION",packet->type);
        sftp_packet_free(packet);
        return -1;
    }
    buffer_get_u32(packet->payload,&version);
    version=ntohl(version);
    if(!(ext_name_s=buffer_get_ssh_string(packet->payload))||!(ext_data_s=buffer_get_ssh_string(packet->payload)))
        ssh_say(2,"sftp server version %d\n",version);
    else{
        ext_name=string_to_char(ext_name_s);
        ext_data=string_to_char(ext_data_s);
        ssh_say(2,"sftp server version %d (%s,%s)\n",version,ext_name,ext_data);
        free(ext_name);
        free(ext_data);
    }
    if(ext_name_s)
        free(ext_name_s);
    if(ext_data_s)
        free(ext_data_s);
    sftp_packet_free(packet);
    sftp->version=sftp->server_version=version;
    return 0;
}

REQUEST_QUEUE *request_queue_new(SFTP_MESSAGE *msg){
    REQUEST_QUEUE *queue=malloc(sizeof(REQUEST_QUEUE));
    memset(queue,0,sizeof(REQUEST_QUEUE));
    queue->message=msg;
    return queue;
}

void request_queue_free(REQUEST_QUEUE *queue){
    memset(queue,0,sizeof(*queue));
    free(queue);
}

void sftp_enqueue(SFTP_SESSION *session, SFTP_MESSAGE *msg){
    REQUEST_QUEUE *queue=request_queue_new(msg);
    REQUEST_QUEUE *ptr;
    ssh_say(2,"queued msg type %d id %d\n",msg->id,msg->packet_type);
    if(!session->queue)
        session->queue=queue;
    else {
        ptr=session->queue;
        while(ptr->next){
            ptr=ptr->next; /* find end of linked list */
        }
        ptr->next=queue; /* add it on bottom */
    }
}

/* pulls of a message from the queue based on the ID. returns null if no message has been found */
SFTP_MESSAGE *sftp_dequeue(SFTP_SESSION *session, u32 id){
    REQUEST_QUEUE *queue,*prev=NULL;
    SFTP_MESSAGE *msg;
    if(session->queue==NULL){
        return NULL;
    }
    queue=session->queue;
    while(queue){
        if(queue->message->id==id){
            /* remove from queue */
            if(prev==NULL){
                session->queue=queue->next;
            } else {
                prev->next=queue->next;
            }
            msg=queue->message;
            request_queue_free(queue);
            ssh_say(2,"dequeued msg id %d type %d\n",msg->id,msg->packet_type);
            return msg;
        }
        prev=queue;
        queue=queue->next;
    }
    return NULL;
}

/* assigns a new sftp ID for new requests and assures there is no collision between them. */
u32 sftp_get_new_id(SFTP_SESSION *session){
    return ++session->id_counter;
}

STATUS_MESSAGE *parse_status_msg(SFTP_MESSAGE *msg){
    STATUS_MESSAGE *status;
    if(msg->packet_type != SSH_FXP_STATUS){
        ssh_set_error(msg->sftp->session, SSH_FATAL,"Not a ssh_fxp_status message passed in !");
        return NULL;
    }
    status=malloc(sizeof(STATUS_MESSAGE));
    memset(status,0,sizeof(*status));
    status->id=msg->id;
    if( (buffer_get_u32(msg->payload,&status->status)!= 4)
     || !(status->error=buffer_get_ssh_string(msg->payload)) ||
     !(status->lang=buffer_get_ssh_string(msg->payload))){
         if(status->error)
             free(status->error);
         /* status->lang never get allocated if something failed */
         free(status);
         ssh_set_error(msg->sftp->session,SSH_FATAL,"invalid SSH_FXP_STATUS message");
         return NULL;
    }
    status->status=ntohl(status->status);
    status->errormsg=string_to_char(status->error);
    status->langmsg=string_to_char(status->lang);
    return status;
}

void status_msg_free(STATUS_MESSAGE *status){
    if(status->errormsg)
        free(status->errormsg);
    if(status->error)
        free(status->error);
    if(status->langmsg)
        free(status->langmsg);
    if(status->lang)
        free(status->lang);
    free(status);
}

SFTP_FILE *parse_handle_msg(SFTP_MESSAGE *msg){
    SFTP_FILE *file;
    if(msg->packet_type != SSH_FXP_HANDLE){
        ssh_set_error(msg->sftp->session,SSH_FATAL,"Not a ssh_fxp_handle message passed in !");
        return NULL;
    }
    file=malloc(sizeof(SFTP_FILE));
    memset(file,0,sizeof(*file));
    file->sftp=msg->sftp;
    file->handle=buffer_get_ssh_string(msg->payload);
    file->offset=0;
    file->eof=0;
    if(!file->handle){
        ssh_set_error(msg->sftp->session,SSH_FATAL,"Invalid SSH_FXP_HANDLE message");
        free(file);
        return NULL;
    }
    return file;
}

SFTP_DIR *sftp_opendir(SFTP_SESSION *sftp, char *path){
    SFTP_DIR *dir=NULL;
    SFTP_FILE *file;
    STATUS_MESSAGE *status;
    SFTP_MESSAGE *msg=NULL;
    STRING *path_s;
    BUFFER *payload=buffer_new();
    u32 id=sftp_get_new_id(sftp);
    buffer_add_u32(payload,id);
    path_s=string_from_char(path);
    buffer_add_ssh_string(payload,path_s);
    free(path_s);
    sftp_packet_write(sftp,SSH_FXP_OPENDIR,payload);
    buffer_free(payload);
    while(!msg){
        if(sftp_read_and_dispatch(sftp))
            /* something nasty has happened */
            return NULL;
        msg=sftp_dequeue(sftp,id);
    }
    switch (msg->packet_type){
        case SSH_FXP_STATUS:
            status=parse_status_msg(msg);
            sftp_message_free(msg);
            if(!status)
                return NULL;
            ssh_set_error(sftp->session,SSH_REQUEST_DENIED,"sftp server : %s",status->errormsg);
            status_msg_free(status);
            return NULL;
        case SSH_FXP_HANDLE:
            file=parse_handle_msg(msg);
            sftp_message_free(msg);
            if(file){
                dir=malloc(sizeof(SFTP_DIR));
                memset(dir,0,sizeof(*dir));
                dir->sftp=sftp;
                dir->name=strdup(path);
                dir->handle=file->handle;
                free(file);
            }
            return dir;
        default:
            ssh_set_error(sftp->session,SSH_FATAL,"Received message %d during opendir!",msg->packet_type);
            sftp_message_free(msg);
    }
    return NULL;
}

/* parse the attributes from a payload from some messages */
/* i coded it on baselines from the protocol version 4. */
/* please excuse me for the inaccuracy of the code. it isn't my fault, it's sftp draft's one */
/* this code is dead anyway ... */
/* version 4 specific code */
SFTP_ATTRIBUTES *sftp_parse_attr_4(SFTP_SESSION *sftp,BUFFER *buf,int expectnames){
    u32 flags=0;
    SFTP_ATTRIBUTES *attr=malloc(sizeof(SFTP_ATTRIBUTES));
    STRING *owner=NULL;
    STRING *group=NULL;
    int ok=0;
    memset(attr,0,sizeof(*attr));
    /* it isn't really a loop, but i use it because it's like a try..catch.. construction in C */
    do {
        if(buffer_get_u32(buf,&flags)!=4)
            break;
        flags=ntohl(flags);
        attr->flags=flags;
        if(flags & SSH_FILEXFER_ATTR_SIZE){
            if(buffer_get_u64(buf,&attr->size)!=8)
                break;
            attr->size=ntohll(attr->size);
        }
        if(flags & SSH_FILEXFER_ATTR_OWNERGROUP){
            if(!(owner=buffer_get_ssh_string(buf)))
                break;
            if(!(group=buffer_get_ssh_string(buf)))
                break;
        }
        if(flags & SSH_FILEXFER_ATTR_PERMISSIONS){
            if(buffer_get_u32(buf,&attr->permissions)!=4)
                break;
            attr->permissions=ntohl(attr->permissions);
        }
        if(flags & SSH_FILEXFER_ATTR_ACCESSTIME){
            if(buffer_get_u64(buf,&attr->atime64)!=8)
                break;
            attr->atime64=ntohll(attr->atime64);
        }
        if(flags & SSH_FILEXFER_ATTR_SUBSECOND_TIMES){
            if(buffer_get_u32(buf,&attr->atime_nseconds)!=4)
                break;
            attr->atime_nseconds=ntohl(attr->atime_nseconds);
        }
        if(flags & SSH_FILEXFER_ATTR_CREATETIME){
            if(buffer_get_u64(buf,&attr->createtime)!=8)
                break;
            attr->createtime=ntohll(attr->createtime);
        }
        if(flags & SSH_FILEXFER_ATTR_SUBSECOND_TIMES){
            if(buffer_get_u32(buf,&attr->createtime_nseconds)!=4)
                break;
            attr->createtime_nseconds=ntohl(attr->createtime_nseconds);
        }
        if(flags & SSH_FILEXFER_ATTR_MODIFYTIME){
            if(buffer_get_u64(buf,&attr->mtime64)!=8)
                break;
            attr->mtime64=ntohll(attr->mtime64);
        }
        if(flags & SSH_FILEXFER_ATTR_SUBSECOND_TIMES){
            if(buffer_get_u32(buf,&attr->mtime_nseconds)!=4)
                break;
            attr->mtime_nseconds=ntohl(attr->mtime_nseconds);
        }
        if(flags & SSH_FILEXFER_ATTR_ACL){
            if(!(attr->acl=buffer_get_ssh_string(buf)))
                break;
        }
        if (flags & SSH_FILEXFER_ATTR_EXTENDED){
            if(buffer_get_u32(buf,&attr->extended_count)!=4)
                break;
            attr->extended_count=ntohl(attr->extended_count);
            while(attr->extended_count && (attr->extended_type=buffer_get_ssh_string(buf))
                    && (attr->extended_data=buffer_get_ssh_string(buf))){
                        attr->extended_count--;
            }
            if(attr->extended_count)
                break;
        }
        ok=1;
    } while (0);
    if(!ok){
        /* break issued somewhere */
        if(owner)
            free(owner);
        if(group)
            free(group);
        if(attr->acl)
            free(attr->acl);
        if(attr->extended_type)
            free(attr->extended_type);
        if(attr->extended_data)
            free(attr->extended_data);
        free(attr);
        ssh_set_error(sftp->session,SSH_FATAL,"Invalid ATTR structure");
        return NULL;
    }
    /* everything went smoothly */
    if(owner){
        attr->owner=string_to_char(owner);
        free(owner);
    }
    if(group){
        attr->group=string_to_char(group);
        free(group);
    }
    return attr;
}

/* Version 3 code. it is the only one really supported (the draft for the 4 misses clarifications) */
/* maybe a paste of the draft is better than the code */
/*
        uint32   flags
        uint64   size           present only if flag SSH_FILEXFER_ATTR_SIZE
        uint32   uid            present only if flag SSH_FILEXFER_ATTR_UIDGID
        uint32   gid            present only if flag SSH_FILEXFER_ATTR_UIDGID
        uint32   permissions    present only if flag SSH_FILEXFER_ATTR_PERMISSIONS
        uint32   atime          present only if flag SSH_FILEXFER_ACMODTIME
        uint32   mtime          present only if flag SSH_FILEXFER_ACMODTIME
        uint32   extended_count present only if flag SSH_FILEXFER_ATTR_EXTENDED
        string   extended_type
        string   extended_data
        ...      more extended data (extended_type - extended_data pairs),
                   so that number of pairs equals extended_count              */
SFTP_ATTRIBUTES *sftp_parse_attr_3(SFTP_SESSION *sftp,BUFFER *buf,int expectname){
    u32 flags=0;
    STRING *name;
    STRING *longname;
    SFTP_ATTRIBUTES *attr=malloc(sizeof(SFTP_ATTRIBUTES));
    int ok=0;
    memset(attr,0,sizeof(*attr));
    /* it isn't really a loop, but i use it because it's like a try..catch.. construction in C */
    do {
        if(expectname){
            if(!(name=buffer_get_ssh_string(buf)))
                break;
            attr->name=string_to_char(name);
            free(name);
            ssh_say(2,"name : %s\n",attr->name);
            if(!(longname=buffer_get_ssh_string(buf)))
                break;
            attr->longname=string_to_char(longname);
            free(longname);
        }
        if(buffer_get_u32(buf,&flags)!=sizeof(u32))
            break;
        flags=ntohl(flags);
        attr->flags=flags;
        ssh_say(2,"flags : %.8lx\n",flags);
        if(flags & SSH_FILEXFER_ATTR_SIZE){
            if(buffer_get_u64(buf,&attr->size)!=sizeof(u64))
                break;
            attr->size=ntohll(attr->size);
            ssh_say(2,"size : %lld\n",attr->size);
        }
        if(flags & SSH_FILEXFER_ATTR_UIDGID){
            if(buffer_get_u32(buf,&attr->uid)!=sizeof(u32))
                break;
            if(buffer_get_u32(buf,&attr->gid)!=sizeof(u32))
                break;
            attr->uid=ntohl(attr->uid);
            attr->gid=ntohl(attr->gid);
        }
        if(flags & SSH_FILEXFER_ATTR_PERMISSIONS){
            if(buffer_get_u32(buf,&attr->permissions)!=sizeof(u32))
                break;
            attr->permissions=ntohl(attr->permissions);
        }
        if(flags & SSH_FILEXFER_ATTR_ACMODTIME){
            if(buffer_get_u32(buf,&attr->atime)!=sizeof(u32))
                break;
            attr->atime=ntohl(attr->atime);
            if(buffer_get_u32(buf,&attr->mtime)!=sizeof(u32))
                break;
            attr->mtime=ntohl(attr->mtime);
        }
        if (flags & SSH_FILEXFER_ATTR_EXTENDED){
            if(buffer_get_u32(buf,&attr->extended_count)!=sizeof(u32))
                break;
            attr->extended_count=ntohl(attr->extended_count);
            while(attr->extended_count && (attr->extended_type=buffer_get_ssh_string(buf))
                    && (attr->extended_data=buffer_get_ssh_string(buf))){
                        attr->extended_count--;
            }
            if(attr->extended_count)
                break;
        }
        ok=1;
    } while (0);
    if(!ok){
        /* break issued somewhere */
        if(attr->name)
            free(attr->name);
        if(attr->extended_type)
            free(attr->extended_type);
        if(attr->extended_data)
            free(attr->extended_data);
        free(attr);
        ssh_set_error(sftp->session,SSH_FATAL,"Invalid ATTR structure");
        return NULL;
    }
    /* everything went smoothly */
    return attr;
}

void buffer_add_attributes(BUFFER *buffer, SFTP_ATTRIBUTES *attr){
	u32 flags=(attr?attr->flags:0);
	flags &= (SSH_FILEXFER_ATTR_SIZE | SSH_FILEXFER_ATTR_UIDGID | SSH_FILEXFER_ATTR_PERMISSIONS | SSH_FILEXFER_ATTR_ACMODTIME);
	buffer_add_u32(buffer,htonl(flags));
	if(attr){
		if (flags & SSH_FILEXFER_ATTR_SIZE)
		{
			buffer_add_u64(buffer, htonll(attr->size));
		}
		if(flags & SSH_FILEXFER_ATTR_UIDGID){
			buffer_add_u32(buffer,htonl(attr->uid));
			buffer_add_u32(buffer,htonl(attr->gid));
		}
		if(flags & SSH_FILEXFER_ATTR_PERMISSIONS){
			buffer_add_u32(buffer,htonl(attr->permissions));
		}
		if (flags & SSH_FILEXFER_ATTR_ACMODTIME)
		{
			buffer_add_u32(buffer, htonl(attr->atime));
			buffer_add_u32(buffer, htonl(attr->mtime));
		}
	}
}

    
SFTP_ATTRIBUTES *sftp_parse_attr(SFTP_SESSION *session, BUFFER *buf,int expectname){
    switch(session->version){
        case 4:
            return sftp_parse_attr_4(session,buf,expectname);
        case 3:
            return sftp_parse_attr_3(session,buf,expectname);
        default:
            ssh_set_error(session->session,SSH_FATAL,"Version %d unsupported by client",session->server_version);
            return NULL;
    }
    return NULL;
}

int sftp_server_version(SFTP_SESSION *sftp){
    return sftp->server_version;
}

SFTP_ATTRIBUTES *sftp_readdir(SFTP_SESSION *sftp, SFTP_DIR *dir){
    BUFFER *payload;
    u32 id;
    SFTP_MESSAGE *msg=NULL;
    STATUS_MESSAGE *status;
    SFTP_ATTRIBUTES *attr;
    if(!dir->buffer){
        payload=buffer_new();
        id=sftp_get_new_id(sftp);
        buffer_add_u32(payload,id);
        buffer_add_ssh_string(payload,dir->handle);
        sftp_packet_write(sftp,SSH_FXP_READDIR,payload);
        buffer_free(payload);
        ssh_say(2,"sent a ssh_fxp_readdir with id %d\n",id);
        while(!msg){
            if(sftp_read_and_dispatch(sftp))
                /* something nasty has happened */
                return NULL;
            msg=sftp_dequeue(sftp,id);
        }
        switch (msg->packet_type){
            case SSH_FXP_STATUS:
                status=parse_status_msg(msg);
                sftp_message_free(msg);
                if(!status)
                    return NULL;
                if(status->status==SSH_FX_EOF){
                    dir->eof=1;
                    status_msg_free(status);
                    return NULL;
                }
                ssh_set_error(sftp->session,SSH_FATAL,"Unknown error status : %d",status->status);
                status_msg_free(status);
                return NULL;
            case SSH_FXP_NAME:
                buffer_get_u32(msg->payload,&dir->count);
                dir->count=ntohl(dir->count);
                dir->buffer=msg->payload;
                msg->payload=NULL;
                sftp_message_free(msg);
                break;
            default:
                ssh_set_error(sftp->session,SSH_FATAL,"unsupported message back %d",msg->packet_type);
                sftp_message_free(msg);
                return NULL;
        }
    }
    /* now dir->buffer contains a buffer and dir->count != 0 */
    if(dir->count==0){
        ssh_set_error(sftp->session,SSH_FATAL,"Count of files sent by the server is zero, which is invalid, or libsftp bug");
        return NULL;
    }
    ssh_say(2,"Count is %d\n",dir->count);
    attr=sftp_parse_attr(sftp,dir->buffer,1);
    dir->count--;
    if(dir->count==0){
        buffer_free(dir->buffer);
        dir->buffer=NULL;
    }
    return attr;
}

int sftp_dir_eof(SFTP_DIR *dir){
    return (dir->eof);
}

void sftp_attributes_free(SFTP_ATTRIBUTES *file){
    if(file->name)
        free(file->name);
    if(file->longname)
        free(file->longname);
    if(file->acl)
        free(file->acl);
    if(file->extended_data)
        free(file->extended_data);
    if(file->extended_type)
        free(file->extended_type);
    if(file->group)
        free(file->group);
    if(file->owner)
        free(file->owner);
    free(file);
}

static int sftp_handle_close(SFTP_SESSION *sftp, STRING *handle){
    SFTP_MESSAGE *msg=NULL;
    STATUS_MESSAGE *status;
    int id=sftp_get_new_id(sftp);
    int err=0;
    BUFFER *buffer=buffer_new();
    buffer_add_u32(buffer,id);
    buffer_add_ssh_string(buffer,handle);
    sftp_packet_write(sftp,SSH_FXP_CLOSE,buffer);
    buffer_free(buffer);
    while(!msg){
        if(sftp_read_and_dispatch(sftp))
        /* something nasty has happened */
            return -1;
        msg=sftp_dequeue(sftp,id);
    }
    switch (msg->packet_type){
        case SSH_FXP_STATUS:
            status=parse_status_msg(msg);
            sftp_message_free(msg);
            if(!status)
                return -1;
            if(status->status != SSH_FX_OK){
                ssh_set_error(sftp->session,SSH_REQUEST_DENIED,"sftp server : %s",status->errormsg);
                err=-1;
            }
            status_msg_free(status);
            return err;
        default:
            ssh_set_error(sftp->session,SSH_FATAL,"Received message %d during sftp_handle_close!",msg->packet_type);
            sftp_message_free(msg);
    }
    return -1;
}

int sftp_file_close(SFTP_FILE *file){
    int err=0;
    if(file->name)
        free(file->name);
    if(file->handle){
        err=sftp_handle_close(file->sftp,file->handle);
        free(file->handle);
    }
    free(file);
    return err;
}

int sftp_dir_close(SFTP_DIR *dir){
    int err=0;
    if(dir->name)
        free(dir->name);
    if(dir->handle){
        err=sftp_handle_close(dir->sftp,dir->handle);
        free(dir->handle);
    }
    if(dir->buffer)
        buffer_free(dir->buffer);
    free(dir);
    return err;
}

SFTP_FILE *sftp_open(SFTP_SESSION *sftp, char *file, int access, SFTP_ATTRIBUTES *attr){
    SFTP_FILE *handle;
    SFTP_MESSAGE *msg=NULL;
    STATUS_MESSAGE *status;
    u32 flags=0;
    u32 id=sftp_get_new_id(sftp);
    BUFFER *buffer=buffer_new();
    STRING *filename;
    if(access & O_RDONLY)
        flags|=SSH_FXF_READ;
    if(access & O_WRONLY)
        flags |= SSH_FXF_WRITE;
    if(access & O_RDWR)
        flags|=(SSH_FXF_WRITE | SSH_FXF_READ);
    if(access & O_CREAT)
        flags |=SSH_FXF_CREAT;
    if(access & O_TRUNC)
        flags |=SSH_FXF_TRUNC;
    if(access & O_EXCL)
        flags |= SSH_FXF_EXCL;
    buffer_add_u32(buffer,id);
    filename=string_from_char(file);
    buffer_add_ssh_string(buffer,filename);
    free(filename);
    buffer_add_u32(buffer,htonl(flags));
    buffer_add_attributes(buffer,attr);
    sftp_packet_write(sftp,SSH_FXP_OPEN,buffer);
    buffer_free(buffer);
    while(!msg){
        if(sftp_read_and_dispatch(sftp))
        /* something nasty has happened */
            return NULL;
        msg=sftp_dequeue(sftp,id);
    }
    switch (msg->packet_type){
        case SSH_FXP_STATUS:
            status=parse_status_msg(msg);
            sftp_message_free(msg);
            if(!status)
                return NULL;
            ssh_set_error(sftp->session,SSH_REQUEST_DENIED,"sftp server : %s",status->errormsg);
            status_msg_free(status);
            return NULL;
        case SSH_FXP_HANDLE:
            handle=parse_handle_msg(msg);
            sftp_message_free(msg);
            return handle;
        default:
            ssh_set_error(sftp->session,SSH_FATAL,"Received message %d during open!",msg->packet_type);
            sftp_message_free(msg);
    }
    return NULL; 
}

void sftp_file_set_nonblocking(SFTP_FILE *handle){
    handle->nonblocking=1;
}
void sftp_file_set_blocking(SFTP_FILE *handle){
    handle->nonblocking=0;
}

int sftp_read(SFTP_FILE *handle, void *data, int len){
    SFTP_MESSAGE *msg=NULL;
    STATUS_MESSAGE *status;
    SFTP_SESSION *sftp=handle->sftp;
    STRING *datastring;
    int id;
    int err=0;
    BUFFER *buffer;
    if(handle->eof)
        return 0; 
    buffer=buffer_new();
    id=sftp_get_new_id(handle->sftp);
    buffer_add_u32(buffer,id);
    buffer_add_ssh_string(buffer,handle->handle);
    buffer_add_u64(buffer,htonll(handle->offset));
    buffer_add_u32(buffer,htonl(len));
    sftp_packet_write(handle->sftp,SSH_FXP_READ,buffer);
    buffer_free(buffer);
    while(!msg){
        if (handle->nonblocking){
            if(channel_poll(handle->sftp->channel,0)==0){
                /* we cannot block */
                return 0;
            }
        }
        if(sftp_read_and_dispatch(handle->sftp))
        /* something nasty has happened */
            return -1;
        msg=sftp_dequeue(handle->sftp,id);
    }
    switch (msg->packet_type){
        case SSH_FXP_STATUS:
            status=parse_status_msg(msg);
            sftp_message_free(msg);
            if(!status)
                return -1;
            if(status->status != SSH_FX_EOF){
                ssh_set_error(sftp->session,SSH_REQUEST_DENIED,"sftp server : %s",status->errormsg);
                err=-1;
            }
            else
                handle->eof=1;
            status_msg_free(status);
            return err?err:0;
        case SSH_FXP_DATA:
            datastring=buffer_get_ssh_string(msg->payload);
            sftp_message_free(msg);
            if(!datastring){
                ssh_set_error(sftp->session,SSH_FATAL,"Received invalid DATA packet from sftp server");
                return -1;
            }
            if(string_len(datastring)>len){
                ssh_set_error(sftp->session,SSH_FATAL,"Received a too big DATA packet from sftp server : %d and asked for %d",
                    string_len(datastring),len);
                free(datastring);
                return -1;
            }
            len=string_len(datastring);
            handle->offset+=len;
            memcpy(data,datastring->string,len);
            free(datastring);
            return len;
        default:
            ssh_set_error(sftp->session,SSH_FATAL,"Received message %d during read!",msg->packet_type);
            sftp_message_free(msg);
            return -1;
    }
    return -1; /* not reached */
}

/** This function does an asynchronous read on a file. Its goal is to avoid the slowdowns related to
 * the request/response pattern of a synchronous read.\n
 * To use if, you must call the function two times.\n
 * The first time, *id must be a pointer to an integer containing 0. The function will always return SSH_AGAIN the first time.\n
 * The second time, the function will block until the data have been read, and will return the number of bytes read.\n
 * If the file is opened in non-blocking mode, the second-time reading will return SSH_AGAIN until the data are available.\n
 * \brief Asynchronous read on a file
 * \param file the file handle
 * \param data pointer to the data to be written
 * \param len length of data to be read
 * \param id a pointer to an identifier
 * \return 0 When the file is EOF\n
 * SSH_AGAIN the function has to be called again\n
 * SSH_ERROR in case of error\n
 * otherwise, the number of bytes read is returned
 * \warning when calling the function for the first time, the internal offset is updated
 * corresponding to the len parameter. If the length effectively read is different from the
 * len parameter, every following read() will be out of sync.
 * */
int sftp_async_read(SFTP_FILE *file, void *data, int len, int *id){
    SFTP_MESSAGE *msg=NULL;
    STATUS_MESSAGE *status;
    SFTP_SESSION *sftp=file->sftp;
    STRING *datastring;
    int err=0;
    BUFFER *buffer;
    if(id==NULL){
    	ssh_set_error(sftp->session,SSH_REQUEST_DENIED,"id parameter must not be null");
    	return -1;
    }
    if(file->eof)
        return 0;
    if(*id==0){
    	// launch a new request
    	buffer=buffer_new();
    	*id=sftp_get_new_id(sftp);
    	buffer_add_u32(buffer,*id);
    	buffer_add_ssh_string(buffer,file->handle);
    	buffer_add_u64(buffer,htonll(file->offset));
    	buffer_add_u32(buffer,htonl(len));
    	sftp_packet_write(sftp,SSH_FXP_READ,buffer);
    	buffer_free(buffer);
    	file->offset += len; // assume we'll read len bytes
    	return SSH_AGAIN; // must call back
    } else {
    	// handle an existing request
    	while(!msg){
    		if (file->nonblocking){
    			if(channel_poll(sftp->channel,0)==0){
    				/* we cannot block */
    	            return SSH_AGAIN;
    	        }
    	    }
    	    if(sftp_read_and_dispatch(sftp))
    	    /* something nasty has happened */
    	    	return -1;
    	    msg=sftp_dequeue(sftp,*id);
    	}
    	*id=0;
    	switch (msg->packet_type){
    		case SSH_FXP_STATUS:
    			status=parse_status_msg(msg);
    	        sftp_message_free(msg);
    	        if(!status)
    	        	return -1;
    	        if(status->status != SSH_FX_EOF){
    	        	ssh_set_error(sftp->session,SSH_REQUEST_DENIED,"sftp server : %s",status->errormsg);
    	            err=-1;
    	        } else
    	        	file->eof=1;
    	        status_msg_free(status);
    	        return err?err:0;
    	    case SSH_FXP_DATA:
    	        datastring=buffer_get_ssh_string(msg->payload);
    	        sftp_message_free(msg);
    	        if(!datastring){
    	            ssh_set_error(sftp->session,SSH_FATAL,"Received invalid DATA packet from sftp server");
    	            return -1;
    	        }
    	        if(string_len(datastring)>len){
    	            ssh_set_error(sftp->session,SSH_FATAL,"Received a too big DATA packet from sftp server : %d and asked for %d",
    	                string_len(datastring),len);
    	            free(datastring);
    	            return -1;
    	        }
    	        len=string_len(datastring);
    	        //handle->offset+=len; 
    	        /* We already have set the offset previously. All we can do is warn that the expected len
    	         * and effective lengths are different */
    	        memcpy(data,datastring->string,len);
    	        free(datastring);
    	        return len;
    	    default:
    	        ssh_set_error(sftp->session,SSH_FATAL,"Received message %d during read!",msg->packet_type);
    	        sftp_message_free(msg);
    	        return -1;
    	    }
    }
    
}
int sftp_write(SFTP_FILE *file, void *data, int len){
    SFTP_MESSAGE *msg=NULL;
    STATUS_MESSAGE *status;
    STRING *datastring;
    SFTP_SESSION *sftp=file->sftp;
    int id;
    int err=0;
    BUFFER *buffer;
    buffer=buffer_new();
    id=sftp_get_new_id(file->sftp);
    buffer_add_u32(buffer,id);
    buffer_add_ssh_string(buffer,file->handle);
    buffer_add_u64(buffer,htonll(file->offset));
    datastring=string_new(len);
    string_fill(datastring,data,len);
    buffer_add_ssh_string(buffer,datastring);
    free(datastring);
    if(sftp_packet_write(file->sftp,SSH_FXP_WRITE,buffer) != buffer_get_len(buffer)){
        ssh_say(1,"sftp_packet_write did not write as much data as expected\n");
    }
    buffer_free(buffer);
    while(!msg){
        if(sftp_read_and_dispatch(file->sftp))
        /* something nasty has happened */
            return -1;
        msg=sftp_dequeue(file->sftp,id);
    }
    switch (msg->packet_type){
        case SSH_FXP_STATUS:
            status=parse_status_msg(msg);
            sftp_message_free(msg);
            if(!status)
                return -1;
            if(status->status != SSH_FX_OK){
                ssh_set_error(sftp->session,SSH_REQUEST_DENIED,"sftp server : %s",status->errormsg);
                err=-1;
            }
            file->offset+=len;
            status_msg_free(status);
            return (err?err:len);
        default:
            ssh_set_error(sftp->session,SSH_FATAL,"Received message %d during write!",msg->packet_type);
            sftp_message_free(msg);
            return -1;
    }
    return -1; /* not reached */
}

void sftp_seek(SFTP_FILE *file, int new_offset){
    file->offset=new_offset;
}

unsigned long sftp_tell(SFTP_FILE *file){
    return file->offset;
}

void sftp_rewind(SFTP_FILE *file){
    file->offset=0;
}

/* code written by Nick */
int sftp_rm(SFTP_SESSION *sftp, char *file) {
    u32 id = sftp_get_new_id(sftp);
    BUFFER *buffer = buffer_new();
    STRING *filename = string_from_char(file);
    SFTP_MESSAGE *msg = NULL;
    STATUS_MESSAGE *status = NULL;

    buffer_add_u32(buffer, id);
    buffer_add_ssh_string(buffer, filename);
    free(filename);
    sftp_packet_write(sftp, SSH_FXP_REMOVE, buffer);
    buffer_free(buffer);
    while (!msg) {
        if (sftp_read_and_dispatch(sftp)) {
            return -1;
        }
        msg = sftp_dequeue(sftp, id);
    }
    if (msg->packet_type == SSH_FXP_STATUS) {
         /* by specification, this command's only supposed to return SSH_FXP_STATUS */    
         status = parse_status_msg(msg);
         sftp_message_free(msg);
         if (!status)
             return -1;
         if (status->status != SSH_FX_OK) {
          /* status should be SSH_FX_OK if the command was successful, if it didn't, then there was an error */
              ssh_set_error(sftp->session,SSH_REQUEST_DENIED, "sftp server: %s", status->errormsg);
              status_msg_free(status);
              return -1;
         }
         status_msg_free(status);
         return 0;   /* at this point, everything turned out OK */
    } else {
        ssh_set_error(sftp->session,SSH_FATAL, "Received message %d when attempting to remove file", msg->packet_type);
        sftp_message_free(msg);
    }
    return -1;
}

/* code written by Nick */
int sftp_rmdir(SFTP_SESSION *sftp, char *directory) {
    u32 id = sftp_get_new_id(sftp);
    BUFFER *buffer = buffer_new();
    STRING *filename = string_from_char(directory);
    SFTP_MESSAGE *msg = NULL;
    STATUS_MESSAGE *status = NULL;

    buffer_add_u32(buffer, id);
    buffer_add_ssh_string(buffer, filename);
    free(filename);
    sftp_packet_write(sftp, SSH_FXP_RMDIR, buffer);
    buffer_free(buffer);
    while (!msg) {
        if (sftp_read_and_dispatch(sftp))
        {
            return -1;
        }
        msg = sftp_dequeue(sftp, id);
    }
    if (msg->packet_type == SSH_FXP_STATUS) /* by specification, this command's only supposed to return SSH_FXP_STATUS */
    {
        status = parse_status_msg(msg);
        sftp_message_free(msg);
        if (!status)
        {
            return -1;
        }
        else if (status->status != SSH_FX_OK)   /* status should be SSH_FX_OK if the command was successful, if it didn't, then there was an error */
        {
            ssh_set_error(sftp->session,SSH_REQUEST_DENIED, "sftp server: %s", status->errormsg);
            status_msg_free(status);
            return -1;
        }
        status_msg_free(status);
        return 0;   /* at this point, everything turned out OK */
    }
    else
    {
        ssh_set_error(sftp->session,SSH_FATAL, "Received message %d when attempting to remove directory", msg->packet_type);
        sftp_message_free(msg);
    }
    return -1;
}

/* Code written by Nick */
int sftp_mkdir(SFTP_SESSION *sftp, char *directory, SFTP_ATTRIBUTES *attr) {
    u32 id = sftp_get_new_id(sftp);
    BUFFER *buffer = buffer_new();
    STRING *path = string_from_char(directory);
    SFTP_MESSAGE *msg = NULL;
    STATUS_MESSAGE *status = NULL;

    buffer_add_u32(buffer, id);
    buffer_add_ssh_string(buffer, path);
    free(path);
    buffer_add_attributes(buffer, attr);
    sftp_packet_write(sftp, SSH_FXP_MKDIR, buffer);
    buffer_free(buffer);
    while (!msg) {
        if (sftp_read_and_dispatch(sftp))
            return -1;
        msg = sftp_dequeue(sftp, id);
    }
    if (msg->packet_type == SSH_FXP_STATUS) {
        /* by specification, this command's only supposed to return SSH_FXP_STATUS */
        status = parse_status_msg(msg);
        sftp_message_free(msg);
        if (!status)
            return -1;
        else
        if (status->status != SSH_FX_OK) {
            /* status should be SSH_FX_OK if the command was successful, if it didn't, then there was an error */
            ssh_set_error(sftp->session,SSH_REQUEST_DENIED, "sftp server: %s", status->errormsg);
            status_msg_free(status);
            return -1;
        }
        status_msg_free(status);
        return 0;   /* at this point, everything turned out OK */
    } else {
       ssh_set_error(sftp->session,SSH_FATAL, "Received message %d when attempting to make directory", msg->packet_type);
       sftp_message_free(msg);
    }
    return -1;
}

/* code written by nick */
int sftp_rename(SFTP_SESSION *sftp, char *original, char *newname) {
    u32 id = sftp_get_new_id(sftp);
    BUFFER *buffer = buffer_new();
    STRING *oldpath = string_from_char(original);
    STRING *newpath = string_from_char(newname);
    SFTP_MESSAGE *msg = NULL;
    STATUS_MESSAGE *status = NULL;

    buffer_add_u32(buffer, id);
    buffer_add_ssh_string(buffer, oldpath);
    free(oldpath);
    buffer_add_ssh_string(buffer, newpath);
    free(newpath);
    sftp_packet_write(sftp, SSH_FXP_RENAME, buffer);
    buffer_free(buffer);
    while (!msg) {
        if (sftp_read_and_dispatch(sftp))
            return -1;
        msg = sftp_dequeue(sftp, id);
    }
    if (msg->packet_type == SSH_FXP_STATUS) {
         /* by specification, this command's only supposed to return SSH_FXP_STATUS */
        status = parse_status_msg(msg);
        sftp_message_free(msg);
        if (!status)
            return -1;
        else if (status->status != SSH_FX_OK) {
               /* status should be SSH_FX_OK if the command was successful, if it didn't, then there was an error */
            ssh_set_error(sftp->session,SSH_REQUEST_DENIED, "sftp server: %s", status->errormsg);
            status_msg_free(status);
            return -1;
        }
        status_msg_free(status);
        return 0;   /* at this point, everything turned out OK */
    } else {
        ssh_set_error(sftp->session,SSH_FATAL, "Received message %d when attempting to rename", msg->packet_type);
        sftp_message_free(msg);
    }
    return -1;
}

/* Code written by Nick */
int sftp_setstat(SFTP_SESSION *sftp, char *file, SFTP_ATTRIBUTES *attr) {
    u32 id = sftp_get_new_id(sftp);
    BUFFER *buffer = buffer_new();
    STRING *path = string_from_char(file);
    SFTP_MESSAGE *msg = NULL;
    STATUS_MESSAGE *status = NULL;

    buffer_add_u32(buffer, id);
    buffer_add_ssh_string(buffer, path);
    free(path);
    buffer_add_attributes(buffer, attr);
    sftp_packet_write(sftp, SSH_FXP_SETSTAT, buffer);
    buffer_free(buffer);
    while (!msg) {
        if (sftp_read_and_dispatch(sftp))
            return -1;
        msg = sftp_dequeue(sftp, id);
    }
    if (msg->packet_type == SSH_FXP_STATUS) {
         /* by specification, this command's only supposed to return SSH_FXP_STATUS */
        status = parse_status_msg(msg);
        sftp_message_free(msg);
        if (!status)
            return -1;
        else if (status->status != SSH_FX_OK) {
               /* status should be SSH_FX_OK if the command was successful, if it didn't, then there was an error */
            ssh_set_error(sftp->session,SSH_REQUEST_DENIED, "sftp server: %s", status->errormsg);
            status_msg_free(status);
            return -1;
        }
        status_msg_free(status);
        return 0;   /* at this point, everything turned out OK */
    } else {
        ssh_set_error(sftp->session,SSH_FATAL, "Received message %d when attempting to set stats", msg->packet_type);
        sftp_message_free(msg);
    }
    return -1;
}

/* another code written by Nick */
char *sftp_canonicalize_path(SFTP_SESSION *sftp, char *path)
{
	u32 id = sftp_get_new_id(sftp);
	BUFFER *buffer = buffer_new();
	STRING *pathstr = string_from_char(path);
	STRING *name = NULL;
	SFTP_MESSAGE *msg = NULL;
	STATUS_MESSAGE *status = NULL;
	char *cname;
	u32 ignored;
	
	buffer_add_u32(buffer, id);
	buffer_add_ssh_string(buffer, pathstr);
	free(pathstr);
	sftp_packet_write(sftp, SSH_FXP_REALPATH, buffer);
	buffer_free(buffer);
	while (!msg)
	{
		if (sftp_read_and_dispatch(sftp))
			return NULL;
		msg = sftp_dequeue(sftp, id);
	}
	if (msg->packet_type == SSH_FXP_NAME)   /* good response */
	{
		buffer_get_u32(msg->payload, &ignored);	/* we don't care about "count" */
		name = buffer_get_ssh_string(msg->payload); /* we only care about the file name string */
		cname = string_to_char(name);
		free(name);
		return cname;
	}
	else if (msg->packet_type == SSH_FXP_STATUS)	/* bad response (error) */
	{
		status = parse_status_msg(msg);
		sftp_message_free(msg);
		if (!status)
            return NULL;
        ssh_set_error(sftp->session,SSH_REQUEST_DENIED, "sftp server: %s", status->errormsg);
        status_msg_free(status);
	}
	else	/* this shouldn't happen */
	{
		ssh_set_error(sftp->session,SSH_FATAL, "Received message %d when attempting to set stats", msg->packet_type);
        sftp_message_free(msg);
	}
	return NULL;
}

SFTP_ATTRIBUTES *sftp_xstat(SFTP_SESSION *sftp, char *path,int param){
    u32 id=sftp_get_new_id(sftp);
    BUFFER *buffer=buffer_new();
    STRING *pathstr= string_from_char(path);
    SFTP_MESSAGE *msg=NULL;
    STATUS_MESSAGE *status=NULL;
    SFTP_ATTRIBUTES *pattr=NULL;

    buffer_add_u32(buffer,id);
    buffer_add_ssh_string(buffer,pathstr);
    free(pathstr);
    sftp_packet_write(sftp,param,buffer);
    buffer_free(buffer);
    while(!msg){
        if(sftp_read_and_dispatch(sftp))
            return NULL;
        msg=sftp_dequeue(sftp,id);
    }
    if(msg->packet_type==SSH_FXP_ATTRS){
        pattr=sftp_parse_attr(sftp,msg->payload,0);
        return pattr;
    }
    if(msg->packet_type== SSH_FXP_STATUS){
        status=parse_status_msg(msg);
        sftp_message_free(msg);
        if(!status)
            return NULL;
        ssh_set_error(sftp->session,SSH_REQUEST_DENIED,"sftp server: %s",status->errormsg);
        status_msg_free(status);
        return NULL;
    }
    ssh_set_error(sftp->session,SSH_FATAL,"Received mesg %d during stat(),mesg->packet_type");
    sftp_message_free(msg);
    return NULL;
}

SFTP_ATTRIBUTES *sftp_stat(SFTP_SESSION *session, char *path){
    return sftp_xstat(session,path,SSH_FXP_STAT);
}
SFTP_ATTRIBUTES *sftp_lstat(SFTP_SESSION *session, char *path){
    return sftp_xstat(session,path,SSH_FXP_LSTAT);
}

SFTP_ATTRIBUTES *sftp_fstat(SFTP_FILE *file) {
    u32 id=sftp_get_new_id(file->sftp);
    BUFFER *buffer=buffer_new();
    SFTP_MESSAGE *msg=NULL;
    STATUS_MESSAGE *status=NULL;
    SFTP_ATTRIBUTES *pattr=NULL;

    buffer_add_u32(buffer,id);
    buffer_add_ssh_string(buffer,file->handle);
    sftp_packet_write(file->sftp,SSH_FXP_FSTAT,buffer);
    buffer_free(buffer);
    while(!msg){
        if(sftp_read_and_dispatch(file->sftp))
            return NULL;
        msg=sftp_dequeue(file->sftp,id);
    }
    if(msg->packet_type==SSH_FXP_ATTRS){
        pattr=sftp_parse_attr(file->sftp,msg->payload,0);
        return pattr;
    }
    if(msg->packet_type== SSH_FXP_STATUS){
        status=parse_status_msg(msg);
        sftp_message_free(msg);
        if(!status)
            return NULL;
        ssh_set_error(file->sftp->session,SSH_REQUEST_DENIED,"sftp server: %s",status->errormsg);
        status_msg_free(status);
        return NULL;
    }
    ssh_set_error(file->sftp->session,SSH_FATAL,"Received mesg %d during fstat(),mesg->packet_type");
    sftp_message_free(msg);
    return NULL;
}

#endif /* NO_SFTP */

/** @} */