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
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
|
/* Copyright (C) Sara Golemon <sarag@libssh2.org>
* Copyright (C) Eli Fant <elifantu@mail.ru>
* Copyright (C) Daniel Stenberg
* All rights reserved.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* Neither the name of the copyright holder nor the names
* of any other contributors may be used to endorse or
* promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "libssh2_priv.h"
#include "libssh2_sftp.h"
#include "channel.h"
#include "session.h"
#include "sftp.h"
#include <assert.h>
/* This release of libssh2 implements Version 5 with automatic downgrade
* based on server's declaration
* https://www.ietf.org/archive/id/draft-ietf-secsh-filexfer-05.txt
*
* Version 6:
* https://www.ietf.org/archive/id/draft-ietf-secsh-filexfer-13.txt
*/
/* SFTP packet types */
#define SSH_FXP_INIT 1
#define SSH_FXP_VERSION 2
#define SSH_FXP_OPEN 3
#define SSH_FXP_CLOSE 4
#define SSH_FXP_READ 5
#define SSH_FXP_WRITE 6
#define SSH_FXP_LSTAT 7
#define SSH_FXP_FSTAT 8
#define SSH_FXP_SETSTAT 9
#define SSH_FXP_FSETSTAT 10
#define SSH_FXP_OPENDIR 11
#define SSH_FXP_READDIR 12
#define SSH_FXP_REMOVE 13
#define SSH_FXP_MKDIR 14
#define SSH_FXP_RMDIR 15
#define SSH_FXP_REALPATH 16
#define SSH_FXP_STAT 17
#define SSH_FXP_RENAME 18
#define SSH_FXP_READLINK 19
#define SSH_FXP_SYMLINK 20
#define SSH_FXP_STATUS 101
#define SSH_FXP_HANDLE 102
#define SSH_FXP_DATA 103
#define SSH_FXP_NAME 104
#define SSH_FXP_ATTRS 105
#define SSH_FXP_EXTENDED 200
#define SSH_FXP_EXTENDED_REPLY 201
/* S_IFREG */
#define LIBSSH2_SFTP_ATTR_PFILETYPE_FILE 0100000
/* S_IFDIR */
#define LIBSSH2_SFTP_ATTR_PFILETYPE_DIR 0040000
#define SSH_FXE_STATVFS_ST_RDONLY 0x00000001
#define SSH_FXE_STATVFS_ST_NOSUID 0x00000002
/* This is the maximum packet length to accept, as larger than this indicate
some kind of server problem. */
#define LIBSSH2_SFTP_PACKET_MAXLEN (256 * 1024)
static int sftp_packet_ask(LIBSSH2_SFTP *sftp, unsigned char packet_type,
uint32_t request_id, unsigned char **data,
size_t *data_len);
static void sftp_packet_flush(LIBSSH2_SFTP *sftp);
/* _libssh2_store_u64
*/
static void _libssh2_store_u64(unsigned char **ptr, libssh2_uint64_t value)
{
uint32_t msl = (uint32_t)(value >> 32);
unsigned char *buf = *ptr;
buf[0] = (unsigned char)((msl >> 24) & 0xFF);
buf[1] = (unsigned char)((msl >> 16) & 0xFF);
buf[2] = (unsigned char)((msl >> 8) & 0xFF);
buf[3] = (unsigned char)( msl & 0xFF);
buf[4] = (unsigned char)((value >> 24) & 0xFF);
buf[5] = (unsigned char)((value >> 16) & 0xFF);
buf[6] = (unsigned char)((value >> 8) & 0xFF);
buf[7] = (unsigned char)( value & 0xFF);
*ptr += 8;
}
/*
* Search list of zombied FXP_READ request IDs.
*
* Returns NULL if ID not in list.
*/
static struct sftp_zombie_requests *
find_zombie_request(LIBSSH2_SFTP *sftp, uint32_t request_id)
{
struct sftp_zombie_requests *zombie =
_libssh2_list_first(&sftp->zombie_requests);
while(zombie) {
if(zombie->request_id == request_id)
break;
else
zombie = _libssh2_list_next(&zombie->node);
}
return zombie;
}
static void
remove_zombie_request(LIBSSH2_SFTP *sftp, uint32_t request_id)
{
LIBSSH2_SESSION *session = sftp->channel->session;
struct sftp_zombie_requests *zombie = find_zombie_request(sftp,
request_id);
if(zombie) {
_libssh2_debug((session, LIBSSH2_TRACE_SFTP,
"Removing request ID %ld from the list of "
"zombie requests",
request_id));
_libssh2_list_remove(&zombie->node);
LIBSSH2_FREE(session, zombie);
}
}
static int
add_zombie_request(LIBSSH2_SFTP *sftp, uint32_t request_id)
{
LIBSSH2_SESSION *session = sftp->channel->session;
struct sftp_zombie_requests *zombie;
_libssh2_debug((session, LIBSSH2_TRACE_SFTP,
"Marking request ID %ld as a zombie request", request_id));
zombie = LIBSSH2_ALLOC(sftp->channel->session,
sizeof(struct sftp_zombie_requests));
if(!zombie)
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"malloc fail for zombie request ID");
else {
zombie->request_id = request_id;
_libssh2_list_add(&sftp->zombie_requests, &zombie->node);
return LIBSSH2_ERROR_NONE;
}
}
/* sftp_packet_add
* Add a packet to the SFTP packet brigade
*/
static int
sftp_packet_add(LIBSSH2_SFTP *sftp, unsigned char *data,
size_t data_len)
{
LIBSSH2_SESSION *session = sftp->channel->session;
LIBSSH2_SFTP_PACKET *packet;
uint32_t request_id;
if(data_len < 5) {
return LIBSSH2_ERROR_OUT_OF_BOUNDARY;
}
_libssh2_debug((session, LIBSSH2_TRACE_SFTP,
"Received packet type %d (len %d)",
(int) data[0], data_len));
/*
* Experience shows that if we mess up EAGAIN handling somewhere or
* otherwise get out of sync with the channel, this is where we first get
* a wrong byte and if so we need to bail out at once to aid tracking the
* problem better.
*/
switch(data[0]) {
case SSH_FXP_INIT:
case SSH_FXP_VERSION:
case SSH_FXP_OPEN:
case SSH_FXP_CLOSE:
case SSH_FXP_READ:
case SSH_FXP_WRITE:
case SSH_FXP_LSTAT:
case SSH_FXP_FSTAT:
case SSH_FXP_SETSTAT:
case SSH_FXP_FSETSTAT:
case SSH_FXP_OPENDIR:
case SSH_FXP_READDIR:
case SSH_FXP_REMOVE:
case SSH_FXP_MKDIR:
case SSH_FXP_RMDIR:
case SSH_FXP_REALPATH:
case SSH_FXP_STAT:
case SSH_FXP_RENAME:
case SSH_FXP_READLINK:
case SSH_FXP_SYMLINK:
case SSH_FXP_STATUS:
case SSH_FXP_HANDLE:
case SSH_FXP_DATA:
case SSH_FXP_NAME:
case SSH_FXP_ATTRS:
case SSH_FXP_EXTENDED:
case SSH_FXP_EXTENDED_REPLY:
break;
default:
sftp->last_errno = LIBSSH2_FX_OK;
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Out of sync with the world");
}
request_id = _libssh2_ntohu32(&data[1]);
_libssh2_debug((session, LIBSSH2_TRACE_SFTP, "Received packet id %d",
request_id));
/* Don't add the packet if it answers a request we've given up on. */
if((data[0] == SSH_FXP_STATUS || data[0] == SSH_FXP_DATA)
&& find_zombie_request(sftp, request_id)) {
/* If we get here, the file ended before the response arrived. We
are no longer interested in the request so we discard it */
LIBSSH2_FREE(session, data);
remove_zombie_request(sftp, request_id);
return LIBSSH2_ERROR_NONE;
}
packet = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_SFTP_PACKET));
if(!packet) {
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate datablock for SFTP packet");
}
packet->data = data;
packet->data_len = data_len;
packet->request_id = request_id;
_libssh2_list_add(&sftp->packets, &packet->node);
return LIBSSH2_ERROR_NONE;
}
/* sftp_packet_read
* Frame an SFTP packet off the channel
*/
static int
sftp_packet_read(LIBSSH2_SFTP *sftp)
{
LIBSSH2_CHANNEL *channel = sftp->channel;
LIBSSH2_SESSION *session = channel->session;
unsigned char *packet = NULL;
ssize_t rc;
unsigned long recv_window;
int packet_type;
uint32_t request_id;
_libssh2_debug((session, LIBSSH2_TRACE_SFTP, "recv packet"));
switch(sftp->packet_state) {
case libssh2_NB_state_sent: /* EAGAIN from window adjusting */
sftp->packet_state = libssh2_NB_state_idle;
packet = sftp->partial_packet;
goto window_adjust;
case libssh2_NB_state_sent1: /* EAGAIN from channel read */
sftp->packet_state = libssh2_NB_state_idle;
packet = sftp->partial_packet;
_libssh2_debug((session, LIBSSH2_TRACE_SFTP,
"partial read cont, len: %lu", sftp->partial_len));
_libssh2_debug((session, LIBSSH2_TRACE_SFTP,
"partial read cont, already recvd: %lu",
sftp->partial_received));
/* fall-through */
default:
if(!packet) {
/* only do this if there's not already a packet buffer allocated
to use */
/* each packet starts with a 32 bit length field */
rc = _libssh2_channel_read(channel, 0,
(char *)&sftp->packet_header[
sftp->packet_header_len],
sizeof(sftp->packet_header) -
sftp->packet_header_len);
if(rc == LIBSSH2_ERROR_EAGAIN)
return (int)rc;
else if(rc < 0)
return _libssh2_error(session, (int)rc, "channel read");
sftp->packet_header_len += rc;
if(sftp->packet_header_len != sizeof(sftp->packet_header))
/* we got a short read for the header part */
return LIBSSH2_ERROR_EAGAIN;
/* parse SFTP packet header */
sftp->partial_len = _libssh2_ntohu32(sftp->packet_header);
packet_type = sftp->packet_header[4];
request_id = _libssh2_ntohu32(sftp->packet_header + 5);
/* make sure we don't proceed if the packet size is unreasonably
large */
if(sftp->partial_len > LIBSSH2_SFTP_PACKET_MAXLEN &&
/* exception: response to SSH_FXP_READDIR request */
!(sftp->readdir_state != libssh2_NB_state_idle &&
sftp->readdir_request_id == request_id &&
packet_type == SSH_FXP_NAME)) {
libssh2_channel_flush(channel);
sftp->packet_header_len = 0;
return _libssh2_error(session,
LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED,
"SFTP packet too large");
}
if(sftp->partial_len < 5)
return _libssh2_error(session,
LIBSSH2_ERROR_ALLOC,
"Invalid SFTP packet size");
_libssh2_debug((session, LIBSSH2_TRACE_SFTP,
"Data begin - Packet Length: %lu",
sftp->partial_len));
packet = LIBSSH2_ALLOC(session, sftp->partial_len);
if(!packet)
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate SFTP packet");
sftp->packet_header_len = 0;
sftp->partial_packet = packet;
/* copy over packet type(4) and request id(1) */
sftp->partial_received = 5;
memcpy(packet, sftp->packet_header + 4, 5);
window_adjust:
recv_window = libssh2_channel_window_read_ex(channel, NULL, NULL);
if(sftp->partial_len > recv_window) {
/* ask for twice the data amount we need at once */
rc = _libssh2_channel_receive_window_adjust(channel,
sftp->partial_len
* 2,
1, NULL);
/* store the state so that we continue with the correct
operation at next invoke */
sftp->packet_state = (rc == LIBSSH2_ERROR_EAGAIN)?
libssh2_NB_state_sent:
libssh2_NB_state_idle;
if(rc == LIBSSH2_ERROR_EAGAIN)
return (int)rc;
}
}
/* Read as much of the packet as we can */
while(sftp->partial_len > sftp->partial_received) {
rc = _libssh2_channel_read(channel, 0,
(char *)&packet[sftp->partial_received],
sftp->partial_len -
sftp->partial_received);
if(rc == LIBSSH2_ERROR_EAGAIN) {
/*
* We received EAGAIN, save what we have and return EAGAIN to
* the caller. Set 'partial_packet' so that this function
* knows how to continue on the next invoke.
*/
sftp->packet_state = libssh2_NB_state_sent1;
return (int)rc;
}
else if(rc < 0) {
LIBSSH2_FREE(session, packet);
sftp->partial_packet = NULL;
return _libssh2_error(session, (int)rc,
"Error waiting for SFTP packet");
}
sftp->partial_received += rc;
}
sftp->partial_packet = NULL;
/* sftp_packet_add takes ownership of the packet and might free it
so we take a copy of the packet type before we call it. */
packet_type = packet[0];
rc = sftp_packet_add(sftp, packet, sftp->partial_len);
if(rc) {
LIBSSH2_FREE(session, packet);
return (int)rc;
}
else {
return packet_type;
}
}
/* WON'T REACH */
}
/* sftp_packetlist_flush
*
* Remove all pending packets in the packet_list and the corresponding one(s)
* in the SFTP packet brigade.
*/
static void sftp_packetlist_flush(LIBSSH2_SFTP_HANDLE *handle)
{
struct sftp_pipeline_chunk *chunk;
LIBSSH2_SFTP *sftp = handle->sftp;
LIBSSH2_SESSION *session = sftp->channel->session;
/* remove pending packets, if any */
chunk = _libssh2_list_first(&handle->packet_list);
while(chunk) {
unsigned char *data;
size_t data_len;
int rc;
struct sftp_pipeline_chunk *next = _libssh2_list_next(&chunk->node);
rc = sftp_packet_ask(sftp, SSH_FXP_STATUS,
chunk->request_id, &data, &data_len);
if(rc)
rc = sftp_packet_ask(sftp, SSH_FXP_DATA,
chunk->request_id, &data, &data_len);
if(!rc)
/* we found a packet, free it */
LIBSSH2_FREE(session, data);
else if(chunk->sent)
/* there was no incoming packet for this request, mark this
request as a zombie if it ever sent the request */
add_zombie_request(sftp, chunk->request_id);
_libssh2_list_remove(&chunk->node);
LIBSSH2_FREE(session, chunk);
chunk = next;
}
}
/*
* sftp_packet_ask
*
* Checks if there's a matching SFTP packet available.
*/
static int
sftp_packet_ask(LIBSSH2_SFTP *sftp, unsigned char packet_type,
uint32_t request_id, unsigned char **data,
size_t *data_len)
{
LIBSSH2_SESSION *session = sftp->channel->session;
LIBSSH2_SFTP_PACKET *packet = _libssh2_list_first(&sftp->packets);
if(!packet)
return -1;
/* Special consideration when getting VERSION packet */
while(packet) {
if((packet->data[0] == packet_type) &&
((packet_type == SSH_FXP_VERSION) ||
(packet->request_id == request_id))) {
/* Match! Fetch the data */
*data = packet->data;
*data_len = packet->data_len;
/* unlink and free this struct */
_libssh2_list_remove(&packet->node);
LIBSSH2_FREE(session, packet);
return 0;
}
/* check next struct in the list */
packet = _libssh2_list_next(&packet->node);
}
return -1;
}
/* sftp_packet_require
* A la libssh2_packet_require
*/
static int
sftp_packet_require(LIBSSH2_SFTP *sftp, unsigned char packet_type,
uint32_t request_id, unsigned char **data,
size_t *data_len, size_t required_size)
{
LIBSSH2_SESSION *session = sftp->channel->session;
int rc;
if(!data || !data_len || required_size == 0) {
return LIBSSH2_ERROR_BAD_USE;
}
_libssh2_debug((session, LIBSSH2_TRACE_SFTP, "Requiring packet %d id %ld",
(int) packet_type, request_id));
if(sftp_packet_ask(sftp, packet_type, request_id, data, data_len) == 0) {
/* The right packet was available in the packet brigade */
_libssh2_debug((session, LIBSSH2_TRACE_SFTP, "Got %d",
(int) packet_type));
if(*data_len < required_size) {
return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
}
return LIBSSH2_ERROR_NONE;
}
while(session->socket_state == LIBSSH2_SOCKET_CONNECTED) {
rc = sftp_packet_read(sftp);
if(rc < 0)
return rc;
/* data was read, check the queue again */
if(!sftp_packet_ask(sftp, packet_type, request_id, data, data_len)) {
/* The right packet was available in the packet brigade */
_libssh2_debug((session, LIBSSH2_TRACE_SFTP, "Got %d",
(int) packet_type));
if(*data_len < required_size) {
return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
}
return LIBSSH2_ERROR_NONE;
}
}
/* Only reached if the socket died */
return LIBSSH2_ERROR_SOCKET_DISCONNECT;
}
/* sftp_packet_requirev
* Require one of N possible responses
*/
static int
sftp_packet_requirev(LIBSSH2_SFTP *sftp, int num_valid_responses,
const unsigned char *valid_responses,
uint32_t request_id, unsigned char **data,
size_t *data_len, size_t required_size)
{
int i;
int rc;
if(!data || !data_len || required_size == 0) {
return LIBSSH2_ERROR_BAD_USE;
}
/* If no timeout is active, start a new one */
if(sftp->requirev_start == 0)
sftp->requirev_start = time(NULL);
while(sftp->channel->session->socket_state == LIBSSH2_SOCKET_CONNECTED) {
for(i = 0; i < num_valid_responses; i++) {
if(sftp_packet_ask(sftp, valid_responses[i], request_id,
data, data_len) == 0) {
/*
* Set to zero before all returns to say
* the timeout is not active
*/
sftp->requirev_start = 0;
if(*data_len < required_size) {
return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
}
return LIBSSH2_ERROR_NONE;
}
}
rc = sftp_packet_read(sftp);
if((rc < 0) && (rc != LIBSSH2_ERROR_EAGAIN)) {
sftp->requirev_start = 0;
return rc;
}
else if(rc <= 0) {
/* prevent busy-looping */
long left =
sftp->channel->session->packet_read_timeout -
(long)(time(NULL) - sftp->requirev_start);
if(left <= 0) {
sftp->requirev_start = 0;
return LIBSSH2_ERROR_TIMEOUT;
}
else if(rc == LIBSSH2_ERROR_EAGAIN) {
return rc;
}
}
}
sftp->requirev_start = 0;
/* Only reached if the socket died */
return LIBSSH2_ERROR_SOCKET_DISCONNECT;
}
/* sftp_attrsize
* Size that attr with this flagset will occupy when turned into a bin struct
*/
static int sftp_attrsize(unsigned long flags)
{
return 4 + /* flags(4) */
((flags & LIBSSH2_SFTP_ATTR_SIZE) ? 8 : 0) +
((flags & LIBSSH2_SFTP_ATTR_UIDGID) ? 8 : 0) +
((flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) ? 4 : 0) +
((flags & LIBSSH2_SFTP_ATTR_ACMODTIME) ? 8 : 0);
/* atime + mtime as u32 */
}
/* sftp_attr2bin
* Populate attributes into an SFTP block
*/
static ssize_t
sftp_attr2bin(unsigned char *p, const LIBSSH2_SFTP_ATTRIBUTES * attrs)
{
unsigned char *s = p;
uint32_t flag_mask =
LIBSSH2_SFTP_ATTR_SIZE |
LIBSSH2_SFTP_ATTR_UIDGID |
LIBSSH2_SFTP_ATTR_PERMISSIONS |
LIBSSH2_SFTP_ATTR_ACMODTIME;
/* TODO: When we add SFTP4+ functionality flag_mask can get additional
bits */
if(!attrs) {
_libssh2_htonu32(s, 0);
return 4;
}
_libssh2_store_u32(&s, (uint32_t)(attrs->flags & flag_mask));
if(attrs->flags & LIBSSH2_SFTP_ATTR_SIZE) {
_libssh2_store_u64(&s, attrs->filesize);
}
if(attrs->flags & LIBSSH2_SFTP_ATTR_UIDGID) {
_libssh2_store_u32(&s, (uint32_t)attrs->uid);
_libssh2_store_u32(&s, (uint32_t)attrs->gid);
}
if(attrs->flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
_libssh2_store_u32(&s, (uint32_t)attrs->permissions);
}
if(attrs->flags & LIBSSH2_SFTP_ATTR_ACMODTIME) {
_libssh2_store_u32(&s, (uint32_t)attrs->atime);
_libssh2_store_u32(&s, (uint32_t)attrs->mtime);
}
return s - p;
}
/* sftp_bin2attr
*/
static ssize_t
sftp_bin2attr(LIBSSH2_SFTP_ATTRIBUTES *attrs, const unsigned char *p,
size_t data_len)
{
struct string_buf buf;
uint32_t flags = 0;
buf.data = (unsigned char *)p;
buf.dataptr = buf.data;
buf.len = data_len;
if(_libssh2_get_u32(&buf, &flags)) {
return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
}
attrs->flags = flags;
if(attrs->flags & LIBSSH2_SFTP_ATTR_SIZE) {
if(_libssh2_get_u64(&buf, &(attrs->filesize))) {
return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
}
}
if(attrs->flags & LIBSSH2_SFTP_ATTR_UIDGID) {
uint32_t uid = 0;
uint32_t gid = 0;
if(_libssh2_get_u32(&buf, &uid) ||
_libssh2_get_u32(&buf, &gid)) {
return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
}
attrs->uid = uid;
attrs->gid = gid;
}
if(attrs->flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
uint32_t permissions;
if(_libssh2_get_u32(&buf, &permissions)) {
return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
}
attrs->permissions = permissions;
}
if(attrs->flags & LIBSSH2_SFTP_ATTR_ACMODTIME) {
uint32_t atime;
uint32_t mtime;
if(_libssh2_get_u32(&buf, &atime) ||
_libssh2_get_u32(&buf, &mtime)) {
return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
}
attrs->atime = atime;
attrs->mtime = mtime;
}
/* Parse extended data, if present, to avoid stream parsing errors */
if(attrs->flags & LIBSSH2_SFTP_ATTR_EXTENDED) {
uint32_t extended_count;
uint32_t i;
size_t etype_len;
unsigned char *etype;
size_t edata_len;
unsigned char *edata;
if(_libssh2_get_u32(&buf, &extended_count)) {
return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
}
for(i = 0; i < extended_count; ++i) {
if(_libssh2_get_string(&buf, &etype, &etype_len) ||
_libssh2_get_string(&buf, &edata, &edata_len)) {
return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
}
}
}
return buf.dataptr - buf.data;
}
/* ************
* SFTP API *
************ */
LIBSSH2_CHANNEL_CLOSE_FUNC(libssh2_sftp_dtor);
/* libssh2_sftp_dtor
* Shutdown an SFTP stream when the channel closes
*/
LIBSSH2_CHANNEL_CLOSE_FUNC(libssh2_sftp_dtor)
{
LIBSSH2_SFTP *sftp = (LIBSSH2_SFTP *) (*channel_abstract);
(void)session_abstract;
(void)channel;
/* Free the partial packet storage for sftp_packet_read */
if(sftp->partial_packet) {
LIBSSH2_FREE(session, sftp->partial_packet);
}
/* Free the packet storage for _libssh2_sftp_packet_readdir */
if(sftp->readdir_packet) {
LIBSSH2_FREE(session, sftp->readdir_packet);
}
LIBSSH2_FREE(session, sftp);
}
/* sftp_init
* Startup an SFTP session
*/
static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
{
unsigned char *data;
size_t data_len = 0;
ssize_t rc;
LIBSSH2_SFTP *sftp_handle;
struct string_buf buf;
unsigned char *endp;
if(session->sftpInit_state == libssh2_NB_state_idle) {
_libssh2_debug((session, LIBSSH2_TRACE_SFTP,
"Initializing SFTP subsystem"));
/*
* The 'sftpInit_sftp' and 'sftpInit_channel' struct fields within the
* session struct are only to be used during the setup phase. As soon
* as the SFTP session is created they are cleared and can thus be
* re-used again to allow any amount of SFTP handles per sessions.
*
* Note that you MUST NOT try to call libssh2_sftp_init() again to get
* another handle until the previous call has finished and either
* successfully made a handle or failed and returned error (not
* including *EAGAIN).
*/
assert(!session->sftpInit_sftp);
session->sftpInit_sftp = NULL;
session->sftpInit_state = libssh2_NB_state_created;
}
sftp_handle = session->sftpInit_sftp;
if(session->sftpInit_state == libssh2_NB_state_created) {
session->sftpInit_channel =
_libssh2_channel_open(session, "session", sizeof("session") - 1,
LIBSSH2_CHANNEL_WINDOW_DEFAULT,
LIBSSH2_CHANNEL_PACKET_DEFAULT, NULL, 0);
if(!session->sftpInit_channel) {
if(libssh2_session_last_errno(session) == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block starting up channel");
}
else {
_libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE,
"Unable to startup channel");
session->sftpInit_state = libssh2_NB_state_idle;
}
return NULL;
}
session->sftpInit_state = libssh2_NB_state_sent;
}
if(session->sftpInit_state == libssh2_NB_state_sent) {
int ret = _libssh2_channel_process_startup(session->sftpInit_channel,
"subsystem",
sizeof("subsystem") - 1,
"sftp",
strlen("sftp"));
if(ret == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block to request SFTP subsystem");
return NULL;
}
else if(ret) {
_libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE,
"Unable to request SFTP subsystem");
goto sftp_init_error;
}
session->sftpInit_state = libssh2_NB_state_sent1;
}
if(session->sftpInit_state == libssh2_NB_state_sent1) {
rc = _libssh2_channel_extended_data(session->sftpInit_channel,
LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE);
if(rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block requesting handle extended data");
return NULL;
}
sftp_handle =
session->sftpInit_sftp =
LIBSSH2_CALLOC(session, sizeof(LIBSSH2_SFTP));
if(!sftp_handle) {
_libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate a new SFTP structure");
goto sftp_init_error;
}
sftp_handle->channel = session->sftpInit_channel;
sftp_handle->request_id = 0;
_libssh2_htonu32(session->sftpInit_buffer, 5);
session->sftpInit_buffer[4] = SSH_FXP_INIT;
_libssh2_htonu32(session->sftpInit_buffer + 5, LIBSSH2_SFTP_VERSION);
session->sftpInit_sent = 0; /* nothing's sent yet */
_libssh2_debug((session, LIBSSH2_TRACE_SFTP,
"Sending FXP_INIT packet advertising "
"version %d support",
(int) LIBSSH2_SFTP_VERSION));
session->sftpInit_state = libssh2_NB_state_sent2;
}
if(session->sftpInit_state == libssh2_NB_state_sent2) {
/* sent off what's left of the init buffer to send */
rc = _libssh2_channel_write(session->sftpInit_channel, 0,
session->sftpInit_buffer +
session->sftpInit_sent,
9 - session->sftpInit_sent);
if(rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending SSH_FXP_INIT");
return NULL;
}
else if(rc < 0) {
_libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send SSH_FXP_INIT");
goto sftp_init_error;
}
else {
/* add up the number of bytes sent */
session->sftpInit_sent += rc;
if(session->sftpInit_sent == 9)
/* move on */
session->sftpInit_state = libssh2_NB_state_sent3;
/* if less than 9, we remain in this state to send more later on */
}
}
if(session->sftpInit_state == libssh2_NB_state_error_closing) {
rc = _libssh2_channel_free(session->sftpInit_channel);
if(rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block closing channel");
return NULL;
}
session->sftpInit_channel = NULL;
if(session->sftpInit_sftp) {
LIBSSH2_FREE(session, session->sftpInit_sftp);
session->sftpInit_sftp = NULL;
}
session->sftpInit_state = libssh2_NB_state_idle;
return NULL;
}
rc = sftp_packet_require(sftp_handle, SSH_FXP_VERSION,
0, &data, &data_len, 5);
if(rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block receiving SSH_FXP_VERSION");
return NULL;
}
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
if(data_len > 0) {
LIBSSH2_FREE(session, data);
}
_libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Invalid SSH_FXP_VERSION response");
goto sftp_init_error;
}
else if(rc) {
_libssh2_error(session, (int)rc,
"Timeout waiting for response from SFTP subsystem");
goto sftp_init_error;
}
buf.data = data;
buf.dataptr = buf.data + 1;
buf.len = data_len;
endp = &buf.data[data_len];
if(_libssh2_get_u32(&buf, &(sftp_handle->version))) {
LIBSSH2_FREE(session, data);
rc = LIBSSH2_ERROR_BUFFER_TOO_SMALL;
goto sftp_init_error;
}
if(sftp_handle->version > LIBSSH2_SFTP_VERSION) {
_libssh2_debug((session, LIBSSH2_TRACE_SFTP,
"Truncating remote SFTP version from %lu",
sftp_handle->version));
sftp_handle->version = LIBSSH2_SFTP_VERSION;
}
_libssh2_debug((session, LIBSSH2_TRACE_SFTP,
"Enabling SFTP version %lu compatibility",
sftp_handle->version));
while(buf.dataptr < endp) {
unsigned char *extname, *extdata;
if(_libssh2_get_string(&buf, &extname, NULL)) {
LIBSSH2_FREE(session, data);
_libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
"Data too short when extracting extname");
goto sftp_init_error;
}
if(_libssh2_get_string(&buf, &extdata, NULL)) {
LIBSSH2_FREE(session, data);
_libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
"Data too short when extracting extdata");
goto sftp_init_error;
}
}
LIBSSH2_FREE(session, data);
/* Make sure that when the channel gets closed, the SFTP service is shut
down too */
sftp_handle->channel->abstract = sftp_handle;
sftp_handle->channel->close_cb = libssh2_sftp_dtor;
session->sftpInit_state = libssh2_NB_state_idle;
/* clear the sftp and channel pointers in this session struct now */
session->sftpInit_sftp = NULL;
session->sftpInit_channel = NULL;
_libssh2_list_init(&sftp_handle->sftp_handles);
return sftp_handle;
sftp_init_error:
session->sftpInit_state = libssh2_NB_state_error_closing;
return NULL;
}
/* libssh2_sftp_init
* Startup an SFTP session
*/
LIBSSH2_API LIBSSH2_SFTP *libssh2_sftp_init(LIBSSH2_SESSION *session)
{
LIBSSH2_SFTP *ptr;
if(!session)
return NULL;
if(!(session->state & LIBSSH2_STATE_AUTHENTICATED)) {
_libssh2_error(session, LIBSSH2_ERROR_INVAL,
"session not authenticated yet");
return NULL;
}
BLOCK_ADJUST_ERRNO(ptr, session, sftp_init(session));
return ptr;
}
/* sftp_shutdown
* Shuts down the SFTP subsystem
*/
static int
sftp_shutdown(LIBSSH2_SFTP *sftp)
{
int rc;
LIBSSH2_SESSION *session = sftp->channel->session;
/*
* Make sure all memory used in the state variables are free
*/
if(sftp->partial_packet) {
LIBSSH2_FREE(session, sftp->partial_packet);
sftp->partial_packet = NULL;
}
if(sftp->open_packet) {
LIBSSH2_FREE(session, sftp->open_packet);
sftp->open_packet = NULL;
}
if(sftp->readdir_packet) {
LIBSSH2_FREE(session, sftp->readdir_packet);
sftp->readdir_packet = NULL;
}
if(sftp->fstat_packet) {
LIBSSH2_FREE(session, sftp->fstat_packet);
sftp->fstat_packet = NULL;
}
if(sftp->unlink_packet) {
LIBSSH2_FREE(session, sftp->unlink_packet);
sftp->unlink_packet = NULL;
}
if(sftp->rename_packet) {
LIBSSH2_FREE(session, sftp->rename_packet);
sftp->rename_packet = NULL;
}
if(sftp->fstatvfs_packet) {
LIBSSH2_FREE(session, sftp->fstatvfs_packet);
sftp->fstatvfs_packet = NULL;
}
if(sftp->statvfs_packet) {
LIBSSH2_FREE(session, sftp->statvfs_packet);
sftp->statvfs_packet = NULL;
}
if(sftp->mkdir_packet) {
LIBSSH2_FREE(session, sftp->mkdir_packet);
sftp->mkdir_packet = NULL;
}
if(sftp->rmdir_packet) {
LIBSSH2_FREE(session, sftp->rmdir_packet);
sftp->rmdir_packet = NULL;
}
if(sftp->stat_packet) {
LIBSSH2_FREE(session, sftp->stat_packet);
sftp->stat_packet = NULL;
}
if(sftp->symlink_packet) {
LIBSSH2_FREE(session, sftp->symlink_packet);
sftp->symlink_packet = NULL;
}
if(sftp->fsync_packet) {
LIBSSH2_FREE(session, sftp->fsync_packet);
sftp->fsync_packet = NULL;
}
sftp_packet_flush(sftp);
/* TODO: We should consider walking over the sftp_handles list and kill
* any remaining sftp handles ... */
rc = _libssh2_channel_free(sftp->channel);
return rc;
}
/* libssh2_sftp_shutdown
* Shutsdown the SFTP subsystem
*/
LIBSSH2_API int
libssh2_sftp_shutdown(LIBSSH2_SFTP *sftp)
{
int rc;
if(!sftp)
return LIBSSH2_ERROR_BAD_USE;
BLOCK_ADJUST(rc, sftp->channel->session, sftp_shutdown(sftp));
return rc;
}
/* *******************************
* SFTP File and Directory Ops *
******************************* */
/* sftp_open
*/
static LIBSSH2_SFTP_HANDLE *
sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
size_t filename_len, uint32_t flags, long mode,
int open_type, LIBSSH2_SFTP_ATTRIBUTES *attrs_in)
{
LIBSSH2_CHANNEL *channel = sftp->channel;
LIBSSH2_SESSION *session = channel->session;
LIBSSH2_SFTP_HANDLE *fp;
LIBSSH2_SFTP_ATTRIBUTES attrs = {
LIBSSH2_SFTP_ATTR_PERMISSIONS, 0, 0, 0, 0, 0, 0
};
unsigned char *s;
ssize_t rc;
int open_file = (open_type == LIBSSH2_SFTP_OPENFILE) ? 1 : 0;
if(sftp->open_state == libssh2_NB_state_idle) {
sftp->last_errno = LIBSSH2_FX_OK;
if(attrs_in) {
memcpy(&attrs, attrs_in, sizeof(LIBSSH2_SFTP_ATTRIBUTES));
}
/* packet_len(4) + packet_type(1) + request_id(4) + filename_len(4) +
flags(4) */
sftp->open_packet_len = (uint32_t)(filename_len + 13 +
(open_file? (4 + sftp_attrsize(attrs.flags)) : 0));
/* surprise! this starts out with nothing sent */
sftp->open_packet_sent = 0;
s = sftp->open_packet = LIBSSH2_ALLOC(session, sftp->open_packet_len);
if(!sftp->open_packet) {
_libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FXP_OPEN or "
"FXP_OPENDIR packet");
return NULL;
}
/* Filetype in SFTP 3 and earlier */
attrs.permissions = mode |
(open_file ? LIBSSH2_SFTP_ATTR_PFILETYPE_FILE :
LIBSSH2_SFTP_ATTR_PFILETYPE_DIR);
_libssh2_store_u32(&s, sftp->open_packet_len - 4);
*(s++) = open_file? SSH_FXP_OPEN : SSH_FXP_OPENDIR;
sftp->open_request_id = sftp->request_id++;
_libssh2_store_u32(&s, sftp->open_request_id);
_libssh2_store_str(&s, filename, filename_len);
if(open_file) {
_libssh2_store_u32(&s, flags);
s += sftp_attr2bin(s, &attrs);
}
_libssh2_debug((session, LIBSSH2_TRACE_SFTP, "Sending %s open request",
open_file? "file" : "directory"));
sftp->open_state = libssh2_NB_state_created;
}
if(sftp->open_state == libssh2_NB_state_created) {
rc = _libssh2_channel_write(channel, 0, sftp->open_packet+
sftp->open_packet_sent,
sftp->open_packet_len -
sftp->open_packet_sent);
if(rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending FXP_OPEN or "
"FXP_OPENDIR command");
return NULL;
}
else if(rc < 0) {
_libssh2_error(session, (int)rc, "Unable to send FXP_OPEN*");
LIBSSH2_FREE(session, sftp->open_packet);
sftp->open_packet = NULL;
sftp->open_state = libssh2_NB_state_idle;
return NULL;
}
/* bump the sent counter and remain in this state until the whole
data is off */
sftp->open_packet_sent += rc;
if(sftp->open_packet_len == sftp->open_packet_sent) {
LIBSSH2_FREE(session, sftp->open_packet);
sftp->open_packet = NULL;
sftp->open_state = libssh2_NB_state_sent;
}
}
if(sftp->open_state == libssh2_NB_state_sent) {
size_t data_len = 0;
unsigned char *data;
static const unsigned char fopen_responses[2] =
{ SSH_FXP_HANDLE, SSH_FXP_STATUS };
rc = sftp_packet_requirev(sftp, 2, fopen_responses,
sftp->open_request_id, &data,
&data_len, 1);
if(rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for status message");
return NULL;
}
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
if(data_len > 0) {
LIBSSH2_FREE(session, data);
}
_libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Response too small");
return NULL;
}
sftp->open_state = libssh2_NB_state_idle;
if(rc) {
_libssh2_error(session, (int)rc,
"Timeout waiting for status message");
return NULL;
}
/* OPEN can basically get STATUS or HANDLE back, where HANDLE implies
a fine response while STATUS means error. It seems though that at
times we get an SSH_FX_OK back in a STATUS, followed the "real"
HANDLE so we need to properly deal with that. */
if(data[0] == SSH_FXP_STATUS) {
int badness = 1;
if(data_len < 9) {
_libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Too small FXP_STATUS");
LIBSSH2_FREE(session, data);
return NULL;
}
sftp->last_errno = _libssh2_ntohu32(data + 5);
if(LIBSSH2_FX_OK == sftp->last_errno) {
_libssh2_debug((session, LIBSSH2_TRACE_SFTP,
"got HANDLE FXOK"));
LIBSSH2_FREE(session, data);
/* silly situation, but check for a HANDLE */
rc = sftp_packet_require(sftp, SSH_FXP_HANDLE,
sftp->open_request_id, &data,
&data_len, 10);
if(rc == LIBSSH2_ERROR_EAGAIN) {
/* go back to sent state and wait for something else */
sftp->open_state = libssh2_NB_state_sent;
return NULL;
}
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
if(data_len > 0) {
LIBSSH2_FREE(session, data);
}
_libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Too small FXP_HANDLE");
return NULL;
}
else if(!rc)
/* we got the handle so this is not a bad situation */
badness = 0;
}
if(badness) {
_libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Failed opening remote file");
_libssh2_debug((session, LIBSSH2_TRACE_SFTP,
"got FXP_STATUS %d",
sftp->last_errno));
LIBSSH2_FREE(session, data);
return NULL;
}
}
if(data_len < 10) {
_libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Too small FXP_HANDLE");
LIBSSH2_FREE(session, data);
return NULL;
}
fp = LIBSSH2_CALLOC(session, sizeof(LIBSSH2_SFTP_HANDLE));
if(!fp) {
_libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate new SFTP handle structure");
LIBSSH2_FREE(session, data);
return NULL;
}
fp->handle_type = open_file ? LIBSSH2_SFTP_HANDLE_FILE :
LIBSSH2_SFTP_HANDLE_DIR;
fp->handle_len = _libssh2_ntohu32(data + 5);
if(fp->handle_len > SFTP_HANDLE_MAXLEN)
/* SFTP doesn't allow handles longer than 256 characters */
fp->handle_len = SFTP_HANDLE_MAXLEN;
if(fp->handle_len > (data_len - 9))
/* do not reach beyond the end of the data we got */
fp->handle_len = data_len - 9;
memcpy(fp->handle, data + 9, fp->handle_len);
LIBSSH2_FREE(session, data);
/* add this file handle to the list kept in the sftp session */
_libssh2_list_add(&sftp->sftp_handles, &fp->node);
fp->sftp = sftp; /* point to the parent struct */
fp->u.file.offset = 0;
fp->u.file.offset_sent = 0;
_libssh2_debug((session, LIBSSH2_TRACE_SFTP,
"Open command successful"));
return fp;
}
return NULL;
}
/* libssh2_sftp_open_ex
*/
LIBSSH2_API LIBSSH2_SFTP_HANDLE *
libssh2_sftp_open_ex(LIBSSH2_SFTP *sftp, const char *filename,
unsigned int filename_len, unsigned long flags, long mode,
int open_type)
{
LIBSSH2_SFTP_HANDLE *hnd;
if(!sftp)
return NULL;
BLOCK_ADJUST_ERRNO(hnd, sftp->channel->session,
sftp_open(sftp, filename, filename_len, (uint32_t)flags,
mode, open_type, NULL));
return hnd;
}
/* libssh2_sftp_open_ex_r
*/
LIBSSH2_API LIBSSH2_SFTP_HANDLE *
libssh2_sftp_open_ex_r(LIBSSH2_SFTP *sftp, const char *filename,
size_t filename_len, unsigned long flags, long mode,
int open_type, LIBSSH2_SFTP_ATTRIBUTES *attrs)
{
LIBSSH2_SFTP_HANDLE *hnd;
if(!sftp)
return NULL;
BLOCK_ADJUST_ERRNO(hnd, sftp->channel->session,
sftp_open(sftp, filename, filename_len, (uint32_t)flags,
mode, open_type, attrs));
return hnd;
}
/* sftp_read
* Read from an SFTP file handle
*/
static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE * handle, char *buffer,
size_t buffer_size)
{
LIBSSH2_SFTP *sftp = handle->sftp;
LIBSSH2_CHANNEL *channel = sftp->channel;
LIBSSH2_SESSION *session = channel->session;
size_t count = 0;
struct sftp_pipeline_chunk *chunk;
struct sftp_pipeline_chunk *next;
ssize_t rc;
struct _libssh2_sftp_handle_file_data *filep =
&handle->u.file;
size_t bytes_in_buffer = 0;
char *sliding_bufferp = buffer;
/* This function can be interrupted in three different places where it
might need to wait for data from the network. It returns EAGAIN to
allow non-blocking clients to do other work but these client are
expected to call this function again (possibly many times) to finish
the operation.
The tricky part is that if we previously aborted a sftp_read due to
EAGAIN, we must continue at the same spot to continue the previously
interrupted operation. This is done using a state machine to record
what phase of execution we were at. The state is stored in
sftp->read_state.
libssh2_NB_state_idle: The first phase is where we prepare multiple
FXP_READ packets to do optimistic read-ahead. We send off as many as
possible in the second phase without waiting for a response to each
one; this is the key to fast reads. But we may have to adjust the
channel window size to do this which may interrupt this function while
waiting. The state machine saves the phase as libssh2_NB_state_idle so
it returns here on the next call.
libssh2_NB_state_sent: The second phase is where we send the FXP_READ
packets. Writing them to the channel can be interrupted with EAGAIN
but the state machine ensures we skip the first phase on the next call
and resume sending.
libssh2_NB_state_sent2: In the third phase (indicated by ) we read the
data from the responses that have arrived so far. Reading can be
interrupted with EAGAIN but the state machine ensures we skip the first
and second phases on the next call and resume sending.
*/
switch(sftp->read_state) {
case libssh2_NB_state_idle:
sftp->last_errno = LIBSSH2_FX_OK;
/* Some data may already have been read from the server in the
previous call but didn't fit in the buffer at the time. If so, we
return that now as we can't risk being interrupted later with data
partially written to the buffer. */
if(filep->data_left) {
size_t copy = LIBSSH2_MIN(buffer_size, filep->data_left);
memcpy(buffer, &filep->data[ filep->data_len - filep->data_left],
copy);
filep->data_left -= copy;
filep->offset += copy;
if(!filep->data_left) {
LIBSSH2_FREE(session, filep->data);
filep->data = NULL;
}
return copy;
}
if(filep->eof) {
return 0;
}
else {
/* We allow a number of bytes being requested at any given time
without having been acked - until we reach EOF. */
/* Number of bytes asked for that haven't been acked yet */
size_t already = (size_t)(filep->offset_sent - filep->offset);
size_t max_read_ahead = buffer_size*4;
unsigned long recv_window;
if(max_read_ahead > LIBSSH2_CHANNEL_WINDOW_DEFAULT*4)
max_read_ahead = LIBSSH2_CHANNEL_WINDOW_DEFAULT*4;
/* if the buffer_size passed in now is smaller than what has
already been sent, we risk getting count become a very large
number */
if(max_read_ahead > already)
count = max_read_ahead - already;
/* 'count' is how much more data to ask for, and 'already' is how
much data that already has been asked for but not yet returned.
Specifically, 'count' means how much data that have or will be
asked for by the nodes that are already added to the linked
list. Some of those read requests may not actually have been
sent off successfully yet.
If 'already' is very large it should be perfectly fine to have
count set to 0 as then we don't have to ask for more data
(right now).
buffer_size*4 is just picked more or less out of the air. The
idea is that when reading SFTP from a remote server, we send
away multiple read requests guessing that the client will read
more than only this 'buffer_size' amount of memory. So we ask
for maximum buffer_size*4 amount of data so that we can return
them very fast in subsequent calls.
*/
recv_window = libssh2_channel_window_read_ex(sftp->channel,
NULL, NULL);
if(max_read_ahead > recv_window) {
/* more data will be asked for than what the window currently
allows, expand it! */
rc = _libssh2_channel_receive_window_adjust(sftp->channel,
(uint32_t)(max_read_ahead * 8),
1, NULL);
/* if this returns EAGAIN, we will get back to this function
at next call */
assert(rc != LIBSSH2_ERROR_EAGAIN || !filep->data_left);
assert(rc != LIBSSH2_ERROR_EAGAIN || !filep->eof);
if(rc)
return rc;
}
}
while(count > 0) {
unsigned char *s;
/* 25 = packet_len(4) + packet_type(1) + request_id(4) +
handle_len(4) + offset(8) + count(4) */
uint32_t packet_len = (uint32_t)(handle->handle_len + 25);
uint32_t request_id;
uint32_t size = (uint32_t)count;
if(size < buffer_size)
size = (uint32_t)buffer_size;
if(size > MAX_SFTP_READ_SIZE)
size = MAX_SFTP_READ_SIZE;
chunk = LIBSSH2_ALLOC(session, packet_len +
sizeof(struct sftp_pipeline_chunk));
if(!chunk)
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"malloc fail for FXP_WRITE");
chunk->offset = filep->offset_sent;
chunk->len = size;
chunk->lefttosend = packet_len;
chunk->sent = 0;
s = chunk->packet;
_libssh2_store_u32(&s, packet_len - 4);
*s++ = SSH_FXP_READ;
request_id = sftp->request_id++;
chunk->request_id = request_id;
_libssh2_store_u32(&s, request_id);
_libssh2_store_str(&s, handle->handle, handle->handle_len);
_libssh2_store_u64(&s, filep->offset_sent);
filep->offset_sent += size; /* advance offset at once */
_libssh2_store_u32(&s, size);
/* add this new entry LAST in the list */
_libssh2_list_add(&handle->packet_list, &chunk->node);
/* deduct the size we used, as we might have to create
more packets */
count -= LIBSSH2_MIN(size, count);
_libssh2_debug((session, LIBSSH2_TRACE_SFTP,
"read request id %d sent (offset: %d, size: %d)",
request_id, (int)chunk->offset, (int)chunk->len));
}
/* FALL-THROUGH */
case libssh2_NB_state_sent:
sftp->read_state = libssh2_NB_state_idle;
/* move through the READ packets that haven't been sent and send as
many as possible - remember that we don't block */
chunk = _libssh2_list_first(&handle->packet_list);
while(chunk) {
if(chunk->lefttosend) {
rc = _libssh2_channel_write(channel, 0,
&chunk->packet[chunk->sent],
chunk->lefttosend);
if(rc < 0) {
sftp->read_state = libssh2_NB_state_sent;
return rc;
}
/* remember where to continue sending the next time */
chunk->lefttosend -= rc;
chunk->sent += rc;
if(chunk->lefttosend) {
/* We still have data left to send for this chunk.
* If there is at least one completely sent chunk,
* we can get out of this loop and start reading. */
if(chunk != _libssh2_list_first(&handle->packet_list)) {
break;
}
else {
continue;
}
}
}
/* move on to the next chunk with data to send */
chunk = _libssh2_list_next(&chunk->node);
}
/* FALL-THROUGH */
case libssh2_NB_state_sent2:
sftp->read_state = libssh2_NB_state_idle;
/*
* Count all ACKed packets and act on the contents of them.
*/
chunk = _libssh2_list_first(&handle->packet_list);
while(chunk) {
unsigned char *data;
size_t data_len = 0;
uint32_t rc32;
static const unsigned char read_responses[2] = {
SSH_FXP_DATA, SSH_FXP_STATUS
};
if(chunk->lefttosend) {
/* if the chunk still has data left to send, we shouldn't wait
for an ACK for it just yet */
if(bytes_in_buffer > 0) {
return bytes_in_buffer;
}
else {
/* we should never reach this point */
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"sftp_read() internal error");
}
}
rc = sftp_packet_requirev(sftp, 2, read_responses,
chunk->request_id, &data, &data_len, 9);
if(rc == LIBSSH2_ERROR_EAGAIN && bytes_in_buffer) {
/* do not return EAGAIN if we have already
* written data into the buffer */
return bytes_in_buffer;
}
if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
if(data_len > 0) {
LIBSSH2_FREE(session, data);
}
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Response too small");
}
else if(rc < 0) {
sftp->read_state = libssh2_NB_state_sent2;
return rc;
}
/*
* We get DATA or STATUS back. STATUS can be error, or it is
* FX_EOF when we reach the end of the file.
*/
switch(data[0]) {
case SSH_FXP_STATUS:
/* remove the chunk we just processed */
_libssh2_list_remove(&chunk->node);
LIBSSH2_FREE(session, chunk);
/* we must remove all outstanding READ requests, as either we
got an error or we're at end of file */
sftp_packetlist_flush(handle);
rc32 = _libssh2_ntohu32(data + 5);
LIBSSH2_FREE(session, data);
if(rc32 == LIBSSH2_FX_EOF) {
filep->eof = TRUE;
return bytes_in_buffer;
}
else {
sftp->last_errno = rc32;
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP READ error");
}
break;
case SSH_FXP_DATA:
if(chunk->offset != filep->offset) {
/* This could happen if the server returns less bytes than
requested, which shouldn't happen for normal files. See:
https://tools.ietf.org/html/draft-ietf-secsh-filexfer-02
#section-6.4
*/
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Read Packet At Unexpected Offset");
}
rc32 = _libssh2_ntohu32(data + 5);
if(rc32 > (data_len - 9))
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol badness");
if(rc32 > chunk->len) {
/* A chunk larger than we requested was returned to us.
This is a protocol violation and we don't know how to
deal with it. Bail out! */
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"FXP_READ response too big");
}
if(rc32 != chunk->len) {
/* a short read does not imply end of file, but we must
adjust the offset_sent since it was advanced with a
full chunk->len before */
filep->offset_sent -= (chunk->len - rc32);
}
if((bytes_in_buffer + rc32) > buffer_size) {
/* figure out the overlap amount */
filep->data_left = (bytes_in_buffer + rc32) - buffer_size;
/* getting the full packet would overflow the buffer, so
only get the correct amount and keep the remainder */
rc32 = (uint32_t)(buffer_size - bytes_in_buffer);
/* store data to keep for next call */
filep->data = data;
filep->data_len = data_len;
}
else
filep->data_len = 0;
/* copy the received data from the received FXP_DATA packet to
the buffer at the correct index */
memcpy(sliding_bufferp, data + 9, rc32);
filep->offset += rc32;
bytes_in_buffer += rc32;
sliding_bufferp += rc32;
if(filep->data_len == 0)
/* free the allocated data if not stored to keep */
LIBSSH2_FREE(session, data);
/* remove the chunk we just processed keeping track of the
* next one in case we need it */
next = _libssh2_list_next(&chunk->node);
_libssh2_list_remove(&chunk->node);
LIBSSH2_FREE(session, chunk);
/* check if we have space left in the buffer
* and either continue to the next chunk or stop
*/
if(bytes_in_buffer < buffer_size) {
chunk = next;
}
else {
chunk = NULL;
}
break;
default:
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol badness: unrecognised "
"read request response");
}
}
if(bytes_in_buffer > 0)
return bytes_in_buffer;
break;
default:
assert(0); /* State machine error; unrecognised read state */
}
/* we should never reach this point */
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"sftp_read() internal error");
}
/* libssh2_sftp_read
* Read from an SFTP file handle
*/
LIBSSH2_API ssize_t
libssh2_sftp_read(LIBSSH2_SFTP_HANDLE *hnd, char *buffer,
size_t buffer_maxlen)
{
ssize_t rc;
if(!hnd)
return LIBSSH2_ERROR_BAD_USE;
BLOCK_ADJUST(rc, hnd->sftp->channel->session,
sftp_read(hnd, buffer, buffer_maxlen));
return rc;
}
/* sftp_readdir
* Read from an SFTP directory handle
*/
static ssize_t sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer,
size_t buffer_maxlen, char *longentry,
size_t longentry_maxlen,
LIBSSH2_SFTP_ATTRIBUTES *attrs)
{
LIBSSH2_SFTP *sftp = handle->sftp;
LIBSSH2_CHANNEL *channel = sftp->channel;
LIBSSH2_SESSION *session = channel->session;
size_t data_len = 0;
uint32_t num_names;
/* 13 = packet_len(4) + packet_type(1) + request_id(4) + handle_len(4) */
uint32_t packet_len = (uint32_t)(handle->handle_len + 13);
unsigned char *s, *data;
static const unsigned char read_responses[2] = {
SSH_FXP_NAME, SSH_FXP_STATUS };
ssize_t retcode;
if(sftp->readdir_state == libssh2_NB_state_idle) {
sftp->last_errno = LIBSSH2_FX_OK;
if(handle->u.dir.names_left) {
/*
* A prior request returned more than one directory entry,
* feed it back from the buffer
*/
LIBSSH2_SFTP_ATTRIBUTES attrs_dummy;
size_t real_longentry_len;
size_t real_filename_len;
size_t filename_len;
size_t longentry_len;
size_t names_packet_len = handle->u.dir.names_packet_len;
ssize_t attr_len = 0;
if(names_packet_len >= 4) {
s = (unsigned char *) handle->u.dir.next_name;
real_filename_len = _libssh2_ntohu32(s);
s += 4;
names_packet_len -= 4;
}
else {
filename_len = (size_t)LIBSSH2_ERROR_BUFFER_TOO_SMALL;
goto end;
}
filename_len = real_filename_len;
if(filename_len >= buffer_maxlen) {
filename_len = (size_t)LIBSSH2_ERROR_BUFFER_TOO_SMALL;
goto end;
}
if(buffer_maxlen >= filename_len && names_packet_len >=
filename_len) {
memcpy(buffer, s, filename_len);
buffer[filename_len] = '\0'; /* zero terminate */
s += real_filename_len;
names_packet_len -= real_filename_len;
}
else {
filename_len = (size_t)LIBSSH2_ERROR_BUFFER_TOO_SMALL;
goto end;
}
if(names_packet_len >= 4) {
real_longentry_len = _libssh2_ntohu32(s);
s += 4;
names_packet_len -= 4;
}
else {
filename_len = (size_t)LIBSSH2_ERROR_BUFFER_TOO_SMALL;
goto end;
}
if(longentry && (longentry_maxlen > 1)) {
longentry_len = real_longentry_len;
if(longentry_len >= longentry_maxlen ||
longentry_len > names_packet_len) {
filename_len = (size_t)LIBSSH2_ERROR_BUFFER_TOO_SMALL;
goto end;
}
memcpy(longentry, s, longentry_len);
longentry[longentry_len] = '\0'; /* zero terminate */
}
if(real_longentry_len <= names_packet_len) {
s += real_longentry_len;
names_packet_len -= real_longentry_len;
}
else {
filename_len = (size_t)LIBSSH2_ERROR_BUFFER_TOO_SMALL;
goto end;
}
if(attrs)
memset(attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES));
attr_len = sftp_bin2attr(attrs ? attrs : &attrs_dummy, s,
names_packet_len);
if(attr_len >= 0) {
s += attr_len;
names_packet_len -= attr_len;
}
else {
filename_len = (size_t)LIBSSH2_ERROR_BUFFER_TOO_SMALL;
goto end;
}
handle->u.dir.next_name = (char *) s;
handle->u.dir.names_packet_len = names_packet_len;
if((--handle->u.dir.names_left) == 0)
LIBSSH2_FREE(session, handle->u.dir.names_packet);
end:
_libssh2_debug((session, LIBSSH2_TRACE_SFTP,
"libssh2_sftp_readdir_ex() return %d",
filename_len));
return (ssize_t)filename_len;
}
/* Request another entry(entries?) */
s = sftp->readdir_packet = LIBSSH2_ALLOC(session, packet_len);
if(!sftp->readdir_packet)
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for "
"FXP_READDIR packet");
_libssh2_store_u32(&s, packet_len - 4);
*(s++) = SSH_FXP_READDIR;
sftp->readdir_request_id = sftp->request_id++;
_libssh2_store_u32(&s, sftp->readdir_request_id);
_libssh2_store_str(&s, handle->handle, handle->handle_len);
sftp->readdir_state = libssh2_NB_state_created;
}
if(sftp->readdir_state == libssh2_NB_state_created) {
_libssh2_debug((session, LIBSSH2_TRACE_SFTP,
"Reading entries from directory handle"));
retcode = _libssh2_channel_write(channel, 0, sftp->readdir_packet,
packet_len);
if(retcode == LIBSSH2_ERROR_EAGAIN) {
return retcode;
}
else if((ssize_t)packet_len != retcode) {
LIBSSH2_FREE(session, sftp->readdir_packet);
sftp->readdir_packet = NULL;
sftp->readdir_state = libssh2_NB_state_idle;
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"_libssh2_channel_write() failed");
}
LIBSSH2_FREE(session, sftp->readdir_packet);
sftp->readdir_packet = NULL;
sftp->readdir_state = libssh2_NB_state_sent;
}
retcode = sftp_packet_requirev(sftp, 2, read_responses,
sftp->readdir_request_id, &data,
&data_len, 9);
if(retcode == LIBSSH2_ERROR_EAGAIN)
return retcode;
else if(retcode == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
if(data_len > 0) {
LIBSSH2_FREE(session, data);
}
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Status message too short");
}
else if(retcode) {
sftp->readdir_state = libssh2_NB_state_idle;
return _libssh2_error(session, (int)retcode,
"Timeout waiting for status message");
}
if(data[0] == SSH_FXP_STATUS) {
unsigned int rerrno;
rerrno = _libssh2_ntohu32(data + 5);
LIBSSH2_FREE(session, data);
if(rerrno == LIBSSH2_FX_EOF) {
sftp->readdir_state = libssh2_NB_state_idle;
return 0;
}
else {
sftp->last_errno = rerrno;
sftp->readdir_state = libssh2_NB_state_idle;
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error");
}
}
sftp->readdir_state = libssh2_NB_state_idle;
num_names = _libssh2_ntohu32(data + 5);
_libssh2_debug((session, LIBSSH2_TRACE_SFTP, "%lu entries returned",
num_names));
if(!num_names) {
LIBSSH2_FREE(session, data);
return 0;
}
handle->u.dir.names_left = num_names;
handle->u.dir.names_packet = data;
handle->u.dir.next_name = (char *) data + 9;
handle->u.dir.names_packet_len = data_len - 9;
/* use the name popping mechanism from the start of the function */
return sftp_readdir(handle, buffer, buffer_maxlen, longentry,
longentry_maxlen, attrs);
}
/* libssh2_sftp_readdir_ex
* Read from an SFTP directory handle
*/
LIBSSH2_API int
libssh2_sftp_readdir_ex(LIBSSH2_SFTP_HANDLE *hnd, char *buffer,
size_t buffer_maxlen, char *longentry,
size_t longentry_maxlen,
LIBSSH2_SFTP_ATTRIBUTES *attrs)
{
ssize_t rc;
if(!hnd)
return LIBSSH2_ERROR_BAD_USE;
BLOCK_ADJUST(rc, hnd->sftp->channel->session,
sftp_readdir(hnd, buffer, buffer_maxlen, longentry,
longentry_maxlen, attrs));
return (int)rc; /* FIXME: -> ssize_t */
}
/* sftp_write
*
* Write data to an SFTP handle. Returns the number of bytes written, or
* a negative error code.
*
* We recommend sending very large data buffers to this function!
*
* Concept:
*
* - Detect how much of the given buffer that was already sent in a previous
* call by inspecting the linked list of outgoing chunks. Make sure to skip
* passed the data that has already been taken care of.
*
* - Split all (new) outgoing data in chunks no larger than N.
*
* - Each N bytes chunk gets created as a separate SFTP packet.
*
* - Add all created outgoing packets to the linked list.
*
* - Walk through the list and send the chunks that haven't been sent,
* as many as possible until EAGAIN. Some of the chunks may have been put
* in the list in a previous invoke.
*
* - For all the chunks in the list that have been completely sent off, check
* for ACKs. If a chunk has been ACKed, it is removed from the linked
* list and the "acked" counter gets increased with that data amount.
*
* - Return TOTAL bytes acked so far.
*
* Caveats:
* - be careful: we must not return a higher number than what was given!
*
* TODO:
* Introduce an option that disables this sort of "speculative" ahead writing
* as there's a risk that it will do harm to some app.
*/
static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer,
size_t count)
{
LIBSSH2_SFTP *sftp = handle->sftp;
LIBSSH2_CHANNEL *channel = sftp->channel;
LIBSSH2_SESSION *session = channel->session;
size_t data_len = 0;
uint32_t retcode;
uint32_t packet_len;
unsigned char *s, *data = NULL;
ssize_t rc;
struct sftp_pipeline_chunk *chunk;
struct sftp_pipeline_chunk *next;
size_t acked = 0;
size_t org_count = count;
size_t already;
switch(sftp->write_state) {
default:
case libssh2_NB_state_idle:
sftp->last_errno = LIBSSH2_FX_OK;
/* Number of bytes sent off that haven't been acked and therefore we
will get passed in here again.
Also, add up the number of bytes that actually already have been
acked but we haven't been able to return as such yet, so we will
get that data as well passed in here again.
*/
already = (size_t)(handle->u.file.offset_sent -
handle->u.file.offset)+
handle->u.file.acked;
if(count >= already) {
/* skip the part already made into packets */
buffer += already;
count -= already;
}
else
/* there is more data already fine than what we got in this call */
count = 0;
sftp->write_state = libssh2_NB_state_idle;
while(count) {
/* TODO: Possibly this should have some logic to prevent a very
very small fraction to be left but lets ignore that for now */
uint32_t size =
(uint32_t)(LIBSSH2_MIN(MAX_SFTP_OUTGOING_SIZE, count));
uint32_t request_id;
/* 25 = packet_len(4) + packet_type(1) + request_id(4) +
handle_len(4) + offset(8) + count(4) */
packet_len = (uint32_t)(handle->handle_len + size + 25);
chunk = LIBSSH2_ALLOC(session, packet_len +
sizeof(struct sftp_pipeline_chunk));
if(!chunk)
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"malloc fail for FXP_WRITE");
chunk->len = size;
chunk->sent = 0;
chunk->lefttosend = packet_len;
s = chunk->packet;
_libssh2_store_u32(&s, packet_len - 4);
*(s++) = SSH_FXP_WRITE;
request_id = sftp->request_id++;
chunk->request_id = request_id;
_libssh2_store_u32(&s, request_id);
_libssh2_store_str(&s, handle->handle, handle->handle_len);
_libssh2_store_u64(&s, handle->u.file.offset_sent);
handle->u.file.offset_sent += size; /* advance offset at once */
_libssh2_store_str(&s, buffer, size);
/* add this new entry LAST in the list */
_libssh2_list_add(&handle->packet_list, &chunk->node);
buffer += size;
count -= size; /* deduct the size we used, as we might have
to create more packets */
}
/* move through the WRITE packets that haven't been sent and send as
many as possible - remember that we don't block */
chunk = _libssh2_list_first(&handle->packet_list);
while(chunk) {
if(chunk->lefttosend) {
rc = _libssh2_channel_write(channel, 0,
&chunk->packet[chunk->sent],
chunk->lefttosend);
if(rc < 0)
/* remain in idle state */
return rc;
/* remember where to continue sending the next time */
chunk->lefttosend -= rc;
chunk->sent += rc;
if(chunk->lefttosend)
/* data left to send, get out of loop */
break;
}
/* move on to the next chunk with data to send */
chunk = _libssh2_list_next(&chunk->node);
}
/* fall-through */
case libssh2_NB_state_sent:
sftp->write_state = libssh2_NB_state_idle;
/*
* Count all ACKed packets
*/
chunk = _libssh2_list_first(&handle->packet_list);
while(chunk) {
if(chunk->lefttosend)
/* if the chunk still has data left to send, we shouldn't wait
for an ACK for it just yet */
break;
else if(acked)
/* if we have sent data that is acked, we must return that
info before we call a function that might return EAGAIN */
break;
/* we check the packets in order */
rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
chunk->request_id, &data, &data_len, 9);
if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
if(data_len > 0) {
LIBSSH2_FREE(session, data);
}
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"FXP write packet too short");
}
else if(rc < 0) {
if(rc == LIBSSH2_ERROR_EAGAIN)
sftp->write_state = libssh2_NB_state_sent;
return rc;
}
retcode = _libssh2_ntohu32(data + 5);
LIBSSH2_FREE(session, data);
sftp->last_errno = retcode;
if(retcode == LIBSSH2_FX_OK) {
acked += chunk->len; /* number of payload data that was acked
here */
/* we increase the offset value for all acks */
handle->u.file.offset += chunk->len;
next = _libssh2_list_next(&chunk->node);
_libssh2_list_remove(&chunk->node); /* remove from list */
LIBSSH2_FREE(session, chunk); /* free memory */
chunk = next;
}
else {
/* flush all pending packets from the outgoing list */
sftp_packetlist_flush(handle);
/* since we return error now, the application will not get any
outstanding data acked, so we need to rewind the offset to
where the application knows it has reached with acked
data */
handle->u.file.offset -= handle->u.file.acked;
/* then reset the offset_sent to be the same as the offset */
handle->u.file.offset_sent = handle->u.file.offset;
/* clear the acked counter since we can have no pending data to
ack after an error */
handle->u.file.acked = 0;
/* the server returned an error for that written chunk,
propagate this back to our parent function */
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"FXP write failed");
}
}
break;
}
/* if there were acked data in a previous call that wasn't returned then,
add that up and try to return it all now. This can happen if the app
first sends a huge buffer of data, and then in a second call it sends a
smaller one. */
acked += handle->u.file.acked;
if(acked) {
ssize_t ret = LIBSSH2_MIN(acked, org_count);
/* we got data acked so return that amount, but no more than what
was asked to get sent! */
/* store the remainder. 'ret' is always equal to or less than 'acked'
here */
handle->u.file.acked = acked - ret;
return ret;
}
else
return 0; /* nothing was acked, and no EAGAIN was received! */
}
/* libssh2_sftp_write
* Write data to a file handle
*/
LIBSSH2_API ssize_t
libssh2_sftp_write(LIBSSH2_SFTP_HANDLE *hnd, const char *buffer,
size_t count)
{
ssize_t rc;
if(!hnd)
return LIBSSH2_ERROR_BAD_USE;
BLOCK_ADJUST(rc, hnd->sftp->channel->session,
sftp_write(hnd, buffer, count));
return rc;
}
static int sftp_fsync(LIBSSH2_SFTP_HANDLE *handle)
{
LIBSSH2_SFTP *sftp = handle->sftp;
LIBSSH2_CHANNEL *channel = sftp->channel;
LIBSSH2_SESSION *session = channel->session;
/* 34 = packet_len(4) + packet_type(1) + request_id(4) +
string_len(4) + strlen("fsync@openssh.com")(17) + handle_len(4) */
uint32_t packet_len = (uint32_t)(handle->handle_len + 34);
size_t data_len = 0;
unsigned char *packet, *s, *data = NULL;
ssize_t rc;
uint32_t retcode;
if(sftp->fsync_state == libssh2_NB_state_idle) {
sftp->last_errno = LIBSSH2_FX_OK;
_libssh2_debug((session, LIBSSH2_TRACE_SFTP,
"Issuing fsync command"));
s = packet = LIBSSH2_ALLOC(session, packet_len);
if(!packet) {
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FXP_EXTENDED "
"packet");
}
_libssh2_store_u32(&s, packet_len - 4);
*(s++) = SSH_FXP_EXTENDED;
sftp->fsync_request_id = sftp->request_id++;
_libssh2_store_u32(&s, sftp->fsync_request_id);
_libssh2_store_str(&s, "fsync@openssh.com", 17);
_libssh2_store_str(&s, handle->handle, handle->handle_len);
sftp->fsync_state = libssh2_NB_state_created;
}
else {
packet = sftp->fsync_packet;
}
if(sftp->fsync_state == libssh2_NB_state_created) {
rc = _libssh2_channel_write(channel, 0, packet, packet_len);
if(rc == LIBSSH2_ERROR_EAGAIN ||
(0 <= rc && rc < (ssize_t)packet_len)) {
sftp->fsync_packet = packet;
return LIBSSH2_ERROR_EAGAIN;
}
LIBSSH2_FREE(session, packet);
sftp->fsync_packet = NULL;
if(rc < 0) {
sftp->fsync_state = libssh2_NB_state_idle;
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"_libssh2_channel_write() failed");
}
sftp->fsync_state = libssh2_NB_state_sent;
}
rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
sftp->fsync_request_id, &data, &data_len, 9);
if(rc == LIBSSH2_ERROR_EAGAIN) {
return (int)rc;
}
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
if(data_len > 0) {
LIBSSH2_FREE(session, data);
}
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP fsync packet too short");
}
else if(rc) {
sftp->fsync_state = libssh2_NB_state_idle;
return _libssh2_error(session, (int)rc,
"Error waiting for FXP EXTENDED REPLY");
}
sftp->fsync_state = libssh2_NB_state_idle;
retcode = _libssh2_ntohu32(data + 5);
LIBSSH2_FREE(session, data);
if(retcode != LIBSSH2_FX_OK) {
sftp->last_errno = retcode;
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"fsync failed");
}
return 0;
}
/* libssh2_sftp_fsync
* Commit data on the handle to disk.
*/
LIBSSH2_API int
libssh2_sftp_fsync(LIBSSH2_SFTP_HANDLE *hnd)
{
int rc;
if(!hnd)
return LIBSSH2_ERROR_BAD_USE;
BLOCK_ADJUST(rc, hnd->sftp->channel->session,
sftp_fsync(hnd));
return rc;
}
/* sftp_fstat
* Get or Set stat on a file
*/
static int sftp_fstat(LIBSSH2_SFTP_HANDLE *handle,
LIBSSH2_SFTP_ATTRIBUTES *attrs, int setstat)
{
LIBSSH2_SFTP *sftp = handle->sftp;
LIBSSH2_CHANNEL *channel = sftp->channel;
LIBSSH2_SESSION *session = channel->session;
size_t data_len = 0;
/* 13 = packet_len(4) + packet_type(1) + request_id(4) + handle_len(4) */
uint32_t packet_len = (uint32_t)(handle->handle_len + 13 +
(setstat ? sftp_attrsize(attrs->flags) : 0));
unsigned char *s, *data = NULL;
static const unsigned char fstat_responses[2] =
{ SSH_FXP_ATTRS, SSH_FXP_STATUS };
ssize_t rc;
if(sftp->fstat_state == libssh2_NB_state_idle) {
sftp->last_errno = LIBSSH2_FX_OK;
_libssh2_debug((session, LIBSSH2_TRACE_SFTP, "Issuing %s command",
setstat ? "set-stat" : "stat"));
s = sftp->fstat_packet = LIBSSH2_ALLOC(session, packet_len);
if(!sftp->fstat_packet) {
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for "
"FSTAT/FSETSTAT packet");
}
_libssh2_store_u32(&s, packet_len - 4);
*(s++) = setstat ? SSH_FXP_FSETSTAT : SSH_FXP_FSTAT;
sftp->fstat_request_id = sftp->request_id++;
_libssh2_store_u32(&s, sftp->fstat_request_id);
_libssh2_store_str(&s, handle->handle, handle->handle_len);
if(setstat) {
s += sftp_attr2bin(s, attrs);
}
sftp->fstat_state = libssh2_NB_state_created;
}
if(sftp->fstat_state == libssh2_NB_state_created) {
rc = _libssh2_channel_write(channel, 0, sftp->fstat_packet,
packet_len);
if(rc == LIBSSH2_ERROR_EAGAIN) {
return (int)rc;
}
else if((ssize_t)packet_len != rc) {
LIBSSH2_FREE(session, sftp->fstat_packet);
sftp->fstat_packet = NULL;
sftp->fstat_state = libssh2_NB_state_idle;
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
(setstat ? "Unable to send FXP_FSETSTAT"
: "Unable to send FXP_FSTAT command"));
}
LIBSSH2_FREE(session, sftp->fstat_packet);
sftp->fstat_packet = NULL;
sftp->fstat_state = libssh2_NB_state_sent;
}
rc = sftp_packet_requirev(sftp, 2, fstat_responses,
sftp->fstat_request_id, &data,
&data_len, 9);
if(rc == LIBSSH2_ERROR_EAGAIN)
return (int)rc;
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
if(data_len > 0) {
LIBSSH2_FREE(session, data);
}
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP fstat packet too short");
}
else if(rc) {
sftp->fstat_state = libssh2_NB_state_idle;
return _libssh2_error(session, (int)rc,
"Timeout waiting for status message");
}
sftp->fstat_state = libssh2_NB_state_idle;
if(data[0] == SSH_FXP_STATUS) {
uint32_t retcode;
retcode = _libssh2_ntohu32(data + 5);
LIBSSH2_FREE(session, data);
if(retcode == LIBSSH2_FX_OK) {
return 0;
}
else {
sftp->last_errno = retcode;
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error");
}
}
if(sftp_bin2attr(attrs, data + 5, data_len - 5) < 0) {
LIBSSH2_FREE(session, data);
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Attributes too short in SFTP fstat");
}
LIBSSH2_FREE(session, data);
return 0;
}
/* libssh2_sftp_fstat_ex
* Get or Set stat on a file
*/
LIBSSH2_API int
libssh2_sftp_fstat_ex(LIBSSH2_SFTP_HANDLE *hnd,
LIBSSH2_SFTP_ATTRIBUTES *attrs, int setstat)
{
int rc;
if(!hnd || !attrs)
return LIBSSH2_ERROR_BAD_USE;
BLOCK_ADJUST(rc, hnd->sftp->channel->session,
sftp_fstat(hnd, attrs, setstat));
return rc;
}
/* libssh2_sftp_seek64
* Set the read/write pointer to an arbitrary position within the file
*/
LIBSSH2_API void
libssh2_sftp_seek64(LIBSSH2_SFTP_HANDLE *handle, libssh2_uint64_t offset)
{
if(!handle)
return;
if(handle->u.file.offset == offset && handle->u.file.offset_sent == offset)
return;
handle->u.file.offset = handle->u.file.offset_sent = offset;
/* discard all pending requests and currently read data */
sftp_packetlist_flush(handle);
/* free the left received buffered data */
if(handle->u.file.data_left) {
LIBSSH2_FREE(handle->sftp->channel->session, handle->u.file.data);
handle->u.file.data_left = handle->u.file.data_len = 0;
handle->u.file.data = NULL;
}
/* reset EOF to False */
handle->u.file.eof = FALSE;
}
/* libssh2_sftp_seek
* Set the read/write pointer to an arbitrary position within the file
*/
LIBSSH2_API void
libssh2_sftp_seek(LIBSSH2_SFTP_HANDLE *handle, size_t offset)
{
libssh2_sftp_seek64(handle, (libssh2_uint64_t)offset);
}
/* libssh2_sftp_tell
* Return the current read/write pointer's offset
*/
LIBSSH2_API size_t
libssh2_sftp_tell(LIBSSH2_SFTP_HANDLE *handle)
{
if(!handle)
return 0; /* no handle, no size */
/* NOTE: this may very well truncate the size if it is larger than what
size_t can hold, so libssh2_sftp_tell64() is really the function you
should use */
return (size_t)(handle->u.file.offset);
}
/* libssh2_sftp_tell64
* Return the current read/write pointer's offset
*/
LIBSSH2_API libssh2_uint64_t
libssh2_sftp_tell64(LIBSSH2_SFTP_HANDLE *handle)
{
if(!handle)
return 0; /* no handle, no size */
return handle->u.file.offset;
}
/*
* Flush all remaining incoming SFTP packets and zombies.
*/
static void sftp_packet_flush(LIBSSH2_SFTP *sftp)
{
LIBSSH2_CHANNEL *channel = sftp->channel;
LIBSSH2_SESSION *session = channel->session;
LIBSSH2_SFTP_PACKET *packet = _libssh2_list_first(&sftp->packets);
struct sftp_zombie_requests *zombie =
_libssh2_list_first(&sftp->zombie_requests);
while(packet) {
LIBSSH2_SFTP_PACKET *next;
/* check next struct in the list */
next = _libssh2_list_next(&packet->node);
_libssh2_list_remove(&packet->node);
LIBSSH2_FREE(session, packet->data);
LIBSSH2_FREE(session, packet);
packet = next;
}
while(zombie) {
/* figure out the next node */
struct sftp_zombie_requests *next = _libssh2_list_next(&zombie->node);
/* unlink the current one */
_libssh2_list_remove(&zombie->node);
/* free the memory */
LIBSSH2_FREE(session, zombie);
zombie = next;
}
}
/* sftp_close_handle
*
* Close a file or directory handle.
* Also frees handle resource and unlinks it from the SFTP structure.
* The handle is no longer usable after return of this function, unless
* the return value is LIBSSH2_ERROR_EAGAIN in which case this function
* should be called again.
*/
static int
sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle)
{
LIBSSH2_SFTP *sftp = handle->sftp;
LIBSSH2_CHANNEL *channel = sftp->channel;
LIBSSH2_SESSION *session = channel->session;
size_t data_len = 0;
/* 13 = packet_len(4) + packet_type(1) + request_id(4) + handle_len(4) */
uint32_t packet_len = (uint32_t)(handle->handle_len + 13);
unsigned char *s, *data = NULL;
int rc = 0;
if(handle->close_state == libssh2_NB_state_idle) {
sftp->last_errno = LIBSSH2_FX_OK;
_libssh2_debug((session, LIBSSH2_TRACE_SFTP, "Closing handle"));
s = handle->close_packet = LIBSSH2_ALLOC(session, packet_len);
if(!handle->close_packet) {
handle->close_state = libssh2_NB_state_idle;
rc = _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FXP_CLOSE "
"packet");
}
else {
_libssh2_store_u32(&s, packet_len - 4);
*(s++) = SSH_FXP_CLOSE;
handle->close_request_id = sftp->request_id++;
_libssh2_store_u32(&s, handle->close_request_id);
_libssh2_store_str(&s, handle->handle, handle->handle_len);
handle->close_state = libssh2_NB_state_created;
}
}
if(handle->close_state == libssh2_NB_state_created) {
ssize_t nwritten;
nwritten = _libssh2_channel_write(channel, 0, handle->close_packet,
packet_len);
if(nwritten == LIBSSH2_ERROR_EAGAIN) {
return (int)nwritten;
}
else if((ssize_t)packet_len != nwritten) {
handle->close_state = libssh2_NB_state_idle;
rc = _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send FXP_CLOSE command");
}
else
handle->close_state = libssh2_NB_state_sent;
LIBSSH2_FREE(session, handle->close_packet);
handle->close_packet = NULL;
}
if(handle->close_state == libssh2_NB_state_sent) {
rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
handle->close_request_id, &data,
&data_len, 9);
if(rc == LIBSSH2_ERROR_EAGAIN) {
return rc;
}
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
if(data_len > 0) {
LIBSSH2_FREE(session, data);
}
data = NULL;
_libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Packet too short in FXP_CLOSE command");
}
else if(rc) {
_libssh2_error(session, rc,
"Error waiting for status message");
}
handle->close_state = libssh2_NB_state_sent1;
}
if(!data) {
/* if it reaches this point with data unset, something unwanted
happened for which we should have set an error code */
assert(rc);
}
else {
uint32_t retcode = _libssh2_ntohu32(data + 5);
LIBSSH2_FREE(session, data);
if(retcode != LIBSSH2_FX_OK) {
sftp->last_errno = retcode;
handle->close_state = libssh2_NB_state_idle;
rc = _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error");
}
}
/* remove this handle from the parent's list */
_libssh2_list_remove(&handle->node);
if(handle->handle_type == LIBSSH2_SFTP_HANDLE_DIR) {
if(handle->u.dir.names_left)
LIBSSH2_FREE(session, handle->u.dir.names_packet);
}
else if(handle->handle_type == LIBSSH2_SFTP_HANDLE_FILE) {
if(handle->u.file.data)
LIBSSH2_FREE(session, handle->u.file.data);
}
sftp_packetlist_flush(handle);
sftp->read_state = libssh2_NB_state_idle;
handle->close_state = libssh2_NB_state_idle;
LIBSSH2_FREE(session, handle);
return rc;
}
/* libssh2_sftp_close_handle
*
* Close a file or directory handle
* Also frees handle resource and unlinks it from the SFTP structure
*/
LIBSSH2_API int
libssh2_sftp_close_handle(LIBSSH2_SFTP_HANDLE *hnd)
{
int rc;
if(!hnd)
return LIBSSH2_ERROR_BAD_USE;
BLOCK_ADJUST(rc, hnd->sftp->channel->session, sftp_close_handle(hnd));
return rc;
}
/* sftp_unlink
* Delete a file from the remote server
*/
static int sftp_unlink(LIBSSH2_SFTP *sftp, const char *filename,
size_t filename_len)
{
LIBSSH2_CHANNEL *channel = sftp->channel;
LIBSSH2_SESSION *session = channel->session;
size_t data_len = 0;
uint32_t retcode;
/* 13 = packet_len(4) + packet_type(1) + request_id(4) + filename_len(4) */
uint32_t packet_len = (uint32_t)(filename_len + 13);
unsigned char *s, *data = NULL;
int rc;
if(sftp->unlink_state == libssh2_NB_state_idle) {
sftp->last_errno = LIBSSH2_FX_OK;
_libssh2_debug((session, LIBSSH2_TRACE_SFTP,
"Unlinking %s", filename));
s = sftp->unlink_packet = LIBSSH2_ALLOC(session, packet_len);
if(!sftp->unlink_packet) {
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FXP_REMOVE "
"packet");
}
_libssh2_store_u32(&s, packet_len - 4);
*(s++) = SSH_FXP_REMOVE;
sftp->unlink_request_id = sftp->request_id++;
_libssh2_store_u32(&s, sftp->unlink_request_id);
_libssh2_store_str(&s, filename, filename_len);
sftp->unlink_state = libssh2_NB_state_created;
}
if(sftp->unlink_state == libssh2_NB_state_created) {
ssize_t nwritten;
nwritten = _libssh2_channel_write(channel, 0, sftp->unlink_packet,
packet_len);
if(nwritten == LIBSSH2_ERROR_EAGAIN) {
return (int)nwritten;
}
else if((ssize_t)packet_len != nwritten) {
LIBSSH2_FREE(session, sftp->unlink_packet);
sftp->unlink_packet = NULL;
sftp->unlink_state = libssh2_NB_state_idle;
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send FXP_REMOVE command");
}
LIBSSH2_FREE(session, sftp->unlink_packet);
sftp->unlink_packet = NULL;
sftp->unlink_state = libssh2_NB_state_sent;
}
rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
sftp->unlink_request_id, &data,
&data_len, 9);
if(rc == LIBSSH2_ERROR_EAGAIN) {
return rc;
}
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
if(data_len > 0) {
LIBSSH2_FREE(session, data);
}
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP unlink packet too short");
}
else if(rc) {
sftp->unlink_state = libssh2_NB_state_idle;
return _libssh2_error(session, rc,
"Error waiting for FXP STATUS");
}
sftp->unlink_state = libssh2_NB_state_idle;
retcode = _libssh2_ntohu32(data + 5);
LIBSSH2_FREE(session, data);
if(retcode == LIBSSH2_FX_OK) {
return 0;
}
else {
sftp->last_errno = retcode;
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error");
}
}
/* libssh2_sftp_unlink_ex
* Delete a file from the remote server
*/
LIBSSH2_API int
libssh2_sftp_unlink_ex(LIBSSH2_SFTP *sftp, const char *filename,
unsigned int filename_len)
{
int rc;
if(!sftp)
return LIBSSH2_ERROR_BAD_USE;
BLOCK_ADJUST(rc, sftp->channel->session,
sftp_unlink(sftp, filename, filename_len));
return rc;
}
/* sftp_rename
* Rename a file on the remote server
*/
static int sftp_rename(LIBSSH2_SFTP *sftp, const char *source_filename,
unsigned int source_filename_len,
const char *dest_filename,
unsigned int dest_filename_len, long flags)
{
LIBSSH2_CHANNEL *channel = sftp->channel;
LIBSSH2_SESSION *session = channel->session;
size_t data_len = 0;
int retcode;
uint32_t packet_len =
source_filename_len + dest_filename_len + 17 +
(sftp->version >= 5 ? 4 : 0);
/* packet_len(4) + packet_type(1) + request_id(4) +
source_filename_len(4) + dest_filename_len(4) + flags(4){SFTP5+) */
unsigned char *data = NULL;
ssize_t rc;
if(sftp->rename_state == libssh2_NB_state_idle) {
sftp->last_errno = LIBSSH2_FX_OK;
if(sftp->version < 2) {
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Server does not support RENAME");
}
_libssh2_debug((session, LIBSSH2_TRACE_SFTP, "Renaming %s to %s",
source_filename, dest_filename));
sftp->rename_s = sftp->rename_packet =
LIBSSH2_ALLOC(session, packet_len);
if(!sftp->rename_packet) {
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FXP_RENAME "
"packet");
}
_libssh2_store_u32(&sftp->rename_s, packet_len - 4);
*(sftp->rename_s++) = SSH_FXP_RENAME;
sftp->rename_request_id = sftp->request_id++;
_libssh2_store_u32(&sftp->rename_s, sftp->rename_request_id);
_libssh2_store_str(&sftp->rename_s, source_filename,
source_filename_len);
_libssh2_store_str(&sftp->rename_s, dest_filename, dest_filename_len);
if(sftp->version >= 5)
_libssh2_store_u32(&sftp->rename_s, (uint32_t)flags);
sftp->rename_state = libssh2_NB_state_created;
}
if(sftp->rename_state == libssh2_NB_state_created) {
rc = _libssh2_channel_write(channel, 0, sftp->rename_packet,
sftp->rename_s - sftp->rename_packet);
if(rc == LIBSSH2_ERROR_EAGAIN) {
return (int)rc;
}
else if((ssize_t)packet_len != rc) {
LIBSSH2_FREE(session, sftp->rename_packet);
sftp->rename_packet = NULL;
sftp->rename_state = libssh2_NB_state_idle;
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send FXP_RENAME command");
}
LIBSSH2_FREE(session, sftp->rename_packet);
sftp->rename_packet = NULL;
sftp->rename_state = libssh2_NB_state_sent;
}
rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
sftp->rename_request_id, &data,
&data_len, 9);
if(rc == LIBSSH2_ERROR_EAGAIN) {
return (int)rc;
}
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
if(data_len > 0) {
LIBSSH2_FREE(session, data);
}
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP rename packet too short");
}
else if(rc) {
sftp->rename_state = libssh2_NB_state_idle;
return _libssh2_error(session, (int)rc,
"Error waiting for FXP STATUS");
}
sftp->rename_state = libssh2_NB_state_idle;
retcode = _libssh2_ntohu32(data + 5);
LIBSSH2_FREE(session, data);
sftp->last_errno = retcode;
/* now convert the SFTP error code to libssh2 return code or error
message */
switch(retcode) {
case LIBSSH2_FX_OK:
retcode = LIBSSH2_ERROR_NONE;
break;
case LIBSSH2_FX_FILE_ALREADY_EXISTS:
retcode = _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"File already exists and "
"SSH_FXP_RENAME_OVERWRITE not specified");
break;
case LIBSSH2_FX_OP_UNSUPPORTED:
retcode = _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Operation Not Supported");
break;
default:
retcode = _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error");
break;
}
return retcode;
}
/* libssh2_sftp_rename_ex
* Rename a file on the remote server
*/
LIBSSH2_API int
libssh2_sftp_rename_ex(LIBSSH2_SFTP *sftp, const char *source_filename,
unsigned int source_filename_len,
const char *dest_filename,
unsigned int dest_filename_len, long flags)
{
int rc;
if(!sftp)
return LIBSSH2_ERROR_BAD_USE;
BLOCK_ADJUST(rc, sftp->channel->session,
sftp_rename(sftp, source_filename, source_filename_len,
dest_filename, dest_filename_len, flags));
return rc;
}
/* sftp_fstatvfs
* Get file system statistics
*/
static int sftp_fstatvfs(LIBSSH2_SFTP_HANDLE *handle, LIBSSH2_SFTP_STATVFS *st)
{
LIBSSH2_SFTP *sftp = handle->sftp;
LIBSSH2_CHANNEL *channel = sftp->channel;
LIBSSH2_SESSION *session = channel->session;
size_t data_len = 0;
/* 17 = packet_len(4) + packet_type(1) + request_id(4) + ext_len(4)
+ handle_len (4) */
/* 20 = strlen ("fstatvfs@openssh.com") */
uint32_t packet_len = (uint32_t)(handle->handle_len + 20 + 17);
unsigned char *packet, *s, *data = NULL;
ssize_t rc;
unsigned int flag;
static const unsigned char responses[2] =
{ SSH_FXP_EXTENDED_REPLY, SSH_FXP_STATUS };
if(sftp->fstatvfs_state == libssh2_NB_state_idle) {
sftp->last_errno = LIBSSH2_FX_OK;
_libssh2_debug((session, LIBSSH2_TRACE_SFTP,
"Getting file system statistics"));
s = packet = LIBSSH2_ALLOC(session, packet_len);
if(!packet) {
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FXP_EXTENDED "
"packet");
}
_libssh2_store_u32(&s, packet_len - 4);
*(s++) = SSH_FXP_EXTENDED;
sftp->fstatvfs_request_id = sftp->request_id++;
_libssh2_store_u32(&s, sftp->fstatvfs_request_id);
_libssh2_store_str(&s, "fstatvfs@openssh.com", 20);
_libssh2_store_str(&s, handle->handle, handle->handle_len);
sftp->fstatvfs_state = libssh2_NB_state_created;
}
else {
packet = sftp->fstatvfs_packet;
}
if(sftp->fstatvfs_state == libssh2_NB_state_created) {
rc = _libssh2_channel_write(channel, 0, packet, packet_len);
if(rc == LIBSSH2_ERROR_EAGAIN ||
(0 <= rc && rc < (ssize_t)packet_len)) {
sftp->fstatvfs_packet = packet;
return LIBSSH2_ERROR_EAGAIN;
}
LIBSSH2_FREE(session, packet);
sftp->fstatvfs_packet = NULL;
if(rc < 0) {
sftp->fstatvfs_state = libssh2_NB_state_idle;
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"_libssh2_channel_write() failed");
}
sftp->fstatvfs_state = libssh2_NB_state_sent;
}
rc = sftp_packet_requirev(sftp, 2, responses, sftp->fstatvfs_request_id,
&data, &data_len, 9);
if(rc == LIBSSH2_ERROR_EAGAIN) {
return (int)rc;
}
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
if(data_len > 0) {
LIBSSH2_FREE(session, data);
}
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP rename packet too short");
}
else if(rc) {
sftp->fstatvfs_state = libssh2_NB_state_idle;
return _libssh2_error(session, (int)rc,
"Error waiting for FXP EXTENDED REPLY");
}
if(data[0] == SSH_FXP_STATUS) {
uint32_t retcode = _libssh2_ntohu32(data + 5);
sftp->fstatvfs_state = libssh2_NB_state_idle;
LIBSSH2_FREE(session, data);
sftp->last_errno = retcode;
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error");
}
if(data_len < 93) {
LIBSSH2_FREE(session, data);
sftp->fstatvfs_state = libssh2_NB_state_idle;
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error: short response");
}
sftp->fstatvfs_state = libssh2_NB_state_idle;
st->f_bsize = _libssh2_ntohu64(data + 5);
st->f_frsize = _libssh2_ntohu64(data + 13);
st->f_blocks = _libssh2_ntohu64(data + 21);
st->f_bfree = _libssh2_ntohu64(data + 29);
st->f_bavail = _libssh2_ntohu64(data + 37);
st->f_files = _libssh2_ntohu64(data + 45);
st->f_ffree = _libssh2_ntohu64(data + 53);
st->f_favail = _libssh2_ntohu64(data + 61);
st->f_fsid = _libssh2_ntohu64(data + 69);
flag = (unsigned int)_libssh2_ntohu64(data + 77);
st->f_namemax = _libssh2_ntohu64(data + 85);
st->f_flag = (flag & SSH_FXE_STATVFS_ST_RDONLY)
? LIBSSH2_SFTP_ST_RDONLY : 0;
st->f_flag |= (flag & SSH_FXE_STATVFS_ST_NOSUID)
? LIBSSH2_SFTP_ST_NOSUID : 0;
LIBSSH2_FREE(session, data);
return 0;
}
/* libssh2_sftp_fstatvfs
* Get filesystem space and inode utilization (requires fstatvfs@openssh.com
* support on the server)
*/
LIBSSH2_API int
libssh2_sftp_fstatvfs(LIBSSH2_SFTP_HANDLE *handle, LIBSSH2_SFTP_STATVFS *st)
{
int rc;
if(!handle || !st)
return LIBSSH2_ERROR_BAD_USE;
BLOCK_ADJUST(rc, handle->sftp->channel->session,
sftp_fstatvfs(handle, st));
return rc;
}
/* sftp_statvfs
* Get file system statistics
*/
static int sftp_statvfs(LIBSSH2_SFTP *sftp, const char *path,
unsigned int path_len, LIBSSH2_SFTP_STATVFS *st)
{
LIBSSH2_CHANNEL *channel = sftp->channel;
LIBSSH2_SESSION *session = channel->session;
size_t data_len = 0;
/* 17 = packet_len(4) + packet_type(1) + request_id(4) + ext_len(4)
+ path_len (4) */
/* 19 = strlen ("statvfs@openssh.com") */
uint32_t packet_len = path_len + 19 + 17;
unsigned char *packet, *s, *data = NULL;
ssize_t rc;
unsigned int flag;
static const unsigned char responses[2] =
{ SSH_FXP_EXTENDED_REPLY, SSH_FXP_STATUS };
if(sftp->statvfs_state == libssh2_NB_state_idle) {
sftp->last_errno = LIBSSH2_FX_OK;
_libssh2_debug((session, LIBSSH2_TRACE_SFTP,
"Getting file system statistics of %s", path));
s = packet = LIBSSH2_ALLOC(session, packet_len);
if(!packet) {
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FXP_EXTENDED "
"packet");
}
_libssh2_store_u32(&s, packet_len - 4);
*(s++) = SSH_FXP_EXTENDED;
sftp->statvfs_request_id = sftp->request_id++;
_libssh2_store_u32(&s, sftp->statvfs_request_id);
_libssh2_store_str(&s, "statvfs@openssh.com", 19);
_libssh2_store_str(&s, path, path_len);
sftp->statvfs_state = libssh2_NB_state_created;
}
else {
packet = sftp->statvfs_packet;
}
if(sftp->statvfs_state == libssh2_NB_state_created) {
rc = _libssh2_channel_write(channel, 0, packet, packet_len);
if(rc == LIBSSH2_ERROR_EAGAIN ||
(0 <= rc && rc < (ssize_t)packet_len)) {
sftp->statvfs_packet = packet;
return LIBSSH2_ERROR_EAGAIN;
}
LIBSSH2_FREE(session, packet);
sftp->statvfs_packet = NULL;
if(rc < 0) {
sftp->statvfs_state = libssh2_NB_state_idle;
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"_libssh2_channel_write() failed");
}
sftp->statvfs_state = libssh2_NB_state_sent;
}
rc = sftp_packet_requirev(sftp, 2, responses, sftp->statvfs_request_id,
&data, &data_len, 9);
if(rc == LIBSSH2_ERROR_EAGAIN) {
return (int)rc;
}
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
if(data_len > 0) {
LIBSSH2_FREE(session, data);
}
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP fstat packet too short");
}
else if(rc) {
sftp->statvfs_state = libssh2_NB_state_idle;
return _libssh2_error(session, (int)rc,
"Error waiting for FXP EXTENDED REPLY");
}
if(data[0] == SSH_FXP_STATUS) {
uint32_t retcode = _libssh2_ntohu32(data + 5);
sftp->statvfs_state = libssh2_NB_state_idle;
LIBSSH2_FREE(session, data);
sftp->last_errno = retcode;
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error");
}
if(data_len < 93) {
LIBSSH2_FREE(session, data);
sftp->statvfs_state = libssh2_NB_state_idle;
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error: short response");
}
sftp->statvfs_state = libssh2_NB_state_idle;
st->f_bsize = _libssh2_ntohu64(data + 5);
st->f_frsize = _libssh2_ntohu64(data + 13);
st->f_blocks = _libssh2_ntohu64(data + 21);
st->f_bfree = _libssh2_ntohu64(data + 29);
st->f_bavail = _libssh2_ntohu64(data + 37);
st->f_files = _libssh2_ntohu64(data + 45);
st->f_ffree = _libssh2_ntohu64(data + 53);
st->f_favail = _libssh2_ntohu64(data + 61);
st->f_fsid = _libssh2_ntohu64(data + 69);
flag = (unsigned int)_libssh2_ntohu64(data + 77);
st->f_namemax = _libssh2_ntohu64(data + 85);
st->f_flag = (flag & SSH_FXE_STATVFS_ST_RDONLY)
? LIBSSH2_SFTP_ST_RDONLY : 0;
st->f_flag |= (flag & SSH_FXE_STATVFS_ST_NOSUID)
? LIBSSH2_SFTP_ST_NOSUID : 0;
LIBSSH2_FREE(session, data);
return 0;
}
/* libssh2_sftp_statvfs_ex
* Get filesystem space and inode utilization (requires statvfs@openssh.com
* support on the server)
*/
LIBSSH2_API int
libssh2_sftp_statvfs(LIBSSH2_SFTP *sftp, const char *path,
size_t path_len, LIBSSH2_SFTP_STATVFS *st)
{
int rc;
if(!sftp || !st)
return LIBSSH2_ERROR_BAD_USE;
BLOCK_ADJUST(rc, sftp->channel->session,
sftp_statvfs(sftp, path, (unsigned int)path_len, st));
return rc;
}
/* sftp_mkdir
* Create an SFTP directory
*/
static int sftp_mkdir(LIBSSH2_SFTP *sftp, const char *path,
unsigned int path_len, long mode)
{
LIBSSH2_CHANNEL *channel = sftp->channel;
LIBSSH2_SESSION *session = channel->session;
LIBSSH2_SFTP_ATTRIBUTES attrs = {
0, 0, 0, 0, 0, 0, 0
};
size_t data_len = 0;
uint32_t retcode;
ssize_t packet_len;
unsigned char *packet, *s, *data = NULL;
int rc;
if(mode != LIBSSH2_SFTP_DEFAULT_MODE) {
/* Filetype in SFTP 3 and earlier */
attrs.flags = LIBSSH2_SFTP_ATTR_PERMISSIONS;
attrs.permissions = mode | LIBSSH2_SFTP_ATTR_PFILETYPE_DIR;
}
/* 13 = packet_len(4) + packet_type(1) + request_id(4) + path_len(4) */
packet_len = path_len + 13 + sftp_attrsize(attrs.flags);
if(sftp->mkdir_state == libssh2_NB_state_idle) {
sftp->last_errno = LIBSSH2_FX_OK;
_libssh2_debug((session, LIBSSH2_TRACE_SFTP,
"Creating directory %s with mode 0%lo", path, mode));
s = packet = LIBSSH2_ALLOC(session, packet_len);
if(!packet) {
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FXP_MKDIR "
"packet");
}
_libssh2_store_u32(&s, (uint32_t)(packet_len - 4));
*(s++) = SSH_FXP_MKDIR;
sftp->mkdir_request_id = sftp->request_id++;
_libssh2_store_u32(&s, sftp->mkdir_request_id);
_libssh2_store_str(&s, path, path_len);
s += sftp_attr2bin(s, &attrs);
sftp->mkdir_state = libssh2_NB_state_created;
}
else {
packet = sftp->mkdir_packet;
}
if(sftp->mkdir_state == libssh2_NB_state_created) {
ssize_t nwritten;
nwritten = _libssh2_channel_write(channel, 0, packet, packet_len);
if(nwritten == LIBSSH2_ERROR_EAGAIN) {
sftp->mkdir_packet = packet;
return (int)nwritten;
}
if(packet_len != nwritten) {
LIBSSH2_FREE(session, packet);
sftp->mkdir_state = libssh2_NB_state_idle;
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"_libssh2_channel_write() failed");
}
LIBSSH2_FREE(session, packet);
sftp->mkdir_state = libssh2_NB_state_sent;
sftp->mkdir_packet = NULL;
}
rc = sftp_packet_require(sftp, SSH_FXP_STATUS, sftp->mkdir_request_id,
&data, &data_len, 9);
if(rc == LIBSSH2_ERROR_EAGAIN) {
return rc;
}
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
if(data_len > 0) {
LIBSSH2_FREE(session, data);
}
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP mkdir packet too short");
}
else if(rc) {
sftp->mkdir_state = libssh2_NB_state_idle;
return _libssh2_error(session, rc,
"Error waiting for FXP STATUS");
}
sftp->mkdir_state = libssh2_NB_state_idle;
retcode = _libssh2_ntohu32(data + 5);
LIBSSH2_FREE(session, data);
if(retcode == LIBSSH2_FX_OK) {
_libssh2_debug((session, LIBSSH2_TRACE_SFTP, "OK"));
return 0;
}
else {
sftp->last_errno = retcode;
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error");
}
}
/* libssh2_sftp_mkdir_ex
* Create an SFTP directory
*/
LIBSSH2_API int
libssh2_sftp_mkdir_ex(LIBSSH2_SFTP *sftp, const char *path,
unsigned int path_len, long mode)
{
int rc;
if(!sftp)
return LIBSSH2_ERROR_BAD_USE;
BLOCK_ADJUST(rc, sftp->channel->session,
sftp_mkdir(sftp, path, path_len, mode));
return rc;
}
/* sftp_rmdir
* Remove a directory
*/
static int sftp_rmdir(LIBSSH2_SFTP *sftp, const char *path,
unsigned int path_len)
{
LIBSSH2_CHANNEL *channel = sftp->channel;
LIBSSH2_SESSION *session = channel->session;
size_t data_len = 0;
uint32_t retcode;
/* 13 = packet_len(4) + packet_type(1) + request_id(4) + path_len(4) */
ssize_t packet_len = path_len + 13;
unsigned char *s, *data = NULL;
int rc;
if(sftp->rmdir_state == libssh2_NB_state_idle) {
sftp->last_errno = LIBSSH2_FX_OK;
_libssh2_debug((session, LIBSSH2_TRACE_SFTP, "Removing directory: %s",
path));
s = sftp->rmdir_packet = LIBSSH2_ALLOC(session, packet_len);
if(!sftp->rmdir_packet) {
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FXP_RMDIR "
"packet");
}
_libssh2_store_u32(&s, (uint32_t)(packet_len - 4));
*(s++) = SSH_FXP_RMDIR;
sftp->rmdir_request_id = sftp->request_id++;
_libssh2_store_u32(&s, sftp->rmdir_request_id);
_libssh2_store_str(&s, path, path_len);
sftp->rmdir_state = libssh2_NB_state_created;
}
if(sftp->rmdir_state == libssh2_NB_state_created) {
ssize_t nwritten;
nwritten = _libssh2_channel_write(channel, 0, sftp->rmdir_packet,
packet_len);
if(nwritten == LIBSSH2_ERROR_EAGAIN) {
return (int)nwritten;
}
else if(packet_len != nwritten) {
LIBSSH2_FREE(session, sftp->rmdir_packet);
sftp->rmdir_packet = NULL;
sftp->rmdir_state = libssh2_NB_state_idle;
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send FXP_RMDIR command");
}
LIBSSH2_FREE(session, sftp->rmdir_packet);
sftp->rmdir_packet = NULL;
sftp->rmdir_state = libssh2_NB_state_sent;
}
rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
sftp->rmdir_request_id, &data, &data_len, 9);
if(rc == LIBSSH2_ERROR_EAGAIN) {
return rc;
}
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
if(data_len > 0) {
LIBSSH2_FREE(session, data);
}
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP rmdir packet too short");
}
else if(rc) {
sftp->rmdir_state = libssh2_NB_state_idle;
return _libssh2_error(session, rc,
"Error waiting for FXP STATUS");
}
sftp->rmdir_state = libssh2_NB_state_idle;
retcode = _libssh2_ntohu32(data + 5);
LIBSSH2_FREE(session, data);
if(retcode == LIBSSH2_FX_OK) {
return 0;
}
else {
sftp->last_errno = retcode;
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error");
}
}
/* libssh2_sftp_rmdir_ex
* Remove a directory
*/
LIBSSH2_API int
libssh2_sftp_rmdir_ex(LIBSSH2_SFTP *sftp, const char *path,
unsigned int path_len)
{
int rc;
if(!sftp)
return LIBSSH2_ERROR_BAD_USE;
BLOCK_ADJUST(rc, sftp->channel->session,
sftp_rmdir(sftp, path, path_len));
return rc;
}
/* sftp_stat
* Stat a file or symbolic link
*/
static int sftp_stat(LIBSSH2_SFTP *sftp, const char *path,
unsigned int path_len, int stat_type,
LIBSSH2_SFTP_ATTRIBUTES * attrs)
{
LIBSSH2_CHANNEL *channel = sftp->channel;
LIBSSH2_SESSION *session = channel->session;
size_t data_len = 0;
/* 13 = packet_len(4) + packet_type(1) + request_id(4) + path_len(4) */
ssize_t packet_len =
path_len + 13 +
((stat_type ==
LIBSSH2_SFTP_SETSTAT) ? sftp_attrsize(attrs->flags) : 0);
unsigned char *s, *data = NULL;
static const unsigned char stat_responses[2] =
{ SSH_FXP_ATTRS, SSH_FXP_STATUS };
int rc;
if(sftp->stat_state == libssh2_NB_state_idle) {
sftp->last_errno = LIBSSH2_FX_OK;
_libssh2_debug((session, LIBSSH2_TRACE_SFTP, "%s %s",
(stat_type == LIBSSH2_SFTP_SETSTAT) ? "Set-statting" :
(stat_type ==
LIBSSH2_SFTP_LSTAT ? "LStatting" : "Statting"), path));
s = sftp->stat_packet = LIBSSH2_ALLOC(session, packet_len);
if(!sftp->stat_packet) {
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FXP_*STAT "
"packet");
}
_libssh2_store_u32(&s, (uint32_t)(packet_len - 4));
switch(stat_type) {
case LIBSSH2_SFTP_SETSTAT:
*(s++) = SSH_FXP_SETSTAT;
break;
case LIBSSH2_SFTP_LSTAT:
*(s++) = SSH_FXP_LSTAT;
break;
case LIBSSH2_SFTP_STAT:
default:
*(s++) = SSH_FXP_STAT;
}
sftp->stat_request_id = sftp->request_id++;
_libssh2_store_u32(&s, sftp->stat_request_id);
_libssh2_store_str(&s, path, path_len);
if(stat_type == LIBSSH2_SFTP_SETSTAT)
s += sftp_attr2bin(s, attrs);
sftp->stat_state = libssh2_NB_state_created;
}
if(sftp->stat_state == libssh2_NB_state_created) {
ssize_t nwritten;
nwritten = _libssh2_channel_write(channel, 0,
sftp->stat_packet, packet_len);
if(nwritten == LIBSSH2_ERROR_EAGAIN) {
return (int)nwritten;
}
else if(packet_len != nwritten) {
LIBSSH2_FREE(session, sftp->stat_packet);
sftp->stat_packet = NULL;
sftp->stat_state = libssh2_NB_state_idle;
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send STAT/LSTAT/SETSTAT command");
}
LIBSSH2_FREE(session, sftp->stat_packet);
sftp->stat_packet = NULL;
sftp->stat_state = libssh2_NB_state_sent;
}
rc = sftp_packet_requirev(sftp, 2, stat_responses,
sftp->stat_request_id, &data, &data_len, 9);
if(rc == LIBSSH2_ERROR_EAGAIN)
return rc;
else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
if(data_len > 0) {
LIBSSH2_FREE(session, data);
}
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP stat packet too short");
}
else if(rc) {
sftp->stat_state = libssh2_NB_state_idle;
return _libssh2_error(session, rc,
"Timeout waiting for status message");
}
sftp->stat_state = libssh2_NB_state_idle;
if(data[0] == SSH_FXP_STATUS) {
uint32_t retcode;
retcode = _libssh2_ntohu32(data + 5);
LIBSSH2_FREE(session, data);
if(retcode == LIBSSH2_FX_OK) {
memset(attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES));
return 0;
}
else {
sftp->last_errno = retcode;
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error");
}
}
memset(attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES));
if(sftp_bin2attr(attrs, data + 5, data_len - 5) < 0) {
LIBSSH2_FREE(session, data);
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Attributes too short in SFTP fstat");
}
LIBSSH2_FREE(session, data);
return 0;
}
/* libssh2_sftp_stat_ex
* Stat a file or symbolic link
*/
LIBSSH2_API int
libssh2_sftp_stat_ex(LIBSSH2_SFTP *sftp, const char *path,
unsigned int path_len, int stat_type,
LIBSSH2_SFTP_ATTRIBUTES *attrs)
{
int rc;
if(!sftp)
return LIBSSH2_ERROR_BAD_USE;
BLOCK_ADJUST(rc, sftp->channel->session,
sftp_stat(sftp, path, path_len, stat_type, attrs));
return rc;
}
/* sftp_symlink
* Read or set a symlink
*/
static int sftp_symlink(LIBSSH2_SFTP *sftp, const char *path,
unsigned int path_len, char *target,
unsigned int target_len, int link_type)
{
LIBSSH2_CHANNEL *channel = sftp->channel;
LIBSSH2_SESSION *session = channel->session;
size_t data_len = 0, link_len;
/* 13 = packet_len(4) + packet_type(1) + request_id(4) + path_len(4) */
ssize_t packet_len =
path_len + 13 +
((link_type == LIBSSH2_SFTP_SYMLINK) ? (4 + target_len) : 0);
unsigned char *s, *data = NULL;
static const unsigned char link_responses[2] =
{ SSH_FXP_NAME, SSH_FXP_STATUS };
int retcode;
if(sftp->symlink_state == libssh2_NB_state_idle) {
sftp->last_errno = LIBSSH2_FX_OK;
if((sftp->version < 3) && (link_type != LIBSSH2_SFTP_REALPATH)) {
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Server does not support SYMLINK or"
" READLINK");
}
s = sftp->symlink_packet = LIBSSH2_ALLOC(session, packet_len);
if(!sftp->symlink_packet) {
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for "
"SYMLINK/READLINK/REALPATH packet");
}
_libssh2_debug((session, LIBSSH2_TRACE_SFTP, "%s %s on %s",
(link_type ==
LIBSSH2_SFTP_SYMLINK) ? "Creating" : "Reading",
(link_type ==
LIBSSH2_SFTP_REALPATH) ? "realpath" : "symlink",
path));
_libssh2_store_u32(&s, (uint32_t)(packet_len - 4));
switch(link_type) {
case LIBSSH2_SFTP_REALPATH:
*(s++) = SSH_FXP_REALPATH;
break;
case LIBSSH2_SFTP_SYMLINK:
*(s++) = SSH_FXP_SYMLINK;
break;
case LIBSSH2_SFTP_READLINK:
default:
*(s++) = SSH_FXP_READLINK;
}
sftp->symlink_request_id = sftp->request_id++;
_libssh2_store_u32(&s, sftp->symlink_request_id);
_libssh2_store_str(&s, path, path_len);
if(link_type == LIBSSH2_SFTP_SYMLINK)
_libssh2_store_str(&s, target, target_len);
sftp->symlink_state = libssh2_NB_state_created;
}
if(sftp->symlink_state == libssh2_NB_state_created) {
ssize_t rc = _libssh2_channel_write(channel, 0, sftp->symlink_packet,
packet_len);
if(rc == LIBSSH2_ERROR_EAGAIN)
return (int)rc;
else if(packet_len != rc) {
LIBSSH2_FREE(session, sftp->symlink_packet);
sftp->symlink_packet = NULL;
sftp->symlink_state = libssh2_NB_state_idle;
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send SYMLINK/READLINK command");
}
LIBSSH2_FREE(session, sftp->symlink_packet);
sftp->symlink_packet = NULL;
sftp->symlink_state = libssh2_NB_state_sent;
}
retcode = sftp_packet_requirev(sftp, 2, link_responses,
sftp->symlink_request_id, &data,
&data_len, 9);
if(retcode == LIBSSH2_ERROR_EAGAIN)
return retcode;
else if(retcode == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
if(data_len > 0) {
LIBSSH2_FREE(session, data);
}
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP symlink packet too short");
}
else if(retcode) {
sftp->symlink_state = libssh2_NB_state_idle;
return _libssh2_error(session, retcode,
"Error waiting for status message");
}
sftp->symlink_state = libssh2_NB_state_idle;
if(data[0] == SSH_FXP_STATUS) {
retcode = _libssh2_ntohu32(data + 5);
LIBSSH2_FREE(session, data);
if(retcode == LIBSSH2_FX_OK)
return LIBSSH2_ERROR_NONE;
else {
sftp->last_errno = retcode;
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error");
}
}
if(_libssh2_ntohu32(data + 5) < 1) {
LIBSSH2_FREE(session, data);
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Invalid READLINK/REALPATH response, "
"no name entries");
}
if(data_len < 13) {
if(data_len > 0) {
LIBSSH2_FREE(session, data);
}
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP stat packet too short");
}
/* this reads a u32 and stores it into a signed 32bit value */
link_len = _libssh2_ntohu32(data + 9);
if(link_len < target_len) {
memcpy(target, data + 13, link_len);
target[link_len] = 0;
retcode = (int)link_len;
}
else
retcode = LIBSSH2_ERROR_BUFFER_TOO_SMALL;
LIBSSH2_FREE(session, data);
return retcode;
}
/* libssh2_sftp_symlink_ex
* Read or set a symlink
*/
LIBSSH2_API int
libssh2_sftp_symlink_ex(LIBSSH2_SFTP *sftp, const char *path,
unsigned int path_len, char *target,
unsigned int target_len, int link_type)
{
int rc;
if(!sftp)
return LIBSSH2_ERROR_BAD_USE;
BLOCK_ADJUST(rc, sftp->channel->session,
sftp_symlink(sftp, path, path_len, target, target_len,
link_type));
return rc;
}
/* libssh2_sftp_last_error
* Returns the last error code reported by SFTP
*/
LIBSSH2_API unsigned long
libssh2_sftp_last_error(LIBSSH2_SFTP *sftp)
{
if(!sftp)
return 0;
return sftp->last_errno;
}
/* libssh2_sftp_get_channel
* Return the channel of sftp, then caller can control the channel's behavior.
*/
LIBSSH2_API LIBSSH2_CHANNEL *
libssh2_sftp_get_channel(LIBSSH2_SFTP *sftp)
{
if(!sftp)
return NULL;
return sftp->channel;
}
|