1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
|
/*
* Copyright (c) 1985, 1988, 1990 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1985, 1988, 1990 Regents of the University of California.\n\
All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)ftpd.c 5.40 (Berkeley) 7/2/91";
#endif /* not lint */
/*
* FTP server.
*/
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#ifndef KRB5_KRB4_COMPAT
/* krb.h gets this, and Ultrix doesn't protect vs multiple inclusion */
#include <sys/socket.h>
#endif
#include <sys/wait.h>
#include <sys/file.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#define FTP_NAMES
#include <arpa/ftp.h>
#include <arpa/inet.h>
#include <arpa/telnet.h>
#include <signal.h>
#include <dirent.h>
#include <fcntl.h>
#include <time.h>
#include <pwd.h>
#ifdef HAVE_SHADOW
#include <shadow.h>
#endif
#include <grp.h>
#include <setjmp.h>
#ifndef POSIX_SETJMP
#undef sigjmp_buf
#undef sigsetjmp
#undef siglongjmp
#define sigjmp_buf jmp_buf
#define sigsetjmp(j,s) setjmp(j)
#define siglongjmp longjmp
#endif
#ifndef KRB5_KRB4_COMPAT
/* krb.h gets this, and Ultrix doesn't protect vs multiple inclusion */
#include <netdb.h>
#endif
#include <errno.h>
#include <syslog.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#ifndef STDARG
#if (defined(__STDC__) && ! defined(VARARGS)) || defined(HAVE_STDARG_H)
#define STDARG
#endif
#endif
#ifdef STDARG
#include <stdarg.h>
#endif
#include "pathnames.h"
#include <libpty.h>
#ifdef NEED_SETENV
extern int setenv(char *, char *, int);
#endif
#ifndef L_SET
#define L_SET 0
#endif
#ifndef L_INCR
#define L_INCR 1
#endif
#ifndef HAVE_STRERROR
#define strerror(error) (sys_errlist[error])
#ifdef NEED_SYS_ERRLIST
extern char *sys_errlist[];
#endif
#endif
extern char *mktemp ();
char *ftpusers;
extern int yyparse(void);
#include <k5-util.h>
#include "port-sockets.h"
#ifdef KRB5_KRB4_COMPAT
#include <krb5.h>
#include <krb.h>
AUTH_DAT kdata;
KTEXT_ST ticket;
MSG_DAT msg_data;
Key_schedule schedule;
char *keyfile;
static char *krb4_services[] = { "ftp", "rcmd", NULL };
#endif /* KRB5_KRB4_COMPAT */
#ifdef GSSAPI
#include <gssapi/gssapi.h>
#include <gssapi/gssapi_generic.h>
#include <gssapi/gssapi_krb5.h>
gss_ctx_id_t gcontext;
gss_buffer_desc client_name;
static char *gss_services[] = { "ftp", "host", NULL };
#include <krb5.h>
krb5_context kcontext;
krb5_ccache ccache;
static void ftpd_gss_convert_creds(char *name, gss_cred_id_t);
static int ftpd_gss_userok(gss_buffer_t, char *name);
static void log_gss_error(int, OM_uint32, OM_uint32, const char *);
#endif /* GSSAPI */
char *auth_type; /* Authentication succeeded? If so, what type? */
static char *temp_auth_type;
int authorized; /* Auth succeeded and was accepted by krb4 or gssapi */
int have_creds; /* User has credentials on disk */
/*
* File containing login names
* NOT to be used on this machine.
* Commonly used to disallow uucp.
*/
#include "ftpd_var.h"
#include "secure.h"
extern char *crypt();
extern char version[];
extern char *home; /* pointer to home directory for glob */
extern FILE *ftpd_popen(), *fopen(), *freopen();
extern int ftpd_pclose(), fclose();
extern char cbuf[];
extern off_t restart_point;
struct sockaddr_in ctrl_addr;
struct sockaddr_in data_source;
struct sockaddr_in data_dest;
struct sockaddr_in his_addr;
struct sockaddr_in pasv_addr;
int data;
jmp_buf errcatch;
sigjmp_buf urgcatch;
int logged_in;
struct passwd *pw;
int debug;
int allow_ccc = 0; /* whether or not the CCC command is allowed */
int ccc_ok = 0; /* whether or not to accept cleartext commands */
int timeout = 900; /* timeout after 15 minutes of inactivity */
int maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */
int logging;
int authlevel;
int want_creds;
int guest;
int restricted;
int type;
int clevel; /* control protection level */
int dlevel; /* data protection level */
int form;
int stru; /* avoid C keyword */
int mode;
int usedefault = 1; /* for data transfers */
int pdata = -1; /* for passive mode */
int transflag;
off_t file_size;
off_t byte_count;
#if !defined(CMASK) || CMASK == 0
#undef CMASK
#define CMASK 027
#endif
int defumask = CMASK; /* default umask value */
char tmpline[FTP_BUFSIZ];
char pathbuf[MAXPATHLEN + 1];
char hostname[MAXHOSTNAMELEN];
char remotehost[MAXHOSTNAMELEN];
char rhost_addra[16];
char *rhost_sane;
/* Defines for authlevel */
#define AUTHLEVEL_NONE 0
#define AUTHLEVEL_AUTHENTICATE 1
#define AUTHLEVEL_AUTHORIZE 2
/*
* Timeout intervals for retrying connections
* to hosts that don't accept PORT cmds. This
* is a kludge, but given the problems with TCP...
*/
#define SWAITMAX 90 /* wait at most 90 seconds */
#define SWAITINT 5 /* interval between retries */
int swaitmax = SWAITMAX;
int swaitint = SWAITINT;
void lostconn(int), myoob(int);
FILE *getdatasock(char *);
#if defined(__STDC__)
/*
* The following prototypes must be ANSI for systems for which
* sizeof(off_t) > sizeof(int) to prevent stack overflow problems
*/
FILE *dataconn(char *name, off_t size, char *mymode);
void send_data(FILE *instr, FILE *outstr, off_t blksize);
#else
void send_data();
FILE *dataconn();
#endif
static void dolog(struct sockaddr_in *);
static int receive_data(FILE *, FILE *);
static void login(char *passwd, int logincode);
static void end_login(void);
static int disallowed_user(char *);
static int restricted_user(char *);
static int checkuser(char *);
static char *gunique(char *);
#ifdef SETPROCTITLE
char **Argv = NULL; /* pointer to argument vector */
char *LastArgv = NULL; /* end of argv */
char proctitle[FTP_BUFSIZ]; /* initial part of title */
#endif /* SETPROCTITLE */
#ifdef __SCO__
/* sco has getgroups and setgroups but no initgroups */
int initgroups(char* name, gid_t basegid) {
gid_t others[NGROUPS_MAX+1];
int ngrps;
others[0] = basegid;
ngrps = getgroups(NGROUPS_MAX, others+1);
return setgroups(ngrps+1, others);
}
#endif
int stripdomain = 1;
int maxhostlen = 0;
int always_ip = 0;
int
main(argc, argv, envp)
int argc;
char *argv[];
char **envp;
{
int addrlen, c, on = 1, tos, port = -1;
extern char *optarg;
extern int optopt;
#ifdef KRB5_KRB4_COMPAT
char *option_string = "AaCcdElp:r:s:T:t:U:u:vw:";
#else /* !KRB5_KRB4_COMPAT */
char *option_string = "AaCcdElp:r:T:t:U:u:vw:";
#endif /* KRB5_KRB4_COMPAT */
ftpusers = _PATH_FTPUSERS_DEFAULT;
#ifdef KRB5_KRB4_COMPAT
keyfile = KEYFILE;
#endif /* KRB5_KRB4_COMPAT */
debug = 0;
#ifdef SETPROCTITLE
/*
* Save start and extent of argv for setproctitle.
*/
Argv = argv;
while (*envp)
envp++;
LastArgv = envp[-1] + strlen(envp[-1]);
#endif /* SETPROCTITLE */
#ifdef GSSAPI
krb5_init_context(&kcontext);
#endif
while ((c = getopt(argc, argv, option_string)) != -1) {
switch (c) {
case 'v':
debug = 1;
break;
case 'd':
debug = 1;
break;
case 'E':
if (!authlevel)
authlevel = AUTHLEVEL_AUTHENTICATE;
break;
case 'l':
logging ++;
break;
case 'a':
authlevel = AUTHLEVEL_AUTHORIZE;
break;
case 'A':
authlevel = AUTHLEVEL_AUTHENTICATE;
break;
case 'C':
want_creds = 1;
break;
case 'c':
allow_ccc = 1;
break;
case 'p':
port = atoi(optarg);
break;
case 'r':
setenv("KRB_CONF", optarg, 1);
break;
#ifdef KRB5_KRB4_COMPAT
case 's':
keyfile = optarg;
break;
#endif /* KRB5_KRB4_COMPAT */
case 't':
timeout = atoi(optarg);
if (maxtimeout < timeout)
maxtimeout = timeout;
break;
case 'T':
maxtimeout = atoi(optarg);
if (timeout > maxtimeout)
timeout = maxtimeout;
break;
case 'u':
{
int val = 0;
char *umask_val = optarg;
while (*umask_val >= '0' && *umask_val <= '9') {
val = val*8 + *umask_val - '0';
umask_val++;
}
if (*umask_val != ' ' && *umask_val != '\0')
fprintf(stderr, "ftpd: Bad value for -u\n");
else
defumask = val;
break;
}
case 'U':
ftpusers = optarg;
break;
case 'w':
{
char *foptarg;
foptarg = optarg;
if (!strcmp(foptarg, "ip"))
always_ip = 1;
else {
char *cp2;
cp2 = strchr(foptarg, ',');
if (cp2 == NULL)
maxhostlen = atoi(foptarg);
else if (*(++cp2)) {
if (!strcmp(cp2, "striplocal"))
stripdomain = 1;
else if (!strcmp(cp2, "nostriplocal"))
stripdomain = 0;
else {
fprintf(stderr,
"ftpd: bad arg to -w\n");
exit(1);
}
*(--cp2) = '\0';
maxhostlen = atoi(foptarg);
}
}
break;
}
default:
fprintf(stderr, "ftpd: Unknown flag -%c ignored.\n",
(char)optopt);
break;
}
}
if (port != -1) {
struct sockaddr_in sin4;
int s, ns;
socklen_t sz;
/* Accept an incoming connection on port. */
sin4.sin_family = AF_INET;
sin4.sin_addr.s_addr = INADDR_ANY;
sin4.sin_port = htons(port);
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) {
perror("socket");
exit(1);
}
(void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
(char *)&on, sizeof(on));
if (bind(s, (struct sockaddr *)&sin4, sizeof sin4) < 0) {
perror("bind");
exit(1);
}
if (listen(s, 1) < 0) {
perror("listen");
exit(1);
}
sz = sizeof sin4;
ns = accept(s, (struct sockaddr *)&sin4, &sz);
if (ns < 0) {
perror("accept");
exit(1);
}
(void) close(s);
(void) dup2(ns, 0);
(void) dup2(ns, 1);
(void) dup2(ns, 2);
if (ns > 2)
(void) close(ns);
}
/*
* LOG_NDELAY sets up the logging connection immediately,
* necessary for anonymous ftp's that chroot and can't do it later.
*/
#ifndef LOG_NDELAY
/* Ultrix syslog does not support NDELAY. */
#define LOG_NDELAY 0
#endif
#ifndef LOG_DAEMON
#define LOG_DAEMON 0
#endif
openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_DAEMON);
addrlen = sizeof (his_addr);
if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) {
syslog(LOG_ERR, "getpeername (%s): %m",argv[0]);
exit(1);
}
addrlen = sizeof (ctrl_addr);
if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) {
syslog(LOG_ERR, "getsockname (%s): %m",argv[0]);
exit(1);
}
#ifdef IP_TOS
#ifdef IPTOS_LOWDELAY
tos = IPTOS_LOWDELAY;
if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
#endif
#endif
port = ntohs(ctrl_addr.sin_port);
data_source.sin_port = htons(port - 1);
(void) freopen("/dev/null", "w", stderr);
(void) signal(SIGPIPE, lostconn);
(void) signal(SIGCHLD, SIG_IGN);
#ifdef SIGURG
#ifdef POSIX_SIGNALS
{
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = myoob;
if (sigaction(SIGURG, &sa, NULL) < 0)
syslog(LOG_ERR, "signal: %m");
}
#else
if ((long)signal(SIGURG, myoob) < 0)
syslog(LOG_ERR, "signal: %m");
#endif /* POSIX_SIGNALS */
#endif /* SIGURG */
/* Try to handle urgent data inline */
#ifdef SO_OOBINLINE
if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) < 0)
syslog(LOG_ERR, "setsockopt: %m");
#endif
#ifdef F_SETOWN
if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1)
syslog(LOG_ERR, "fcntl F_SETOWN: %m");
#endif
dolog(&his_addr);
/*
* Set up default state
*/
data = -1;
clevel = dlevel = PROT_C;
type = TYPE_A;
form = FORM_N;
stru = STRU_F;
mode = MODE_S;
tmpline[0] = '\0';
(void) gethostname(hostname, sizeof (hostname));
reply(220, "%s FTP server (%s) ready.", hostname, version);
(void) setjmp(errcatch);
for (;;)
(void) yyparse();
/* NOTREACHED */
}
void
lostconn(sig)
int sig;
{
if (debug)
syslog(LOG_DEBUG, "lost connection");
dologout(-1);
}
static char ttyline[20];
/*
* Helper function for sgetpwnam().
*/
static char *
sgetsave(s)
char *s;
{
char *new = malloc((unsigned) strlen(s) + 1);
if (new == NULL) {
perror_reply(421, "Local resource failure: malloc");
dologout(1);
/* NOTREACHED */
}
(void) strcpy(new, s);
return (new);
}
/*
* Save the result of a getpwnam. Used for USER command, since
* the data returned must not be clobbered by any other command
* (e.g., globbing).
*/
static struct passwd *
sgetpwnam(name)
char *name;
{
static struct passwd save;
register struct passwd *p;
#ifdef HAVE_SHADOW
register struct spwd *sp;
#endif
if ((p = getpwnam(name)) == NULL)
return (p);
if (save.pw_name) {
free(save.pw_name);
free(save.pw_passwd);
free(save.pw_gecos);
free(save.pw_dir);
free(save.pw_shell);
}
save = *p;
save.pw_name = sgetsave(p->pw_name);
#ifdef HAVE_SHADOW
if ((sp = getspnam(name)) == NULL)
save.pw_passwd = sgetsave(p->pw_passwd);
else
save.pw_passwd = sgetsave(sp->sp_pwdp);
#else
save.pw_passwd = sgetsave(p->pw_passwd);
#endif
save.pw_gecos = sgetsave(p->pw_gecos);
save.pw_dir = sgetsave(p->pw_dir);
save.pw_shell = sgetsave(p->pw_shell);
return (&save);
}
/*
* Expand the given pathname relative to the current working directory.
*/
static char *
path_expand(path)
char *path;
{
pathbuf[0] = '\x0';
if (!path) return pathbuf;
/* Don't bother with getcwd() if the path is absolute */
if (path[0] != '/') {
if (!getcwd(pathbuf, sizeof pathbuf)) {
pathbuf[0] = '\x0';
syslog(LOG_ERR, "getcwd() failed");
}
else {
int len = strlen(pathbuf);
if (pathbuf[len-1] != '/') {
pathbuf[len++] = '/';
pathbuf[len] = '\x0';
}
}
}
return strncat(pathbuf, path,
sizeof (pathbuf) - strlen(pathbuf) - 1);
}
/*
* Set data channel protection level
*/
void
setdlevel(prot_level)
int prot_level;
{
switch (prot_level) {
case PROT_S:
#ifndef NOENCRYPTION
case PROT_P:
#endif
if (auth_type)
case PROT_C:
reply(200, "Data channel protection level set to %s.",
(dlevel = prot_level) == PROT_S ?
"safe" : dlevel == PROT_P ?
"private" : "clear");
else
default: reply(536, "%s protection level not supported.",
levelnames[prot_level]);
}
}
int login_attempts; /* number of failed login attempts */
int askpasswd; /* had user command, ask for passwd */
/*
* USER command.
* Sets global passwd pointer pw if named account exists and is acceptable;
* sets askpasswd if a PASS command is expected. If logged in previously,
* need to reset state. If name is "ftp" or "anonymous", the name is not in
* ftpusers, and ftp account exists, set guest and pw, then just return.
* If account doesn't exist, ask for passwd anyway. Otherwise, check user
* requesting login privileges. Disallow anyone who does not have a standard
* shell as returned by getusershell(). Disallow anyone mentioned in the file
* ftpusers to allow people such as root and uucp to be avoided, except
* for users whose names are followed by whitespace and then the keyword
* "restrict." Restricted users are allowed to login, but a chroot() is
* done to their home directory.
*/
void
user(name)
char *name;
{
register char *cp;
char *shell;
char buf[FTP_BUFSIZ];
#ifdef HAVE_GETUSERSHELL
char *getusershell();
#endif
if (logged_in) {
if (guest) {
reply(530, "Can't change user from guest login.");
return;
}
end_login();
}
authorized = guest = 0;
if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) {
if (disallowed_user("ftp") || disallowed_user("anonymous"))
reply(530, "User %s access denied.", name);
else if ((pw = sgetpwnam("ftp")) != NULL) {
guest = 1;
askpasswd = 1;
reply(331, "Guest login ok, send ident as password.");
} else
reply(530, "User %s unknown.", name);
return;
}
/*
* If authentication is required, check that before anything
* else to avoid leaking information.
*/
if (authlevel && !auth_type) {
reply(530,
"Must perform authentication before identifying USER.");
return;
}
pw = sgetpwnam(name);
if (pw) {
if ((shell = pw->pw_shell) == NULL || *shell == 0)
shell = "/bin/sh";
#ifdef HAVE_GETUSERSHELL
setusershell();
while ((cp = getusershell()) != NULL)
if (strcmp(cp, shell) == 0)
break;
endusershell();
#else
cp = shell;
#endif
if (cp == NULL || disallowed_user(name)) {
reply(530, "User %s access denied.", name);
if (logging)
syslog(LOG_NOTICE,
"FTP LOGIN REFUSED FROM %s, %s (%s)",
rhost_addra, remotehost, name);
pw = (struct passwd *) NULL;
return;
}
restricted = restricted_user(name);
}
if (auth_type) {
int result;
#ifdef GSSAPI
if (auth_type && strcmp(auth_type, "GSSAPI") == 0) {
int len;
authorized = ftpd_gss_userok(&client_name, name) == 0;
len = sizeof("GSSAPI user is not authorized as "
"; Password required.")
+ strlen(client_name.value)
+ strlen(name);
if (len >= sizeof(buf)) {
syslog(LOG_ERR, "user: username too long");
name = "[username too long]";
}
sprintf(buf, "GSSAPI user %s is%s authorized as %s",
(char *) client_name.value,
authorized ? "" : " not",
name);
}
#ifdef KRB5_KRB4_COMPAT
else
#endif /* KRB5_KRB4_COMPAT */
#endif /* GSSAPI */
#ifdef KRB5_KRB4_COMPAT
if (auth_type && strcmp(auth_type, "KERBEROS_V4") == 0) {
int len;
authorized = kuserok(&kdata,name) == 0;
len = sizeof("Kerberos user .@ is not authorized as "
"; Password required.")
+ strlen(kdata.pname)
+ strlen(kdata.pinst)
+ strlen(kdata.prealm)
+ strlen(name);
if (len >= sizeof(buf)) {
syslog(LOG_ERR, "user: username too long");
name = "[username too long]";
}
sprintf(buf, "Kerberos user %s%s%s@%s is%s authorized as %s",
kdata.pname, *kdata.pinst ? "." : "",
kdata.pinst, kdata.prealm,
authorized ? "" : " not", name);
}
#endif /* KRB5_KRB4_COMPAT */
if (!authorized && authlevel == AUTHLEVEL_AUTHORIZE) {
strncat(buf, "; Access denied.",
sizeof(buf) - strlen(buf) - 1);
result = 530;
pw = NULL;
} else if (!authorized || (want_creds && !have_creds)) {
strncat(buf, "; Password required.",
sizeof(buf) - strlen(buf) - 1);
askpasswd = 1;
result = 331;
} else
result = 232;
reply(result, "%s", buf);
syslog(authorized ? LOG_INFO : LOG_ERR, "%s", buf);
if (result == 232)
login(NULL, result);
return;
}
/* User didn't authenticate and authentication wasn't required. */
reply(331, "Password required for %s.", name);
askpasswd = 1;
/*
* Delay before reading passwd after first failed
* attempt to slow down passwd-guessing programs.
*/
if (login_attempts)
sleep((unsigned) login_attempts);
}
/*
* Check if a user is in the file ftpusers.
* Return 1 if they are (a disallowed user), -1 if their username
* is followed by "restrict." (a restricted user). Otherwise return 0.
*/
static int
checkuser(name)
char *name;
{
register FILE *fd;
register char *p;
char line[FTP_BUFSIZ];
if ((fd = fopen(ftpusers, "r")) != NULL) {
while (fgets(line, sizeof(line), fd) != NULL) {
if ((p = strchr(line, '\n')) != NULL) {
*p = '\0';
if (line[0] == '#')
continue;
if (strcmp(line, name) == 0)
return (1);
if (strncmp(line, name, strlen(name)) == 0) {
int i = strlen(name) + 1;
/* Make sure foo doesn't match foobar */
if (line[i] == '\0' || !isspace((int) line[i]))
continue;
/* Ignore whitespace */
while (isspace((int) line[++i]));
if (strcmp(&line[i], "restrict") == 0)
return (-1);
else
return (1);
}
}
}
(void) fclose(fd);
}
return (0);
}
static int
disallowed_user(name)
char *name;
{
return(checkuser(name) == 1);
}
static int
restricted_user(name)
char *name;
{
return(checkuser(name) == -1);
}
/*
* Terminate login as previous user, if any, resetting state;
* used when USER command is given or login fails.
*/
static void
end_login()
{
(void) krb5_seteuid((uid_t)0);
if (logged_in)
pty_logwtmp(ttyline, "", "");
if (have_creds) {
#ifdef GSSAPI
krb5_cc_destroy(kcontext, ccache);
#endif
#ifdef KRB5_KRB4_COMPAT
dest_tkt();
#endif
have_creds = 0;
}
pw = NULL;
logged_in = 0;
guest = 0;
}
static int
kpass(name, passwd)
char *name, *passwd;
{
#ifdef GSSAPI
krb5_principal server, me;
krb5_creds my_creds;
krb5_timestamp now;
#endif /* GSSAPI */
#ifdef KRB5_KRB4_COMPAT
char realm[REALM_SZ];
#ifndef GSSAPI
char **service;
KTEXT_ST ticket;
AUTH_DAT authdata;
des_cblock key;
char instance[INST_SZ];
unsigned long faddr;
struct hostent *hp;
#endif /* GSSAPI */
#endif /* KRB5_KRB4_COMPAT */
char ccname[MAXPATHLEN];
#ifdef GSSAPI
memset((char *)&my_creds, 0, sizeof(my_creds));
if (krb5_parse_name(kcontext, name, &me))
return 0;
my_creds.client = me;
sprintf(ccname, "FILE:/tmp/krb5cc_ftpd%ld", (long) getpid());
if (krb5_cc_resolve(kcontext, ccname, &ccache))
return(0);
if (krb5_cc_initialize(kcontext, ccache, me))
return(0);
if (krb5_build_principal_ext(kcontext, &server,
krb5_princ_realm(kcontext, me)->length,
krb5_princ_realm(kcontext, me)->data,
KRB5_TGS_NAME_SIZE, KRB5_TGS_NAME,
krb5_princ_realm(kcontext, me)->length,
krb5_princ_realm(kcontext, me)->data,
0))
goto nuke_ccache;
my_creds.server = server;
if (krb5_timeofday(kcontext, &now))
goto nuke_ccache;
my_creds.times.starttime = 0; /* start timer when
request gets to KDC */
my_creds.times.endtime = now + 60 * 60 * 10;
my_creds.times.renew_till = 0;
if (krb5_get_init_creds_password(kcontext, &my_creds, me,
passwd, NULL, NULL, 0, NULL, NULL))
goto nuke_ccache;
if (krb5_cc_store_cred(kcontext, ccache, &my_creds))
goto nuke_ccache;
if (!want_creds) {
krb5_cc_destroy(kcontext, ccache);
return(1);
}
#endif /* GSSAPI */
#ifdef KRB5_KRB4_COMPAT
if (krb_get_lrealm(realm, 1) != KSUCCESS)
goto nuke_ccache;
sprintf(ccname, "%s_ftpd%ld", TKT_ROOT, (long) getpid());
krb_set_tkt_string(ccname);
if (krb_get_pw_in_tkt(name, "", realm, "krbtgt", realm, 1, passwd))
goto nuke_ccache;
#ifndef GSSAPI
/* Verify the ticket since we didn't verify the krb5 one. */
strncpy(instance, krb_get_phost(hostname), sizeof(instance));
if ((hp = gethostbyname(instance)) == NULL)
goto nuke_ccache;
memcpy((char *) &faddr, (char *)hp->h_addr, sizeof(faddr));
for (service = krb4_services; *service; service++) {
if (!read_service_key(*service, instance,
realm, 0, keyfile, key)) {
(void) memset(key, 0, sizeof(key));
if (krb_mk_req(&ticket, *service,
instance, realm, 33) ||
krb_rd_req(&ticket, *service, instance,
faddr, &authdata,keyfile) ||
kuserok(&authdata, name)) {
dest_tkt();
goto nuke_ccache;
} else
break;
}
}
if (!*service) {
dest_tkt();
goto nuke_ccache;
}
if (!want_creds) {
dest_tkt();
return(1);
}
#endif /* GSSAPI */
#endif /* KRB5_KRB4_COMPAT */
#if defined(GSSAPI) || defined(KRB5_KRB4_COMPAT)
have_creds = 1;
return(1);
#endif /* GSSAPI || KRB5_KRB4_COMPAT */
nuke_ccache:
#ifdef GSSAPI
krb5_cc_destroy(kcontext, ccache);
#endif /* GSSAPI */
return(0);
}
void
pass(passwd)
char *passwd;
{
char *xpasswd, *salt;
if (authorized && !want_creds) {
reply(202, "PASS command superfluous.");
return;
}
if (logged_in || askpasswd == 0) {
reply(503, "Login with USER first.");
return;
}
if (!guest) {
/* "ftp" is only account allowed no password */
if (pw == NULL)
salt = "xx";
else
salt = pw->pw_passwd;
#ifdef __SCO__
/* SCO does not provide crypt. */
xpasswd = "";
#else
xpasswd = crypt(passwd, salt);
#endif
/* Fail if:
* pw is NULL
* kpass fails and we want_creds
* kpass fails and the user has no local password
* kpass fails and the provided password doesn't match pw
*/
if (pw == NULL || (!kpass(pw->pw_name, passwd) &&
(want_creds || !*pw->pw_passwd ||
strcmp(xpasswd, pw->pw_passwd)))) {
pw = NULL;
sleep(5);
if (++login_attempts >= 3) {
reply(421,
"Login incorrect, closing connection.");
syslog(LOG_NOTICE,
"repeated login failures from %s (%s)",
rhost_addra, remotehost);
dologout(0);
}
reply(530, "Login incorrect.");
return;
}
}
login_attempts = 0; /* this time successful */
login(passwd, 0);
return;
}
static void
login(passwd, logincode)
char *passwd;
int logincode;
{
if (have_creds) {
#ifdef GSSAPI
const char *ccname = krb5_cc_get_name(kcontext, ccache);
chown(ccname, pw->pw_uid, pw->pw_gid);
#endif
#ifdef KRB5_KRB4_COMPAT
chown(tkt_string(), pw->pw_uid, pw->pw_gid);
#endif
}
(void) krb5_setegid((gid_t)pw->pw_gid);
(void) initgroups(pw->pw_name, pw->pw_gid);
/* open wtmp before chroot */
(void) sprintf(ttyline, "ftp%ld", (long) getpid());
pty_logwtmp(ttyline, pw->pw_name, rhost_sane);
logged_in = 1;
if (guest || restricted) {
if (chroot(pw->pw_dir) < 0) {
reply(550, "Can't set privileges.");
goto bad;
}
}
#ifdef HAVE_SETLUID
/*
* If we're on a system which keeps track of login uids, then
* set the login uid. If this fails this opens up a problem on DEC OSF
* with C2 enabled.
*/
if (((uid_t)getluid() != pw->pw_uid)
&& setluid((uid_t)pw->pw_uid) < 0) {
reply(550, "Can't set luid.");
goto bad;
}
#endif
if (krb5_seteuid((uid_t)pw->pw_uid) < 0) {
reply(550, "Can't set uid.");
goto bad;
}
if (guest) {
/*
* We MUST do a chdir() after the chroot. Otherwise
* the old current directory will be accessible as "."
* outside the new root!
*/
if (chdir("/") < 0) {
reply(550, "Can't set guest privileges.");
goto bad;
}
} else {
if (chdir(restricted ? "/" : pw->pw_dir) < 0) {
if (chdir("/") < 0) {
reply(530, "User %s: can't change directory to %s.",
pw->pw_name, pw->pw_dir);
goto bad;
} else {
if (!logincode)
logincode = 230;
lreply(logincode, "No directory! Logging in with home=/");
}
}
}
if (guest) {
reply(230, "Guest login ok, access restrictions apply.");
#ifdef SETPROCTITLE
sprintf(proctitle, "%s: anonymous/%.*s", rhost_sane,
sizeof(proctitle) - strlen(rhost_sane) -
sizeof(": anonymous/"), passwd);
setproctitle(proctitle);
#endif /* SETPROCTITLE */
if (logging)
syslog(LOG_INFO,
"ANONYMOUS FTP LOGIN FROM %s, %s (%s)",
rhost_addra, remotehost, passwd);
} else {
if (askpasswd) {
askpasswd = 0;
reply(230, "User %s logged in.", pw->pw_name);
}
#ifdef SETPROCTITLE
sprintf(proctitle, "%s: %s", rhost_sane, pw->pw_name);
setproctitle(proctitle);
#endif /* SETPROCTITLE */
if (logging)
syslog(LOG_INFO, "FTP LOGIN FROM %s, %s (%s)",
rhost_addra, remotehost, pw->pw_name);
}
home = pw->pw_dir; /* home dir for globbing */
(void) umask(defumask);
return;
bad:
/* Forget all about it... */
end_login();
}
void
retrieve(cmd, name)
char *cmd, *name;
{
FILE *fin, *dout;
struct stat st;
int (*closefunc)();
if (logging > 1 && !cmd)
syslog(LOG_NOTICE, "get %s", path_expand(name));
if (cmd == 0) {
fin = fopen(name, "r"), closefunc = fclose;
st.st_size = 0;
} else {
char line[FTP_BUFSIZ];
if (strlen(cmd) + strlen(name) + 1 >= sizeof(line)) {
syslog(LOG_ERR, "retrieve: filename too long");
reply(501, "filename too long");
return;
}
(void) sprintf(line, cmd, name), name = line;
fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose;
st.st_size = -1;
#ifndef NOSTBLKSIZE
st.st_blksize = FTP_BUFSIZ;
#endif
}
if (fin == NULL) {
if (errno != 0)
perror_reply(550, name);
return;
}
if (cmd == 0 &&
(fstat(fileno(fin), &st) < 0 || (st.st_mode&S_IFMT) != S_IFREG)) {
reply(550, "%s: not a plain file.", name);
goto done;
}
if (restart_point) {
if (type == TYPE_A) {
register int i, n, c;
n = restart_point;
i = 0;
while (i++ < n) {
if ((c=getc(fin)) == EOF) {
perror_reply(550, name);
goto done;
}
if (c == '\n')
i++;
}
} else if (lseek(fileno(fin), restart_point, L_SET) < 0) {
perror_reply(550, name);
goto done;
}
}
dout = dataconn(name, st.st_size, "w");
if (dout == NULL)
goto done;
#ifndef NOSTBLKSIZE
send_data(fin, dout, st.st_blksize);
#else
send_data(fin, dout, FTP_BUFSIZ);
#endif
(void) fclose(dout);
data = -1;
pdata = -1;
done:
(*closefunc)(fin);
if (logging > 2 && !cmd)
syslog(LOG_NOTICE, "get: %i bytes transferred", byte_count);
}
void
store_file(name, fmode, unique)
char *name, *fmode;
int unique;
{
FILE *fout, *din;
struct stat st;
int (*closefunc)();
if (logging > 1) syslog(LOG_NOTICE, "put %s", path_expand(name));
if (unique && stat(name, &st) == 0 &&
(name = gunique(name)) == NULL)
return;
if (restart_point)
fmode = "r+w";
fout = fopen(name, fmode);
closefunc = fclose;
if (fout == NULL) {
perror_reply(553, name);
return;
}
if (restart_point) {
if (type == TYPE_A) {
register int i, n, c;
n = restart_point;
i = 0;
while (i++ < n) {
if ((c=getc(fout)) == EOF) {
perror_reply(550, name);
goto done;
}
if (c == '\n')
i++;
}
/*
* We must do this seek to "current" position
* because we are changing from reading to
* writing.
*/
if (fseek(fout, 0L, L_INCR) < 0) {
perror_reply(550, name);
goto done;
}
} else if (lseek(fileno(fout), restart_point, L_SET) < 0) {
perror_reply(550, name);
goto done;
}
}
din = dataconn(name, (off_t)-1, "r");
if (din == NULL)
goto done;
if (receive_data(din, fout) == 0) {
if (unique)
reply(226, "Transfer complete (unique file name:%s).",
name);
else
reply(226, "Transfer complete.");
}
(void) fclose(din);
data = -1;
pdata = -1;
done:
(*closefunc)(fout);
if (logging > 2)
syslog(LOG_NOTICE, "put: %i bytes transferred", byte_count);
}
FILE *
getdatasock(fmode)
char *fmode;
{
int s, on = 1, tries;
if (data >= 0)
return (fdopen(data, fmode));
(void) krb5_seteuid((uid_t)0);
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0)
goto bad;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
(char *) &on, sizeof (on)) < 0)
goto bad;
/* anchor socket to avoid multi-homing problems */
data_source.sin_family = AF_INET;
data_source.sin_addr = ctrl_addr.sin_addr;
for (tries = 1; ; tries++) {
if (bind(s, (struct sockaddr *)&data_source,
sizeof (data_source)) >= 0)
break;
if (errno != EADDRINUSE || tries > 10)
goto bad;
sleep(tries);
}
if (krb5_seteuid((uid_t)pw->pw_uid)) {
fatal("seteuid user");
}
#ifdef IP_TOS
#ifdef IPTOS_THROUGHPUT
on = IPTOS_THROUGHPUT;
if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
#endif
#endif
return (fdopen(s, fmode));
bad:
if (krb5_seteuid((uid_t)pw->pw_uid)) {
fatal("seteuid user");
}
(void) close(s);
return (NULL);
}
FILE *
dataconn(name, size, fmode)
char *name;
off_t size;
char *fmode;
{
char sizebuf[32];
FILE *file;
int retry = 0, tos;
file_size = size;
byte_count = 0;
if (size != (off_t) -1)
/* cast size to long in case sizeof(off_t) > sizeof(long) */
(void) sprintf (sizebuf, " (%ld bytes)", (long)size);
else
(void) strcpy(sizebuf, "");
if (pdata >= 0) {
int s, fromlen = sizeof(data_dest);
s = accept(pdata, (struct sockaddr *)&data_dest, &fromlen);
if (s < 0) {
reply(425, "Can't open data connection.");
(void) close(pdata);
pdata = -1;
return(NULL);
}
(void) close(pdata);
pdata = s;
#ifdef IP_TOS
#ifdef IPTOS_LOWDELAY
tos = IPTOS_LOWDELAY;
(void) setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos,
sizeof(int));
#endif
#endif
reply(150, "Opening %s mode data connection for %s%s.",
type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
return(fdopen(pdata, fmode));
}
if (data >= 0) {
reply(125, "Using existing data connection for %s%s.",
name, sizebuf);
usedefault = 1;
return (fdopen(data, fmode));
}
if (usedefault)
data_dest = his_addr;
usedefault = 1;
file = getdatasock(fmode);
if (file == NULL) {
reply(425, "Can't create data socket (%s,%d): %s.",
inet_ntoa(data_source.sin_addr),
ntohs(data_source.sin_port), strerror(errno));
return (NULL);
}
data = fileno(file);
while (connect(data, (struct sockaddr *)&data_dest,
sizeof (data_dest)) < 0) {
if (errno == EADDRINUSE && retry < swaitmax) {
sleep((unsigned) swaitint);
retry += swaitint;
continue;
}
perror_reply(425, "Can't build data connection");
(void) fclose(file);
data = -1;
return (NULL);
}
reply(150, "Opening %s mode data connection for %s%s.",
type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
return (file);
}
/*
* XXX callers need to limit total length of output string to
* FTP_BUFSIZ
*/
void
secure_error(char *fmt, ...)
{
char buf[FTP_BUFSIZ];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
reply(535, "%s", buf);
syslog(LOG_ERR, "%s", buf);
}
/*
* Tranfer the contents of "instr" to
* "outstr" peer using the appropriate
* encapsulation of the data subject
* to Mode, Structure, and Type.
*
* NB: Form isn't handled.
*/
void send_data(instr, outstr, blksize)
FILE *instr, *outstr;
off_t blksize;
{
register int c, cnt;
register char *buf;
int netfd, filefd;
volatile int ret = 0;
transflag++;
if (sigsetjmp(urgcatch, 1)) {
transflag = 0;
(void)secure_flush(fileno(outstr));
return;
}
switch (type) {
case TYPE_A:
while ((c = getc(instr)) != EOF) {
byte_count++;
if (c == '\n') {
if (ferror(outstr) ||
(ret = secure_putc('\r', outstr)) < 0)
goto data_err;
}
if ((ret = secure_putc(c, outstr)) < 0)
goto data_err;
}
transflag = 0;
if (ferror(instr))
goto file_err;
if (ferror(outstr) ||
(ret = secure_flush(fileno(outstr))) < 0)
goto data_err;
reply(226, "Transfer complete.");
return;
case TYPE_I:
case TYPE_L:
if ((buf = malloc((u_int)blksize)) == NULL) {
transflag = 0;
perror_reply(451, "Local resource failure: malloc");
return;
}
netfd = fileno(outstr);
filefd = fileno(instr);
while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 &&
(ret = secure_write(netfd, buf, cnt)) == cnt)
byte_count += cnt;
transflag = 0;
(void)free(buf);
if (cnt != 0) {
if (cnt < 0)
goto file_err;
goto data_err;
}
if ((ret = secure_flush(netfd)) < 0)
goto data_err;
reply(226, "Transfer complete.");
return;
default:
transflag = 0;
reply(550, "Unimplemented TYPE %d in send_data", type);
return;
}
data_err:
transflag = 0;
if (ret != -2) perror_reply(426, "Data connection");
return;
file_err:
transflag = 0;
perror_reply(551, "Error on input file");
}
/*
* Transfer data from peer to
* "outstr" using the appropriate
* encapulation of the data subject
* to Mode, Structure, and Type.
*
* N.B.: Form isn't handled.
*/
static int
receive_data(instr, outstr)
FILE *instr, *outstr;
{
register int c;
volatile int cnt, bare_lfs = 0;
char buf[FTP_BUFSIZ];
int ret = 0;
transflag++;
if (sigsetjmp(urgcatch, 1)) {
transflag = 0;
return (-1);
}
switch (type) {
case TYPE_I:
case TYPE_L:
while ((cnt = secure_read(fileno(instr), buf, sizeof buf)) > 0) {
if (write(fileno(outstr), buf, cnt) != cnt)
goto file_err;
byte_count += cnt;
}
transflag = 0;
ret = cnt;
if (cnt < 0)
goto data_err;
return (0);
case TYPE_E:
reply(553, "TYPE E not implemented.");
transflag = 0;
return (-1);
case TYPE_A:
while ((c = secure_getc(instr)) >= 0) {
byte_count++;
if (c == '\n')
bare_lfs++;
while (c == '\r') {
if (ferror(outstr))
goto data_err;
if ((c = secure_getc(instr)) != '\n') {
(void) putc ('\r', outstr);
if (c == '\0')
goto contin2;
}
}
if (c < 0) break;
(void) putc(c, outstr);
contin2: ;
}
fflush(outstr);
ret = c;
if (c == -2 || ferror(instr))
goto data_err;
if (ferror(outstr))
goto file_err;
transflag = 0;
if (bare_lfs) {
lreply(226, "WARNING! %d bare linefeeds received in ASCII mode", bare_lfs);
reply(0, " File may not have transferred correctly.");
}
return (0);
default:
reply(550, "Unimplemented TYPE %d in receive_data", type);
transflag = 0;
return (-1);
}
data_err:
transflag = 0;
if (ret != -2) perror_reply(426, "Data Connection");
return (-1);
file_err:
transflag = 0;
perror_reply(452, "Error writing file");
return (-1);
}
void
statfilecmd(filename)
char *filename;
{
char line[FTP_BUFSIZ];
FILE *fin;
int c, n;
char str[FTP_BUFSIZ], *p;
if (strlen(filename) + sizeof("/bin/ls -lgA ")
>= sizeof(line)) {
reply(501, "filename too long");
return;
}
(void) sprintf(line, "/bin/ls -lgA %s", filename);
fin = ftpd_popen(line, "r");
lreply(211, "status of %s:", filename);
p = str;
n = 0;
while ((c = getc(fin)) != EOF) {
if (c == '\n') {
if (ferror(stdout)){
perror_reply(421, "control connection");
(void) ftpd_pclose(fin);
dologout(1);
/* NOTREACHED */
}
if (ferror(fin)) {
perror_reply(551, filename);
(void) ftpd_pclose(fin);
return;
}
*p = '\0';
reply(0, "%s", str);
p = str;
n = 0;
} else {
*p++ = c;
n++;
if (n >= sizeof(str)) {
reply(551, "output line too long");
(void) ftpd_pclose(fin);
return;
}
}
}
if (p != str) {
*p = '\0';
reply(0, "%s", str);
}
(void) ftpd_pclose(fin);
reply(211, "End of Status");
}
void
statcmd()
{
struct sockaddr_in *sin4;
u_char *a, *p;
char str[FTP_BUFSIZ];
lreply(211, "%s FTP server status:", hostname);
reply(0, " %s", version);
sprintf(str, " Connected to %s", remotehost[0] ? remotehost : "");
sprintf(&str[strlen(str)], " (%s)", rhost_addra);
reply(0, "%s", str);
if (auth_type) reply(0, " Authentication type: %s", auth_type);
if (logged_in) {
if (guest)
reply(0, " Logged in anonymously");
else
reply(0, " Logged in as %s", pw->pw_name);
} else if (askpasswd)
reply(0, " Waiting for password");
else if (temp_auth_type)
reply(0, " Waiting for authentication data");
else
reply(0, " Waiting for user name");
reply(0, " Protection level: %s", levelnames[dlevel]);
sprintf(str, " TYPE: %s", typenames[type]);
if (type == TYPE_A || type == TYPE_E)
sprintf(&str[strlen(str)], ", FORM: %s", formnames[form]);
if (type == TYPE_L)
#if 1
strncat(str, " 8", sizeof (str) - strlen(str) - 1);
#else
/* this is silly. -- eichin@cygnus.com */
#if NBBY == 8
sprintf(&str[strlen(str)], " %d", NBBY);
#else
sprintf(&str[strlen(str)], " %d", bytesize); /* need definition! */
#endif
#endif
sprintf(&str[strlen(str)], "; STRUcture: %s; transfer MODE: %s",
strunames[stru], modenames[mode]);
reply(0, "%s", str);
if (data != -1)
strcpy(str, " Data connection open");
else if (pdata != -1) {
strcpy(str, " in Passive mode");
sin4 = &pasv_addr;
goto printaddr;
} else if (usedefault == 0) {
strcpy(str, " PORT");
sin4 = &data_dest;
printaddr:
a = (u_char *) &sin4->sin_addr;
p = (u_char *) &sin4->sin_port;
#define UC(b) (((int) b) & 0xff)
sprintf(&str[strlen(str)], " (%d,%d,%d,%d,%d,%d)", UC(a[0]),
UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
#undef UC
} else
strcpy(str, " No data connection");
reply(0, "%s", str);
reply(211, "End of status");
}
void
fatal(s)
char *s;
{
reply(451, "Error in server: %s", s);
reply(221, "Closing connection due to server error.");
dologout(0);
/* NOTREACHED */
}
char cont_char = ' ';
/*
* XXX callers need to limit total length of output string to
* FTP_BUFSIZ bytes for now.
*/
#ifdef STDARG
void
reply(int n, char *fmt, ...)
#else
/* VARARGS2 */
void
reply(n, fmt, p0, p1, p2, p3, p4, p5)
int n;
char *fmt;
#endif
{
char buf[FTP_BUFSIZ];
#ifdef STDARG
va_list ap;
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
va_end(ap);
#else
sprintf(buf, fmt, p0, p1, p2, p3, p4, p5);
#endif
if (auth_type) {
/*
* Deal with expansion in mk_{safe,priv},
* radix_encode, gss_seal, plus slop.
*/
char in[FTP_BUFSIZ*3/2], out[FTP_BUFSIZ*3/2];
int length = 0, kerror;
if (n) sprintf(in, "%d%c", n, cont_char);
else in[0] = '\0';
strncat(in, buf, sizeof (in) - strlen(in) - 1);
#ifdef KRB5_KRB4_COMPAT
if (strcmp(auth_type, "KERBEROS_V4") == 0) {
if (clevel == PROT_P)
length = krb_mk_priv((unsigned char *)in,
(unsigned char *)out,
strlen(in),
schedule, &kdata.session,
&ctrl_addr,
&his_addr);
else
length = krb_mk_safe((unsigned char *)in,
(unsigned char *)out,
strlen(in),
&kdata.session,
&ctrl_addr,
&his_addr);
if (length == -1) {
syslog(LOG_ERR,
"krb_mk_%s failed for KERBEROS_V4",
clevel == PROT_P ? "priv" : "safe");
fputs(in,stdout);
}
} else
#endif /* KRB5_KRB4_COMPAT */
#ifdef GSSAPI
/* reply (based on level) */
if (strcmp(auth_type, "GSSAPI") == 0) {
gss_buffer_desc in_buf, out_buf;
OM_uint32 maj_stat, min_stat;
int conf_state;
in_buf.value = in;
in_buf.length = strlen(in);
maj_stat = gss_seal(&min_stat, gcontext,
clevel == PROT_P, /* private */
GSS_C_QOP_DEFAULT,
&in_buf, &conf_state,
&out_buf);
if (maj_stat != GSS_S_COMPLETE) {
#if 0
/* Don't setup an infinite loop */
/* generally need to deal */
secure_gss_error(maj_stat, min_stat,
(clevel==PROT_P)?
"gss_seal ENC didn't complete":
"gss_seal MIC didn't complete");
#endif /* 0 */
} else if ((clevel == PROT_P) && !conf_state) {
#if 0
/* Don't setup an infinite loop */
secure_error("GSSAPI didn't encrypt message");
#endif /* 0 */
} else {
memcpy(out, out_buf.value,
length=out_buf.length);
gss_release_buffer(&min_stat, &out_buf);
}
}
#endif /* GSSAPI */
/* Other auth types go here ... */
if (length >= sizeof(in) / 4 * 3) {
syslog(LOG_ERR, "input to radix_encode too long");
fputs(in, stdout);
} else if ((kerror = radix_encode(out, in, &length, 0))) {
syslog(LOG_ERR, "Couldn't encode reply (%s)",
radix_error(kerror));
fputs(in,stdout);
} else
printf("%s%c%s", clevel == PROT_P ? "632" : "631",
n ? cont_char : '-', in);
} else {
if (n) printf("%d%c", n, cont_char);
fputs(buf, stdout);
}
printf("\r\n");
(void)fflush(stdout);
if (debug) {
if (n) syslog(LOG_DEBUG, "<--- %d%c", n, cont_char);
syslog(LOG_DEBUG, "%s", buf);
}
}
/*
* XXX callers need to limit total length of output string to
* FTP_BUFSIZ
*/
#ifdef STDARG
void
lreply(int n, char *fmt, ...)
#else
/* VARARGS2 */
void
lreply(n, fmt, p0, p1, p2, p3, p4, p5)
int n;
char *fmt;
#endif
{
char buf[FTP_BUFSIZ];
#ifdef STDARG
va_list ap;
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
va_end(ap);
#else
sprintf(buf, fmt, p0, p1, p2, p3, p4, p5);
#endif
cont_char = '-';
reply(n, "%s", buf);
cont_char = ' ';
}
void
ack(s)
char *s;
{
reply(250, "%s command successful.", s);
}
void
nack(s)
char *s;
{
reply(502, "%s command not implemented.", s);
}
/* ARGSUSED */
void
yyerror(s)
char *s;
{
char *cp;
cp = strchr(cbuf,'\n');
if (cp)
*cp = '\0';
reply(500, "'%.*s': command not understood.",
(int) (FTP_BUFSIZ - sizeof("'': command not understood.")),
cbuf);
}
void
delete_file(name)
char *name;
{
struct stat st;
if (logging > 1) syslog(LOG_NOTICE, "del %s", path_expand(name));
if (stat(name, &st) < 0) {
perror_reply(550, name);
return;
}
if ((st.st_mode&S_IFMT) == S_IFDIR) {
if (rmdir(name) < 0) {
perror_reply(550, name);
return;
}
goto done;
}
if (unlink(name) < 0) {
perror_reply(550, name);
return;
}
done:
ack("DELE");
}
void
cwd(path)
char *path;
{
if (chdir(path) < 0)
perror_reply(550, path);
else
ack("CWD");
}
void
makedir(name)
char *name;
{
if (logging > 1) syslog(LOG_NOTICE, "mkdir %s", path_expand(name));
if (mkdir(name, 0777) < 0)
perror_reply(550, name);
else
reply(257, "MKD command successful.");
}
void
removedir(name)
char *name;
{
if (logging > 1) syslog(LOG_NOTICE, "rmdir %s", path_expand(name));
if (rmdir(name) < 0)
perror_reply(550, name);
else
ack("RMD");
}
void
pwd()
{
if (getcwd(pathbuf, sizeof pathbuf) == (char *)NULL)
#ifdef POSIX
perror_reply(550, pathbuf);
#else
reply(550, "%s.", pathbuf);
#endif
else
reply(257, "\"%s\" is current directory.", pathbuf);
}
char *
renamefrom(name)
char *name;
{
struct stat st;
if (stat(name, &st) < 0) {
perror_reply(550, name);
return ((char *)0);
}
reply(350, "File exists, ready for destination name");
return (name);
}
void
renamecmd(from, to)
char *from, *to;
{
if(logging > 1)
syslog(LOG_NOTICE, "rename %s %s", path_expand(from), to);
if (rename(from, to) < 0)
perror_reply(550, "rename");
else
ack("RNTO");
}
static void
dolog(sin4)
struct sockaddr_in *sin4;
{
struct hostent *hp = gethostbyaddr((char *)&sin4->sin_addr,
sizeof (struct in_addr), AF_INET);
time_t t, time();
extern char *ctime();
krb5_error_code retval;
if (hp != NULL) {
(void) strncpy(remotehost, hp->h_name, sizeof (remotehost));
remotehost[sizeof (remotehost) - 1] = '\0';
} else
remotehost[0] = '\0';
strncpy(rhost_addra, inet_ntoa(sin4->sin_addr), sizeof (rhost_addra));
rhost_addra[sizeof (rhost_addra) - 1] = '\0';
retval = pty_make_sane_hostname((struct sockaddr *) sin4, maxhostlen,
stripdomain, always_ip, &rhost_sane);
if (retval) {
fprintf(stderr, "make_sane_hostname: %s\n",
error_message(retval));
exit(1);
}
#ifdef SETPROCTITLE
sprintf(proctitle, "%s: connected", rhost_sane);
setproctitle(proctitle);
#endif /* SETPROCTITLE */
if (logging) {
t = time((time_t *) 0);
syslog(LOG_INFO, "connection from %s (%s) at %s",
rhost_addra, remotehost, ctime(&t));
}
}
/*
* Record logout in wtmp file
* and exit with supplied status.
*/
void
dologout(status)
int status;
{
if (logged_in) {
(void) krb5_seteuid((uid_t)0);
pty_logwtmp(ttyline, "", "");
}
if (have_creds) {
#ifdef GSSAPI
krb5_cc_destroy(kcontext, ccache);
#endif
#ifdef KRB5_KRB4_COMPAT
dest_tkt();
#endif
}
/* beware of flushing buffers after a SIGPIPE */
_exit(status);
}
void
myoob(sig)
int sig;
{
char *cp, *cs;
#ifndef strpbrk
extern char *strpbrk();
#endif
/* only process if transfer occurring */
if (!transflag)
return;
cp = tmpline;
if (ftpd_getline(cp, sizeof(tmpline), stdin) == NULL) {
reply(221, "You could at least say goodbye.");
dologout(0);
}
upper(cp);
if ((cs = strpbrk(cp, "\r\n")))
*cs++ = '\0';
if (strcmp(cp, "ABOR") == 0) {
tmpline[0] = '\0';
reply(426, "Transfer aborted. Data connection closed.");
reply(226, "Abort successful");
siglongjmp(urgcatch, 1);
}
if (strcmp(cp, "STAT") == 0) {
if (file_size != (off_t) -1)
reply(213, "Status: %lu of %lu bytes transferred",
(unsigned long) byte_count,
(unsigned long) file_size);
else
reply(213, "Status: %lu bytes transferred",
(unsigned long) byte_count);
}
}
/*
* Note: a response of 425 is not mentioned as a possible response to
* the PASV command in RFC959. However, it has been blessed as
* a legitimate response by Jon Postel in a telephone conversation
* with Rick Adams on 25 Jan 89.
*/
void
passive()
{
int len;
register char *p, *a;
pdata = socket(AF_INET, SOCK_STREAM, 0);
if (pdata < 0) {
perror_reply(425, "Can't open passive connection");
return;
}
pasv_addr = ctrl_addr;
pasv_addr.sin_port = 0;
(void) krb5_seteuid((uid_t)0);
if (bind(pdata, (struct sockaddr *)&pasv_addr, sizeof(pasv_addr)) < 0) {
(void) krb5_seteuid((uid_t)pw->pw_uid);
goto pasv_error;
}
if (krb5_seteuid((uid_t)pw->pw_uid)) {
fatal("seteuid user");
}
len = sizeof(pasv_addr);
if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
goto pasv_error;
if (listen(pdata, 1) < 0)
goto pasv_error;
a = (char *) &pasv_addr.sin_addr;
p = (char *) &pasv_addr.sin_port;
#define UC(b) (((int) b) & 0xff)
reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]),
UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
return;
pasv_error:
(void) close(pdata);
pdata = -1;
perror_reply(425, "Can't open passive connection");
return;
}
/*
* Generate unique name for file with basename "local".
* The file named "local" is already known to exist.
* Generates failure reply on error.
*/
static char *
gunique(local)
char *local;
{
static char new[MAXPATHLEN];
struct stat st;
char *cp = strrchr(local, '/');
int count = 0;
if (cp)
*cp = '\0';
if (stat(cp ? local : ".", &st) < 0) {
perror_reply(553, cp ? local : ".");
return((char *) 0);
}
if (cp)
*cp = '/';
(void) strncpy(new, local, sizeof(new) - 1);
new[sizeof(new) - 1] = '\0';
cp = new + strlen(new);
*cp++ = '.';
for (count = 1; count < 100; count++) {
(void) sprintf(cp, "%d", count);
if (stat(new, &st) < 0)
return(new);
}
reply(452, "Unique file name cannot be created.");
return((char *) 0);
}
/*
* Format and send reply containing system error number.
*/
void
perror_reply(code, string)
int code;
char *string;
{
char *err_string;
size_t extra_len;
err_string = strerror(errno);
if (err_string == NULL)
err_string = "(unknown error)";
extra_len = strlen(err_string) + sizeof("(truncated): .");
/*
* XXX knows about FTP_BUFSIZ in reply()
*/
if (strlen(string) + extra_len > FTP_BUFSIZ) {
reply(code, "(truncated)%.*s: %s.",
(int) (FTP_BUFSIZ - extra_len), string, err_string);
} else {
reply(code, "%s: %s.", string, err_string);
}
}
void
auth(atype)
char *atype;
{
if (auth_type)
reply(534, "Authentication type already set to %s", auth_type);
else
#ifdef KRB5_KRB4_COMPAT
if (strcmp(atype, "KERBEROS_V4") == 0)
reply(334, "Using authentication type %s; ADAT must follow",
temp_auth_type = atype);
else
#endif /* KRB5_KRB4_COMPAT */
#ifdef GSSAPI
if (strcmp(atype, "GSSAPI") == 0)
reply(334, "Using authentication type %s; ADAT must follow",
temp_auth_type = atype);
else
#endif /* GSSAPI */
/* Other auth types go here ... */
reply(504, "Unknown authentication type: %s", atype);
}
int
auth_data(adata)
char *adata;
{
int kerror, length;
#ifdef KRB5_KRB4_COMPAT
static char **service=NULL;
char instance[INST_SZ];
KRB4_32 cksum;
char buf[FTP_BUFSIZ];
u_char out_buf[sizeof(buf)];
#endif /* KRB5_KRB4_COMPAT */
if (auth_type) {
reply(503, "Authentication already established");
return(0);
}
if (!temp_auth_type) {
reply(503, "Must identify AUTH type before ADAT");
return(0);
}
#ifdef KRB5_KRB4_COMPAT
if (strcmp(temp_auth_type, "KERBEROS_V4") == 0) {
kerror = radix_encode(adata, out_buf, &length, 1);
if (kerror) {
reply(501, "Couldn't decode ADAT (%s)",
radix_error(kerror));
syslog(LOG_ERR, "Couldn't decode ADAT (%s)",
radix_error(kerror));
return(0);
}
(void) memcpy((char *)ticket.dat, (char *)out_buf, ticket.length = length);
strcpy(instance, "*");
kerror = 255;
for (service = krb4_services; *service; service++) {
kerror = krb_rd_req(&ticket, *service, instance,
his_addr.sin_addr.s_addr,
&kdata, keyfile);
/* Success */
if(!kerror) break;
}
/* rd_req failed.... */
if(kerror) {
secure_error("ADAT: Kerberos V4 krb_rd_req: %s",
krb_get_err_text(kerror));
return(0);
}
/* add one to the (formerly) sealed checksum, and re-seal it */
cksum = kdata.checksum + 1;
cksum = htonl(cksum);
key_sched(kdata.session,schedule);
if ((length = krb_mk_safe((u_char *)&cksum, out_buf, sizeof(cksum),
&kdata.session,&ctrl_addr, &his_addr)) == -1) {
secure_error("ADAT: krb_mk_safe failed");
return(0);
}
if (length >= (FTP_BUFSIZ - sizeof("ADAT=")) / 4 * 3) {
secure_error("ADAT: reply too long");
return(0);
}
kerror = radix_encode(out_buf, buf, &length, 0);
if (kerror) {
secure_error("Couldn't encode ADAT reply (%s)",
radix_error(kerror));
return(0);
}
reply(235, "ADAT=%s", buf);
/* Kerberos V4 authentication succeeded */
auth_type = temp_auth_type;
temp_auth_type = NULL;
return(1);
}
#endif /* KRB5_KRB4_COMPAT */
#ifdef GSSAPI
if (strcmp(temp_auth_type, "GSSAPI") == 0) {
int replied = 0;
int found = 0;
gss_cred_id_t server_creds, deleg_creds;
gss_name_t client;
OM_uint32 ret_flags;
int rad_len;
gss_buffer_desc name_buf;
gss_name_t server_name;
OM_uint32 acquire_maj, acquire_min, accept_maj, accept_min,
stat_maj, stat_min;
gss_OID mechid;
gss_buffer_desc tok, out_tok;
char gbuf[FTP_BUFSIZ];
u_char gout_buf[FTP_BUFSIZ];
char localname[MAXHOSTNAMELEN];
char service_name[MAXHOSTNAMELEN+10];
char **gservice;
struct hostent *hp;
stat_maj = 0;
accept_maj = 0;
acquire_maj = 0;
kerror = radix_encode(adata, gout_buf, &length, 1);
if (kerror) {
reply(501, "Couldn't decode ADAT (%s)",
radix_error(kerror));
syslog(LOG_ERR, "Couldn't decode ADAT (%s)",
radix_error(kerror));
return(0);
}
tok.value = gout_buf;
tok.length = length;
if (gethostname(localname, MAXHOSTNAMELEN)) {
reply(501, "couldn't get local hostname (%d)\n", errno);
syslog(LOG_ERR, "Couldn't get local hostname (%d)", errno);
return 0;
}
if (!(hp = gethostbyname(localname))) {
reply(501, "couldn't canonicalize local hostname\n");
syslog(LOG_ERR, "Couldn't canonicalize local hostname");
return 0;
}
strncpy(localname, hp->h_name, sizeof(localname) - 1);
localname[sizeof(localname) - 1] = '\0';
for (gservice = gss_services; *gservice; gservice++) {
sprintf(service_name, "%s@%s", *gservice, localname);
name_buf.value = service_name;
name_buf.length = strlen(name_buf.value) + 1;
if (debug)
syslog(LOG_INFO, "importing <%s>", service_name);
stat_maj = gss_import_name(&stat_min, &name_buf,
gss_nt_service_name,
&server_name);
if (stat_maj != GSS_S_COMPLETE) {
reply_gss_error(501, stat_maj, stat_min,
"importing name");
syslog(LOG_ERR, "gssapi error importing name");
return 0;
}
acquire_maj = gss_acquire_cred(&acquire_min, server_name, 0,
GSS_C_NULL_OID_SET, GSS_C_ACCEPT,
&server_creds, NULL, NULL);
(void) gss_release_name(&stat_min, &server_name);
if (acquire_maj != GSS_S_COMPLETE)
continue;
found++;
gcontext = GSS_C_NO_CONTEXT;
accept_maj = gss_accept_sec_context(&accept_min,
&gcontext, /* context_handle */
server_creds, /* verifier_cred_handle */
&tok, /* input_token */
GSS_C_NO_CHANNEL_BINDINGS, /* channel bindings */
&client, /* src_name */
&mechid, /* mech_type */
&out_tok, /* output_token */
&ret_flags,
NULL, /* ignore time_rec */
&deleg_creds /* forwarded credentials */
);
if (accept_maj==GSS_S_COMPLETE||accept_maj==GSS_S_CONTINUE_NEEDED)
break;
}
if (found) {
if (accept_maj!=GSS_S_COMPLETE && accept_maj!=GSS_S_CONTINUE_NEEDED) {
reply_gss_error(535, accept_maj, accept_min,
"accepting context");
syslog(LOG_ERR, "failed accepting context");
(void) gss_release_cred(&stat_min, &server_creds);
if (ret_flags & GSS_C_DELEG_FLAG)
(void) gss_release_cred(&stat_min,
&deleg_creds);
return 0;
}
} else {
/* Kludge to make sure the right error gets reported, so we don't *
* get those nasty "error: no error" messages. */
if(stat_maj != GSS_S_COMPLETE)
reply_gss_error(501, stat_maj, stat_min,
"acquiring credentials");
else
reply_gss_error(501, acquire_maj, acquire_min,
"acquiring credentials");
syslog(LOG_ERR, "gssapi error acquiring credentials");
return 0;
}
if (out_tok.length) {
if (out_tok.length >= ((FTP_BUFSIZ - sizeof("ADAT="))
/ 4 * 3)) {
secure_error("ADAT: reply too long");
syslog(LOG_ERR, "ADAT: reply too long");
(void) gss_release_cred(&stat_min, &server_creds);
if (ret_flags & GSS_C_DELEG_FLAG)
(void) gss_release_cred(&stat_min,
&deleg_creds);
return(0);
}
rad_len = out_tok.length;
kerror = radix_encode(out_tok.value, gbuf,
&rad_len, 0);
out_tok.length = rad_len;
if (kerror) {
secure_error("Couldn't encode ADAT reply (%s)",
radix_error(kerror));
syslog(LOG_ERR, "couldn't encode ADAT reply");
(void) gss_release_cred(&stat_min, &server_creds);
if (ret_flags & GSS_C_DELEG_FLAG)
(void) gss_release_cred(&stat_min,
&deleg_creds);
return(0);
}
if (accept_maj == GSS_S_COMPLETE) {
reply(235, "ADAT=%s", gbuf);
} else {
/* If the server accepts the security data, and
requires additional data, it should respond
with reply code 335. */
reply(335, "ADAT=%s", gbuf);
}
replied = 1;
(void) gss_release_buffer(&stat_min, &out_tok);
}
if (accept_maj == GSS_S_COMPLETE) {
/* GSSAPI authentication succeeded */
stat_maj = gss_display_name(&stat_min, client,
&client_name, &mechid);
if (stat_maj != GSS_S_COMPLETE) {
/* "If the server rejects the security data (if
a checksum fails, for instance), it should
respond with reply code 535." */
reply_gss_error(535, stat_maj, stat_min,
"extracting GSSAPI identity name");
log_gss_error(LOG_ERR, stat_maj, stat_min,
"gssapi error extracting identity");
(void) gss_release_cred(&stat_min, &server_creds);
if (ret_flags & GSS_C_DELEG_FLAG)
(void) gss_release_cred(&stat_min,
&deleg_creds);
return 0;
}
auth_type = temp_auth_type;
temp_auth_type = NULL;
(void) gss_release_cred(&stat_min, &server_creds);
if (ret_flags & GSS_C_DELEG_FLAG) {
if (want_creds)
ftpd_gss_convert_creds(client_name.value,
deleg_creds);
(void) gss_release_cred(&stat_min, &deleg_creds);
}
/* If the server accepts the security data, but does
not require any additional data (i.e., the security
data exchange has completed successfully), it must
respond with reply code 235. */
if (!replied)
{
if (ret_flags & GSS_C_DELEG_FLAG && !have_creds)
reply(235, "GSSAPI Authentication succeeded, but could not accept forwarded credentials");
else
reply(235, "GSSAPI Authentication succeeded");
}
return(1);
} else if (accept_maj == GSS_S_CONTINUE_NEEDED) {
/* If the server accepts the security data, and
requires additional data, it should respond with
reply code 335. */
if (!replied)
reply(335, "more data needed");
(void) gss_release_cred(&stat_min, &server_creds);
if (ret_flags & GSS_C_DELEG_FLAG)
(void) gss_release_cred(&stat_min, &deleg_creds);
return(0);
} else {
/* "If the server rejects the security data (if
a checksum fails, for instance), it should
respond with reply code 535." */
reply_gss_error(535, stat_maj, stat_min,
"GSSAPI failed processing ADAT");
syslog(LOG_ERR, "GSSAPI failed processing ADAT");
(void) gss_release_cred(&stat_min, &server_creds);
if (ret_flags & GSS_C_DELEG_FLAG)
(void) gss_release_cred(&stat_min, &deleg_creds);
return(0);
}
}
#endif /* GSSAPI */
/* Other auth types go here ... */
/* Also need to check authorization, but that is done in user() */
return(0);
}
static char *onefile[] = {
"",
0
};
/* returns:
* n>=0 on success
* -1 on error
* -2 on security error
*
* XXX callers need to limit total length of output string to
* FTP_BUFSIZ
*/
static int
secure_fprintf(FILE *stream, char *fmt, ...)
#if !defined(__cplusplus) && (__GNUC__ > 2)
__attribute__((__format__(__printf__, 2, 3)))
#endif
;
static int
secure_fprintf(FILE *stream, char *fmt, ...)
{
char s[FTP_BUFSIZ];
int rval;
va_list ap;
va_start(ap, fmt);
if (dlevel == PROT_C) rval = vfprintf(stream, fmt, ap);
else {
vsnprintf(s, sizeof(s), fmt, ap);
rval = secure_write(fileno(stream), s, strlen(s));
}
va_end(ap);
return(rval);
}
void
send_file_list(whichfiles)
char *whichfiles;
{
struct stat st;
DIR *dirp = NULL;
struct dirent *dir;
FILE *volatile dout = NULL;
register char **volatile dirlist, *dirname;
volatile int simple = 0;
#ifndef strpbrk
char *strpbrk();
#endif
volatile int ret = 0;
if (strpbrk(whichfiles, "~{[*?") != NULL) {
extern char **ftpglob(), *globerr;
globerr = NULL;
dirlist = ftpglob(whichfiles);
if (globerr != NULL) {
reply(550, globerr);
return;
} else if (dirlist == NULL) {
errno = ENOENT;
perror_reply(550, whichfiles);
return;
}
} else {
onefile[0] = whichfiles;
dirlist = onefile;
simple = 1;
}
if (sigsetjmp(urgcatch, 1)) {
transflag = 0;
(void)secure_flush(fileno(dout));
return;
}
while ((dirname = *dirlist++)) {
if (stat(dirname, &st) < 0) {
/*
* If user typed "ls -l", etc, and the client
* used NLST, do what the user meant.
*/
if (dirname[0] == '-' && *dirlist == NULL &&
transflag == 0) {
retrieve("/bin/ls %s", dirname);
return;
}
perror_reply(550, whichfiles);
if (dout != NULL) {
(void) fclose(dout);
transflag = 0;
data = -1;
pdata = -1;
}
return;
}
if ((st.st_mode&S_IFMT) == S_IFREG) {
if (dout == NULL) {
dout = dataconn("file list", (off_t)-1, "w");
if (dout == NULL)
return;
transflag++;
}
if ((ret = secure_fprintf(dout, "%s%s\n", dirname,
type == TYPE_A ? "\r" : "")) < 0)
goto data_err;
byte_count += strlen(dirname) + 1;
continue;
} else if ((st.st_mode&S_IFMT) != S_IFDIR)
continue;
if ((dirp = opendir(dirname)) == NULL)
continue;
while ((dir = readdir(dirp)) != NULL) {
char nbuf[MAXPATHLEN];
if (dir->d_name[0] == '.' && dir->d_name[1] == '\0')
continue;
if (dir->d_name[0] == '.' && dir->d_name[1] == '.' &&
dir->d_name[2] == '\0')
continue;
if (strlen(dirname) + strlen(dir->d_name)
+ 1 /* slash */
+ 2 /* CRLF */
+ 1 > sizeof(nbuf)) {
syslog(LOG_ERR,
"send_file_list: pathname too long");
ret = -2; /* XXX */
goto data_err;
}
sprintf(nbuf, "%s/%s", dirname, dir->d_name);
/*
* We have to do a stat to insure it's
* not a directory or special file.
*/
if (simple || (stat(nbuf, &st) == 0 &&
(st.st_mode&S_IFMT) == S_IFREG)) {
if (dout == NULL) {
dout = dataconn("file list", (off_t)-1,
"w");
if (dout == NULL)
return;
transflag++;
}
if (nbuf[0] == '.' && nbuf[1] == '/')
{
if ((ret = secure_fprintf(dout, "%s%s\n", &nbuf[2],
type == TYPE_A ? "\r" : "")) < 0)
goto data_err;
}
else
if ((ret = secure_fprintf(dout, "%s%s\n", nbuf,
type == TYPE_A ? "\r" : "")) < 0)
goto data_err;
byte_count += strlen(nbuf) + 1;
}
}
(void) closedir(dirp);
}
if (dout != NULL ) {
ret = secure_write(fileno(dout), "", 0);
if (ret >= 0)
ret = secure_flush(fileno(dout));
}
data_err:
if (dout == NULL)
reply(550, "No files found.");
else if (ferror(dout) != 0 || ret == EOF)
perror_reply(550, "Data connection");
else if (ret != -2)
reply(226, "Transfer complete.");
transflag = 0;
if (dout != NULL)
(void) fclose(dout);
data = -1;
pdata = -1;
}
#ifdef SETPROCTITLE
/*
* clobber argv so ps will show what we're doing.
* (stolen from sendmail)
* warning, since this is usually started from inetd.conf, it
* often doesn't have much of an environment or arglist to overwrite.
*/
setproctitle(buf)
char *buf;
{
register char *p, *bp, ch;
register int i;
/* make ps print our process name */
p = Argv[0];
*p++ = '-';
i = strlen(buf);
if (i > LastArgv - p - 2) {
i = LastArgv - p - 2;
buf[i] = '\0';
}
bp = buf;
while (ch = *bp++)
if (ch != '\n' && ch != '\r')
*p++ = ch;
while (p < LastArgv)
*p++ = ' ';
}
#endif /* SETPROCTITLE */
#ifdef GSSAPI
/* A more general callback would probably use a void*, but currently I
only need an int in both cases. */
static void with_gss_error_text(void (*cb)(const char *, int, int),
OM_uint32 maj_stat, OM_uint32 min_stat,
int misc);
static void
log_gss_error_1(const char *msg, int severity, int is_major)
{
syslog(severity, "... GSSAPI error %s: %s",
is_major ? "major" : "minor", msg);
}
static void
log_gss_error(int severity, OM_uint32 maj_stat, OM_uint32 min_stat,
const char *s)
{
syslog(severity, s);
with_gss_error_text(log_gss_error_1, maj_stat, min_stat, severity);
}
static void
reply_gss_error_1(const char *msg, int code, int is_major)
{
lreply(code, "GSSAPI error %s: %s",
is_major ? "major" : "minor", msg);
}
void
reply_gss_error(int code, OM_uint32 maj_stat, OM_uint32 min_stat, char *s)
{
with_gss_error_text(reply_gss_error_1, maj_stat, min_stat, code);
reply(code, "GSSAPI error: %s", s);
}
static void with_gss_error_text(void (*cb)(const char *, int, int),
OM_uint32 maj_stat, OM_uint32 min_stat,
int misc)
{
/* a lot of work just to report the error */
OM_uint32 gmaj_stat, gmin_stat;
gss_buffer_desc msg;
OM_uint32 msg_ctx;
msg_ctx = 0;
while (!msg_ctx) {
gmaj_stat = gss_display_status(&gmin_stat, maj_stat,
GSS_C_GSS_CODE,
GSS_C_NULL_OID,
&msg_ctx, &msg);
if ((gmaj_stat == GSS_S_COMPLETE)||
(gmaj_stat == GSS_S_CONTINUE_NEEDED)) {
(*cb)((char*)msg.value, misc, 1);
(void) gss_release_buffer(&gmin_stat, &msg);
}
if (gmaj_stat != GSS_S_CONTINUE_NEEDED)
break;
}
msg_ctx = 0;
while (!msg_ctx) {
gmaj_stat = gss_display_status(&gmin_stat, min_stat,
GSS_C_MECH_CODE,
GSS_C_NULL_OID,
&msg_ctx, &msg);
if ((gmaj_stat == GSS_S_COMPLETE)||
(gmaj_stat == GSS_S_CONTINUE_NEEDED)) {
(*cb)((char*)msg.value, misc, 0);
(void) gss_release_buffer(&gmin_stat, &msg);
}
if (gmaj_stat != GSS_S_CONTINUE_NEEDED)
break;
}
}
void
secure_gss_error(maj_stat, min_stat, s)
OM_uint32 maj_stat, min_stat;
char *s;
{
reply_gss_error(535, maj_stat, min_stat, s);
return;
}
/* ftpd_gss_userok -- hide details of getting the name and verifying it */
/* returns 0 for OK */
static int
ftpd_gss_userok(gclient_name, name)
gss_buffer_t gclient_name;
char *name;
{
int retval = -1;
krb5_principal p;
if (krb5_parse_name(kcontext, gclient_name->value, &p) != 0)
return -1;
if (krb5_kuserok(kcontext, p, name))
retval = 0;
else
retval = 1;
krb5_free_principal(kcontext, p);
return retval;
}
/* ftpd_gss_convert_creds -- write out forwarded creds */
/* (code lifted from login.krb5) */
static void
ftpd_gss_convert_creds(name, creds)
char *name;
gss_cred_id_t creds;
{
OM_uint32 major_status, minor_status;
krb5_principal me;
char ccname[MAXPATHLEN];
#ifdef KRB5_KRB4_COMPAT
krb5_principal kpcserver;
krb5_creds increds, *v5creds;
CREDENTIALS v4creds;
#endif
/* Set up ccache */
if (krb5_parse_name(kcontext, name, &me))
return;
sprintf(ccname, "FILE:/tmp/krb5cc_ftpd%ld", (long) getpid());
if (krb5_cc_resolve(kcontext, ccname, &ccache))
return;
if (krb5_cc_initialize(kcontext, ccache, me))
return;
/* Copy GSS creds into ccache */
major_status = gss_krb5_copy_ccache(&minor_status, creds, ccache);
if (major_status != GSS_S_COMPLETE)
goto cleanup;
#ifdef KRB5_KRB4_COMPAT
/* Convert krb5 creds to krb4 */
if (krb5_build_principal_ext(kcontext, &kpcserver,
krb5_princ_realm(kcontext, me)->length,
krb5_princ_realm(kcontext, me)->data,
6, "krbtgt",
krb5_princ_realm(kcontext, me)->length,
krb5_princ_realm(kcontext, me)->data,
0))
goto cleanup;
memset((char *) &increds, 0, sizeof(increds));
increds.client = me;
increds.server = kpcserver;
increds.times.endtime = 0;
increds.keyblock.enctype = ENCTYPE_DES_CBC_CRC;
if (krb5_get_credentials(kcontext, 0, ccache, &increds, &v5creds))
goto cleanup;
if (krb524_convert_creds_kdc(kcontext, v5creds, &v4creds))
goto cleanup;
sprintf(ccname, "%s_ftpd%ld", TKT_ROOT, (long) getpid());
krb_set_tkt_string(ccname);
if (in_tkt(v4creds.pname, v4creds.pinst) != KSUCCESS)
goto cleanup;
if (krb_save_credentials(v4creds.service, v4creds.instance,
v4creds.realm, v4creds.session,
v4creds.lifetime, v4creds.kvno,
&(v4creds.ticket_st), v4creds.issue_date))
goto cleanup_v4;
#endif /* KRB5_KRB4_COMPAT */
have_creds = 1;
return;
#ifdef KRB5_KRB4_COMPAT
cleanup_v4:
dest_tkt();
#endif
cleanup:
krb5_cc_destroy(kcontext, ccache);
}
#endif /* GSSAPI */
|