1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
|
/*
* command.c Command socket processing.
*
* Version: $Id$
*
* 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 St, Fifth Floor, Boston, MA 02110-1301, USA
*
* Copyright 2008 The FreeRADIUS server project
* Copyright 2008 Alan DeKok <aland@deployingradius.com>
*/
#ifdef WITH_COMMAND_SOCKET
#include <freeradius-devel/parser.h>
#include <freeradius-devel/modcall.h>
#include <freeradius-devel/md5.h>
#include <freeradius-devel/channel.h>
#include <freeradius-devel/connection.h>
#include <libgen.h>
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#ifdef HAVE_SYS_UN_H
#include <sys/un.h>
#ifndef SUN_LEN
#define SUN_LEN(su) (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
#endif
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include <pwd.h>
#include <grp.h>
#include <ctype.h>
typedef struct fr_command_table_t fr_command_table_t;
typedef int (*fr_command_func_t)(rad_listen_t *, int, char *argv[]);
#define FR_READ (1)
#define FR_WRITE (2)
#define CMD_FAIL FR_CHANNEL_FAIL
#define CMD_OK FR_CHANNEL_SUCCESS
struct fr_command_table_t {
char const *command;
int mode; /* read/write */
char const *help;
fr_command_func_t func;
fr_command_table_t *table;
};
#define COMMAND_BUFFER_SIZE (1024)
typedef struct fr_cs_buffer_t {
int auth;
int mode;
ssize_t offset;
ssize_t next;
char buffer[COMMAND_BUFFER_SIZE];
} fr_cs_buffer_t;
#define COMMAND_SOCKET_MAGIC (0xffdeadee)
typedef struct fr_command_socket_t {
uint32_t magic;
char const *path;
char *copy; /* <sigh> */
uid_t uid;
gid_t gid;
char const *uid_name;
char const *gid_name;
char const *mode_name;
bool peercred;
char user[256];
/*
* The next few entries handle fake packets injected by
* the control socket.
*/
fr_ipaddr_t src_ipaddr; /* src_port is always 0 */
fr_ipaddr_t dst_ipaddr;
uint16_t dst_port;
rad_listen_t *inject_listener;
RADCLIENT *inject_client;
fr_cs_buffer_t co;
} fr_command_socket_t;
static const CONF_PARSER command_config[] = {
{ "socket", FR_CONF_OFFSET(PW_TYPE_STRING, fr_command_socket_t, path), "${run_dir}/radiusd.sock" },
{ "uid", FR_CONF_OFFSET(PW_TYPE_STRING, fr_command_socket_t, uid_name), NULL },
{ "gid", FR_CONF_OFFSET(PW_TYPE_STRING, fr_command_socket_t, gid_name), NULL },
{ "mode", FR_CONF_OFFSET(PW_TYPE_STRING, fr_command_socket_t, mode_name), NULL },
{ "peercred", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_command_socket_t, peercred), "yes" },
CONF_PARSER_TERMINATOR
};
static FR_NAME_NUMBER mode_names[] = {
{ "ro", FR_READ },
{ "read-only", FR_READ },
{ "read-write", FR_READ | FR_WRITE },
{ "rw", FR_READ | FR_WRITE },
{ NULL, 0 }
};
#if !defined(HAVE_GETPEEREID) && defined(SO_PEERCRED)
static int getpeereid(int s, uid_t *euid, gid_t *egid)
{
struct ucred cr;
socklen_t cl = sizeof(cr);
if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cl) < 0) {
return -1;
}
*euid = cr.uid;
*egid = cr.gid;
return 0;
}
/* we now have getpeereid() in this file */
#define HAVE_GETPEEREID (1)
#endif /* HAVE_GETPEEREID */
/** Initialise a socket for use with peercred authentication
*
* This function initialises a socket and path in a way suitable for use with
* peercred.
*
* @param path to socket.
* @param uid that should own the socket (linux only).
* @param gid that should own the socket (linux only).
* @return 0 on success -1 on failure.
*/
#ifdef __linux__
static int fr_server_domain_socket_peercred(char const *path, uid_t uid, gid_t gid)
#else
static int fr_server_domain_socket_peercred(char const *path, uid_t UNUSED uid, UNUSED gid_t gid)
#endif
{
int sockfd;
size_t len;
socklen_t socklen;
struct sockaddr_un salocal;
struct stat buf;
if (!path) {
fr_strerror_printf("No path provided, was NULL");
return -1;
}
len = strlen(path);
if (len >= sizeof(salocal.sun_path)) {
fr_strerror_printf("Path too long in socket filename");
return -1;
}
if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
fr_strerror_printf("Failed creating socket: %s", fr_syserror(errno));
return -1;
}
memset(&salocal, 0, sizeof(salocal));
salocal.sun_family = AF_UNIX;
memcpy(salocal.sun_path, path, len + 1); /* SUN_LEN does strlen */
socklen = SUN_LEN(&salocal);
/*
* Check the path.
*/
if (stat(path, &buf) < 0) {
if (errno != ENOENT) {
fr_strerror_printf("Failed to stat %s: %s", path, fr_syserror(errno));
close(sockfd);
return -1;
}
/*
* FIXME: Check the enclosing directory?
*/
} else { /* it exists */
int client_fd;
if (!S_ISREG(buf.st_mode)
#ifdef S_ISSOCK
&& !S_ISSOCK(buf.st_mode)
#endif
) {
fr_strerror_printf("Cannot turn %s into socket", path);
close(sockfd);
return -1;
}
/*
* Refuse to open sockets not owned by us.
*/
if (buf.st_uid != geteuid()) {
fr_strerror_printf("We do not own %s", path);
close(sockfd);
return -1;
}
/*
* Check if a server is already listening on the
* socket?
*/
client_fd = fr_socket_client_unix(path, false);
if (client_fd >= 0) {
fr_strerror_printf("Control socket '%s' is already in use", path);
close(client_fd);
close(sockfd);
return -1;
}
if (unlink(path) < 0) {
fr_strerror_printf("Failed to delete %s: %s", path, fr_syserror(errno));
close(sockfd);
return -1;
}
}
if (bind(sockfd, (struct sockaddr *)&salocal, socklen) < 0) {
fr_strerror_printf("Failed binding to %s: %s", path, fr_syserror(errno));
close(sockfd);
return -1;
}
/*
* FIXME: There's a race condition here. But Linux
* doesn't seem to permit fchmod on domain sockets.
*/
if (chmod(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) < 0) {
fr_strerror_printf("Failed setting permissions on %s: %s", path, fr_syserror(errno));
close(sockfd);
return -1;
}
if (listen(sockfd, 8) < 0) {
fr_strerror_printf("Failed listening to %s: %s", path, fr_syserror(errno));
close(sockfd);
return -1;
}
#ifdef O_NONBLOCK
{
int flags;
if ((flags = fcntl(sockfd, F_GETFL, NULL)) < 0) {
fr_strerror_printf("Failure getting socket flags: %s", fr_syserror(errno));
close(sockfd);
return -1;
}
flags |= O_NONBLOCK;
if( fcntl(sockfd, F_SETFL, flags) < 0) {
fr_strerror_printf("Failure setting socket flags: %s", fr_syserror(errno));
close(sockfd);
return -1;
}
}
#endif
/*
* Changing socket permissions only works on linux.
* BSDs ignore socket permissions.
*/
#ifdef __linux__
/*
* Don't chown it from (possibly) non-root to root.
* Do chown it from (possibly) root to non-root.
*/
if ((uid != (uid_t) -1) || (gid != (gid_t) -1)) {
/*
* Don't do chown if it's already owned by us.
*/
if (fstat(sockfd, &buf) < 0) {
fr_strerror_printf("Failed reading %s: %s", path, fr_syserror(errno));
close(sockfd);
return -1;
}
if ((buf.st_uid != uid) || (buf.st_gid != gid)) {
rad_suid_up();
if (fchown(sockfd, uid, gid) < 0) {
fr_strerror_printf("Failed setting ownership of %s to (%d, %d): %s",
path, uid, gid, fr_syserror(errno));
rad_suid_down();
close(sockfd);
return -1;
}
rad_suid_down();
}
}
#endif
return sockfd;
}
#if !defined(HAVE_OPENAT) || !defined(HAVE_MKDIRAT) || !defined(HAVE_UNLINKAT)
static int fr_server_domain_socket_perm(UNUSED char const *path, UNUSED uid_t uid, UNUSED gid_t gid)
{
fr_strerror_printf("Unable to initialise control socket. Set peercred = yes or update to "
"POSIX-2008 compliant libc");
return -1;
}
#else
/** Alternative function for creating Unix domain sockets and enforcing permissions
*
* Unlike fr_server_unix_socket which is intended to be used with peercred auth
* this function relies on the file system to enforce access.
*
* The way it does this depends on the operating system. On Linux systems permissions
* can be set on the socket directly and the system will enforce them.
*
* On most other systems fchown and fchmod fail when called with socket descriptors,
* and although permissions can be changed in other ways, they're not enforced.
*
* For these systems we use the permissions on the parent directory to enforce
* permissions on the socket. It's not safe to modify these permissions ourselves
* due to TOCTOU attacks, so if they don't match what we require, we error out and
* get the user to change them (which arguably isn't any safer, but releases us of
* the responsibility).
*
* @note must be called without effective root permissions (fr_suid_down).
*
* @param path where domain socket should be created.
* @return a file descriptor for the bound socket on success, -1 on failure.
*/
static int fr_server_domain_socket_perm(char const *path, uid_t uid, gid_t gid)
{
int dir_fd = -1, sock_fd = -1, parent_fd = -1;
char const *name;
char *buff = NULL, *dir = NULL, *p;
uid_t euid;
gid_t egid;
mode_t perm = 0;
struct stat st;
size_t len;
socklen_t socklen;
struct sockaddr_un salocal;
rad_assert(path);
euid = geteuid();
egid = getegid();
/*
* Determine the correct permissions for the socket, or its
* containing directory.
*/
perm |= S_IREAD | S_IWRITE | S_IEXEC;
if (gid != (gid_t) -1) perm |= S_IRGRP | S_IWGRP | S_IXGRP;
buff = talloc_strdup(NULL, path);
if (!buff) return -1;
/*
* Some implementations modify it in place others use internal
* storage *sigh*. dirname also formats the path else we wouldn't
* be using it.
*/
dir = dirname(buff);
if (dir != buff) {
dir = talloc_strdup(NULL, dir);
if (!dir) return -1;
talloc_free(buff);
}
p = strrchr(dir, FR_DIR_SEP);
if (!p) {
fr_strerror_printf("Failed determining parent directory");
error:
talloc_free(dir);
if (sock_fd >= 0) close(sock_fd);
if (dir_fd >= 0) close(dir_fd);
if (parent_fd >= 0) close(parent_fd);
return -1;
}
*p = '\0';
/*
* Ensure the parent of the control socket directory exists,
* and the euid we're running under has access to it.
*/
parent_fd = open(dir, O_DIRECTORY);
if (parent_fd < 0) {
struct passwd *user;
struct group *group;
if (rad_getpwuid(NULL, &user, euid) < 0) goto error;
if (rad_getgrgid(NULL, &group, egid) < 0) {
talloc_free(user);
goto error;
}
fr_strerror_printf("Can't open directory \"%s\": %s. Must be created manually, or modified, "
"with permissions that allow writing by user %s or group %s", dir,
user->pw_name, group->gr_name, fr_syserror(errno));
talloc_free(user);
talloc_free(group);
goto error;
}
*p = FR_DIR_SEP;
dir_fd = openat(parent_fd, p + 1, O_NOFOLLOW | O_DIRECTORY);
if (dir_fd < 0) {
int ret = 0;
if (errno != ENOENT) {
fr_strerror_printf("Failed opening control socket directory: %s", fr_syserror(errno));
goto error;
}
/*
* This fails if the radius user can't write
* to the parent directory.
*/
if (mkdirat(parent_fd, p + 1, 0700) < 0) {
fr_strerror_printf("Failed creating control socket directory: %s", fr_syserror(errno));
goto error;
}
dir_fd = openat(parent_fd, p + 1, O_NOFOLLOW | O_DIRECTORY);
if (dir_fd < 0) {
fr_strerror_printf("Failed opening the control socket directory we created: %s",
fr_syserror(errno));
goto error;
}
if (fchmod(dir_fd, perm) < 0) {
fr_strerror_printf("Failed setting permissions on control socket directory: %s",
fr_syserror(errno));
goto error;
}
rad_suid_up();
if ((uid != (uid_t)-1) || (gid != (gid_t)-1)) ret = fchown(dir_fd, uid, gid);
rad_suid_down();
if (ret < 0) {
fr_strerror_printf("Failed changing ownership of control socket directory: %s",
fr_syserror(errno));
goto error;
}
/*
* Control socket dir already exists, but we still need to
* check the permissions are what we expect.
*/
} else {
int ret;
int client_fd;
ret = fstat(dir_fd, &st);
if (ret < 0) {
fr_strerror_printf("Failed checking permissions of control socket directory: %s",
fr_syserror(errno));
goto error;
}
if ((uid != (uid_t)-1) && (st.st_uid != uid)) {
struct passwd *need_user, *have_user;
if (rad_getpwuid(NULL, &need_user, uid) < 0) goto error;
if (rad_getpwuid(NULL, &have_user, st.st_uid) < 0) {
talloc_free(need_user);
goto error;
}
fr_strerror_printf("Control socket directory must be owned by user %s, "
"currently owned by %s", need_user->pw_name, have_user->pw_name);
talloc_free(need_user);
talloc_free(have_user);
goto error;
}
if ((gid != (gid_t)-1) && (st.st_gid != gid)) {
struct group *need_group, *have_group;
if (rad_getgrgid(NULL, &need_group, gid) < 0) goto error;
if (rad_getgrgid(NULL, &have_group, st.st_gid) < 0) {
talloc_free(need_group);
goto error;
}
fr_strerror_printf("Control socket directory \"%s\" must be owned by group %s, "
"currently owned by %s", dir, need_group->gr_name, have_group->gr_name);
talloc_free(need_group);
talloc_free(have_group);
goto error;
}
if ((perm & 0x0c) != (st.st_mode & 0x0c)) {
char str_need[10], oct_need[5];
char str_have[10], oct_have[5];
rad_mode_to_str(str_need, perm);
rad_mode_to_oct(oct_need, perm);
rad_mode_to_str(str_have, st.st_mode);
rad_mode_to_oct(oct_have, st.st_mode);
fr_strerror_printf("Control socket directory must have permissions %s (%s), current "
"permissions are %s (%s)", str_need, oct_need, str_have, oct_have);
goto error;
}
/*
* Check if a server is already listening on the
* socket?
*/
client_fd = fr_socket_client_unix(path, false);
if (client_fd >= 0) {
fr_strerror_printf("Control socket '%s' is already in use", path);
close(client_fd);
goto error;
}
}
name = strrchr(path, FR_DIR_SEP);
if (!name) {
fr_strerror_printf("Can't determine socket name");
goto error;
}
name++;
/*
* We've checked the containing directory has the permissions
* we expect, and as we have the FD, and aren't following
* symlinks no one can trick us into changing or creating a
* file elsewhere.
*
* It's possible an attacker may still be able to create hard
* links, for the socket file. But they would need write
* access to the directory we just created or verified, so
* this attack vector is unlikely.
*/
if ((uid != (uid_t)-1) && (rad_seuid(uid) < 0)) goto error;
if ((gid != (gid_t)-1) && (rad_segid(gid) < 0)) {
rad_seuid(euid);
goto error;
}
/*
* The original code, did openat, used fstat to figure out
* what type the file was and then used unlinkat to unlink
* it. Except on OSX (at least) openat refuses to open
* socket files. So we now rely on the fact that unlinkat
* has sane and consistent behaviour, and will not unlink
* directories. unlinkat should also fail if the socket user
* hasn't got permission to modify the socket.
*/
if ((unlinkat(dir_fd, name, 0) < 0) && (errno != ENOENT)) {
fr_strerror_printf("Failed removing stale socket: %s", fr_syserror(errno));
sock_error:
if (uid != (uid_t)-1) rad_seuid(euid);
if (gid != (gid_t)-1) rad_segid(egid);
close(sock_fd);
goto error;
}
/*
* At this point we should have established a secure directory
* to house our socket, and cleared out any stale sockets.
*/
sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock_fd < 0) {
fr_strerror_printf("Failed creating socket: %s", fr_syserror(errno));
goto sock_error;
}
#ifdef HAVE_BINDAT
len = strlen(name);
#else
len = strlen(path);
#endif
if (len >= sizeof(salocal.sun_path)) {
fr_strerror_printf("Path too long in socket filename");
goto error;
}
memset(&salocal, 0, sizeof(salocal));
salocal.sun_family = AF_UNIX;
#ifdef HAVE_BINDAT
memcpy(salocal.sun_path, name, len + 1); /* SUN_LEN does strlen */
#else
memcpy(salocal.sun_path, path, len + 1); /* SUN_LEN does strlen */
#endif
socklen = SUN_LEN(&salocal);
/*
* Direct socket permissions are only useful on Linux which
* actually enforces them. BSDs don't. They also need to be
* set before binding the socket to a file.
*/
#ifdef __linux__
if (fchmod(sock_fd, perm) < 0) {
char str_need[10], oct_need[5];
rad_mode_to_str(str_need, perm);
rad_mode_to_oct(oct_need, perm);
fr_strerror_printf("Failed changing socket permissions to %s (%s)", str_need, oct_need);
goto sock_error;
}
if (fchown(sock_fd, uid, gid) < 0) {
struct passwd *user;
struct group *group;
if (rad_getpwuid(NULL, &user, uid) < 0) goto sock_error;
if (rad_getgrgid(NULL, &group, gid) < 0) {
talloc_free(user);
goto sock_error;
}
fr_strerror_printf("Failed changing ownership of socket to %s:%s", user->pw_name, group->gr_name);
talloc_free(user);
talloc_free(group);
goto sock_error;
}
#endif
/*
* The correct function to use here is bindat(), but only
* quite recent versions of FreeBSD actually have it, and
* it's definitely not POSIX.
*/
#ifdef HAVE_BINDAT
if (bindat(dir_fd, sock_fd, (struct sockaddr *)&salocal, socklen) < 0) {
#else
if (bind(sock_fd, (struct sockaddr *)&salocal, socklen) < 0) {
#endif
fr_strerror_printf("Failed binding socket: %s", fr_syserror(errno));
goto sock_error;
}
if (listen(sock_fd, 8) < 0) {
fr_strerror_printf("Failed listening on socket: %s", fr_syserror(errno));
goto sock_error;
}
#ifdef O_NONBLOCK
{
int flags;
flags = fcntl(sock_fd, F_GETFL, NULL);
if (flags < 0) {
fr_strerror_printf("Failed getting socket flags: %s", fr_syserror(errno));
goto sock_error;
}
flags |= O_NONBLOCK;
if (fcntl(sock_fd, F_SETFL, flags) < 0) {
fr_strerror_printf("Failed setting nonblocking socket flag: %s", fr_syserror(errno));
goto sock_error;
}
}
#endif
if (uid != (uid_t)-1) rad_seuid(euid);
if (gid != (gid_t)-1) rad_segid(egid);
if (dir_fd >= 0) close(dir_fd);
if (parent_fd >= 0) close(parent_fd);
return sock_fd;
}
#endif
static void command_close_socket(rad_listen_t *this)
{
this->status = RAD_LISTEN_STATUS_EOL;
/*
* This removes the socket from the event fd, so no one
* will be calling us any more.
*/
radius_update_listener(this);
}
static ssize_t CC_HINT(format (printf, 2, 3)) cprintf(rad_listen_t *listener, char const *fmt, ...)
{
ssize_t r, len;
va_list ap;
char buffer[256];
va_start(ap, fmt);
len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
va_end(ap);
if (listener->status == RAD_LISTEN_STATUS_EOL) return 0;
r = fr_channel_write(listener->fd, FR_CHANNEL_STDOUT, buffer, len);
if (r <= 0) command_close_socket(listener);
/*
* FIXME: Keep writing until done?
*/
return r;
}
static ssize_t CC_HINT(format (printf, 2, 3)) cprintf_error(rad_listen_t *listener, char const *fmt, ...)
{
ssize_t r, len;
va_list ap;
char buffer[256];
va_start(ap, fmt);
len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
va_end(ap);
if (listener->status == RAD_LISTEN_STATUS_EOL) return 0;
r = fr_channel_write(listener->fd, FR_CHANNEL_STDERR, buffer, len);
if (r <= 0) command_close_socket(listener);
/*
* FIXME: Keep writing until done?
*/
return r;
}
static int command_hup(rad_listen_t *listener, int argc, char *argv[])
{
CONF_SECTION *cs;
module_instance_t *mi;
char buffer[256];
if (argc == 0) {
radius_signal_self(RADIUS_SIGNAL_SELF_HUP);
return CMD_OK;
}
/*
* Hack a "main" HUP thingy
*/
if (strcmp(argv[0], "main.log") == 0) {
hup_logfile();
return CMD_OK;
}
cs = cf_section_find("modules");
if (!cs) return CMD_FAIL;
mi = module_find(cs, argv[0]);
if (!mi) {
cprintf_error(listener, "No such module \"%s\"\n", argv[0]);
return CMD_FAIL;
}
if ((mi->entry->module->type & RLM_TYPE_HUP_SAFE) == 0) {
cprintf_error(listener, "Module %s cannot be hup'd\n",
argv[0]);
return CMD_FAIL;
}
if (!module_hup_module(mi->cs, mi, time(NULL))) {
cprintf_error(listener, "Failed to reload module\n");
return CMD_FAIL;
}
snprintf(buffer, sizeof(buffer), "modules.%s.hup",
cf_section_name1(mi->cs));
exec_trigger(NULL, mi->cs, buffer, true);
return CMD_OK;
}
static int command_terminate(UNUSED rad_listen_t *listener,
UNUSED int argc, UNUSED char *argv[])
{
radius_signal_self(RADIUS_SIGNAL_SELF_TERM);
return CMD_OK;
}
static int command_uptime(rad_listen_t *listener,
UNUSED int argc, UNUSED char *argv[])
{
char buffer[128];
CTIME_R(&fr_start_time, buffer, sizeof(buffer));
cprintf(listener, "Up since %s", buffer); /* no \r\n */
return CMD_OK;
}
static int command_show_config(rad_listen_t *listener, int argc, char *argv[])
{
CONF_ITEM *ci;
CONF_PAIR *cp;
char const *value;
if (argc != 1) {
cprintf_error(listener, "No path was given\n");
return CMD_FAIL;
}
ci = cf_reference_item(main_config.config, main_config.config, argv[0]);
if (!ci) return CMD_FAIL;
if (!cf_item_is_pair(ci)) return CMD_FAIL;
cp = cf_item_to_pair(ci);
value = cf_pair_value(cp);
if (!value) return CMD_FAIL;
cprintf(listener, "%s\n", value);
return CMD_OK;
}
static char const tabs[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
/*
* FIXME: Recurse && indent?
*/
static void cprint_conf_parser(rad_listen_t *listener, int indent, CONF_SECTION *cs,
void const *base)
{
int i;
char const *name1 = cf_section_name1(cs);
char const *name2 = cf_section_name2(cs);
CONF_PARSER const *variables = cf_section_parse_table(cs);
if (name2) {
cprintf(listener, "%.*s%s %s {\n", indent, tabs, name1, name2);
} else {
cprintf(listener, "%.*s%s {\n", indent, tabs, name1);
}
indent++;
/*
* Print
*/
if (variables) for (i = 0; variables[i].name != NULL; i++) {
void const *data;
char buffer[256];
/*
* No base struct offset, data must be the pointer.
* If data doesn't exist, ignore the entry, there
* must be something wrong.
*/
if (!base) {
if (!variables[i].data) {
continue;
}
data = variables[i].data;
} else if (variables[i].data) {
data = variables[i].data;
} else {
data = (((char const *)base) + variables[i].offset);
}
/*
* Ignore the various flags
*/
switch (variables[i].type & 0xff) {
default:
cprintf(listener, "%.*s%s = ?\n", indent, tabs,
variables[i].name);
break;
case PW_TYPE_INTEGER:
cprintf(listener, "%.*s%s = %u\n", indent, tabs,
variables[i].name, *(int const *) data);
break;
case PW_TYPE_IPV4_ADDR:
inet_ntop(AF_INET, data, buffer, sizeof(buffer));
break;
case PW_TYPE_IPV6_ADDR:
inet_ntop(AF_INET6, data, buffer, sizeof(buffer));
break;
case PW_TYPE_BOOLEAN:
cprintf(listener, "%.*s%s = %s\n", indent, tabs,
variables[i].name,
((*(bool const *) data) == false) ? "no" : "yes");
break;
case PW_TYPE_STRING:
case PW_TYPE_FILE_INPUT:
case PW_TYPE_FILE_OUTPUT:
/*
* FIXME: Escape things in the string!
*/
if (*(char const * const *) data) {
cprintf(listener, "%.*s%s = \"%s\"\n", indent, tabs,
variables[i].name, *(char const * const *) data);
} else {
cprintf(listener, "%.*s%s = \n", indent, tabs,
variables[i].name);
}
break;
}
}
indent--;
cprintf(listener, "%.*s}\n", indent, tabs);
}
static void cprint_conf_section(rad_listen_t *listener, int indent, CONF_SECTION *cs)
{
char const *name1 = cf_section_name1(cs);
char const *name2 = cf_section_name2(cs);
CONF_ITEM *ci;
if (name2) {
cprintf(listener, "%.*s%s %s {\n", indent, tabs, name1, name2);
} else {
cprintf(listener, "%.*s%s {\n", indent, tabs, name1);
}
indent++;
for (ci = cf_item_find_next(cs, NULL);
ci != NULL;
ci = cf_item_find_next(cs, ci)) {
CONF_PAIR const *cp;
char const *value;
if (cf_item_is_section(ci)) {
cprint_conf_section(listener, indent, cf_item_to_section(ci));
continue;
}
if (!cf_item_is_pair(ci)) continue;
cp = cf_item_to_pair(ci);
value = cf_pair_value(cp);
if (value) {
/*
* @todo - quote the value if necessary.
*/
cprintf(listener, "%.*s%s = %s\n",
indent, tabs,
cf_pair_attr(cp), value);
} else {
cprintf(listener, "%.*s%s\n",
indent, tabs,
cf_pair_attr(cp));
}
}
indent--;
cprintf(listener, "%.*s}\n", indent, tabs);
}
static int command_show_module_config(rad_listen_t *listener, int argc, char *argv[])
{
CONF_SECTION *cs;
module_instance_t *mi;
if (argc != 1) {
cprintf_error(listener, "No module name was given\n");
return CMD_FAIL;
}
cs = cf_section_find("modules");
if (!cs) return CMD_FAIL;
mi = module_find(cs, argv[0]);
if (!mi) {
cprintf_error(listener, "No such module \"%s\"\n", argv[0]);
return CMD_FAIL;
}
cprint_conf_parser(listener, 0, mi->cs, mi->insthandle);
return CMD_OK;
}
static char const *method_names[MOD_COUNT] = {
"authenticate",
"authorize",
"preacct",
"accounting",
"session",
"pre-proxy",
"post-proxy",
"post-auth"
#ifdef WITH_COA
,
"recv-coa",
"send-coa"
#endif
};
static int command_show_module_methods(rad_listen_t *listener, int argc, char *argv[])
{
int i;
CONF_SECTION *cs;
module_instance_t const *mi;
module_t const *mod;
if (argc != 1) {
cprintf_error(listener, "No module name was given\n");
return CMD_FAIL;
}
cs = cf_section_find("modules");
if (!cs) return CMD_FAIL;
mi = module_find(cs, argv[0]);
if (!mi) {
cprintf_error(listener, "No such module \"%s\"\n", argv[0]);
return CMD_FAIL;
}
mod = mi->entry->module;
for (i = 0; i < MOD_COUNT; i++) {
if (mod->methods[i]) cprintf(listener, "%s\n", method_names[i]);
}
return CMD_OK;
}
static int command_show_module_flags(rad_listen_t *listener, int argc, char *argv[])
{
CONF_SECTION *cs;
module_instance_t const *mi;
module_t const *mod;
if (argc != 1) {
cprintf_error(listener, "No module name was given\n");
return CMD_FAIL;
}
cs = cf_section_find("modules");
if (!cs) return CMD_FAIL;
mi = module_find(cs, argv[0]);
if (!mi) {
cprintf_error(listener, "No such module \"%s\"\n", argv[0]);
return CMD_FAIL;
}
mod = mi->entry->module;
if ((mod->type & RLM_TYPE_THREAD_UNSAFE) != 0)
cprintf(listener, "thread-unsafe\n");
if ((mod->type & RLM_TYPE_HUP_SAFE) != 0)
cprintf(listener, "reload-on-hup\n");
return CMD_OK;
}
static int command_show_module_status(rad_listen_t *listener, int argc, char *argv[])
{
CONF_SECTION *cs;
const module_instance_t *mi;
if (argc != 1) {
cprintf_error(listener, "No module name was given\n");
return CMD_FAIL;
}
cs = cf_section_find("modules");
if (!cs) return CMD_FAIL;
mi = module_find(cs, argv[0]);
if (!mi) {
cprintf_error(listener, "No such module \"%s\"\n", argv[0]);
return CMD_FAIL;
}
if (!mi->force) {
cprintf(listener, "alive\n");
} else {
cprintf(listener, "%s\n", fr_int2str(mod_rcode_table, mi->code, "<invalid>"));
}
return CMD_OK;
}
/*
* Show all loaded modules
*/
static int command_show_modules(rad_listen_t *listener, UNUSED int argc, UNUSED char *argv[])
{
CONF_SECTION *cs, *subcs;
cs = cf_section_find("modules");
if (!cs) return CMD_FAIL;
subcs = NULL;
while ((subcs = cf_subsection_find_next(cs, subcs, NULL)) != NULL) {
char const *name1 = cf_section_name1(subcs);
char const *name2 = cf_section_name2(subcs);
module_instance_t *mi;
if (name2) {
mi = module_find(cs, name2);
if (!mi) continue;
cprintf(listener, "%s (%s)\n", name2, name1);
} else {
mi = module_find(cs, name1);
if (!mi) continue;
cprintf(listener, "%s\n", name1);
}
}
return CMD_OK;
}
#ifdef WITH_PROXY
static int command_show_home_servers(rad_listen_t *listener, int argc, char *argv[])
{
int i;
home_server_t *home;
char const *type, *state, *proto;
char buffer[256];
for (i = 0; i < home_server_max_number; i++) {
if ((home = home_server_bynumber(i)) == NULL)
continue;
/*
* Internal "virtual" home server.
*/
if (home->ipaddr.af == AF_UNSPEC) continue;
if (home->type == HOME_TYPE_AUTH) {
type = "auth";
} else if (home->type == HOME_TYPE_ACCT) {
type = "acct";
} else if (home->type == HOME_TYPE_AUTH_ACCT) {
type = "auth+acct";
#ifdef WITH_COA
} else if (home->type == HOME_TYPE_COA) {
type = "coa";
#endif
} else continue;
if (home->proto == IPPROTO_UDP) {
proto = "udp";
}
#ifdef WITH_TCP
else if (home->proto == IPPROTO_TCP) {
proto = "tcp";
}
#endif
else proto = "??";
if (home->state == HOME_STATE_ALIVE) {
state = "alive";
} else if (home->state == HOME_STATE_ZOMBIE) {
state = "zombie";
} else if (home->state == HOME_STATE_IS_DEAD) {
state = "dead";
} else if (home->state == HOME_STATE_ADMIN_DOWN) {
state = "down";
} else if (home->state == HOME_STATE_UNKNOWN) {
time_t now = time(NULL);
/*
* We've recently received a packet, so
* the home server seems to be alive.
*
* The *reported* state changes because
* the internal state machine NEEDS THE
* RIGHT STATE. However, reporting that
* to the admin will confuse them.
* So... we lie. No, that dress doesn't
* make you look fat...
*/
if ((home->last_packet_recv + (int)home->ping_interval) >= now) {
state = "alive";
} else {
state = "unknown";
}
} else continue;
if (argc > 0 && !strcmp(argv[0], "all")) {
char const *dynamic = home->dynamic ? "yes" : "no";
cprintf(listener, "%s\t%d\t%s\t%s\t%s\t%d\t(name=%s, dynamic=%s)\n",
ip_ntoh(&home->ipaddr, buffer, sizeof(buffer)),
home->port, proto, type, state,
home->currently_outstanding, home->name, dynamic);
} else {
cprintf(listener, "%s\t%d\t%s\t%s\t%s\t%d\n",
ip_ntoh(&home->ipaddr, buffer, sizeof(buffer)),
home->port, proto, type, state,
home->currently_outstanding);
}
}
return CMD_OK;
}
#endif
static RADCLIENT *get_client(rad_listen_t *listener, int argc, char *argv[]);
static int command_show_client_config(rad_listen_t *listener, int argc, char *argv[])
{
RADCLIENT *client;
client = get_client(listener, argc, argv);
if (!client) {
return 0;
}
if (!client->cs) return 1;
cprint_conf_section(listener, 0, client->cs);
return 1;
}
/*
* @todo - copied from clients.c. Better to re-use, but whatever.
*/
struct radclient_list {
char const *name; /* name of this list */
char const *server; /* virtual server associated with this client list */
/*
* FIXME: One set of trees for IPv4, and another for IPv6?
*/
rbtree_t *trees[129]; /* for 0..128, inclusive. */
uint32_t min_prefix;
};
static int command_show_clients(rad_listen_t *listener, int argc, char *argv[])
{
int i;
RADCLIENT *client;
char buffer[256];
if (argc == 0) {
for (i = 0; (client = client_findbynumber(NULL, i)) != NULL; i++) {
ip_ntoh(&client->ipaddr, buffer, sizeof(buffer));
if (((client->ipaddr.af == AF_INET) &&
(client->ipaddr.prefix != 32)) ||
((client->ipaddr.af == AF_INET6) &&
(client->ipaddr.prefix != 128))) {
cprintf(listener, "%s/%d\n", buffer, client->ipaddr.prefix);
} else {
cprintf(listener, "%s\n", buffer);
}
}
return CMD_OK;
}
if (argc != 1) {
cprintf_error(listener, "Unknown command %s %s ...\n", argv[0], argv[1]);
return -1;
}
if (strcmp(argv[0], "verbose") != 0) {
cprintf_error(listener, "Unknown command %s\n", argv[0]);
return -1;
}
for (i = 0; (client = client_findbynumber(NULL, i)) != NULL; i++) {
if (client->cs) {
cprintf(listener, "client %s {\n", cf_section_name2(client->cs));
} else {
cprintf(listener, "client {\n");
}
fr_ntop(buffer, sizeof(buffer), &client->ipaddr);
cprintf(listener, "\tipaddr = %s\n", buffer);
if (client->src_ipaddr.af != AF_UNSPEC) {
fr_ntop(buffer, sizeof(buffer), &client->src_ipaddr);
cprintf(listener, "\tsrc_ipaddr = %s\n", buffer);
}
if (client->proto == IPPROTO_UDP) {
cprintf(listener, "\tproto = udp\n");
} else if (client->proto == IPPROTO_TCP) {
cprintf(listener, "\tproto = tcp\n");
} else {
cprintf(listener, "\tproto = *\n");
}
cprintf(listener, "\tsecret = %s\n", client->secret);
cprintf(listener, "\tlongname = %s\n", client->longname);
cprintf(listener, "\tshortname = %s\n", client->shortname);
if (client->nas_type) cprintf(listener, "\tnas_type = %s\n", client->nas_type);
cprintf(listener, "\tnumber = %d\n", client->number);
if (client->server) {
cprintf(listener, "\tvirtual_server = %s\n", client->server);
}
#ifdef WITH_DYNAMIC_CLIENTS
if (client->dynamic) {
cprintf(listener, "\tdynamic = yes\n");
cprintf(listener, "\tlifetime = %u\n", client->lifetime);
}
#endif
#ifdef WITH_TLS
if (client->tls_required) {
cprintf(listener, "\ttls = yes\n");
}
#endif
if (client->list && client->list->server) {
cprintf(listener, "\tparent_virtual_server = %s\n", client->list->server);
} else {
cprintf(listener, "\tglobal = yes\n");
}
cprintf(listener, "}\n");
}
return CMD_OK;
}
static int command_show_version(rad_listen_t *listener, UNUSED int argc, UNUSED char *argv[])
{
cprintf(listener, "%s\n", radiusd_version);
return CMD_OK;
}
static int command_debug_level(rad_listen_t *listener, int argc, char *argv[])
{
int number;
if (argc == 0) {
cprintf_error(listener, "Must specify <number>\n");
return -1;
}
number = atoi(argv[0]);
if ((number < 0) || (number > 4)) {
cprintf_error(listener, "<number> must be between 0 and 4\n");
return -1;
}
fr_debug_lvl = rad_debug_lvl = number;
return CMD_OK;
}
static char debug_log_file_buffer[1024];
static int command_debug_file(rad_listen_t *listener, int argc, char *argv[])
{
if (rad_debug_lvl && default_log.dst == L_DST_STDOUT) {
cprintf_error(listener, "Cannot redirect debug logs to a file when already in debugging mode.\n");
return -1;
}
if ((argc > 0) && (strchr(argv[0], FR_DIR_SEP) != NULL)) {
cprintf_error(listener, "Cannot direct debug logs to absolute path.\n");
}
default_log.debug_file = NULL;
if (argc == 0) return CMD_OK;
/*
* This looks weird, but it's here to avoid locking
* a mutex for every log message.
*/
memset(debug_log_file_buffer, 0, sizeof(debug_log_file_buffer));
/*
* Debug files always go to the logging directory.
*/
snprintf(debug_log_file_buffer, sizeof(debug_log_file_buffer),
"%s/%s", radlog_dir, argv[0]);
default_log.debug_file = &debug_log_file_buffer[0];
return CMD_OK;
}
extern fr_cond_t *debug_condition;
static int command_debug_condition(rad_listen_t *listener, int argc, char *argv[])
{
int i;
char const *error;
ssize_t slen = 0;
fr_cond_t *new_condition = NULL;
char *p, buffer[1024];
/*
* Disable it.
*/
if (argc == 0) {
TALLOC_FREE(debug_condition);
debug_condition = NULL;
return CMD_OK;
}
if (!((argc == 1) &&
((argv[0][0] == '"') || (argv[0][0] == '\'')))) {
p = buffer;
*p = '\0';
for (i = 0; i < argc; i++) {
size_t len;
len = strlcpy(p, argv[i], buffer + sizeof(buffer) - p);
p += len;
*(p++) = ' ';
*p = '\0';
}
} else {
/*
* Backwards compatibility. De-escape the string.
*/
char quote;
char *q;
p = argv[0];
q = buffer;
quote = *(p++);
while (true) {
if (!*p) {
error = "Unexpected end of string";
slen = -strlen(argv[0]);
p = argv[0];
goto parse_error;
}
if (*p == quote) {
if (p[1]) {
error = "Unexpected text after end of string";
slen = -(p - argv[0]);
p = argv[0];
goto parse_error;
}
*q = '\0';
break;
}
if (*p == '\\') {
*(q++) = p[1];
p += 2;
continue;
}
*(q++) = *(p++);
}
}
p = buffer;
slen = fr_condition_tokenize(NULL, NULL, p, &new_condition, &error, FR_COND_ONE_PASS);
if (slen <= 0) {
char *spaces, *text;
parse_error:
fr_canonicalize_error(NULL, &spaces, &text, slen, p);
ERROR("Parse error in condition");
ERROR("%s", p);
ERROR("%s^ %s", spaces, error);
cprintf_error(listener, "Parse error in condition \"%s\": %s\n", p, error);
talloc_free(spaces);
talloc_free(text);
return CMD_FAIL;
}
(void) modcall_pass2_condition(new_condition);
/*
* Delete old condition.
*
* This is thread-safe because the condition is evaluated
* in the main server thread, along with this code.
*/
TALLOC_FREE(debug_condition);
debug_condition = new_condition;
return CMD_OK;
}
static int command_show_debug_condition(rad_listen_t *listener,
UNUSED int argc, UNUSED char *argv[])
{
char buffer[1024];
if (!debug_condition) {
cprintf(listener, "\n");
return CMD_OK;
}
fr_cond_sprint(buffer, sizeof(buffer), debug_condition);
cprintf(listener, "%s\n", buffer);
return CMD_OK;
}
static int command_show_debug_file(rad_listen_t *listener,
UNUSED int argc, UNUSED char *argv[])
{
if (!default_log.debug_file) return CMD_FAIL;
cprintf(listener, "%s\n", default_log.debug_file);
return CMD_OK;
}
static int command_show_debug_level(rad_listen_t *listener,
UNUSED int argc, UNUSED char *argv[])
{
cprintf(listener, "%d\n", rad_debug_lvl);
return CMD_OK;
}
static RADCLIENT *get_client(rad_listen_t *listener, int argc, char *argv[])
{
RADCLIENT *client;
fr_ipaddr_t ipaddr;
int myarg;
int proto = IPPROTO_UDP;
RADCLIENT_LIST *list = NULL;
if (argc < 1) {
cprintf_error(listener, "Must specify <ipaddr>\n");
return NULL;
}
/*
* First arg is IP address.
*/
if (ip_hton(&ipaddr, AF_UNSPEC, argv[0], false) < 0) {
cprintf_error(listener, "Failed parsing IP address; %s\n",
fr_strerror());
return NULL;
}
myarg = 1;
while (myarg < argc) {
if (strcmp(argv[myarg], "udp") == 0) {
proto = IPPROTO_UDP;
myarg++;
continue;
}
#ifdef WITH_TCP
if (strcmp(argv[myarg], "tcp") == 0) {
proto = IPPROTO_TCP;
myarg++;
continue;
}
#endif
if (strcmp(argv[myarg], "listen") == 0) {
uint16_t server_port;
fr_ipaddr_t server_ipaddr;
if ((argc - myarg) < 2) {
cprintf_error(listener, "Must specify listen <ipaddr> <port>\n");
return NULL;
}
if (ip_hton(&server_ipaddr, ipaddr.af, argv[myarg + 1], false) < 0) {
cprintf_error(listener, "Failed parsing IP address; %s\n",
fr_strerror());
return NULL;
}
server_port = atoi(argv[myarg + 2]);
list = listener_find_client_list(&server_ipaddr, server_port, proto);
if (!list) {
cprintf_error(listener, "No such listener %s %s\n", argv[myarg + 1], argv[myarg + 2]);
return NULL;
}
myarg += 3;
continue;
}
cprintf_error(listener, "Unknown argument %s.\n", argv[myarg]);
return NULL;
}
client = client_find(list, &ipaddr, proto);
if (!client) {
cprintf_error(listener, "No such client\n");
return NULL;
}
return client;
}
#ifdef WITH_PROXY
static home_server_t *get_home_server(rad_listen_t *listener, int argc,
char *argv[], int *last)
{
int myarg = 2;
home_server_t *home;
uint16_t port;
int proto = IPPROTO_UDP;
fr_ipaddr_t ipaddr, src_ipaddr;
if (argc < 2) {
cprintf_error(listener, "Must specify <ipaddr> <port> [udp|tcp] OR <name> <type>\n");
return NULL;
}
if (isdigit(*argv[1])) {
if (ip_hton(&ipaddr, AF_UNSPEC, argv[0], false) < 0) {
cprintf_error(listener, "Failed parsing IP address; %s\n",
fr_strerror());
return NULL;
}
memset(&src_ipaddr, 0, sizeof(src_ipaddr));
src_ipaddr.af = ipaddr.af;
port = atoi(argv[1]);
while (myarg < argc) {
if (strcmp(argv[myarg], "udp") == 0) {
proto = IPPROTO_UDP;
myarg++;
continue;
}
#ifdef WITH_TCP
if (strcmp(argv[myarg], "tcp") == 0) {
proto = IPPROTO_TCP;
myarg++;
continue;
}
#endif
/*
* Allow the caller to specify src, too.
*/
if (strcmp(argv[myarg], "src") == 0) {
if ((myarg + 2) < argc) {
cprintf_error(listener, "You must specify an address after 'src' \n");
return NULL;
}
if (ip_hton(&src_ipaddr, ipaddr.af, argv[myarg + 1], false) < 0) {
cprintf_error(listener, "Failed parsing IP address; %s\n",
fr_strerror());
return NULL;
}
myarg += 2;
continue;
}
/*
* Unknown argument. Leave it for the caller.
*/
break;
}
home = home_server_find_bysrc(&ipaddr, port, proto, &src_ipaddr);
} else {
int type;
static const FR_NAME_NUMBER home_server_types[] = {
{ "auth", HOME_TYPE_AUTH },
{ "acct", HOME_TYPE_ACCT },
{ "auth+acct", HOME_TYPE_AUTH_ACCT },
{ "coa", HOME_TYPE_COA },
#ifdef WITH_COA_TUNNEL
{ "auth+coa", HOME_TYPE_AUTH_COA },
{ "auth+acct+coa", HOME_TYPE_AUTH_ACCT_COA },
#endif
{ NULL, 0 }
};
type = fr_str2int(home_server_types, argv[1], HOME_TYPE_INVALID);
if (type == HOME_TYPE_INVALID) {
cprintf_error(listener, "Invalid home server type '%s'\n", argv[1]);
return NULL;
}
home = home_server_byname(argv[0], type);
}
if (!home) {
cprintf_error(listener, "No such home server - %s %s\n", argv[0], argv[1]);
return NULL;
}
if (last) *last = myarg;
return home;
}
static int command_set_home_server_state(rad_listen_t *listener, int argc, char *argv[])
{
int last;
home_server_t *home;
if (argc < 3) {
cprintf_error(listener, "Must specify <ipaddr> <port> [udp|tcp] <state>\n");
return CMD_FAIL;
}
home = get_home_server(listener, argc, argv, &last);
if (!home) {
return CMD_FAIL;
}
if (strcmp(argv[last], "alive") == 0) {
revive_home_server(home);
} else if (strcmp(argv[last], "dead") == 0) {
struct timeval now;
gettimeofday(&now, NULL); /* we do this WAY too often */
mark_home_server_dead(home, &now, false);
} else if (strcmp(argv[last], "down") == 0) {
struct timeval now;
gettimeofday(&now, NULL); /* we do this WAY too often */
mark_home_server_dead(home, &now, true);
} else {
cprintf_error(listener, "Unknown state \"%s\"\n", argv[last]);
return CMD_FAIL;
}
return CMD_OK;
}
static int command_show_home_server_state(rad_listen_t *listener, int argc, char *argv[])
{
home_server_t *home;
home = get_home_server(listener, argc, argv, NULL);
if (!home) return CMD_FAIL;
switch (home->state) {
case HOME_STATE_ALIVE:
cprintf(listener, "alive\n");
break;
case HOME_STATE_IS_DEAD:
cprintf(listener, "dead\n");
break;
case HOME_STATE_ZOMBIE:
cprintf(listener, "zombie\n");
break;
case HOME_STATE_ADMIN_DOWN:
cprintf(listener, "down\n");
break;
case HOME_STATE_UNKNOWN:
cprintf(listener, "unknown\n");
break;
default:
cprintf(listener, "invalid\n");
break;
}
return CMD_OK;
}
#endif
/*
* For encode/decode stuff
*/
static int null_socket_dencode(UNUSED rad_listen_t *listener, UNUSED REQUEST *request)
{
return 0;
}
static int null_socket_send(UNUSED rad_listen_t *listener, REQUEST *request)
{
vp_cursor_t cursor;
char *output_file;
FILE *fp;
output_file = request_data_reference(request, (void *)null_socket_send, 0);
if (!output_file) {
ERROR("No output file for injected packet %d", request->number);
return 0;
}
fp = fopen(output_file, "w");
if (!fp) {
ERROR("Failed to send injected file to %s: %s", output_file, fr_syserror(errno));
return 0;
}
if (request->reply->code != 0) {
char const *what = "reply";
VALUE_PAIR *vp;
char buffer[1024];
if (request->reply->code < FR_MAX_PACKET_CODE) {
what = fr_packet_codes[request->reply->code];
}
fprintf(fp, "%s\n", what);
if (rad_debug_lvl) {
RDEBUG("Injected %s packet to host %s port 0 code=%d, id=%d", what,
inet_ntop(request->reply->src_ipaddr.af,
&request->reply->src_ipaddr.ipaddr,
buffer, sizeof(buffer)),
request->reply->code, request->reply->id);
}
RINDENT();
for (vp = fr_cursor_init(&cursor, &request->reply->vps);
vp;
vp = fr_cursor_next(&cursor)) {
vp_prints(buffer, sizeof(buffer), vp);
fprintf(fp, "%s\n", buffer);
RDEBUG("%s", buffer);
}
REXDENT();
}
fclose(fp);
return 0;
}
static rad_listen_t *get_socket(rad_listen_t *listener, int argc,
char *argv[], int *last)
{
rad_listen_t *sock;
uint16_t port;
int proto = IPPROTO_UDP;
fr_ipaddr_t ipaddr;
if (argc < 2) {
cprintf_error(listener, "Must specify <ipaddr> <port> [udp|tcp]\n");
return NULL;
}
if (ip_hton(&ipaddr, AF_UNSPEC, argv[0], false) < 0) {
cprintf_error(listener, "Failed parsing IP address; %s\n",
fr_strerror());
return NULL;
}
port = atoi(argv[1]);
if (last) *last = 2;
if (argc > 2) {
if (strcmp(argv[2], "udp") == 0) {
proto = IPPROTO_UDP;
if (last) *last = 3;
}
#ifdef WITH_TCP
if (strcmp(argv[2], "tcp") == 0) {
proto = IPPROTO_TCP;
if (last) *last = 3;
}
#endif
}
sock = listener_find_byipaddr(&ipaddr, port, proto);
if (!sock) {
cprintf_error(listener, "No such listen section\n");
return NULL;
}
return sock;
}
static int command_inject_to(rad_listen_t *listener, int argc, char *argv[])
{
fr_command_socket_t *sock;
listen_socket_t *data;
rad_listen_t *found;
if (listener->recv == command_tcp_recv) {
cprintf_error(listener, "Cannot inject from command socket over TCP");
return CMD_FAIL;
}
found = get_socket(listener, argc, argv, NULL);
if (!found) {
return 0;
}
sock = listener->data;
data = found->data;
sock->inject_listener = found;
sock->dst_ipaddr = data->my_ipaddr;
sock->dst_port = data->my_port;
return CMD_OK;
}
static int command_inject_from(rad_listen_t *listener, int argc, char *argv[])
{
RADCLIENT *client;
fr_command_socket_t *sock;
if (argc < 1) {
cprintf_error(listener, "No <ipaddr> was given\n");
return 0;
}
if (listener->recv == command_tcp_recv) {
cprintf_error(listener, "Cannot inject from command socket over TCP");
return CMD_FAIL;
}
sock = listener->data;
if (!sock->inject_listener) {
cprintf_error(listener, "You must specify \"inject to\" before using \"inject from\"\n");
return 0;
}
sock->src_ipaddr.af = AF_UNSPEC;
if (ip_hton(&sock->src_ipaddr, AF_UNSPEC, argv[0], false) < 0) {
cprintf_error(listener, "Failed parsing IP address; %s\n",
fr_strerror());
return 0;
}
client = client_listener_find(sock->inject_listener, &sock->src_ipaddr,
0);
if (!client) {
cprintf_error(listener, "No such client %s\n", argv[0]);
return 0;
}
sock->inject_client = client;
return CMD_OK;
}
static int command_inject_file(rad_listen_t *listener, int argc, char *argv[])
{
static int inject_id = 0;
int ret;
bool filedone;
fr_command_socket_t *sock;
rad_listen_t *fake;
RADIUS_PACKET *packet;
vp_cursor_t cursor;
VALUE_PAIR *vp;
FILE *fp;
RAD_REQUEST_FUNP fun = NULL;
char buffer[2048];
if (argc < 2) {
cprintf_error(listener, "You must specify <input-file> <output-file>\n");
return 0;
}
if (listener->recv == command_tcp_recv) {
cprintf_error(listener, "Cannot inject from command socket over TCP");
return CMD_FAIL;
}
sock = listener->data;
if (!sock->inject_listener) {
cprintf_error(listener, "You must specify \"inject to\" before using \"inject file\"\n");
return 0;
}
if (!sock->inject_client) {
cprintf_error(listener, "You must specify \"inject from\" before using \"inject file\"\n");
return 0;
}
/*
* Output files always go to the logging directory.
*/
snprintf(buffer, sizeof(buffer), "%s/%s", radlog_dir, argv[1]);
fp = fopen(argv[0], "r");
if (!fp ) {
cprintf_error(listener, "Failed opening %s: %s\n",
argv[0], fr_syserror(errno));
return 0;
}
ret = fr_pair_list_afrom_file(NULL, &vp, fp, &filedone);
fclose(fp);
if (ret < 0) {
cprintf_error(listener, "Failed reading attributes from %s: %s\n",
argv[0], fr_strerror());
return 0;
}
fake = talloc(NULL, rad_listen_t);
memcpy(fake, sock->inject_listener, sizeof(*fake));
/*
* Re-write the IO for the listener.
*/
fake->encode = null_socket_dencode;
fake->decode = null_socket_dencode;
fake->send = null_socket_send;
packet = rad_alloc(NULL, false);
packet->src_ipaddr = sock->src_ipaddr;
packet->src_port = 0;
packet->dst_ipaddr = sock->dst_ipaddr;
packet->dst_port = sock->dst_port;
packet->vps = vp;
packet->id = inject_id++;
if (fake->type == RAD_LISTEN_AUTH) {
packet->code = PW_CODE_ACCESS_REQUEST;
fun = rad_authenticate;
} else {
#ifdef WITH_ACCOUNTING
packet->code = PW_CODE_ACCOUNTING_REQUEST;
fun = rad_accounting;
#else
cprintf_error(listener, "This server was built without accounting support.\n");
rad_free(&packet);
free(fake);
return 0;
#endif
}
if (rad_debug_lvl) {
DEBUG("Injecting %s packet from host %s port 0 code=%d, id=%d",
fr_packet_codes[packet->code],
inet_ntop(packet->src_ipaddr.af,
&packet->src_ipaddr.ipaddr,
buffer, sizeof(buffer)),
packet->code, packet->id);
for (vp = fr_cursor_init(&cursor, &packet->vps);
vp;
vp = fr_cursor_next(&cursor)) {
vp_prints(buffer, sizeof(buffer), vp);
DEBUG("\t%s", buffer);
}
WARN("INJECTION IS LEAKING MEMORY!");
}
if (!request_receive(NULL, fake, packet, sock->inject_client, fun)) {
cprintf_error(listener, "Failed to inject request. See log file for details\n");
rad_free(&packet);
free(fake);
return 0;
}
#if 0
/*
* Remember what the output file is, and remember to
* delete the fake listener when done.
*/
request_data_add(request, null_socket_send, 0, talloc_typed_strdup(NULL, buffer), true);
request_data_add(request, null_socket_send, 1, fake, true);
#endif
return CMD_OK;
}
static fr_command_table_t command_table_inject[] = {
{ "to", FR_WRITE,
"inject to <ipaddr> <port> - Inject packets to the destination IP and port.",
command_inject_to, NULL },
{ "from", FR_WRITE,
"inject from <ipaddr> - Inject packets as if they came from <ipaddr>",
command_inject_from, NULL },
{ "file", FR_WRITE,
"inject file <input-file> <output-file> - Inject packet from <input-file>, with results sent to <output-file>",
command_inject_file, NULL },
{ NULL, 0, NULL, NULL, NULL }
};
static fr_command_table_t command_table_debug[] = {
{ "condition", FR_WRITE,
"debug condition [condition] - Enable debugging for requests matching [condition]",
command_debug_condition, NULL },
{ "level", FR_WRITE,
"debug level <number> - Set debug level to <number>. Higher is more debugging.",
command_debug_level, NULL },
{ "file", FR_WRITE,
"debug file [filename] - Send all debugging output to [filename]",
command_debug_file, NULL },
{ NULL, 0, NULL, NULL, NULL }
};
static fr_command_table_t command_table_show_debug[] = {
{ "condition", FR_READ,
"show debug condition - Shows current debugging condition.",
command_show_debug_condition, NULL },
{ "level", FR_READ,
"show debug level - Shows current debugging level.",
command_show_debug_level, NULL },
{ "file", FR_READ,
"show debug file - Shows current debugging file.",
command_show_debug_file, NULL },
{ NULL, 0, NULL, NULL, NULL }
};
static fr_command_table_t command_table_show_module[] = {
{ "config", FR_READ,
"show module config <module> - show configuration for given module",
command_show_module_config, NULL },
{ "flags", FR_READ,
"show module flags <module> - show other module properties",
command_show_module_flags, NULL },
{ "list", FR_READ,
"show module list - shows list of loaded modules",
command_show_modules, NULL },
{ "methods", FR_READ,
"show module methods <module> - show sections where <module> may be used",
command_show_module_methods, NULL },
{ "status", FR_READ,
"show module status <module> - show the module status",
command_show_module_status, NULL },
{ NULL, 0, NULL, NULL, NULL }
};
static fr_command_table_t command_table_show_client[] = {
{ "config", FR_READ,
"show client config <ipaddr> "
#ifdef WITH_TCP
"[udp|tcp] "
#endif
"- show configuration for given client",
command_show_client_config, NULL },
{ "list", FR_READ,
"show client list [verbose] - shows list of global clients",
command_show_clients, NULL },
{ NULL, 0, NULL, NULL, NULL }
};
#ifdef WITH_PROXY
static fr_command_table_t command_table_show_home[] = {
{ "list", FR_READ,
"show home_server list [all] - shows list of home servers",
command_show_home_servers, NULL },
{ "state", FR_READ,
"show home_server state <ipaddr> <port> [udp|tcp] [src <ipaddr>] - shows state of given home server",
command_show_home_server_state, NULL },
{ NULL, 0, NULL, NULL, NULL }
};
#endif
static fr_command_table_t command_table_show[] = {
{ "client", FR_READ,
"show client <command> - do sub-command of client",
NULL, command_table_show_client },
{ "config", FR_READ,
"show config <path> - shows the value of configuration option <path>",
command_show_config, NULL },
{ "debug", FR_READ,
"show debug <command> - show debug properties",
NULL, command_table_show_debug },
#ifdef WITH_PROXY
{ "home_server", FR_READ,
"show home_server <command> - do sub-command of home_server",
NULL, command_table_show_home },
#endif
{ "module", FR_READ,
"show module <command> - do sub-command of module",
NULL, command_table_show_module },
{ "uptime", FR_READ,
"show uptime - shows time at which server started",
command_uptime, NULL },
{ "version", FR_READ,
"show version - Prints version of the running server",
command_show_version, NULL },
{ NULL, 0, NULL, NULL, NULL }
};
static int command_set_module_config(rad_listen_t *listener, int argc, char *argv[])
{
int i, rcode;
CONF_PAIR *cp;
CONF_SECTION *cs;
module_instance_t *mi;
CONF_PARSER const *variables;
void *data;
if (argc < 3) {
cprintf_error(listener, "No module name or variable was given\n");
return 0;
}
cs = cf_section_find("modules");
if (!cs) return 0;
mi = module_find(cs, argv[0]);
if (!mi) {
cprintf_error(listener, "No such module \"%s\"\n", argv[0]);
return 0;
}
if ((mi->entry->module->type & RLM_TYPE_HUP_SAFE) == 0) {
cprintf_error(listener, "Cannot change configuration of module as it is cannot be HUP'd.\n");
return 0;
}
variables = cf_section_parse_table(mi->cs);
if (!variables) {
cprintf_error(listener, "Cannot find configuration for module\n");
return 0;
}
rcode = -1;
for (i = 0; variables[i].name != NULL; i++) {
/*
* FIXME: Recurse into sub-types somehow...
*/
if (variables[i].type == PW_TYPE_SUBSECTION) continue;
if (strcmp(variables[i].name, argv[1]) == 0) {
rcode = i;
break;
}
}
if (rcode < 0) {
cprintf_error(listener, "No such variable \"%s\"\n", argv[1]);
return 0;
}
i = rcode; /* just to be safe */
/*
* It's not part of the dynamic configuration. The module
* needs to re-parse && validate things.
*/
if (variables[i].data) {
cprintf_error(listener, "Variable cannot be dynamically updated\n");
return 0;
}
data = ((char *) mi->insthandle) + variables[i].offset;
cp = cf_pair_find(mi->cs, argv[1]);
if (!cp) return 0;
/*
* Replace the OLD value in the configuration file with
* the NEW value.
*
* FIXME: Parse argv[2] depending on it's data type!
* If it's a string, look for leading single/double quotes,
* end then call tokenize functions???
*/
cf_pair_replace(mi->cs, cp, argv[2]);
rcode = cf_item_parse(mi->cs, argv[1], variables[i].type, data, argv[2]);
if (rcode < 0) {
cprintf_error(listener, "Failed to parse value\n");
return 0;
}
return CMD_OK;
}
static int command_set_module_status(rad_listen_t *listener, int argc, char *argv[])
{
CONF_SECTION *cs;
module_instance_t *mi;
if (argc < 2) {
cprintf_error(listener, "No module name or status was given\n");
return 0;
}
cs = cf_section_find("modules");
if (!cs) return 0;
mi = module_find(cs, argv[0]);
if (!mi) {
cprintf_error(listener, "No such module \"%s\"\n", argv[0]);
return 0;
}
if (strcmp(argv[1], "alive") == 0) {
mi->force = false;
} else if (strcmp(argv[1], "dead") == 0) {
mi->code = RLM_MODULE_FAIL;
mi->force = true;
} else {
int rcode;
rcode = fr_str2int(mod_rcode_table, argv[1], -1);
if (rcode < 0) {
cprintf_error(listener, "Unknown status \"%s\"\n", argv[1]);
return 0;
}
mi->code = rcode;
mi->force = true;
}
return CMD_OK;
}
#ifdef WITH_STATS
static char const *elapsed_names[8] = {
"1us", "10us", "100us", "1ms", "10ms", "100ms", "1s", "10s"
};
static int command_print_stats(rad_listen_t *listener, fr_stats_t *stats,
int auth, int server)
{
int i;
cprintf(listener, "requests\t%" PRIu64 "\n", stats->total_requests);
cprintf(listener, "responses\t%" PRIu64 "\n", stats->total_responses);
if (auth) {
cprintf(listener, "accepts\t\t%" PRIu64 "\n",
stats->total_access_accepts);
cprintf(listener, "rejects\t\t%" PRIu64 "\n",
stats->total_access_rejects);
cprintf(listener, "challenges\t%" PRIu64 "\n",
stats->total_access_challenges);
}
cprintf(listener, "dup\t\t%" PRIu64 "\n", stats->total_dup_requests);
cprintf(listener, "invalid\t\t%" PRIu64 "\n", stats->total_invalid_requests);
cprintf(listener, "malformed\t%" PRIu64 "\n", stats->total_malformed_requests);
cprintf(listener, "bad_authenticator\t%" PRIu64 "\n", stats->total_bad_authenticators);
cprintf(listener, "dropped\t\t%" PRIu64 "\n", stats->total_packets_dropped);
cprintf(listener, "unknown_types\t%" PRIu64 "\n", stats->total_unknown_types);
if (server) {
cprintf(listener, "timeouts\t%" PRIu64 "\n", stats->total_timeouts);
} else {
cprintf(listener, "conflicts\t%" PRIu64 "\n", stats->total_conflicts);
cprintf(listener, "unresponsive_child\t%" PRIu64 "\n", stats->unresponsive_child);
}
cprintf(listener, "last_packet\t%" PRId64 "\n", (int64_t) stats->last_packet);
for (i = 0; i < 8; i++) {
cprintf(listener, "elapsed.%s\t%" PRIu64 "\n",
elapsed_names[i], stats->elapsed[i]);
}
return CMD_OK;
}
#ifdef HAVE_PTHREAD_H
static int command_stats_queue(rad_listen_t *listener, UNUSED int argc, UNUSED char *argv[])
{
int array[RAD_LISTEN_MAX], pps[2];
thread_pool_queue_stats(array, pps);
cprintf(listener, "queue_len_internal\t%d\n", array[0]);
cprintf(listener, "queue_len_proxy\t\t%d\n", array[1]);
cprintf(listener, "queue_len_auth\t\t%d\n", array[2]);
cprintf(listener, "queue_len_acct\t\t%d\n", array[3]);
cprintf(listener, "queue_len_detail\t%d\n", array[4]);
cprintf(listener, "queue_pps_in\t\t%d\n", pps[0]);
cprintf(listener, "queue_pps_out\t\t%d\n", pps[1]);
return CMD_OK;
}
#endif
#ifndef NDEBUG
static int command_stats_memory(rad_listen_t *listener, int argc, char *argv[])
{
if (!main_config.debug_memory || !main_config.memory_report) {
cprintf(listener, "No memory debugging was enabled.\n");
return CMD_OK;
}
if (argc == 0) goto fail;
if (strcmp(argv[0], "total") == 0) {
cprintf(listener, "%zd\n", talloc_total_size(NULL));
return CMD_OK;
}
if (strcmp(argv[0], "blocks") == 0) {
cprintf(listener, "%zd\n", talloc_total_blocks(NULL));
return CMD_OK;
}
if (strcmp(argv[0], "full") == 0) {
cprintf(listener, "see stdout of the server for the full report.\n");
fr_log_talloc_report(NULL);
return CMD_OK;
}
fail:
cprintf_error(listener, "Must use 'stats memory [blocks|full|total]'\n");
return CMD_FAIL;
}
#endif
#ifdef WITH_DETAIL
static FR_NAME_NUMBER state_names[] = {
{ "unopened", STATE_UNOPENED },
{ "unlocked", STATE_UNLOCKED },
{ "header", STATE_HEADER },
{ "reading", STATE_READING },
{ "queued", STATE_QUEUED },
{ "running", STATE_RUNNING },
{ "no-reply", STATE_NO_REPLY },
{ "replied", STATE_REPLIED },
{ NULL, 0 }
};
static int command_stats_detail(rad_listen_t *listener, int argc, char *argv[])
{
rad_listen_t *this;
listen_detail_t *data, *needle;
struct stat buf;
if (argc == 0) {
cprintf_error(listener, "Must specify <filename>\n");
return 0;
}
data = NULL;
for (this = main_config.listen; this != NULL; this = this->next) {
if (this->type != RAD_LISTEN_DETAIL) continue;
needle = this->data;
if (!strcmp(argv[0], needle->filename)) {
data = needle;
break;
}
}
if (!data) {
cprintf_error(listener, "No detail file listener\n");
return 0;
}
cprintf(listener, "state\t%s\n",
fr_int2str(state_names, data->state, "?"));
if ((data->state == STATE_UNOPENED) ||
(data->state == STATE_UNLOCKED)) {
return CMD_OK;
}
/*
* Race conditions: file might not exist.
*/
if (stat(data->filename_work, &buf) < 0) {
cprintf(listener, "packets\t0\n");
cprintf(listener, "tries\t0\n");
cprintf(listener, "offset\t0\n");
cprintf(listener, "size\t0\n");
return CMD_OK;
}
cprintf(listener, "packets\t%d\n", data->packets);
cprintf(listener, "tries\t%d\n", data->tries);
cprintf(listener, "offset\t%u\n", (unsigned int) data->offset);
cprintf(listener, "size\t%u\n", (unsigned int) buf.st_size);
return CMD_OK;
}
#endif
#ifdef WITH_PROXY
static int command_stats_home_server(rad_listen_t *listener, int argc, char *argv[])
{
home_server_t *home;
if (argc == 0) {
cprintf_error(listener, "Must specify [auth|acct|coa|disconnect] OR <ipaddr> <port>\n");
return 0;
}
if (argc == 1) {
if (strcmp(argv[0], "auth") == 0) {
return command_print_stats(listener,
&proxy_auth_stats, 1, 1);
}
#ifdef WITH_ACCOUNTING
if (strcmp(argv[0], "acct") == 0) {
return command_print_stats(listener,
&proxy_acct_stats, 0, 1);
}
#endif
#ifdef WITH_ACCOUNTING
if (strcmp(argv[0], "coa") == 0) {
return command_print_stats(listener,
&proxy_coa_stats, 0, 1);
}
#endif
#ifdef WITH_ACCOUNTING
if (strcmp(argv[0], "disconnect") == 0) {
return command_print_stats(listener,
&proxy_dsc_stats, 0, 1);
}
#endif
cprintf_error(listener, "Should specify [auth|acct|coa|disconnect]\n");
return 0;
}
home = get_home_server(listener, argc, argv, NULL);
if (!home) return 0;
command_print_stats(listener, &home->stats,
(home->type == HOME_TYPE_AUTH), 1);
cprintf(listener, "outstanding\t%d\n", home->currently_outstanding);
return CMD_OK;
}
#endif
static int command_stats_client(rad_listen_t *listener, int argc, char *argv[])
{
bool auth = true;
fr_stats_t *stats;
RADCLIENT *client, fake;
if (argc < 1) {
cprintf_error(listener, "Must specify [auth/acct]\n");
return 0;
}
if (argc == 1) {
/*
* Global statistics.
*/
fake.auth = radius_auth_stats;
#ifdef WITH_ACCOUNTING
fake.acct = radius_acct_stats;
#endif
#ifdef WITH_COA
fake.coa = radius_coa_stats;
fake.dsc = radius_dsc_stats;
#endif
client = &fake;
} else {
/*
* Per-client statistics.
*/
client = get_client(listener, argc - 1, argv + 1);
if (!client) return 0;
}
if (strcmp(argv[0], "auth") == 0) {
auth = true;
stats = &client->auth;
} else if (strcmp(argv[0], "acct") == 0) {
#ifdef WITH_ACCOUNTING
auth = false;
stats = &client->acct;
#else
cprintf_error(listener, "This server was built without accounting support.\n");
return 0;
#endif
} else if (strcmp(argv[0], "coa") == 0) {
#ifdef WITH_COA
auth = false;
stats = &client->coa;
#else
cprintf_error(listener, "This server was built without CoA support.\n");
return 0;
#endif
} else if (strcmp(argv[0], "disconnect") == 0) {
#ifdef WITH_COA
auth = false;
stats = &client->dsc;
#else
cprintf_error(listener, "This server was built without CoA support.\n");
return 0;
#endif
} else {
cprintf_error(listener, "Unknown statistics type\n");
return 0;
}
/*
* Global results for all client.
*/
if (argc == 1) {
#ifdef WITH_ACCOUNTING
if (!auth) {
return command_print_stats(listener,
&radius_acct_stats, auth, 0);
}
#endif
return command_print_stats(listener, &radius_auth_stats, auth, 0);
}
return command_print_stats(listener, stats, auth, 0);
}
static int command_stats_socket(rad_listen_t *listener, int argc, char *argv[])
{
bool auth = true;
rad_listen_t *sock;
sock = get_socket(listener, argc, argv, NULL);
if (!sock) return 0;
if (sock->type != RAD_LISTEN_AUTH) auth = false;
return command_print_stats(listener, &sock->stats, auth, 0);
}
static int command_stats_pool(rad_listen_t *listener, UNUSED int argc, UNUSED char *argv[])
{
CONF_SECTION *cs;
module_instance_t *mi;
fr_connection_pool_stats_t const *stats;
if (argc < 1) {
cprintf_error(listener, "Must specify <name>\n");
return CMD_FAIL;
}
cs = cf_section_find("modules");
if (!cs) return CMD_FAIL;
mi = module_find(cs, argv[0]);
if (!mi) {
cprintf_error(listener, "No such module \"%s\"\n", argv[0]);
return CMD_FAIL;
}
stats = fr_connection_pool_stats(mi->cs);
if (!stats) {
cprintf_error(listener, "Module %s has no pool statistics", argv[0]);
return CMD_FAIL;
}
cprintf(listener, "last_checked\t\t%zu\n", stats->last_checked);
cprintf(listener, "last_opened\t\t%zu\n", stats->last_opened);
cprintf(listener, "last_closed\t\t%zu\n", stats->last_closed);
cprintf(listener, "last_failed\t\t%zu\n", stats->last_failed);
cprintf(listener, "last_throttled\t\t%zu\n", stats->last_throttled);
cprintf(listener, "total_opened\t\t%" PRIu64 "\n", stats->opened);
cprintf(listener, "total_closed\t\t%" PRIu64 "\n", stats->closed);
cprintf(listener, "total_failed\t\t%" PRIu64 "\n", stats->failed);
cprintf(listener, "num_open\t\t%u\n", stats->num);
cprintf(listener, "num_in_use\t\t%u\n", stats->active);
return CMD_OK;
}
#endif /* WITH_STATS */
#ifdef WITH_DYNAMIC_CLIENTS
static int command_add_client_file(rad_listen_t *listener, int argc, char *argv[])
{
RADCLIENT *c;
if (argc < 1) {
cprintf_error(listener, "<file> is required\n");
return 0;
}
/*
* Read the file and generate the client.
*/
c = client_read(argv[0], false, false);
if (!c) {
cprintf_error(listener, "Unknown error reading client file.\n");
return 0;
}
if (!client_add(NULL, c)) {
cprintf_error(listener, "Unknown error inserting new client.\n");
client_free(c);
return 0;
}
return CMD_OK;
}
static int command_del_client(rad_listen_t *listener, int argc, char *argv[])
{
RADCLIENT *client;
client = get_client(listener, argc, argv);
if (!client) return 0;
if (!client->dynamic) {
cprintf_error(listener, "Client %s was not dynamically defined.\n", argv[0]);
return 0;
}
/*
* DON'T delete it. Instead, mark it as "dead now". The
* next time we receive a packet for the client, it will
* be deleted.
*
* If we don't receive a packet from it, the client
* structure will stick around for a while. Oh well...
*/
client->lifetime = 1;
return CMD_OK;
}
static int command_del_home_server(rad_listen_t *listener, int argc, char *argv[])
{
if (argc < 2) {
cprintf_error(listener, "<name> and <type> are required\n");
return 0;
}
if (home_server_delete(argv[0], argv[1]) < 0) {
cprintf_error(listener, "Failed deleted home_server %s - %s\n", argv[1], fr_strerror());
return 0;
}
return CMD_OK;
}
static int command_add_home_server_file(rad_listen_t *listener, int argc, char *argv[])
{
if (argc < 1) {
cprintf_error(listener, "<file> is required\n");
return 0;
}
if (home_server_afrom_file(argv[0]) < 0) {
cprintf_error(listener, "Unable to add home server - %s\n", fr_strerror());
return 0;
}
return CMD_OK;
}
static fr_command_table_t command_table_del_client[] = {
{ "ipaddr", FR_WRITE,
"del client ipaddr <ipaddr> [udp|tcp] [listen <ipaddr> <port>] - Delete a dynamically created client",
command_del_client, NULL },
{ NULL, 0, NULL, NULL, NULL }
};
static fr_command_table_t command_table_del_home_server[] = {
{ "file", FR_WRITE,
"del home_server file <name> [auth|acct|coa] - Delete a dynamically created home_server",
command_del_home_server, NULL },
{ NULL, 0, NULL, NULL, NULL }
};
static fr_command_table_t command_table_del[] = {
{ "client", FR_WRITE,
"del client <command> - Delete client configuration commands",
NULL, command_table_del_client },
{ "home_server", FR_WRITE,
"del home_server <command> - Delete home_server configuration commands",
NULL, command_table_del_home_server },
{ NULL, 0, NULL, NULL, NULL }
};
static fr_command_table_t command_table_add_client[] = {
{ "file", FR_WRITE,
"add client file <filename> - Add new client definition from <filename>",
command_add_client_file, NULL },
{ NULL, 0, NULL, NULL, NULL }
};
static fr_command_table_t command_table_add_home_server[] = {
{ "file", FR_WRITE,
"add home_server file <filename> - Add new home serverdefinition from <filename>",
command_add_home_server_file, NULL },
{ NULL, 0, NULL, NULL, NULL }
};
static fr_command_table_t command_table_add[] = {
{ "client", FR_WRITE,
"add client <command> - Add client configuration commands",
NULL, command_table_add_client },
{ "home_server", FR_WRITE,
"add home_server <command> - Add home server configuration commands",
NULL, command_table_add_home_server },
{ NULL, 0, NULL, NULL, NULL }
};
#endif
#ifdef WITH_PROXY
static fr_command_table_t command_table_set_home[] = {
{ "state", FR_WRITE,
"set home_server state <ipaddr> <port> [udp|tcp] [src <ipaddr>] [alive|dead|down] - set state for given home server",
command_set_home_server_state, NULL },
{ NULL, 0, NULL, NULL, NULL }
};
#endif
static fr_command_table_t command_table_set_module[] = {
{ "config", FR_WRITE,
"set module config <module> variable value - set configuration for <module>",
command_set_module_config, NULL },
{ "status", FR_WRITE,
"set module status <module> [alive|...] - set the module status to be alive (operating normally), or force a particular code (ok,fail, etc.)",
command_set_module_status, NULL },
{ NULL, 0, NULL, NULL, NULL }
};
static fr_command_table_t command_table_set[] = {
{ "module", FR_WRITE,
"set module <command> - set module commands",
NULL, command_table_set_module },
#ifdef WITH_PROXY
{ "home_server", FR_WRITE,
"set home_server <command> - set home server commands",
NULL, command_table_set_home },
#endif
{ NULL, 0, NULL, NULL, NULL }
};
#ifdef WITH_STATS
static fr_command_table_t command_table_stats[] = {
{ "client", FR_READ,
"stats client [auth/acct/coa] <ipaddr> [udp|tcp] [listen <ipaddr> <port>] "
"- show statistics for given client, or for all clients (auth or acct)",
command_stats_client, NULL },
#ifdef WITH_DETAIL
{ "detail", FR_READ,
"stats detail <filename> - show statistics for the given detail file",
command_stats_detail, NULL },
#endif
#ifdef WITH_PROXY
{ "home_server", FR_READ,
"stats home_server [<ipaddr>|auth|acct|coa|disconnect] <port> [udp|tcp] [src <ipaddr>] - show statistics for given home server (ipaddr and port), or for all home servers (auth or acct)",
command_stats_home_server, NULL },
#endif
{ "pool", FR_READ,
"pool <name> "
"- show pool statistics for given module",
command_stats_pool, NULL },
#ifdef HAVE_PTHREAD_H
{ "queue", FR_READ,
"stats queue - show statistics for packet queues",
command_stats_queue, NULL },
#endif
{ "socket", FR_READ,
"stats socket <ipaddr> <port> [udp|tcp] "
"- show statistics for given socket",
command_stats_socket, NULL },
#ifndef NDEBUG
{ "memory", FR_READ,
"stats memory [blocks|full|total] - show statistics on used memory",
command_stats_memory, NULL },
#endif
{ NULL, 0, NULL, NULL, NULL }
};
#endif
static fr_command_table_t command_table[] = {
#ifdef WITH_DYNAMIC_CLIENTS
{ "add", FR_WRITE, NULL, NULL, command_table_add },
#endif
{ "debug", FR_WRITE,
"debug <command> - debugging commands",
NULL, command_table_debug },
#ifdef WITH_DYNAMIC_CLIENTS
{ "del", FR_WRITE, NULL, NULL, command_table_del },
#endif
{ "hup", FR_WRITE,
"hup [module] - sends a HUP signal to the server, or optionally to one module",
command_hup, NULL },
{ "inject", FR_WRITE,
"inject <command> - commands to inject packets into a running server",
NULL, command_table_inject },
{ "reconnect", FR_READ,
"reconnect - reconnect to a running server",
NULL, NULL }, /* just here for "help" */
{ "terminate", FR_WRITE,
"terminate - terminates the server, and cause it to exit",
command_terminate, NULL },
{ "set", FR_WRITE, NULL, NULL, command_table_set },
{ "show", FR_READ, NULL, NULL, command_table_show },
#ifdef WITH_STATS
{ "stats", FR_READ, NULL, NULL, command_table_stats },
#endif
{ NULL, 0, NULL, NULL, NULL }
};
static void command_socket_free(rad_listen_t *this)
{
fr_command_socket_t *cmd = this->data;
/*
* If it's a TCP socket, don't do anything.
*/
if (cmd->magic != COMMAND_SOCKET_MAGIC) {
return;
}
if (!cmd->copy) return;
unlink(cmd->copy);
}
/*
* Parse the unix domain sockets.
*
* FIXME: TCP + SSL, after RadSec is in.
*/
static int command_socket_parse_unix(CONF_SECTION *cs, rad_listen_t *this)
{
fr_command_socket_t *sock;
if (check_config) return 0;
sock = this->data;
if (cf_section_parse(cs, sock, command_config) < 0) return -1;
/*
* Can't get uid or gid of connecting user, so can't do
* peercred authentication.
*/
#ifndef HAVE_GETPEEREID
if (sock->peercred && (sock->uid_name || sock->gid_name)) {
ERROR("System does not support uid or gid authentication for sockets");
return -1;
}
#endif
sock->magic = COMMAND_SOCKET_MAGIC;
sock->copy = NULL;
if (sock->path) sock->copy = talloc_typed_strdup(sock, sock->path);
if (sock->uid_name) {
struct passwd *pwd;
if (rad_getpwnam(cs, &pwd, sock->uid_name) < 0) {
ERROR("Failed getting uid for %s: %s", sock->uid_name, fr_strerror());
return -1;
}
sock->uid = pwd->pw_uid;
talloc_free(pwd);
} else {
sock->uid = -1;
}
if (sock->gid_name) {
if (rad_getgid(cs, &sock->gid, sock->gid_name) < 0) {
ERROR("Failed getting gid for %s: %s", sock->gid_name, fr_strerror());
return -1;
}
} else {
sock->gid = -1;
}
if (!sock->mode_name) {
sock->co.mode = FR_READ;
} else {
sock->co.mode = fr_str2int(mode_names, sock->mode_name, 0);
if (!sock->co.mode) {
ERROR("Invalid mode name \"%s\"",
sock->mode_name);
return -1;
}
}
if (sock->peercred) {
this->fd = fr_server_domain_socket_peercred(sock->path, sock->uid, sock->gid);
} else {
uid_t uid = sock->uid;
gid_t gid = sock->gid;
if (uid == ((uid_t)-1)) uid = 0;
if (gid == ((gid_t)-1)) gid = 0;
this->fd = fr_server_domain_socket_perm(sock->path, uid, gid);
}
if (this->fd < 0) {
ERROR("Failed creating control socket \"%s\": %s", sock->path, fr_strerror());
if (sock->copy) talloc_free(sock->copy);
sock->copy = NULL;
return -1;
}
return 0;
}
static int command_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
{
int rcode;
CONF_PAIR const *cp;
listen_socket_t *sock;
cp = cf_pair_find(cs, "socket");
if (cp) return command_socket_parse_unix(cs, this);
rcode = common_socket_parse(cs, this);
if (rcode < 0) return -1;
#ifdef WITH_TLS
if (this->tls) {
cf_log_err_cs(cs,
"TLS is not supported for control sockets");
return -1;
}
#endif
sock = this->data;
if (sock->proto != IPPROTO_TCP) {
cf_log_err_cs(cs,
"UDP is not supported for control sockets");
return -1;
}
return 0;
}
static int command_socket_print(rad_listen_t const *this, char *buffer, size_t bufsize)
{
fr_command_socket_t *sock = this->data;
if (sock->magic != COMMAND_SOCKET_MAGIC) {
return common_socket_print(this, buffer, bufsize);
}
snprintf(buffer, bufsize, "command file %s", sock->path);
return 1;
}
/*
* String split routine. Splits an input string IN PLACE
* into pieces, based on spaces.
*/
static int str2argvX(char *str, char **argv, int max_argc)
{
int argc = 0;
while (*str) {
if (argc >= max_argc) return argc;
/*
* Chop out comments early.
*/
if (*str == '#') {
*str = '\0';
break;
}
while ((*str == ' ') ||
(*str == '\t') ||
(*str == '\r') ||
(*str == '\n')) *(str++) = '\0';
if (!*str) return argc;
argv[argc++] = str;
if ((*str == '\'') || (*str == '"')) {
char quote = *str;
char *p = str + 1;
while (true) {
if (!*p) return -1;
if (*p == quote) {
str = p + 1;
break;
}
/*
* Handle \" and nothing else.
*/
if (*p == '\\') {
p += 2;
continue;
}
p++;
}
}
while (*str &&
(*str != ' ') &&
(*str != '\t') &&
(*str != '\r') &&
(*str != '\n')) str++;
}
return argc;
}
static void print_help(rad_listen_t *listener, int argc, char *argv[],
fr_command_table_t *table, int recursive)
{
int i;
/* this should never happen, but if it does then just return gracefully */
if (!table) return;
for (i = 0; table[i].command != NULL; i++) {
if (argc > 0) {
if (strcmp(table[i].command, argv[0]) == 0) {
if (table[i].table) {
print_help(listener, argc - 1, argv + 1, table[i].table, recursive);
} else {
if (table[i].help) {
cprintf(listener, "%s\n", table[i].help);
}
}
return;
}
continue;
}
if (table[i].help) {
cprintf(listener, "%s\n",
table[i].help);
} else {
cprintf(listener, "%s <command> - do sub-command of %s\n",
table[i].command, table[i].command);
}
if (recursive && table[i].table) {
print_help(listener, 0, NULL, table[i].table, recursive);
}
}
}
#define MAX_ARGV (16)
/*
* Check if an incoming request is "ok"
*
* It takes packets, not requests. It sees if the packet looks
* OK. If so, it does a number of sanity checks on it.
*/
static int command_domain_recv_co(rad_listen_t *listener, fr_cs_buffer_t *co)
{
int i;
uint32_t status;
ssize_t r, len;
int argc;
fr_channel_type_t channel;
char *my_argv[MAX_ARGV], **argv;
fr_command_table_t *table;
uint8_t *command;
r = fr_channel_drain(listener->fd, &channel, co->buffer, sizeof(co->buffer) - 1, &command, co->offset);
if (r <= 0) {
do_close:
command_close_socket(listener);
return 0;
}
/*
* We need more data. Go read it.
*/
if (channel == FR_CHANNEL_WANT_MORE) {
co->offset = r;
return 0;
}
status = 0;
command[r] = '\0';
DEBUG("radmin> %s", command);
argc = str2argvX((char *) command, my_argv, MAX_ARGV);
if (argc == 0) goto do_next; /* empty strings are OK */
if (argc < 0) {
cprintf_error(listener, "Failed parsing command.\n");
goto do_next;
}
argv = my_argv;
for (len = 0; len <= co->offset; len++) {
if (command[len] < 0x20) {
command[len] = '\0';
break;
}
}
/*
* Hard-code exit && quit.
*/
if ((strcmp(argv[0], "exit") == 0) ||
(strcmp(argv[0], "quit") == 0)) goto do_close;
table = command_table;
retry:
len = 0;
for (i = 0; table[i].command != NULL; i++) {
if (strcmp(table[i].command, argv[0]) == 0) {
/*
* Check permissions.
*/
if (((co->mode & FR_WRITE) == 0) &&
((table[i].mode & FR_WRITE) != 0)) {
cprintf_error(listener, "You do not have write permission. See \"mode = rw\" in the \"listen\" section for this socket.\n");
goto do_next;
}
if (table[i].table) {
/*
* This is the last argument, but
* there's a sub-table. Print help.
*
*/
if (argc == 1) {
table = table[i].table;
goto do_help;
}
argc--;
argv++;
table = table[i].table;
goto retry;
}
if ((argc == 2) && (strcmp(argv[1], "?") == 0)) goto do_help;
if (!table[i].func) {
cprintf_error(listener, "Invalid command\n");
goto do_next;
}
status = table[i].func(listener, argc - 1, argv + 1);
goto do_next;
}
}
/*
* No such command
*/
if (!len) {
if ((strcmp(argv[0], "help") == 0) ||
(strcmp(argv[0], "?") == 0)) {
int recursive;
do_help:
if ((argc > 1) && (strcmp(argv[1], "-r") == 0)) {
recursive = true;
argc--;
argv++;
} else {
recursive = false;
}
print_help(listener, argc - 1, argv + 1, table, recursive);
goto do_next;
}
cprintf_error(listener, "Unknown command \"%s\"\n",
argv[0]);
}
do_next:
r = fr_channel_write(listener->fd, FR_CHANNEL_CMD_STATUS, &status, sizeof(status));
if (r <= 0) goto do_close;
return 0;
}
/*
* Write 32-bit magic number && version information.
*/
static int command_write_magic(int newfd,
#ifndef WITH_TCP
UNUSED
#endif
listen_socket_t *sock
)
{
ssize_t r;
uint32_t magic;
fr_channel_type_t channel;
char buffer[16];
r = fr_channel_read(newfd, &channel, buffer, 8);
if (r <= 0) {
do_close:
ERROR("Cannot talk to socket: %s",
fr_syserror(errno));
return -1;
}
magic = htonl(0xf7eead16);
if ((r != 8) || (channel != FR_CHANNEL_INIT_ACK) ||
(memcmp(&magic, &buffer, sizeof(magic)) != 0)) {
ERROR("Incompatible versions");
return -1;
}
r = fr_channel_write(newfd, FR_CHANNEL_INIT_ACK, buffer, 8);
if (r <= 0) goto do_close;
#ifdef WITH_TCP
/*
* Write an initial challenge
*/
if (sock) {
int i;
fr_cs_buffer_t *co;
co = talloc_zero(sock, fr_cs_buffer_t);
sock->packet = (void *) co;
for (i = 0; i < 16; i++) {
co->buffer[i] = fr_rand();
}
r = fr_channel_write(newfd, FR_CHANNEL_AUTH_CHALLENGE, co->buffer, 16);
if (r <= 0) goto do_close;
}
#endif
return 0;
}
#ifdef WITH_TCP
static int command_tcp_recv(rad_listen_t *this)
{
ssize_t r;
listen_socket_t *sock = this->data;
fr_cs_buffer_t *co = (void *) sock->packet;
fr_channel_type_t channel;
if (!co) {
do_close:
command_close_socket(this);
return 0;
}
if (!co->auth) {
uint8_t expected[16];
r = fr_channel_read(this->fd, &channel, co->buffer, 16);
if ((r != 16) || (channel != FR_CHANNEL_AUTH_RESPONSE)) {
goto do_close;
}
fr_hmac_md5(expected, (void const *) sock->client->secret,
strlen(sock->client->secret),
(uint8_t *) co->buffer, 16);
if (rad_digest_cmp(expected,
(uint8_t *) co->buffer + 16, 16 != 0)) {
ERROR("radmin failed challenge: Closing socket");
goto do_close;
}
co->auth = true;
co->offset = 0;
}
return command_domain_recv_co(this, co);
}
/*
* Should never be called. The functions should just call write().
*/
static int command_tcp_send(UNUSED rad_listen_t *listener, UNUSED REQUEST *request)
{
return 0;
}
#endif
static int command_domain_recv(rad_listen_t *listener)
{
fr_command_socket_t *sock = listener->data;
return command_domain_recv_co(listener, &sock->co);
}
static int command_domain_accept(rad_listen_t *listener)
{
int newfd;
rad_listen_t *this;
socklen_t salen;
struct sockaddr_storage src;
fr_command_socket_t *sock = listener->data;
salen = sizeof(src);
DEBUG2(" ... new connection request on command socket");
newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
if (newfd < 0) {
/*
* Non-blocking sockets must handle this.
*/
if (errno == EWOULDBLOCK) {
return 0;
}
DEBUG2(" ... failed to accept connection");
return 0;
}
#ifdef HAVE_GETPEEREID
/*
* Perform user authentication.
*/
if (sock->peercred && (sock->uid_name || sock->gid_name)) {
uid_t uid;
gid_t gid;
if (getpeereid(newfd, &uid, &gid) < 0) {
ERROR("Failed getting peer credentials for %s: %s",
sock->path, fr_syserror(errno));
close(newfd);
return 0;
}
/*
* Only do UID checking if the caller is
* non-root. The superuser can do anything, so
* we might as well let them.
*/
if (uid != 0) do {
/*
* Allow entry if UID or GID matches.
*/
if (sock->uid_name && (sock->uid == uid)) break;
if (sock->gid_name && (sock->gid == gid)) break;
if (sock->uid_name && (sock->uid != uid)) {
ERROR("Unauthorized connection to %s from uid %ld",
sock->path, (long int) uid);
close(newfd);
return 0;
}
if (sock->gid_name && (sock->gid != gid)) {
ERROR("Unauthorized connection to %s from gid %ld",
sock->path, (long int) gid);
close(newfd);
return 0;
}
} while (0);
}
#endif
if (command_write_magic(newfd, NULL) < 0) {
close(newfd);
return 0;
}
/*
* Add the new listener.
*/
this = listen_alloc(listener, listener->type);
if (!this) return 0;
/*
* Copy everything, including the pointer to the socket
* information.
*/
sock = this->data;
memcpy(this, listener, sizeof(*this));
this->status = RAD_LISTEN_STATUS_INIT;
this->next = NULL;
this->data = sock; /* fix it back */
sock->magic = COMMAND_SOCKET_MAGIC;
sock->user[0] = '\0';
sock->path = ((fr_command_socket_t *) listener->data)->path;
sock->co.offset = 0;
sock->co.mode = ((fr_command_socket_t *) listener->data)->co.mode;
this->fd = newfd;
this->recv = command_domain_recv;
/*
* Tell the event loop that we have a new FD
*/
radius_update_listener(this);
return 0;
}
/*
* Send an authentication response packet
*/
static int command_domain_send(UNUSED rad_listen_t *listener,
UNUSED REQUEST *request)
{
return 0;
}
static int command_socket_encode(UNUSED rad_listen_t *listener,
UNUSED REQUEST *request)
{
return 0;
}
static int command_socket_decode(UNUSED rad_listen_t *listener,
UNUSED REQUEST *request)
{
return 0;
}
#endif /* WITH_COMMAND_SOCKET */
|