File: scp.c

package info (click to toggle)
proftpd-dfsg 1.3.9~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 58,708 kB
  • sloc: perl: 290,309; ansic: 244,301; sh: 17,315; php: 11,586; makefile: 1,092; xml: 93
file content (2932 lines) | stat: -rw-r--r-- 82,061 bytes parent folder | download
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
/*
 * ProFTPD - mod_sftp SCP
 * Copyright (c) 2008-2025 TJ Saunders
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
 *
 * As a special exemption, TJ Saunders and other respective copyright holders
 * give permission to link this program with OpenSSL, and distribute the
 * resulting executable, without including the source code for OpenSSL in the
 * source distribution.
 */

#include "mod_sftp.h"
#include "ssh2.h"
#include "packet.h"
#include "msg.h"
#include "channel.h"
#include "scp.h"
#include "misc.h"
#include "disconnect.h"

#define SFTP_SCP_ST_MODE_MASK	(S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)

/* Define a maximum limit on the amount of data we buffer when handling
 * fragmented control messages.
 */
#define SFTP_SCP_MAX_CTL_LEN	(PR_TUNABLE_PATH_MAX + 256)

extern pr_response_t *resp_list, *resp_err_list;

struct scp_path {
  char *path;

  /* The original path, as provided in the scp command. */
  const char *orig_path;

  pr_fh_t *fh;

  /* Points to the parent directory "context" path, if any.  For handling
   * the push/pop approach that SCP uses for receiving directories from
   * recursive SCP uploads.
   *
   * Note: for very wide/deep recursive uploads, the amount of memory used
   * for these scp_path structs could grow quite a bit.  If each struct
   * was allocated out of its own sub pool, then they could be freed
   * during the recursive upload.  Something to keep in mind.
   */
  struct scp_path *parent_dir;

  /* Track state of file metadata we've received. */
  int checked_errors;

  int have_mode;
  mode_t st_mode;

  struct timeval times[2];
  int recvd_timeinfo;

  mode_t perms;
  off_t filesz;
  const char *filename;
  const char *best_path;
  int recvd_finfo;
  int recvd_data;

  /* For reading of control messages. */
  pool *ctl_pool;
  unsigned char *ctl_data;
  uint32_t ctl_datalen;

  /* For the reading of bytes of files. */
  off_t recvlen;

  int wrote_errors;

  /* Track state of how much file metadata we've sent. */
  int sent_timeinfo;
  int sent_dirinfo;
  int sent_finfo;
  int sent_data;

  /* For sending the bytes of files. */
  off_t sentlen;

  /* For directories. */
  void *dirh;
  struct scp_path *dir_spi;

  /* For supporting the HiddenStores directive. */
  int hiddenstore;

  /* For indicating whether the file existed prior to being opened/created. */
  int file_existed;
};

static pool *scp_pool = NULL;

/* Use a struct to maintain the per-channel SCP-specific values. */
struct scp_session {
  struct scp_session *next, *prev;

  pool *pool;
  uint32_t channel_id;
  array_header *paths;
  unsigned int path_idx;
};

static struct scp_session *scp_session = NULL, *scp_sessions = NULL;

/* This structure is a container, for holding the paths and index until
 * the session object for the channel is opened.  sftp_scp_set_params(),
 * which parses out the paths, is called _before_ sftp_scp_open_session(),
 * hence why we need to track these separately.
 */

struct scp_paths {
  struct scp_paths *next, *prev;

  pool *pool;
  uint32_t channel_id;
  array_header *paths;
  unsigned int path_idx;
};

static struct scp_paths *scp_paths = NULL;

static unsigned int scp_opts = 0;
#define SFTP_SCP_OPT_ISSRC	0x0001
#define SFTP_SCP_OPT_ISDST	0x0002
#define SFTP_SCP_OPT_DIR	0x0004
#define SFTP_SCP_OPT_VERBOSE	0x0008
#define SFTP_SCP_OPT_PRESERVE	0x0010
#define SFTP_SCP_OPT_RECURSE	0x0020

/* Boolean flag indicating whether we need to wait for the confirmation
 * response (byte) from the client before proceeding.
 */
static int need_confirm = FALSE;

static const char *trace_channel = "scp";

static int send_path(pool *, uint32_t, struct scp_path *);

static int scp_timeout_stalled_cb(CALLBACK_FRAME) {
  pr_event_generate("core.timeout-stalled", NULL);

  (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
    "SCP data transfer stalled timeout (%d secs) reached",
    pr_data_get_timeout(PR_DATA_TIMEOUT_STALLED));
  SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION,
    "data stalled timeout reached");

  return 0;
}

static cmd_rec *scp_cmd_alloc(pool *p, const char *name, const char *arg) {
  cmd_rec *cmd;

  cmd = pr_cmd_alloc(p, 2, pstrdup(p, name), arg ? arg : "");
  cmd->arg = (char *) arg;

  return cmd;
}

static int scp_destroy_paths(struct scp_paths *paths) {
  if (paths == NULL) {
    return 0;
  }

  if (paths->next)
    paths->next->prev = paths->prev;

  if (paths->prev) {
    paths->prev->next = paths->next;

  } else {
    scp_paths = paths->next;
  }

  destroy_pool(paths->pool);
  return 0;
}

static struct scp_paths *scp_new_paths(uint32_t channel_id) {
  pool *sub_pool;
  struct scp_paths *paths, *last;

  /* Check to see if we already have an paths object for this channel ID. */
  paths = last = scp_paths;
  while (paths) {
    pr_signals_handle();

    if (paths->channel_id == channel_id) {
      errno = EEXIST;
      return NULL;
    }

    if (paths->next == NULL) {
      /* This is the last item in the list. */
      last = paths;
    }

    paths = paths->next;
  }

  /* Looks like we get to allocate a new one. */
  sub_pool = make_sub_pool(scp_pool);
  pr_pool_tag(sub_pool, "SCP paths pool");

  paths = pcalloc(sub_pool, sizeof(struct scp_paths));
  paths->pool = sub_pool;
  paths->channel_id = channel_id;

  if (last != NULL) {
    last->next = paths;
    paths->prev = last;

  } else {
    scp_paths = paths;
  }

  return paths;
}

static struct scp_paths *scp_get_paths(uint32_t channel_id) {
  struct scp_paths *paths;

  paths = scp_paths;
  while (paths) {
    pr_signals_handle();

    if (paths->channel_id == channel_id) {
      return paths;
    }

    paths = paths->next;
  }

  errno = ENOENT;
  return NULL;
}

static struct scp_session *scp_get_session(uint32_t channel_id) {
  struct scp_session *sess;

  sess = scp_sessions;
  while (sess != NULL) {
    pr_signals_handle();

    if (sess->channel_id == channel_id) {
      return sess;
    }

    sess = sess->next;
  }

  errno = ENOENT;
  return NULL;
}

static void reset_path(struct scp_path *sp) {
  if (sp->fh) {
    pr_fsio_close(sp->fh);
    sp->fh = NULL;
  }

  /* XXX Should clear/reset the sent fields as well, but this function
   * is mainly for use when receiving files, not sending files.
   */

  sp->checked_errors = FALSE;

  sp->st_mode = 0;
  sp->have_mode = FALSE;
  sp->recvd_timeinfo = FALSE;

  sp->perms = 0;
  sp->filesz = 0;
  sp->filename = NULL;
  sp->best_path = NULL;
  sp->recvd_finfo = FALSE;
  sp->recvd_data = FALSE;

  sp->recvlen = 0;
  sp->hiddenstore = FALSE;
  sp->file_existed = FALSE;

  sp->wrote_errors = FALSE;
}

static int read_confirm(struct ssh2_packet *pkt, unsigned char **buf,
    uint32_t *buflen) {
  char code;

  code = sftp_msg_read_byte(pkt->pool, buf, buflen);
  pr_trace_msg(trace_channel, 9, "recvd confirmation/error code = %d", code);

  switch (code) {
    case 0:
      break;

    case 1: {
      register unsigned int i;
      char *msg;

      /* Error; message to follow. Since it won't be encoded as an SSH2 string,
       * we will need to read it character by character.  Whee.
       */

      msg = pcalloc(pkt->pool, *buflen + 1);
      for (i = 0; *buflen; ) {
        char c;

        c = sftp_msg_read_byte(pkt->pool, buf, buflen);
        if (c == '\n') {
          break;
        }

        msg[i++] = c;
      }

      (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
        "error from client: %s", msg);
      return -1;
    }

    case 2:
      /* Fatal error, no message. */
      return -1;
  }

  need_confirm = FALSE;
  return 0;
}

static int write_confirm(pool *p, uint32_t channel_id, int code,
    const char *msg) {
  unsigned char *buf, *ptr;
  uint32_t buflen, bufsz;

  /* XXX Is this big enough?  Too big? */
  buflen = bufsz = 128;
  buf = ptr = palloc(p, bufsz);

  if (code == 0) {
    pr_trace_msg(trace_channel, 9, "sending confirmation/error code = %d",
      code);
    sftp_msg_write_byte(&buf, &buflen, code);

  } else {
    char *errstr;
    size_t errlen;

    pr_trace_msg(trace_channel, 9, "sending confirmation/error code = %d (%s)",
      code, msg ? msg : "null");

    errstr = pstrcat(p, msg, "\n", NULL);
    errlen = strlen(errstr);

    sftp_msg_write_byte(&buf, &buflen, code);
    sftp_msg_write_data(&buf, &buflen, (const unsigned char *) errstr, errlen,
      FALSE);
  }

  return sftp_channel_write_data(p, channel_id, ptr, (bufsz - buflen));
}

/* Functions for receiving files from the client. */

static int recv_ctl(uint32_t channel_id, struct scp_path *sp,
    unsigned char *data, uint32_t datalen,
    unsigned char **ctl_data, uint32_t *ctl_datalen) {
  register int i;
  int have_newline = FALSE;
  char *tmp;
  uint32_t tmplen;

  for (i = datalen-1; i >= 0; i--) {
    if (data[i] == '\n') {
      have_newline = TRUE;
      break;
    }
  }

  if (sp->ctl_data == NULL) {
    if (have_newline == TRUE) {
      *ctl_data = data;
      *ctl_datalen = datalen;

      return 1;
    }

    sp->ctl_pool = pr_pool_create_sz(scp_session->pool, 128);
    sp->ctl_datalen = datalen;
    sp->ctl_data = palloc(sp->ctl_pool, sp->ctl_datalen);
    memmove(sp->ctl_data, data, datalen);

    return 0;
  }

  /* Add the given data to the existing cache of data. */
  tmplen = sp->ctl_datalen + datalen;
  tmp = palloc(sp->ctl_pool, tmplen);
  memmove(tmp, sp->ctl_data, sp->ctl_datalen);
  memmove(tmp + sp->ctl_datalen, data, datalen);

  sp->ctl_data = (unsigned char *) tmp;
  sp->ctl_datalen = tmplen;

  /* Now, if we saw a newline, we can return all of the cached data as the
   * complete control message.
   */
  if (have_newline == TRUE) {
    *ctl_data = sp->ctl_data;
    *ctl_datalen = sp->ctl_datalen;

    sp->ctl_data = NULL;
    sp->ctl_datalen = 0;
    destroy_pool(sp->ctl_pool);
    sp->ctl_pool = NULL;
    return 1;
  }

  if (sp->ctl_datalen >= SFTP_SCP_MAX_CTL_LEN) {
    write_confirm(sp->ctl_pool, channel_id, 1,
      "max control message size exceeded");
    sp->wrote_errors = TRUE;
    return 1;
  }

  /* Otherwise, we need to aggregate more data from the client. */
  return 0;
}

static int recv_errors(pool *p, uint32_t channel_id, struct scp_path *sp,
    unsigned char *data, uint32_t datalen) {

  /* Check for error messages from the client first. */
  if (data[0] == '\01') {
    register unsigned int i;
    char *msg;

    for (i = 1; i < datalen; i++) {
      if (data[i] == '\n') {
        break;
      }
    }

    if (i < datalen) {
      msg = pstrndup(p, (char *) &(data[1]), i + 1);

    } else {
      msg = pcalloc(p, i + 1);
      memcpy(msg, &(data[1]), i);
    }

    pr_trace_msg(trace_channel, 3,
      "received error '%s' from client while receiving path '%s', skipping",
      msg, sp->path);

    sp->checked_errors = TRUE;
    return 1;
  }

  if (data[0] == '\02') {
    pr_trace_msg(trace_channel, 3,
      "received fatal error from client while receiving path '%s', skipping",
      sp->path);

    sp->checked_errors = TRUE;
    return 1;
  }

  sp->checked_errors = TRUE;
  return 0;
}

static int recv_timeinfo(pool *p, uint32_t channel_id, struct scp_path *sp,
    unsigned char *buf, uint32_t buflen, unsigned char **remain,
    uint32_t *remainlen) {
  register unsigned int i;
  unsigned char *data = NULL, *msg, *ptr = NULL;
  uint32_t datalen = 0;
  char *tmp = NULL;
  int res;

  res = recv_ctl(channel_id, sp, buf, buflen, &data, &datalen);
  if (res != 1) {
    return res;
  }

  if (data[0] != 'T') {
    /* Not a timeinfo message; let someone else process this. */
    *remain = data;
    *remainlen = datalen;

    errno = EINVAL;
    return -1;
  }

  for (i = 1; i < datalen; i++) {
    if (data[i] == '\n') {
      ptr = &data[i++];
      break;
    }
  }

  msg = data + 1;

  if (ptr)
    *ptr = '\0';

  pr_trace_msg(trace_channel, 5, "'%s' control message: T%s", sp->path, msg);

  sp->times[1].tv_sec = strtoul((char *) msg, &tmp, 10);
  if (tmp == NULL ||
      *tmp != ' ') {
    write_confirm(p, channel_id, 1, "mtime secs not delimited");
    sp->wrote_errors = TRUE;
    return 1;
  }

  msg = ((unsigned char *) tmp) + 1;
  sp->times[1].tv_usec = strtoul((char *) msg, &tmp, 10);
  if (tmp == NULL ||
      *tmp != ' ') {
    write_confirm(p, channel_id, 1, "mtime usecs not delimited");
    sp->wrote_errors = TRUE;
    return 1;
  }

  msg = ((unsigned char *) tmp) + 1;
  sp->times[0].tv_sec = strtoul((char *) msg, &tmp, 10);
  if (tmp == NULL ||
      *tmp != ' ') {
    write_confirm(p, channel_id, 1, "atime secs not delimited");
    sp->wrote_errors = TRUE;
    return 1;
  }

  msg = ((unsigned char *) tmp) + 1;
  sp->times[0].tv_usec = strtoul((char *) msg, &tmp, 10);
  if (tmp == NULL ||
      *tmp != '\0') {
    write_confirm(p, channel_id, 1, "atime usecs not delimited");
    sp->wrote_errors = TRUE;
    return 1;
  }

  sp->recvd_timeinfo = TRUE;
  write_confirm(p, channel_id, 0, NULL);
  return 0;
}

static int recv_perms(pool *p, uint32_t channel_id, char *mode_str,
    mode_t *perms) {
  register unsigned int i;

  if (strlen(mode_str) < 5) {
    /* This needs to be at least 5 characters: 4 mode digits, and space. */
    pr_trace_msg(trace_channel, 2, "mode string too short: '%s'", mode_str);
    write_confirm(p, channel_id, 1, "bad mode");
    return -1;
  }

  for (i = 0; i < 4; i++) {
    /* Make sure the characters are numeric, and in the octal range. */
    if (mode_str[i] < '0' ||
        mode_str[i] > '7') {
      pr_trace_msg(trace_channel, 2, "non-octal mode character in '%s'",
        mode_str);
      *perms = 0;
      write_confirm(p, channel_id, 1, "bad mode");
      return -1;
    }

    *perms = (*perms << 3) | (mode_str[i] - '0');
  }

  /* Make sure the next character in the string is a space. */
  if (mode_str[i] != ' ') {
    pr_trace_msg(trace_channel, 2, "mode not followed by space delimiter");
    write_confirm(p, channel_id, 1, "mode not delimited");
    return -1;
  }

  pr_trace_msg(trace_channel, 8, "client sent file perms: %04o",
    (unsigned int) *perms);
  return 0;
}

static int recv_filesz(pool *p, uint32_t channel_id, char *size_str,
    off_t *filesz) {
  register unsigned int i;

  /* The file size field could be of arbitrary length. */
  for (i = 0, *filesz = 0; PR_ISDIGIT(size_str[i]); i++) {
    pr_signals_handle();

    *filesz = (*filesz * 10) + (size_str[i] - '0');
  }

  if (size_str[i] != ' ') {
    pr_trace_msg(trace_channel, 2, "file size not followed by space delimiter");
    write_confirm(p, channel_id, 1, "file size not delimited");
    return -1;
  }

  pr_trace_msg(trace_channel, 8, "client sent file size: %" PR_LU " bytes",
    (pr_off_t) *filesz);
  return 0;
}

static int recv_filename(pool *p, uint32_t channel_id, char *name_str,
    struct scp_path *sp) {

  if (strchr(name_str, '/') != NULL ||
      strncmp(name_str, "..", 3) == 0) {
    pr_trace_msg(trace_channel, 2, "bad filename: '%s'", name_str);
    write_confirm(p, channel_id, 1,
      pstrcat(p, "unexpected filename: ", name_str, NULL));
    return -1;
  }

  /* name_str contains the name of the source file, on the client machine.
   * Our task is to determine whether we want use that same filename
   * for the destination file here or not, and if not, what filename to use
   * instead.
   *
   * sp->path contains the path that the client gaves to us when starting the
   * SCP session.  This path might be a relative or absolute path to a
   * directory (which may or may not exist), or might be a relative or absolute
   * path to an actual file.  And whether we are chrooted or not might also
   * factor into things.
   *
   * Examples:
   *
   * 1. scp src.txt 1.2.3.4:dst.txt
   *
   *   name_str = "src.txt"
   *   sp->path = "dst.txt"
   *
   * 2. scp src.txt 1.2.3.4:dir
   *
   *   name_str = "src.txt"
   *   sp->path = "dir"
   *
   * 3. scp src.txt 1.2.3.4:dir/
   *
   *   name_str = "src.txt"
   *   sp->path = "dir/"
   *
   * 4. scp src.txt 1.2.3.4:dir/dst.txt
   *
   *   name_str = "src.txt"
   *   sp->path = "dir/dst.txt"
   *
   * 5. scp src.txt 1.2.3.4:/dir
   *
   *   name_str = "src.txt"
   *   sp->path = "/dir"
   *
   * 6. scp src.txt 1.2.3.4:/dir/
   *
   *   name_str = "src.txt"
   *   sp->path = "/dir/"
   *
   * 7. scp src.txt 1.2.3.4:/dir/dst.txt
   *
   *   name_str = "src.txt"
   *   sp->path = "/dir/dst.txt"
   *
   * All of the above examples are effectively the same.  We need to determine
   * whether sp->path is a directory or not.  The sp->st_mode struct stat can
   * be used for this.  We should not rely on the presence (or not) of a
   * trailing slash in the sp->path string.
   *
   * If we determine that sp->path is a directory, then we need to append
   * name_str to get the path to the destination file.  Otherwise,
   * we should use sp->path as is, as the path to the destination file.
   */

  if (sp->parent_dir == NULL) {
    if (!S_ISDIR(sp->st_mode)) {
      /* sp->path is not a directory; use it as the destination filename. */
      sp->filename = pstrdup(scp_pool, sp->path);

    } else {
      /* sp->path is a directory; append the source filename to it to get the
       * destination filename.
       */
      sp->filename = pdircat(scp_pool, sp->path, name_str, NULL);
    }

  } else {
    /* Fortunately, in the case of recursive SCP uploads, we always use the
     * source filename as the destination file.
     */
    sp->filename = pdircat(scp_pool, sp->path, name_str, NULL);
  }

  if (sp->filename != NULL) {
    struct stat st;

    sp->best_path = dir_canonical_vpath(scp_pool, sp->filename);

    pr_fs_clear_cache2(sp->best_path);
    if (pr_fsio_lstat(sp->best_path, &st) == 0) {
      if (S_ISLNK(st.st_mode)) {
        char link_path[PR_TUNABLE_PATH_MAX];
        int len;

        memset(link_path, '\0', sizeof(link_path));
        len = dir_readlink(scp_pool, sp->best_path, link_path,
          sizeof(link_path)-1, PR_DIR_READLINK_FL_HANDLE_REL_PATH);
        if (len > 0) {
          link_path[len] = '\0';
          sp->best_path = pstrdup(scp_pool, link_path);
        }
      }
    }

    /* Update the session.xfer.path value with this better, fuller path. */
    session.xfer.path = pstrdup(session.xfer.p, sp->best_path);
  }

  pr_trace_msg(trace_channel, 8,
    "client sent filename '%s' (path '%s')", name_str, sp->best_path);
  return 0;
}

static int recv_finfo(pool *p, uint32_t channel_id, struct scp_path *sp,
    unsigned char *buf, uint32_t buflen) {
  register unsigned int i;
  const char *hiddenstore_path = NULL;
  struct stat st;
  unsigned char *data = NULL, *msg;
  uint32_t datalen = 0;
  char *ptr = NULL;
  int have_dir = FALSE, res;
  cmd_rec *cmd = NULL;

  res = recv_ctl(channel_id, sp, buf, buflen, &data, &datalen);
  if (res != 1) {
    return res;
  }

  switch (data[0]) {
    case 'C':
      break;

    case 'D':
      if (!(scp_opts & SFTP_SCP_OPT_RECURSE)) {
        pr_trace_msg(trace_channel, 3,
          "received D control message for '%s' without RECURSE set, "
          "rejecting", sp->path);
        write_confirm(p, channel_id, 1,
          pstrcat(p, sp->path, ": cannot use directory (no -r option)", NULL));
        sp->wrote_errors = TRUE;
        return 1;
      }

      have_dir = TRUE;
      break;

    default:
      pr_trace_msg(trace_channel, 3,
        "expected file info control message for '%s', got '%c'",
        sp->path, data[0]);
      write_confirm(p, channel_id, 1,
        pstrcat(p, sp->path, ": expected control message", NULL));
      sp->wrote_errors = TRUE;
      return 1;
  }

  for (i = 1; i < datalen; i++) {
    if (data[i] == '\n') {
      ptr = (char *) &data[i++];
      break;
    }
  }

  msg = data + 1;
  if (ptr != NULL) {
    *ptr = '\0';
  }

  pr_trace_msg(trace_channel, 5, "'%s' control message: %c%s", sp->path,
    !have_dir ? 'C' : 'D', msg);

  ptr = (char *) msg;
  if (recv_perms(p, channel_id, ptr, &sp->perms) < 0) {
    sp->wrote_errors = TRUE;
    return 1;
  }

  ptr = strchr(ptr, ' ');
  if (ptr == NULL) {
    pr_trace_msg(trace_channel, 3,
      "bad control message (undelimited mode)");
    write_confirm(p, channel_id, 1,
      pstrcat(p, sp->path, ": bad control message (undelimited mode)", NULL));
    sp->wrote_errors = TRUE;
    return 1;
  }

  /* Advance past the space delimiter. */
  ptr++;
  if (recv_filesz(p, channel_id, ptr, &sp->filesz) < 0) {
    sp->wrote_errors = TRUE;
    return 1;
  }

  ptr = strchr(ptr, ' ');
  if (ptr == NULL) {
    pr_trace_msg(trace_channel, 3,
      "bad control message (undelimited file size)");
    write_confirm(p, channel_id, 1,
      pstrcat(p, sp->path, ": bad control message (undelimited file size)",
      NULL));
    sp->wrote_errors = TRUE;
    return 1;
  }

  /* Advance past the space delimiter. */
  ptr++;
  if (recv_filename(p, channel_id, ptr, sp) < 0) {
    sp->wrote_errors = TRUE;
    return 1;
  }

  sp->recvd_finfo = TRUE;

  if (have_dir) {
    struct scp_path *parent_sp;

    pr_fs_clear_cache2(sp->filename);
    if (pr_fsio_stat(sp->filename, &st) < 0) {
      int xerrno = errno;

      /* We only want to create the directory if it doesn't already exist. */
      if (xerrno == ENOENT) {
        pr_trace_msg(trace_channel, 5, "creating directory '%s'", sp->filename);

        /* XXX Dispatch a C_MKD command here?  Should <Limit MKD> apply to
         * recursive directory uploads via SCP?
         */

        pr_fs_clear_cache2(sp->filename);
        if (pr_fsio_smkdir(p, sp->filename, 0777, (uid_t) -1, (gid_t) -1) < 0) {
          xerrno = errno;

          (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
            "scp: error creating directory '%s': %s", sp->filename,
            strerror(xerrno));
          write_confirm(p, channel_id, 1,
            pstrcat(p, sp->filename, ": ", strerror(xerrno), NULL));
          sp->wrote_errors = TRUE;

          errno = xerrno;
          return 1;
        }

        sftp_misc_chown_path(p, sp->filename);

      } else {
        (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
          "scp: error checking directory '%s': %s", sp->filename,
          strerror(xerrno));
        write_confirm(p, channel_id, 1,
          pstrcat(p, sp->filename, ": ", strerror(xerrno), NULL));
        sp->wrote_errors = TRUE;

        errno = xerrno;
        return 1;
      }

    } else {
      /* Make sure that the path actually is a directory. */
      if (!S_ISDIR(st.st_mode)) {
        pr_trace_msg(trace_channel, 2, "error handling '%s': %s",
          sp->best_path, strerror(ENOTDIR));
        write_confirm(p, channel_id, 1,
          pstrcat(p, sp->filename, ": ", strerror(ENOTDIR), NULL));
        sp->wrote_errors = TRUE;
        return 1;
      }
    }

    /* At this point, the info in sp is for the parent directory; we can
     * now expect to receive info for the files/directories contained by
     * this parent directory.
     *
     * So we create a new struct scp_path for this parent directory, copy
     * the relevant bits, push it onto the stack, and clear sp for the
     * incoming path.
     */

    parent_sp = pcalloc(scp_pool, sizeof(struct scp_path));
    parent_sp->orig_path = pstrdup(scp_pool, sp->orig_path);
    parent_sp->path = pstrdup(scp_pool, sp->filename);
    parent_sp->filename = pstrdup(scp_pool, sp->filename);
    parent_sp->best_path = pstrdup(scp_pool, sp->best_path);

    /* Copy any timeinfo as well. */
    parent_sp->times[0].tv_sec = sp->times[0].tv_sec;
    parent_sp->times[0].tv_usec = sp->times[0].tv_usec;
    parent_sp->times[1].tv_sec = sp->times[1].tv_sec;
    parent_sp->times[1].tv_usec = sp->times[1].tv_usec;
    parent_sp->recvd_timeinfo = sp->recvd_timeinfo;

    /* And the perms. */
    parent_sp->perms = sp->perms;
    parent_sp->parent_dir = sp->parent_dir;

    /* Reset sp, for re-use for the next file coming in. */
    reset_path(sp);

    /* Adjust sp->path to account for the directory we just received; the
     * next file coming in should be relative to the just-received directory.
     */
    sp->path = pstrdup(scp_pool, parent_sp->filename);
    sp->parent_dir = parent_sp;

    write_confirm(p, channel_id, 0, NULL);
    return 0;
  }

  pr_scoreboard_entry_update(session.pid,
    PR_SCORE_CMD, "%s", "scp upload", NULL, NULL);
  pr_scoreboard_entry_update(session.pid,
    PR_SCORE_CMD_ARG, "%s", sp->best_path, NULL, NULL);

  cmd = scp_cmd_alloc(p, C_STOR, sp->best_path);

  pr_fs_clear_cache2(sp->best_path);
  if (exists2(p, sp->best_path)) {
    if (pr_table_add(cmd->notes, "mod_xfer.file-modified",
        pstrdup(cmd->pool, "true"), 0) < 0) {
      if (errno != EEXIST) {
        pr_log_pri(PR_LOG_NOTICE,
          "notice: error adding 'mod_xfer.file-modified' note: %s",
          strerror(errno));
      }
    }

    sp->file_existed = TRUE;
  }

  if (pr_cmd_dispatch_phase(cmd, PRE_CMD, 0) < 0) {
    (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
      "scp upload to '%s' blocked by '%s' handler", sp->path,
      (char *) cmd->argv[0]);

    (void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
    (void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);

    write_confirm(p, channel_id, 1,
      pstrcat(p, sp->filename, ": ", strerror(EACCES), NULL));
    sp->wrote_errors = TRUE;

    return 1;
  }

  if (strcmp(sp->filename, cmd->arg) != 0) {
    sp->filename = cmd->arg;
    sp->best_path = dir_canonical_vpath(scp_pool, sp->filename);
  }

  if (session.xfer.xfer_type == STOR_HIDDEN) {
    hiddenstore_path = pr_table_get(cmd->notes, "mod_xfer.store-hidden-path",
      NULL);
  }

  if (!dir_check(p, cmd, G_WRITE, (char *) sp->best_path, NULL)) {
    (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
      "scp upload to '%s' blocked by <Limit> configuration", sp->best_path);

    (void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
    (void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);

    write_confirm(p, channel_id, 1,
      pstrcat(p, sp->filename, ": ", strerror(EACCES), NULL));
    sp->wrote_errors = TRUE;

    return 1;
  }

  /* We automatically add the O_NONBLOCK flag to the set of open() flags
   * in order to deal with writing to a FIFO whose other end may not be
   * open.  Then, after a successful open, we return the file to blocking
   * mode.
   */

  sp->fh = pr_fsio_open(hiddenstore_path ? hiddenstore_path : sp->best_path,
    O_WRONLY|O_CREAT|O_NONBLOCK|O_TRUNC);
  if (sp->fh == NULL) {
    int xerrno = errno;

    (void) pr_trace_msg("fileperms", 1, "%s, user '%s' (UID %s, GID %s): "
      "error opening '%s': %s", "scp upload", session.user,
      pr_uid2str(cmd->tmp_pool, session.uid), pr_gid2str(NULL, session.gid),
      hiddenstore_path ? hiddenstore_path : sp->best_path, strerror(xerrno));

    (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
      "scp: error opening '%s': %s",
      hiddenstore_path ? hiddenstore_path : sp->best_path, strerror(xerrno));

    (void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
    (void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);

    write_confirm(p, channel_id, 1,
      pstrcat(p, sp->filename, ": ", strerror(xerrno), NULL));
    sp->wrote_errors = TRUE;

    errno = xerrno;
    return 1;

  } else {
    off_t curr_offset;

    /* Stash the offset at which we're writing to this file. */
    curr_offset = pr_fsio_lseek(sp->fh, (off_t) 0, SEEK_CUR);
    if (curr_offset != (off_t) -1) {
      off_t *file_offset;

      file_offset = palloc(cmd->pool, sizeof(off_t));
      *file_offset = (off_t) curr_offset;
      (void) pr_table_add(cmd->notes, "mod_xfer.file-offset", file_offset,
        sizeof(off_t));
    }
  }

  if (hiddenstore_path) {
    sp->hiddenstore = TRUE;
  }

  if (pr_fsio_fstat(sp->fh, &st) < 0) {
    pr_trace_msg(trace_channel, 3,
      "fstat(2) error on '%s': %s", sp->fh->fh_path, strerror(errno));

  } else {
    /* The path in question might be a FIFO.  The FIFO case requires some
     * special handling, modulo any IgnoreFIFOs SFTPOption that might be in
     * effect.
     */
#ifdef S_ISFIFO
    if (S_ISFIFO(st.st_mode)) {
      if (sftp_opts & SFTP_OPT_IGNORE_FIFOS) {
        int xerrno = EPERM;

        (void) pr_fsio_close(sp->fh);
        sp->fh = NULL;

        (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
          "scp: error using FIFO '%s': %s (IgnoreFIFOs SFTPOption in effect)",
          hiddenstore_path ? hiddenstore_path : sp->best_path,
          strerror(xerrno));

        (void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
        (void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);

        write_confirm(p, channel_id, 1,
          pstrcat(p, sp->filename, ": ", strerror(xerrno), NULL));
        sp->wrote_errors = TRUE;

        errno = xerrno;
        return 1;
      }
    }
#endif /* S_ISFIFO */
  }

  if (pr_fsio_set_block(sp->fh) < 0) {
    pr_trace_msg(trace_channel, 3,
      "error setting fd %d (file '%s') as blocking: %s", sp->fh->fh_fd,
      sp->fh->fh_path, strerror(errno));
  }

  sftp_misc_chown_file(p, sp->fh);

  write_confirm(p, channel_id, 0, NULL);
  return 0;
}

static int recv_data(pool *p, uint32_t channel_id, struct scp_path *sp,
    unsigned char *data, uint32_t datalen) {
  uint32_t writelen;
  config_rec *c;
  off_t nbytes_max_store = 0;

  /* Check MaxStoreFileSize */
  c = find_config(get_dir_ctxt(p, sp->fh->fh_path), CONF_PARAM,
    "MaxStoreFileSize", FALSE);
  if (c != NULL) {
    nbytes_max_store = *((off_t *) c->argv[0]);
  }

  writelen = datalen;
  if (writelen > (sp->filesz - sp->recvlen)) {
    writelen = (uint32_t) (sp->filesz - sp->recvlen);
  }

  if (nbytes_max_store > 0) {
    if (sp->recvlen > nbytes_max_store) {
#if defined(EFBIG)
        int xerrno = EFBIG;
#elif defined(ENOSPC)
        int xerrno = ENOSPC;
#else
        int xerrno = EIO;
#endif

        pr_log_pri(PR_LOG_NOTICE, "MaxStoreFileSize (%" PR_LU " %s) reached: "
          "aborting transfer of '%s'", (pr_off_t) nbytes_max_store,
          nbytes_max_store != 1 ? "bytes" : "byte", sp->fh->fh_path);

        (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
          "error writing %lu bytes to '%s': %s "
          "(MaxStoreFileSize %" PR_LU " exceeded)", (unsigned long) writelen,
          sp->fh->fh_path, strerror(xerrno), (pr_off_t) nbytes_max_store);

        write_confirm(p, channel_id, 1,
          pstrcat(p, sp->filename, ": write error: ", strerror(xerrno), NULL));
        sp->wrote_errors = TRUE;

        /* Note that we do NOT explicitly close the filehandle here; we leave
         * that to the calling function, so that it can do e.g. other cleanup.
         */

        errno = xerrno;
        return 1;
    }
  }

  if (writelen > 0) {
    while (TRUE) {
      int res;

      /* XXX Do we need to properly handle short writes here? */
      res = pr_fsio_write(sp->fh, (char *) data, writelen);
      if ((uint32_t) res != writelen) {
        int xerrno = errno;

        if (xerrno == EINTR ||
            xerrno == EAGAIN) {
          pr_signals_handle();
          continue;
        }

        pr_trace_msg(trace_channel, 2, "error writing to '%s': %s",
          sp->best_path, strerror(xerrno));
        write_confirm(p, channel_id, 1,
          pstrcat(p, sp->filename, ": write error: ", strerror(xerrno), NULL));
        sp->wrote_errors = TRUE;

        /* Note that we do NOT explicitly close the filehandle here; we leave
         * that to the calling function, so that it can do e.g. other cleanup.
         */

        errno = xerrno;
        return 1;
      }

      break;
    }

    sp->recvlen += writelen;

    session.xfer.total_bytes += writelen;
    session.total_bytes += writelen;

    if (writelen < datalen) {
      if (data[writelen] != '\0') {
        pr_trace_msg(trace_channel, 2, "expected end-of-data marker when "
          "receiving file data, received '%c'", data[writelen]);
      }

      pr_throttle_pause(sp->recvlen, TRUE, 0);

      sp->recvd_data = TRUE;
      return 1;
    }

    pr_throttle_pause(sp->recvlen, FALSE, 0);

  } else {
    /* We should have just one extra end-of-stream byte. */
    if (data[writelen] != '\0') {
      pr_trace_msg(trace_channel, 2, "expected end-of-data marker when "
        "receiving file data, received '%c'", data[writelen]);
    }

    pr_throttle_pause(sp->recvlen, TRUE, 0);

    sp->recvd_data = TRUE;
    return 1;
  }

  return 0;
}

static int recv_eod(pool *p, uint32_t channel_id, struct scp_path *sp,
    unsigned char *buf, uint32_t buflen, unsigned char **remain,
    uint32_t *remainlen) {
  struct scp_path *parent_sp;
  unsigned char *data = NULL;
  uint32_t datalen = 0;
  int ok = TRUE, res;

  res = recv_ctl(channel_id, sp, buf, buflen, &data, &datalen);
  if (res != 1) {
    return res;
  }

  if (data[0] != 'E') {
    /* Not an EOD message; let someone else process this. */
    *remain = data;
    *remainlen = datalen;

    errno = EINVAL;
    return -1;
  }

  pr_trace_msg(trace_channel, 5, "'%s' control message: E", sp->path);

  parent_sp = sp->parent_dir;

  /* If the SFTPOption for ignoring perms for SCP uploads is set, then
   * skip the chmod on the upload file.
   */
  if (!(sftp_opts & SFTP_OPT_IGNORE_SCP_UPLOAD_PERMS)) {
    pr_trace_msg(trace_channel, 9, "setting perms %04o on directory '%s'",
      (unsigned int) parent_sp->perms, parent_sp->path);
    if (pr_fsio_chmod(parent_sp->path, parent_sp->perms) < 0) {
      int xerrno = errno;

      pr_trace_msg(trace_channel, 2, "error setting mode %04o on '%s': %s",
        (unsigned int) parent_sp->perms, parent_sp->path, strerror(xerrno));
      write_confirm(p, channel_id, 1,
        pstrcat(p, parent_sp->path, ": error setting mode: ", strerror(xerrno),
        NULL));
      parent_sp->wrote_errors = TRUE;
      ok = FALSE;
    }

  } else {
    pr_trace_msg(trace_channel, 7, "SFTPOption 'IgnoreSCPUploadPerms' "
      "configured, ignoring perms sent by client");
  }

  if (parent_sp->recvd_timeinfo) {
    pr_trace_msg(trace_channel, 9, "setting times on directory '%s'",
      parent_sp->filename);

    /* If the SFTPOption for ignoring times for SCP uploads is set, then
     * skip the utimes on the upload file.
     */
    if (!(sftp_opts & SFTP_OPT_IGNORE_SCP_UPLOAD_TIMES)) {
      if (pr_fsio_utimes(parent_sp->filename, parent_sp->times) < 0) {
        int xerrno = errno;

        pr_trace_msg(trace_channel, 2,
          "error setting atime %lu, mtime %lu on '%s': %s",
          (unsigned long) sp->times[0].tv_sec,
          (unsigned long) sp->times[1].tv_sec, parent_sp->filename,
          strerror(xerrno));

        write_confirm(p, channel_id, 1,
          pstrcat(p, parent_sp->filename, ": error setting times: ",
          strerror(xerrno), NULL));
        parent_sp->wrote_errors = TRUE;
        ok = FALSE;
      }

    } else {
      pr_trace_msg(trace_channel, 7, "SFTPOption 'IgnoreSCPUploadTimes' "
        "configured, ignoring times sent by client");
    }
  }

  if (ok) {
    write_confirm(p, channel_id, 0, NULL);
  }

  return 1;
}

/* Return 1 when we should skip to the next path in the list, either because
 * we have received all the data for this path, or because we can never
 * receive it (due to some error).
 */
static int recv_path(pool *p, uint32_t channel_id, struct scp_path *sp,
    unsigned char *data, uint32_t datalen) {
  int res;
  cmd_rec *cmd = NULL;
  char *curr_path = NULL;

  if (!sp->checked_errors) {
    res = recv_errors(p, channel_id, sp, data, datalen);
    if (res == 1) {
      return 1;
    }
  }

  if (!sp->have_mode) {
    struct stat st;

    pr_fs_clear_cache2(sp->path);
    res = pr_fsio_stat(sp->path, &st);
    if (res == 0) {
      sp->st_mode = st.st_mode;
      sp->have_mode = TRUE;
    }

    if (scp_opts & SFTP_SCP_OPT_DIR) {
      /* If the path should be a directory, stat it and make sure that
       * is the case.  If not, we have a problem.
       */
      if (res == 0) {
        if (!S_ISDIR(st.st_mode)) {
          write_confirm(p, channel_id, 1,
            pstrcat(p, sp->path, ": ", strerror(ENOTDIR), NULL));
          sp->wrote_errors = TRUE;
          return 1;
        }

      } else {
        write_confirm(p, channel_id, 1,
          pstrcat(p, sp->path, ": ", strerror(errno), NULL));
        sp->wrote_errors = TRUE;
        return 1;
      }

    } else {
      char *ptr;

      /* If the given path contains a directory component, make sure that the
       * directory exists.
       */
      ptr = strrchr(sp->path, '/');
      if (ptr != NULL) {
        *ptr = '\0';

        pr_fs_clear_cache2(sp->path);
        res = pr_fsio_stat(sp->path, &st);
        *ptr = '/';

        if (res < 0) {
          write_confirm(p, channel_id, 1,
            pstrcat(p, sp->path, ": ", strerror(errno), NULL));
          sp->wrote_errors = TRUE;
          return 1;
        }
      }
    }
  }

  /* Check for end-of-directory control messages under the following
   * conditions:
   *
   * 1. We can handle an end-of-directory marker, i.e. sp->parent_dir is
   *    not null.
   * 2. We have not already received any file info messages for this path.
   * 3. We have not already received any data for this path.
   */
  if (sp->parent_dir != NULL &&
      sp->recvd_finfo == FALSE &&
      (sp->recvlen == 0 || sp->recvd_data)) {
    unsigned char *remain = NULL;
    uint32_t remainlen = 0;

    res = recv_eod(p, channel_id, sp, data, datalen, &remain, &remainlen);
    if (res == 0) {
      return res;
    }

    if (res == 1) {
      struct scp_path *parent_dir = NULL;

      if (sp->parent_dir != NULL) {
        parent_dir = sp->parent_dir->parent_dir;
      }

      if (parent_dir != NULL) {
        pr_trace_msg(trace_channel, 18,
          "received EOD, resetting path from '%s' to '%s'", sp->path,
          parent_dir->path);
        sp->path = parent_dir->path;

      } else {
        if (sp->orig_path != NULL) {
          sp->path = pstrdup(scp_pool, sp->orig_path);
        }

        pr_trace_msg(trace_channel, 18,
          "received EOD, no parent found for '%s'", sp->path);
      }

      sp->parent_dir = parent_dir;

      /* We return 1 here, and the caller will call reset_path() on the same
       * sp pointer.  That's OK, since reset_path() does NOT change sp->path or
       * sp->parent_dir, which is what we are most concerned with here.
       */
      return 1;
    }

    data = remain;
    datalen = remainlen;
  }

  if ((scp_opts & SFTP_SCP_OPT_PRESERVE) &&
      !sp->recvd_timeinfo &&
      !sp->recvd_finfo) {
    unsigned char *remain = NULL;
    uint32_t remainlen = 0;

    /* It possible that this is not a timeinfo message; we need to be
     * prepared for this.  PuTTY, for example, when recursively uploading
     * a directory with the -p (preserve time) option enabled, does NOT
     * send the timeinfo message, whereas OpenSSH's scp(1) does.
     */
    res = recv_timeinfo(p, channel_id, sp, data, datalen, &remain, &remainlen);
    if (res < 0) {
      data = remain;
      datalen = remainlen;

    } else {
      return res;
    }
  }

  if (!sp->recvd_finfo) {
    return recv_finfo(p, channel_id, sp, data, datalen);
  }

  if (!sp->recvd_data &&
      sp->recvlen != sp->filesz) {
    if (cmd == NULL) {
      cmd = scp_cmd_alloc(p, C_STOR, sp->best_path);

      if (pr_table_add(cmd->notes, "mod_xfer.store-path",
          pstrdup(p, sp->best_path), 0) < 0) {
        if (errno != EEXIST) {
          (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
            "error adding 'mod_xfer.store-path for SCP upload: %s",
            strerror(errno));
        }
      }
    }

    pr_throttle_init(cmd);

    /* recv_data() indicates that it has received all of the data, including
     * the end-of-data marker, by returning 1.  If that happens, we need
     * to continue one to the end-of-path processing.
     */
    res = recv_data(p, channel_id, sp, data, datalen);
    if (res != 1) {
      return res;
    }
  }

  if (sp->wrote_errors == FALSE) {
    /* The uploaded file may be smaller than an existing file; call
     * pr_fsio_truncate() to ensure proper file size.
     */
    if (S_ISREG(sp->st_mode)) {
      pr_trace_msg(trace_channel, 9, "truncating file '%s' to %" PR_LU " bytes",
        sp->fh->fh_path, (pr_off_t) sp->filesz);

      if (pr_fsio_ftruncate(sp->fh, sp->filesz) < 0) {
        int xerrno = errno;

        pr_trace_msg(trace_channel, 2, "error truncating '%s' to %" PR_LU
          " bytes: %s", sp->best_path, (pr_off_t) sp->filesz, strerror(xerrno));

        write_confirm(p, channel_id, 1,
          pstrcat(p, sp->filename, ": error truncating file: ",
          strerror(xerrno), NULL));
        sp->wrote_errors = TRUE;
      }
    }
  }

  if (sp->wrote_errors == FALSE) {
    /* If the SFTPOption for ignoring perms for SCP uploads is set, then
     * skip the chmod on the upload file.
     */
    if (!(sftp_opts & SFTP_OPT_IGNORE_SCP_UPLOAD_PERMS)) {
      pr_trace_msg(trace_channel, 9, "setting perms %04o on file '%s'",
        (unsigned int) sp->perms, sp->fh->fh_path);

      if (pr_fsio_fchmod(sp->fh, sp->perms) < 0) {
        int xerrno = errno;

        pr_trace_msg(trace_channel, 2, "error setting mode %04o on '%s': %s",
          (unsigned int) sp->perms, sp->best_path, strerror(xerrno));

        write_confirm(p, channel_id, 1,
          pstrcat(p, sp->filename, ": error setting mode: ", strerror(xerrno),
          NULL));
        sp->wrote_errors = TRUE;
      }

    } else {
      pr_trace_msg(trace_channel, 7, "SFTPOption 'IgnoreSCPUploadPerms' "
        "configured, ignoring perms sent by client");
    }
  }

  if (sp->fh) {
    curr_path = pstrdup(scp_pool, sp->fh->fh_path);

    /* Set session.curr_cmd, for any FSIO callbacks that might be interested. */
    session.curr_cmd = C_STOR;

    res = pr_fsio_close(sp->fh);
    if (res < 0) {
      int xerrno = errno;

      (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
        "scp: error closing '%s': %s", sp->best_path, strerror(xerrno));

      write_confirm(p, channel_id, 1,
        pstrcat(p, sp->filename, ": ", strerror(xerrno), NULL));
      sp->wrote_errors = TRUE;
    }

    sp->fh = NULL;
  }

  if (sp->hiddenstore == TRUE &&
      curr_path != NULL) {
    if (sp->wrote_errors == TRUE) {
      /* There was an error writing this HiddenStores file; be sure to clean
       * things up.
       */
      pr_trace_msg(trace_channel, 8, "deleting HiddenStores path '%s'",
        curr_path);

      if (pr_fsio_unlink(curr_path) < 0) {
        if (errno != ENOENT) {
          pr_log_debug(DEBUG0, MOD_SFTP_VERSION
            ": error deleting HiddenStores file '%s': %s", curr_path,
            strerror(errno));
        }
      }

    } else {
      /* This is a HiddenStores file, and needs to be renamed to the real
       * path (i.e. sp->best_path).
       */
      pr_trace_msg(trace_channel, 8,
        "renaming HiddenStores path '%s' to '%s'", curr_path, sp->best_path);

      res = pr_fsio_rename(curr_path, sp->best_path);
      if (res < 0) {
        int xerrno = errno;

        pr_log_pri(PR_LOG_WARNING, "Rename of %s to %s failed: %s",
          curr_path, sp->best_path, strerror(xerrno));

        (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
          "renaming of HiddenStore path '%s' to '%s' failed: %s",
          curr_path, sp->best_path, strerror(xerrno));

        if (pr_fsio_unlink(curr_path) < 0) {
          pr_trace_msg(trace_channel, 1,
            "error deleting HiddenStores file '%s': %s", curr_path,
            strerror(errno));
        }
      }
    }
  }

  /* After receiving all the data and metadata, we need to make sure that
   * the requested times and mode/perms are enforced on the uploaded file.
   */
  if (sp->recvd_timeinfo) {
    pr_trace_msg(trace_channel, 9, "setting times on file '%s'", sp->filename);

    /* If the SFTPOption for ignoring times for SCP uploads is set, then
     * skip the utimes on the upload file.
     */
    if (!(sftp_opts & SFTP_OPT_IGNORE_SCP_UPLOAD_TIMES)) {
      if (pr_fsio_utimes(sp->filename, sp->times) < 0) {
        int xerrno = errno;

        pr_trace_msg(trace_channel, 2,
          "error setting atime %lu, mtime %lu on '%s': %s",
          (unsigned long) sp->times[0].tv_sec,
          (unsigned long) sp->times[1].tv_sec, sp->best_path, strerror(xerrno));

        write_confirm(p, channel_id, 1,
          pstrcat(p, sp->filename, ": error setting times: ", strerror(xerrno),
          NULL));
        sp->wrote_errors = TRUE;
      }

    } else {
      pr_trace_msg(trace_channel, 7, "SFTPOption 'IgnoreSCPUploadTimes' "
        "configured, ignoring times sent by client");
    }
  }

  if (!sp->wrote_errors) {
    /* We only send this if there were no end-of-path handling errors. */
    write_confirm(p, channel_id, 0, NULL);

    if (cmd == NULL) {
      cmd = scp_cmd_alloc(p, C_STOR, sp->best_path);

      if (pr_table_add(cmd->notes, "mod_xfer.store-path",
          pstrdup(p, sp->best_path), 0) < 0) {
        if (errno != EEXIST) {
          (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
            "error adding 'mod_xfer.store-path: %s", strerror(errno));
        }
      }
    }

    if (sp->file_existed) {
      if (pr_table_add(cmd->notes, "mod_xfer.file-modified",
          pstrdup(cmd->pool, "true"), 0) < 0) {
        if (errno != EEXIST) {
          pr_log_pri(PR_LOG_NOTICE,
            "notice: error adding 'mod_xfer.file-modified' note: %s",
            strerror(errno));
        }
      }
    }

    session.xfer.path = sftp_misc_vroot_abs_path(session.xfer.p,
      session.xfer.path, FALSE);
    (void) pr_cmd_dispatch_phase(cmd, POST_CMD, 0);
    (void) pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);

  } else {
    if (cmd == NULL) {
      cmd = scp_cmd_alloc(p, C_STOR, sp->best_path);

      if (pr_table_add(cmd->notes, "mod_xfer.store-path",
          pstrdup(p, sp->best_path), 0) < 0) {
        if (errno != EEXIST) {
          (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
            "error adding 'mod_xfer.store-path: %s", strerror(errno));
        }
      }
    }

    if (sp->file_existed) {
      if (pr_table_add(cmd->notes, "mod_xfer.file-modified",
          pstrdup(cmd->pool, "true"), 0) < 0) {
        pr_log_pri(PR_LOG_NOTICE,
          "notice: error adding 'mod_xfer.file-modified' note: %s",
          strerror(errno));
      }
    }

    (void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
    (void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
  }

  return 1;
}

/* Functions for sending files to the client. */

static int send_timeinfo(pool *p, uint32_t channel_id, struct scp_path *sp,
    struct stat *st) {
  int res;
  unsigned char ctrl_msg[64];
  size_t ctrl_msglen;

  memset(ctrl_msg, '\0', sizeof(ctrl_msg));

  /* The field of this message are:
   *
   *  T       (time info)
   *  number  (mtime, in secs)
   *  0       (future proof field for sending mtime usecs)
   *  number  (atime, in secs)
   *  0       (future proof field for sending atime usecs)
   */

  pr_snprintf((char *) ctrl_msg, sizeof(ctrl_msg), "T%lu 0 %lu 0",
    (unsigned long) (st->st_mtime > 0 ? st->st_mtime : 0),
    (unsigned long) (st->st_atime > 0 ? st->st_atime : 0));

  pr_trace_msg(trace_channel, 3, "sending '%s' T (timestamps): %s", sp->path,
    ctrl_msg);

  ctrl_msg[strlen((char *) ctrl_msg)] = '\n';
  ctrl_msglen = strlen((char *) ctrl_msg);

  need_confirm = TRUE;

  res = sftp_channel_write_data(p, channel_id, ctrl_msg, ctrl_msglen);
  if (res < 0)
    return -1;

  sp->sent_timeinfo = TRUE;
  return 0;
}

static int send_dirinfo(pool *p, uint32_t channel_id, struct scp_path *sp,
    struct stat *st) {
  int res;
  unsigned char ctrl_msg[1536];
  size_t ctrl_msglen;
  char *tmp;

  /* We need to find the last path component, if any; no path separators
   * in the control messages.
   */
  tmp = strrchr(sp->path, '/');
  if (tmp == NULL) {
    tmp = sp->path;

  } else {
    tmp++;
  }

  memset(ctrl_msg, '\0', sizeof(ctrl_msg));
  pr_snprintf((char *) ctrl_msg, sizeof(ctrl_msg), "D%04o 0 %.1024s",
    (unsigned int) (st->st_mode & SFTP_SCP_ST_MODE_MASK), tmp);

  pr_trace_msg(trace_channel, 3, "sending '%s' D (directory): %s", sp->path,
    ctrl_msg);

  ctrl_msg[strlen((char *) ctrl_msg)] = '\n';
  ctrl_msglen = strlen((char *) ctrl_msg);

  need_confirm = TRUE;

  res = sftp_channel_write_data(p, channel_id, ctrl_msg, ctrl_msglen);
  if (res < 0) {
    return -1;
  }

  sp->sent_dirinfo = TRUE;
  return 0;
}

static int send_finfo(pool *p, uint32_t channel_id, struct scp_path *sp,
    struct stat *st) {
  int res;
  unsigned char ctrl_msg[1536];
  size_t ctrl_msglen;
  char *tmp;

  /* We need to find the last path component, if any; no path separators
   * in the control messages.
   */
  tmp = strrchr(sp->path, '/');
  if (tmp == NULL) {
    tmp = sp->path;

  } else {
    tmp++;
  }

  memset(ctrl_msg, '\0', sizeof(ctrl_msg));
  pr_snprintf((char *) ctrl_msg, sizeof(ctrl_msg), "C%04o %" PR_LU " %.1024s",
    (unsigned int) (st->st_mode & SFTP_SCP_ST_MODE_MASK),
    (pr_off_t) st->st_size, tmp);

  pr_trace_msg(trace_channel, 3, "sending '%s' C (info): %s", sp->path,
    ctrl_msg);

  ctrl_msg[strlen((char *) ctrl_msg)] = '\n';
  ctrl_msglen = strlen((char *) ctrl_msg);

  need_confirm = TRUE;

  res = sftp_channel_write_data(p, channel_id, ctrl_msg, ctrl_msglen);
  if (res < 0)
    return -1;

  sp->sent_finfo = TRUE;
  return 0;
}

static int send_data(pool *p, uint32_t channel_id, struct scp_path *sp,
    struct stat *st) {
  unsigned char *chunk;
  size_t chunksz;

  /* Include space for one more character, i.e. for the terminating NUL
   * character that indicates the last chunk of the file.
   */
  chunksz = pr_config_get_server_xfer_bufsz(PR_NETIO_IO_WR) + 1;
  chunk = palloc(p, chunksz);

  /* Keep sending chunks until we have sent the entire file, or until the
   * channel window closes.
   */
  while (TRUE) {
    int res, chunklen;

    pr_signals_handle();

    if (S_ISREG(st->st_mode)) {
      /* Seek to where we last left off with this file. */
      if (pr_fsio_lseek(sp->fh, sp->sentlen, SEEK_SET) < 0) {
        (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
          "error seeking to offset %" PR_LU " in '%s': %s",
          (pr_off_t) sp->sentlen, sp->path, strerror(errno));
        return 1;
      }

      pr_trace_msg(trace_channel, 15, "at %.2f%% (%" PR_LU " of %" PR_LU
        " bytes) of '%s'",
        (float) (((float) sp->sentlen / (float) st->st_size) * 100),
        (pr_off_t) sp->sentlen, (pr_off_t) st->st_size, sp->path);
    }

    chunklen = pr_fsio_read(sp->fh, (char *) chunk, chunksz - 1);
    if (chunklen < 0) {
      (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
        "error reading from '%s': %s", sp->path, strerror(errno));
      return 1;
    }

    session.xfer.total_bytes += chunklen;
    session.total_bytes += chunklen;

    /* If this was the last chunk of the file, write one more space
     * character.
     */
    if (sp->sentlen + chunklen == st->st_size) {
      chunk[chunklen++] = '\0';
      need_confirm = TRUE;

      pr_throttle_pause(sp->sentlen, TRUE, 0);

    } else {
      pr_throttle_pause(sp->sentlen, FALSE, 0);
    }

    pr_trace_msg(trace_channel, 3, "sending '%s' data (%lu bytes)", sp->path,
      need_confirm ? (unsigned long) (chunklen - 1) : (unsigned long) chunklen);

    res = sftp_channel_write_data(p, channel_id, chunk, chunklen);
    if (res < 0) {
      return 1;
    }

    /* If our channel window has closed, try handling some packets; hopefully
     * some of them are WINDOW_ADJUST messages.
     *
     * XXX I wonder if this can be more efficient by waiting until we
     * have a certain amount of data buffered up (N * transfer data size?)
     * AND the window is closed before handling incoming packets?  That way
     * we can handle more WINDOW_ADJUSTS at a whack, at the cost of buffering
     * more data in memory.  Hmm.
     *
     * We also need to watch for when rekeying is occurring; handle packets
     * until that state clears.
     */
    while ((sftp_sess_state & SFTP_SESS_STATE_REKEYING) ||
           sftp_channel_get_windowsz(channel_id) == 0) {
      pr_signals_handle();

      if (sftp_ssh2_packet_process(sftp_pool) < 0) {
        return 1;
      }
    }

    sp->sentlen += chunklen;
    if (sp->sentlen >= st->st_size) {
      sp->sent_data = TRUE;
      break;
    }
  }

  return 0;
}

static int send_dir(pool *p, uint32_t channel_id, struct scp_path *sp,
    struct stat *st) {
  struct dirent *dent;
  struct stat link_st;
  int res = 0;

  if (sp->dirh == NULL) {
    sp->dirh = pr_fsio_opendir(sp->path);
    if (sp->dirh == NULL) {
      (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
        "error reading directory '%s': %s", sp->path, strerror(errno));
      return -1;
    }

    /* If we're a directory, send a D control message. */
    if (!sp->sent_dirinfo) {
      return send_dirinfo(p, channel_id, sp, st);
    }
  }

  /* If we were already in the middle of sending a path from this
   * directory, continue with it.  Otherwise, read the next dent from the
   * directory handle.
   */

  if (sp->dir_spi) {
    res = send_path(p, channel_id, sp->dir_spi);
    if (res <= 0) {
      return res;
    }

    /* Clear out any transfer-specific data. */
    if (session.xfer.p != NULL) {
      destroy_pool(session.xfer.p);
    }
    memset(&session.xfer, 0, sizeof(session.xfer));

    sp->dir_spi = NULL;
    return 0;
  }

  while ((dent = pr_fsio_readdir(sp->dirh)) != NULL) {
    struct scp_path *spi;
    size_t pathlen;

    pr_signals_handle();

    /* Skip "." and "..". */
    if (strncmp(dent->d_name, ".", 2) == 0 ||
        strncmp(dent->d_name, "..", 3) == 0) {
      continue;
    }

    /* Add these to the list of paths that need to be sent. */
    spi = pcalloc(scp_pool, sizeof(struct scp_path));
    spi->path = pdircat(scp_pool, sp->path, dent->d_name, NULL);
    pathlen = strlen(spi->path);

    /* Trim any trailing path separators.  It's important. */
    while (pathlen > 1 &&
           spi->path[pathlen-1] == '/') {
      pr_signals_handle();
      spi->path[pathlen-1] = '\0';
      pathlen--;
    }

    spi->best_path = dir_canonical_vpath(scp_pool, spi->path);

    pr_fs_clear_cache2(spi->best_path);
    if (pr_fsio_lstat(spi->best_path, &link_st) == 0) {
      if (S_ISLNK(link_st.st_mode)) {
        char link_path[PR_TUNABLE_PATH_MAX];
        int len;

        memset(link_path, '\0', sizeof(link_path));
        len = dir_readlink(scp_pool, spi->best_path, link_path,
          sizeof(link_path)-1, PR_DIR_READLINK_FL_HANDLE_REL_PATH);
        if (len > 0) {
          link_path[len] = '\0';
          spi->best_path = pstrdup(scp_pool, link_path);
        }
      }
    }

    if (pathlen > 0) {
      sp->dir_spi = spi;

      res = send_path(p, channel_id, spi);
      if (res == 1) {
        /* Clear out any transfer-specific data. */
        if (session.xfer.p != NULL) {
          destroy_pool(session.xfer.p);
        }

        memset(&session.xfer, 0, sizeof(session.xfer));
      }

      return res;
    }
  }

  if (sp->dirh != NULL) {
    pr_fsio_closedir(sp->dirh);
    sp->dirh = NULL;

    /* Send end-of-directory control message */

    need_confirm = TRUE;
    res = sftp_channel_write_data(p, channel_id, (unsigned char *) "E\n", 2);
    if (res < 0) {
      return res;
    }
  }

  return 1;
}

/* Return 1 when the we should skip to the next path in the list, either
 * because we have sent all the data for this path, or because we can
 * never send it (due to some error).
 */
static int send_path(pool *p, uint32_t channel_id, struct scp_path *sp) {
  int res, is_file = FALSE;
  struct stat st;
  cmd_rec *cmd = NULL;

  if (sp->sent_data == TRUE) {
    /* Already sent everything for this path. */
    return 1;
  }

  pr_scoreboard_entry_update(session.pid,
    PR_SCORE_CMD, "%s", "scp download", NULL, NULL);
  pr_scoreboard_entry_update(session.pid,
    PR_SCORE_CMD_ARG, "%s", sp->path, NULL, NULL);

  cmd = scp_cmd_alloc(p, C_RETR, sp->path);
  session.curr_cmd_rec = cmd;

  /* First, dispatch the command to the PRE_CMD handlers.  They might,
   * for example, change the path.
   */
  if (sp->fh == NULL) {
    /* Note, however, that SCP also has to deal with directories, which will
     * be blocked by the PRE_CMD RETR handler in mod_xfer.
     */

    if (pr_cmd_dispatch_phase(cmd, PRE_CMD, 0) < 0) {
      int xerrno = errno;

      if (xerrno != EISDIR) {
        (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
          "scp download of '%s' blocked by '%s' handler", sp->path,
          (char *) cmd->argv[0]);

        (void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
        (void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);

        destroy_pool(cmd->pool);
        session.curr_cmd_rec = NULL;

        write_confirm(p, channel_id, 1,
          pstrcat(p, sp->path, ": ", strerror(xerrno), NULL));
        sp->wrote_errors = TRUE;
        return 1;
      }
    }

    if (strcmp(sp->path, cmd->arg) != 0) {
      sp->path = pstrdup(scp_session->pool, cmd->arg);
    }
  }

  if (pr_table_add(cmd->notes, "mod_xfer.retr-path",
      pstrdup(cmd->pool, sp->path), 0) < 0) {
    if (errno != EEXIST) {
      (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
        "error adding 'mod_xfer.retr-path' for SCP download: %s",
        strerror(errno));
    }
  }

  pr_fs_clear_cache2(sp->path);
  if (pr_fsio_lstat(sp->path, &st) == 0) {
    if (S_ISLNK(st.st_mode)) {
      char link_path[PR_TUNABLE_PATH_MAX];
      int len;

      memset(link_path, '\0', sizeof(link_path));
      len = dir_readlink(scp_pool, sp->path, link_path, sizeof(link_path)-1,
        PR_DIR_READLINK_FL_HANDLE_REL_PATH);
      if (len > 0) {
        link_path[len] = '\0';
        sp->path = pstrdup(scp_pool, link_path);
      }
    }
  }

  if (pr_fsio_stat(sp->path, &st) < 0) {
    int xerrno = errno;

    (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
      "error stat'ing '%s': %s", sp->path, strerror(xerrno));

    if (sp->fh != NULL) {
      /* Set session.curr_cmd, for any FSIO callbacks that might be
       * interested.
       */
      session.curr_cmd = C_RETR;

      pr_fsio_close(sp->fh);
      sp->fh = NULL;

      (void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
      (void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
    }

    destroy_pool(cmd->pool);
    session.curr_cmd_rec = NULL;

    write_confirm(p, channel_id, 1,
      pstrcat(p, sp->path, ": ", strerror(xerrno), NULL));
    sp->wrote_errors = TRUE;
    return 1;
  }

  /* The path in question might be a file, a directory, or a FIFO.  The FIFO
   * case requires some special handling, modulo any IgnoreFIFOs SFTPOption
   * that might be in effect.
   */
  if (S_ISREG(st.st_mode)) {
    is_file = TRUE;

  } else {
#ifdef S_ISFIFO
    if (S_ISFIFO(st.st_mode)) {
      is_file = TRUE;

      if (sftp_opts & SFTP_OPT_IGNORE_FIFOS) {
        is_file = FALSE;
      }
    }
#endif /* S_ISFIFO */
  }

  if (is_file == FALSE) {
    if (S_ISDIR(st.st_mode)) {
      if (scp_opts & SFTP_SCP_OPT_RECURSE) {
        res = send_dir(p, channel_id, sp, &st);
        destroy_pool(cmd->pool);
        session.curr_cmd_rec = NULL;
        return res;
      }

      (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
        "cannot send directory '%s' (no -r option)", sp->path);

      (void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
      (void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);

      destroy_pool(cmd->pool);
      session.curr_cmd_rec = NULL;

      write_confirm(p, channel_id, 1,
        pstrcat(p, sp->path, ": ", strerror(EPERM), NULL));
      sp->wrote_errors = TRUE;
      return 1;
    }

    (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
      "cannot send '%s': Not a regular file", sp->path);

    (void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
    (void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);

    destroy_pool(cmd->pool);
    session.curr_cmd_rec = NULL;

    write_confirm(p, channel_id, 1,
      pstrcat(p, sp->path, ": ", strerror(EPERM), NULL));
    sp->wrote_errors = TRUE;
    return 1;
  }

  if (sp->fh == NULL) {
    sp->best_path = dir_canonical_vpath(scp_pool, sp->path);

    if (!dir_check(p, cmd, G_READ, sp->best_path, NULL)) {
      (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
        "scp download of '%s' blocked by <Limit> configuration", sp->best_path);

      (void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
      (void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);

      destroy_pool(cmd->pool);
      session.curr_cmd_rec = NULL;

      write_confirm(p, channel_id, 1,
        pstrcat(p, sp->path, ": ", strerror(EACCES), NULL));
      sp->wrote_errors = TRUE;
      return 1;
    }

    sp->fh = pr_fsio_open(sp->best_path, O_RDONLY|O_NONBLOCK);
    if (sp->fh == NULL) {
      int xerrno = errno;

      (void) pr_trace_msg("fileperms", 1, "%s, user '%s' (UID %s, GID %s): "
        "error opening '%s': %s", "scp download", session.user,
        pr_uid2str(cmd->tmp_pool, session.uid), pr_gid2str(NULL, session.gid),
        sp->best_path, strerror(xerrno));

      (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
        "error reading '%s': %s", sp->best_path, strerror(xerrno));

      (void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
      (void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);

      destroy_pool(cmd->pool);
      session.curr_cmd_rec = NULL;

      write_confirm(p, channel_id, 1,
        pstrcat(p, sp->path, ": ", strerror(xerrno), NULL));
      sp->wrote_errors = TRUE;

      errno = xerrno;
      return 1;

    } else {
      off_t curr_offset;

      /* Stash the offset at which we're reading from this file. */
      curr_offset = pr_fsio_lseek(sp->fh, (off_t) 0, SEEK_CUR);
      if (curr_offset != (off_t) -1) {
        off_t *file_offset;

        file_offset = palloc(cmd->pool, sizeof(off_t));
        *file_offset = (off_t) curr_offset;
        (void) pr_table_add(cmd->notes, "mod_xfer.file-offset", file_offset,
          sizeof(off_t));
      }
    }
  }

  if (pr_fsio_set_block(sp->fh) < 0) {
    pr_trace_msg(trace_channel, 3,
      "error setting fd %d (file '%s') as blocking: %s", sp->fh->fh_fd,
      sp->fh->fh_path, strerror(errno));
  }

  if (session.xfer.p == NULL) {
    session.xfer.p = pr_pool_create_sz(scp_pool, 64);
    session.xfer.path = pstrdup(session.xfer.p, sp->best_path);
    memset(&session.xfer.start_time, 0, sizeof(session.xfer.start_time));
    gettimeofday(&session.xfer.start_time, NULL);
    session.xfer.direction = PR_NETIO_IO_WR;
  }

  /* If the PRESERVE flag is set, then we need to send a T control message
   * that includes the file timestamps.
   */
  if ((scp_opts & SFTP_SCP_OPT_PRESERVE) &&
      sp->sent_timeinfo == FALSE) {
    res = send_timeinfo(p, channel_id, sp, &st);
    if (res == 1) {
      (void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
      (void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
    }

    destroy_pool(cmd->pool);
    session.curr_cmd_rec = NULL;
    return res;
  }

  if (sp->sent_finfo == FALSE) {
    res = send_finfo(p, channel_id, sp, &st);
    if (res == 1) {
      (void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
      (void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
    }

    destroy_pool(cmd->pool);
    session.curr_cmd_rec = NULL;
    return res;
  }

  if (sp->sent_data == FALSE) {
    pr_throttle_init(cmd);

    res = send_data(p, channel_id, sp, &st);
    if (res == 1) {
      (void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
      (void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);

      destroy_pool(cmd->pool);
      session.curr_cmd_rec = NULL;

      return res;
    }
  }

  pr_fsio_close(sp->fh);
  sp->fh = NULL;

  session.xfer.path = sftp_misc_vroot_abs_path(session.xfer.p,
    session.xfer.path, FALSE);
  (void) pr_cmd_dispatch_phase(cmd, POST_CMD, 0);
  (void) pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);

  destroy_pool(cmd->pool);
  session.curr_cmd_rec = NULL;

  return 1;
}

/* Main entry point */
int sftp_scp_handle_packet(pool *p, void *ssh2, uint32_t channel_id,
    unsigned char *data, uint32_t datalen) {
  int res = -1;
  struct ssh2_packet *pkt;

  scp_session = scp_get_session(channel_id);
  if (scp_session == NULL) {
    (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
      "no existing SCP session for channel ID %lu, rejecting request",
      (unsigned long) channel_id);
    return -1;
  }

  pkt = ssh2;

  /* This is a bit of a hack, for playing along better with mod_vroot,
   * which pays attention to the session.curr_phase value.
   *
   * I'm not sure which is better here, PRE_CMD vs CMD.  Let's go with
   * PRE_CMD for now.
   */
  session.curr_phase = PRE_CMD;

  if (pr_data_get_timeout(PR_DATA_TIMEOUT_NO_TRANSFER) > 0) {
    pr_timer_reset(PR_TIMER_NOXFER, ANY_MODULE);
  }

  if (pr_data_get_timeout(PR_DATA_TIMEOUT_STALLED) > 0) {
    pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
  }

  pr_response_set_pool(pkt->pool);

  if (need_confirm == TRUE) {
    /* Handle the confirmation/response from the client. */
    if (read_confirm(pkt, &data, &datalen) < 0) {
      return 1;
    }
  }

  if (scp_opts & SFTP_SCP_OPT_ISSRC) {
    struct scp_path **paths;

    pr_proctitle_set("%s - %s: scp download", session.user,
      session.proc_prefix);

    if (scp_session->path_idx == scp_session->paths->nelts) {
      /* Done sending our paths; need confirmation that the client received
       * all of them.
       */
      return 1;
    }

    paths = scp_session->paths->elts;

    if (scp_session->path_idx < scp_session->paths->nelts) {
      pr_signals_handle();

      res = send_path(pkt->pool, channel_id, paths[scp_session->path_idx]);
      if (res < 0) {
        return -1;
      }

      if (res == 1) {
        /* If send_path() returns 1, it means we've finished that path,
         * and are ready for another.
         */
        scp_session->path_idx++;

        /* Clear out any transfer-specific data. */
        if (session.xfer.p) {
          destroy_pool(session.xfer.p);
        }
        memset(&session.xfer, 0, sizeof(session.xfer));

        /* Make sure to clear the response lists of any cruft from previous
         * requests.
         */
        pr_response_clear(&resp_list);
        pr_response_clear(&resp_err_list);
      }
    }

    /* We would normally return 1 here, to indicate that we are done with
     * the transfer.  However, doing so indicates to the channel-handling
     * code that the channel is done, and should be closed.
     *
     * In the case of scp, though, we want the client to close the connection,
     * in order ensure that it has received all of the data (see Bug#3544).
     *
     * If we haven't sent data, but instead have sent an error, then we DO
     * want to return 1 here, since it will be us, not the client, which needs
     * to close the connection.
     */
    if (res == 1) {
      if (paths[scp_session->path_idx-1]->wrote_errors == TRUE) {
        return 1;
      }
    }

    return 0;

  } else if (scp_opts & SFTP_SCP_OPT_ISDST) {
    struct scp_path **paths;

    pr_proctitle_set("%s - %s: scp upload", session.user,
      session.proc_prefix);

    paths = scp_session->paths->elts;

    if (session.xfer.p == NULL) {
      session.xfer.p = pr_pool_create_sz(scp_pool, 64);
      session.xfer.path = pstrdup(session.xfer.p,
        paths[scp_session->path_idx]->path);
      memset(&session.xfer.start_time, 0, sizeof(session.xfer.start_time));
      gettimeofday(&session.xfer.start_time, NULL);
      session.xfer.direction = PR_NETIO_IO_RD;
    }

    res = recv_path(pkt->pool, channel_id, paths[scp_session->path_idx], data,
      datalen);
    if (res < 0) {
      return -1;
    }

    if (res == 1) {
      /* Clear out any transfer-specific data. */
      if (session.xfer.p) {
        destroy_pool(session.xfer.p);
      }
      memset(&session.xfer, 0, sizeof(session.xfer));

      /* Make sure to clear the response lists of any cruft from previous
       * requests.
       */
      pr_response_clear(&resp_list);
      pr_response_clear(&resp_err_list);

      /* Note: we don't increment path_idx here because when we're receiving
       * files (i.e. it's an SCP upload), we either receive a single file,
       * or a single (recursive) directory.  Therefore, there are not
       * multiple struct scp_path elements in the scp_session->paths array,
       * just one.
       */
      reset_path(paths[scp_session->path_idx]);
    }
  }

  return 0;
}

static char *scp_canonicalize_target(pool *p, char *target) {
  size_t target_len;

  target_len = strlen(target);

  /* Remove any enclosing shell quotations, e.g. single and double quotation
   * marks.  Some SCP clients (i.e. newer libssh2) will quote the paths,
   * assuming that the handling server (us) uses a shell to handle the
   * command.  Sigh.
   */

  if ((target[0] == '\'' &&
       target[target_len-1] == '\'') ||
      (target[0] == '"' &&
       target[target_len-1] == '"')) {
    target[target_len-1] = '\0';
    return pstrdup(p, target + 1);
  }

  return target;
}

int sftp_scp_set_params(pool *p, uint32_t channel_id, array_header *req) {
  register unsigned int i;
  int optc, use_glob = TRUE;
  char **reqargv, *target_path = NULL;
  const char *opts = "dfprtv";
  config_rec *c;
  struct scp_paths *paths;
  struct scp_path *sp;

  if (!(sftp_services & SFTP_SERVICE_FL_SCP)) {
    (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
      "%s", "'scp' exec request denied by Protocols config");
    errno = EPERM;
    return -1;
  }

  /* Possible options are:
   *
   *  -d (target should be a directory)
   *  -f (copying data from the server)
   *  -p (preserve times, mode using ctrl messages)
   *  -r (recursive)
   *  -t (copying data to the server)
   *  -v (verbose)
   */

  pr_getopt_reset();

  reqargv = (char **) req->elts;
  for (i = 0; i < req->nelts; i++) {
    if (reqargv[i] != NULL) {
      pr_trace_msg(trace_channel, 5, "reqargv[%u] = '%s'", i, reqargv[i]);
    }
  }

  c = find_config(main_server->conf, CONF_PARAM, "UseGlobbing", FALSE);
  if (c != NULL) {
    use_glob = *((unsigned char *) c->argv[0]);
  }

  need_confirm = FALSE;
  scp_pool = make_sub_pool(sftp_pool);
  pr_pool_tag(scp_pool, "SSH2 SCP Pool");

  while ((optc = getopt(req->nelts-1, reqargv, opts)) != -1) {
    switch (optc) {
      case 'd':
        scp_opts |= SFTP_SCP_OPT_DIR;
        break;

      case 'f':
        scp_opts |= SFTP_SCP_OPT_ISSRC;
        need_confirm = TRUE;
        break;

      case 'p':
        scp_opts |= SFTP_SCP_OPT_PRESERVE;
        break;

      case 'r':
        scp_opts |= SFTP_SCP_OPT_RECURSE;
        break;

      case 't':
        scp_opts |= SFTP_SCP_OPT_ISDST;
        write_confirm(p, channel_id, 0, NULL);
        break;

      case 'v':
        scp_opts |= SFTP_SCP_OPT_VERBOSE;
        break;
    }
  }

  /* If we don't have paths, then it's an error. */
  if (reqargv[optind] == NULL) {
    (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
      "'scp' request provided no paths, ignoring");
    errno = EINVAL;
    return -1;
  }

  paths = scp_new_paths(channel_id);
  if (paths == NULL) {
    (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
      "unable to handle paths for 'scp' request: %s", strerror(errno));
    return -1;
  }

  /* Make a copy of the remaining paths, for later handling.
   *
   * Normally there's only one path, but we might be handling a glob which
   * matches multiple paths.
   */
  paths->paths = make_array(paths->pool, 1, sizeof(struct scp_path *));
  paths->path_idx = 0;

  /* First concatenate any remaining command-line arguments, using spaces,
   * in order to handle paths that contain spaces; see Issue #1886.
   */
  for (i = optind; i < req->nelts; i++) {
    pr_signals_handle();

    if (reqargv[i] != NULL) {
      size_t reqarg_len;

      /* Note that we might be dealing with an empty-length argument, which
       * we get from pr_str_get_word() parsing the client-provided string,
       * which might have spaces.  Watch for them.
       */

      reqarg_len = strlen(reqargv[i]);
      if (reqarg_len == 0) {
        continue;
      }

      if (target_path != NULL) {
        target_path = pstrcat(p, target_path, " ", reqargv[i], NULL);

      } else {
        target_path = pstrdup(p, reqargv[i]);
      }
    }
  }

  target_path = scp_canonicalize_target(p, target_path);

  if (use_glob == TRUE &&
      (scp_opts & SFTP_SCP_OPT_ISSRC) &&
      strpbrk(target_path, "{[*?") != NULL) {
    int res, xerrno;
    char *glob_path;
    size_t path_len;
    glob_t gl;

    /* Whee, glob characters.  Need to expand the pattern to the
     * list of matching files, just as the shell would do.
     */

    memset(&gl, 0, sizeof(gl));

    glob_path = pstrdup(paths->pool, target_path);
    path_len = strlen(glob_path);

    res = pr_fs_glob(glob_path, GLOB_NOSORT|GLOB_BRACE, NULL, &gl);
    switch (res) {
      case 0: {
        register unsigned int j;

        for (j = 0; j < gl.gl_pathc; j++) {
          pr_signals_handle();

          sp = pcalloc(paths->pool, sizeof(struct scp_path));
          sp->path = pstrdup(paths->pool, gl.gl_pathv[j]);
          path_len = strlen(sp->path);

          /* Trim any trailing path separators.  It's important. */
          while (path_len > 1 &&
                 sp->path[path_len-1] == '/') {
            pr_signals_handle();
            sp->path[--path_len] = '\0';
          }

          sp->orig_path = pstrdup(paths->pool, sp->path);

          if (path_len > 0) {
            *((struct scp_path **) push_array(paths->paths)) = sp;
          }
        }

        break;
      }

      case GLOB_NOSPACE:
        xerrno = errno;
        pr_trace_msg(trace_channel, 1, "error globbing '%s': Not "
          "enough memory (%s)", reqargv[i], strerror(xerrno));
        write_confirm(p, channel_id, 1, pstrcat(p, target_path, ": ",
          strerror(xerrno), NULL));
        errno = xerrno;
        return 0;

      case GLOB_NOMATCH:
        xerrno = ENOENT;
        pr_trace_msg(trace_channel, 1, "error globbing '%s': No "
          "matches found (%s)", reqargv[i], strerror(xerrno));
        write_confirm(p, channel_id, 1, pstrcat(p, target_path, ": ",
          strerror(xerrno), NULL));
        errno = xerrno;
        return 0;
    }

    pr_fs_globfree(&gl);

  } else {
    size_t path_len;

    sp = pcalloc(paths->pool, sizeof(struct scp_path));
    sp->path = pstrdup(paths->pool, target_path);

    path_len = strlen(sp->path);

    /* Trim any trailing path separators.  It's important. */
    while (path_len > 1 &&
           sp->path[path_len-1] == '/') {
      pr_signals_handle();
      sp->path[--path_len] = '\0';
    }

    sp->orig_path = pstrdup(paths->pool, sp->path);

    if (path_len > 0) {
      *((struct scp_path **) push_array(paths->paths)) = sp;
    }
  }

  /* If we're receiving files, and the client provided more than one
   * path, then it's ambiguous -- we don't know which of the files
   * the client will be sending should be written to which path.
   */
  if ((scp_opts & SFTP_SCP_OPT_ISDST) &&
      paths->paths->nelts != 1) {
    (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
      "'scp' request provided more than one destination path, ignoring");
    errno = EINVAL;
    return -1;
  }

  for (i = 0; i < paths->paths->nelts; i++) {
    struct scp_path *sp;

    sp = ((struct scp_path **) paths->paths->elts)[i];
    if (sp != NULL) {
      pr_trace_msg(trace_channel, 5, "scp path[%u] = '%s'", i, sp->path);
    }
  }

  return 0;
}

int sftp_scp_open_session(uint32_t channel_id) {
  register unsigned int i;
  pool *sub_pool;
  struct scp_paths *paths;
  struct scp_session *sess, *last;
  int timeout_stalled;

  /* Check to see if we already have an SCP session opened for the given
   * channel ID.
   */
  sess = last = scp_sessions;
  while (sess != NULL) {
    pr_signals_handle();

    if (sess->channel_id == channel_id) {
      errno = EEXIST;
      return -1;
    }

    if (sess->next == NULL) {
      /* This is the last item in the list. */
      last = sess;
    }

    sess = sess->next;
  }

  paths = scp_get_paths(channel_id);
  if (paths == NULL) {
    pr_trace_msg(trace_channel, 1, "missing paths for SCP channel ID %lu",
      (unsigned long) channel_id);
    errno = EACCES;
    return -1;
  }

  /* Looks like we get to allocate a new one. */
  sub_pool = make_sub_pool(scp_pool);
  pr_pool_tag(sub_pool, "SCP session pool");

  sess = pcalloc(sub_pool, sizeof(struct scp_session));
  sess->pool = sub_pool;
  sess->channel_id = channel_id;

  /* Now copy all of the struct scp_path elements from the paths list into
   * the session object.
   */

  sess->paths = make_array(sess->pool, paths->paths->nelts,
    sizeof(struct scp_path *));

  for (i = 0; i < paths->paths->nelts; i++) {
    struct scp_path *src_sp, *dst_sp;

    src_sp = ((struct scp_path **) paths->paths->elts)[i];

    dst_sp = pcalloc(sess->pool, sizeof(struct scp_path));
    dst_sp->orig_path = pstrdup(sess->pool, src_sp->orig_path);
    dst_sp->path = pstrdup(sess->pool, src_sp->path);

    *((struct scp_path **) push_array(sess->paths)) = dst_sp;
  }

  sess->path_idx = paths->path_idx;

  scp_destroy_paths(paths);

  if (last != NULL) {
    last->next = sess;
    sess->prev = last;

  } else {
    scp_sessions = sess;
  }

  pr_event_generate("mod_sftp.scp.session-opened", NULL);

  pr_timer_remove(PR_TIMER_STALLED, ANY_MODULE);

  timeout_stalled = pr_data_get_timeout(PR_DATA_TIMEOUT_STALLED);
  if (timeout_stalled > 0) {
    pr_timer_add(timeout_stalled, PR_TIMER_STALLED, NULL,
      scp_timeout_stalled_cb, "TimeoutStalled");
  }

  pr_session_set_protocol("scp");

  /* Clear any ASCII flags (set by default for FTP sessions. */
  session.sf_flags &= ~SF_ASCII;

  return 0;
}

int sftp_scp_close_session(uint32_t channel_id) {
  struct scp_session *sess;

  /* Check to see if we have an SCP session opened for this channel ID. */
  sess = scp_sessions;
  while (sess != NULL) {
    pr_signals_handle();

    if (sess->channel_id == channel_id) {
      pr_timer_remove(PR_TIMER_STALLED, ANY_MODULE);

      if (sess->next != NULL) {
        sess->next->prev = sess->prev;
      }

      if (sess->prev != NULL) {
        sess->prev->next = sess->next;

      } else {
        /* This is the start of the session list. */
        scp_sessions = sess->next;
      }

      /* XXX How to handle dangling directory lists?? */

      if (sess->paths != NULL) {
        if (sess->paths != NULL &&
            sess->paths->nelts > 0) {
          register unsigned int i;
          int count = 0;
          struct scp_path **elts;

          elts = sess->paths->elts;
          for (i = 0; i < sess->paths->nelts; i++) {
            struct scp_path *elt = elts[i];

            if (elt->fh != NULL) {
              count++;
            }
          }

          if (count > 0) {
            config_rec *c;
            unsigned char delete_aborted_stores = FALSE;

            c = find_config(main_server->conf, CONF_PARAM,
              "DeleteAbortedStores", FALSE);
            if (c) {
              delete_aborted_stores = *((unsigned char *) c->argv[0]);
            }

            (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
              "aborting %d unclosed file %s", count,
              count != 1 ? "handles" : "handle");

            for (i = 0; i < sess->paths->nelts; i++) {
              struct scp_path *elt = elts[i];

              if (elt->fh != NULL) {
                char *abs_path, *curr_path;

                curr_path = pstrdup(scp_pool, elt->fh->fh_path);

                /* Write out an 'incomplete' TransferLog entry for this. */
                abs_path = sftp_misc_vroot_abs_path(scp_pool, elt->best_path,
                  TRUE);

                if (elt->recvlen > 0) {
                  xferlog_write(0, pr_netaddr_get_sess_remote_name(),
                    elt->recvlen, abs_path, 'b', 'i', 'r', session.user, 'i',
                    "_");

                } else {
                  xferlog_write(0, pr_netaddr_get_sess_remote_name(),
                    elt->sentlen, abs_path, 'b', 'o', 'r', session.user, 'i',
                    "_");
                }

                if (pr_fsio_close(elt->fh) < 0) {
                  (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
                    "error writing aborted file '%s': %s", elt->best_path,
                    strerror(errno));
                }

                elt->fh = NULL;

                if (delete_aborted_stores == TRUE &&
                    elt->recvlen > 0) {
                  (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
                    "removing aborted uploaded file '%s'", curr_path);

                  if (pr_fsio_unlink(curr_path) < 0) {
                    (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
                      "error unlinking file '%s': %s", curr_path,
                      strerror(errno));
                  }
                }
              }
            }
          }
        }
      }

      sess->paths = NULL;
      destroy_pool(sess->pool);

      pr_session_set_protocol("ssh2");

      pr_event_generate("mod_sftp.scp.session-closed", NULL);
      return 0;
    }

    sess = sess->next;
  }

  errno = ENOENT;
  return -1;
}