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
|
/*
* adcli
*
* Copyright (C) 2012 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* Author: Stef Walter <stefw@gnome.org>
*/
#include "config.h"
#include "adenroll.h"
#include "adprivate.h"
#include "seq.h"
#include <gssapi/gssapi_krb5.h>
#include <krb5/krb5.h>
#include <ldap.h>
#include <sasl/sasl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifndef SAMBA_DATA_TOOL
#define SAMBA_DATA_TOOL "/usr/bin/net"
#endif
static krb5_enctype v60_later_enctypes_fips[] = {
ENCTYPE_AES256_CTS_HMAC_SHA1_96,
ENCTYPE_AES128_CTS_HMAC_SHA1_96,
0
};
static krb5_enctype v60_later_enctypes[] = {
ENCTYPE_AES256_CTS_HMAC_SHA1_96,
ENCTYPE_AES128_CTS_HMAC_SHA1_96,
ENCTYPE_DES3_CBC_SHA1,
ENCTYPE_ARCFOUR_HMAC,
ENCTYPE_DES_CBC_MD5,
ENCTYPE_DES_CBC_CRC,
0
};
static krb5_enctype v51_earlier_enctypes[] = {
ENCTYPE_DES_CBC_CRC,
ENCTYPE_DES_CBC_MD5,
ENCTYPE_ARCFOUR_HMAC,
0
};
/* The following list containst all attributes handled by adcli, some are
* read-only and the others can be written as well. To properly document the
* required permissions each attribute which adcli tries to modify should have
* a comment starting with ':ADPermissions:' and the related permissions in AD
* on the same line. Multiple permissions can be seperated with a '*'. For all
* other attribute a suitable comment is very welcome. */
static char *default_ad_ldap_attrs[] = {
"sAMAccountName", /* Only set during creation */
"userPrincipalName", /* :ADPermissions: Read/Write userPrincipal Name */
"msDS-KeyVersionNumber", /* Manages by AD */
"msDS-supportedEncryptionTypes", /* :ADPermissions: Read/Write msDS-SupportedEncryptionTypes */
"dNSHostName", /* :ADPermissions: Read/Write dNSHostName * Read and write DNS host name attributes * Validated write to DNS host name */
"servicePrincipalName", /* :ADPermissions: Read/Write servicePrincipalName * Validated write to service principal name */
"operatingSystem", /* :ADPermissions: Read/Write Operating System */
"operatingSystemVersion", /* :ADPermissions: Read/Write Operating System Version */
"operatingSystemServicePack", /* :ADPermissions: Read/Write operatingSystemServicePack */
"pwdLastSet", /* Managed by AD */
"userAccountControl", /* :ADPermissions: Read/Write userAccountControl */
"description", /* :ADPermissions: Read/Write Description */
NULL,
};
/* Some constants for the userAccountControl AD LDAP attribute, see e.g.
* https://support.microsoft.com/en-us/help/305144/how-to-use-the-useraccountcontrol-flags-to-manipulate-user-account-pro
* for details. */
#define UAC_WORKSTATION_TRUST_ACCOUNT 0x1000
#define UAC_DONT_EXPIRE_PASSWORD 0x10000
#define UAC_TRUSTED_FOR_DELEGATION 0x80000
struct _adcli_enroll {
int refs;
adcli_conn *conn;
bool is_service;
bool is_service_explicit;
char *host_fqdn;
int host_fqdn_explicit;
char *computer_name;
int computer_name_explicit;
char *computer_sam;
char *computer_password;
int computer_password_explicit;
int reset_password;
krb5_principal computer_principal;
char *domain_ou;
int domain_ou_validated;
int domain_ou_explicit;
char *computer_dn;
char *computer_container;
LDAPMessage *computer_attributes;
char **service_names;
char **service_principals;
int service_principals_explicit;
char **service_principals_to_add;
char **service_principals_to_remove;
char *user_principal;
int user_princpal_generate;
char *os_name;
int os_name_explicit;
char *os_version;
int os_version_explicit;
char *os_service_pack;
int os_service_pack_explicit;
krb5_kvno kvno;
char *keytab_name;
int keytab_name_is_krb5;
krb5_keytab keytab;
krb5_principal *keytab_principals;
krb5_enctype *keytab_enctypes;
int keytab_enctypes_explicit;
unsigned int computer_password_lifetime;
int computer_password_lifetime_explicit;
char *samba_data_tool;
bool trusted_for_delegation;
int trusted_for_delegation_explicit;
char *description;
};
static void
check_if_service (adcli_enroll *enroll,
LDAP *ldap,
LDAPMessage *results)
{
char **objectclasses = NULL;
objectclasses = _adcli_ldap_parse_values (ldap, results, "objectClass");
enroll->is_service = _adcli_strv_has_ex (objectclasses,
"msDS-ManagedServiceAccount",
strcasecmp) == 1 ? true : false;
_adcli_strv_free (objectclasses);
}
static adcli_result
ensure_host_fqdn (adcli_result res,
adcli_enroll *enroll)
{
const char *fqdn;
if (res != ADCLI_SUCCESS)
return res;
if (enroll->host_fqdn) {
_adcli_info ("Using fully qualified name: %s",
enroll->host_fqdn);
return ADCLI_SUCCESS;
}
if (enroll->host_fqdn_explicit) {
_adcli_info ("Not setting fully qualified name");
return ADCLI_SUCCESS;
}
/* By default use our actual host name discovered during connecting */
fqdn = adcli_conn_get_host_fqdn (enroll->conn);
_adcli_str_set (&enroll->host_fqdn, fqdn);
return ADCLI_SUCCESS;
}
static adcli_result
ensure_computer_name (adcli_result res,
adcli_enroll *enroll)
{
if (res != ADCLI_SUCCESS)
return res;
if (enroll->computer_name) {
_adcli_info ("Enrolling computer name: %s",
enroll->computer_name);
return ADCLI_SUCCESS;
}
if (!enroll->host_fqdn) {
_adcli_err ("No host name from which to determine the computer name");
return ADCLI_ERR_CONFIG;
}
enroll->computer_name = _adcli_calc_netbios_name (enroll->host_fqdn);
if (enroll->computer_name == NULL)
return ADCLI_ERR_CONFIG;
return ADCLI_SUCCESS;
}
static adcli_result
ensure_computer_sam (adcli_result res,
adcli_enroll *enroll)
{
krb5_error_code code;
krb5_context k5;
if (res != ADCLI_SUCCESS)
return res;
free (enroll->computer_sam);
enroll->computer_sam = NULL;
if (asprintf (&enroll->computer_sam, "%s$", enroll->computer_name) < 0)
return_unexpected_if_fail (enroll->computer_sam != NULL);
k5 = adcli_conn_get_krb5_context (enroll->conn);
return_unexpected_if_fail (k5 != NULL);
if (enroll->computer_principal)
krb5_free_principal (k5, enroll->computer_principal);
enroll->computer_principal = NULL;
code = _adcli_krb5_build_principal (k5, enroll->computer_sam,
adcli_conn_get_domain_realm (enroll->conn),
&enroll->computer_principal);
return_unexpected_if_fail (code == 0);
return ADCLI_SUCCESS;
}
static int
filter_password_chars (char *password,
int length)
{
int i, j;
/*
* The MS documentation says their servers only use ASCII characters
* between 32 and 122 inclusive. We do that as well, and filter out
* all other random characters. We also remove certain characters
* special for use in a shell.
*/
for (i = 0, j = 0; i < length; i++) {
if (password[i] >= 32 && password[i] <= 122 &&
strchr (" !'\"$`", password[i]) == NULL)
password[j++] = password[i];
}
/* return the number of valid characters remaining */
return j;
}
static char *
generate_host_password (adcli_enroll *enroll,
size_t length)
{
char *password;
krb5_context k5;
krb5_error_code code;
krb5_data buffer;
int at;
k5 = adcli_conn_get_krb5_context (enroll->conn);
return_val_if_fail (k5 != NULL, NULL);
password = malloc (length + 1);
return_val_if_fail (password != NULL, NULL);
at = 0;
while (at != length) {
buffer.length = length - at;
buffer.data = password + at;
code = krb5_c_random_make_octets (k5, &buffer);
return_val_if_fail (code == 0, NULL);
at += filter_password_chars (buffer.data, buffer.length);
assert (at <= length);
}
/* This null termination works around a bug in krb5 */
password[length] = '\0';
return password;
}
static adcli_result
ensure_computer_password (adcli_result res,
adcli_enroll *enroll)
{
const int length = 120;
if (res != ADCLI_SUCCESS)
return res;
if (enroll->computer_password)
return ADCLI_SUCCESS;
if (enroll->reset_password) {
assert (enroll->computer_name != NULL);
enroll->computer_password = _adcli_calc_reset_password (enroll->computer_name);
return_unexpected_if_fail (enroll->computer_password != NULL);
_adcli_info ("Using default reset computer password");
} else {
enroll->computer_password = generate_host_password (enroll, length);
return_unexpected_if_fail (enroll->computer_password != NULL);
_adcli_info ("Generated %d character computer password", length);
}
return ADCLI_SUCCESS;
}
static adcli_result
ensure_default_service_names (adcli_enroll *enroll)
{
int length = 0;
if (enroll->service_names != NULL) {
length = seq_count (enroll->service_names);
/* Make sure there is no entry with an unexpected case. AD
* would not care but since the client side is case-sensitive
* we should make sure we use the expected spelling. */
seq_remove_unsorted (enroll->service_names,
&length, "host",
(seq_compar)strcasecmp, free);
seq_remove_unsorted (enroll->service_names,
&length, "RestrictedKrbHost",
(seq_compar)strcasecmp, free);
}
/* The default ones specified by MS */
enroll->service_names = _adcli_strv_add (enroll->service_names,
strdup ("host"), &length);
enroll->service_names = _adcli_strv_add (enroll->service_names,
strdup ("RestrictedKrbHost"), &length);
return ADCLI_SUCCESS;
}
static adcli_result
ensure_service_names (adcli_result res,
adcli_enroll *enroll)
{
if (res != ADCLI_SUCCESS)
return res;
if (enroll->service_names || enroll->service_principals)
return ADCLI_SUCCESS;
return ensure_default_service_names (enroll);
}
static adcli_result
add_service_names_to_service_principals (adcli_enroll *enroll)
{
char *name;
int length = 0;
int i;
if (enroll->service_principals != NULL) {
length = seq_count (enroll->service_principals);
}
for (i = 0; enroll->service_names[i] != NULL; i++) {
if (asprintf (&name, "%s/%s", enroll->service_names[i], enroll->computer_name) < 0)
return_unexpected_if_reached ();
enroll->service_principals = _adcli_strv_add_unique (enroll->service_principals,
name, &length, false);
if (enroll->host_fqdn) {
if (asprintf (&name, "%s/%s", enroll->service_names[i], enroll->host_fqdn) < 0)
return_unexpected_if_reached ();
enroll->service_principals = _adcli_strv_add_unique (enroll->service_principals,
name, &length, false);
}
}
return ADCLI_SUCCESS;
}
static adcli_result
add_and_remove_service_principals (adcli_enroll *enroll)
{
int length = 0;
size_t c;
const char **list;
if (enroll->service_principals != NULL) {
length = seq_count (enroll->service_principals);
}
list = adcli_enroll_get_service_principals_to_add (enroll);
if (list != NULL) {
for (c = 0; list[c] != NULL; c++) {
enroll->service_principals = _adcli_strv_add_unique (enroll->service_principals,
strdup (list[c]),
&length, false);
if (enroll->service_principals == NULL) {
return ADCLI_ERR_UNEXPECTED;
}
}
}
list = adcli_enroll_get_service_principals_to_remove (enroll);
if (list != NULL) {
for (c = 0; list[c] != NULL; c++) {
/* enroll->service_principals typically refects the
* order of the principal in the keytabm so it is not
* ordered. */
_adcli_strv_remove_unsorted (enroll->service_principals,
list[c], &length);
}
}
return ADCLI_SUCCESS;
}
static adcli_result
ensure_service_principals (adcli_result res,
adcli_enroll *enroll)
{
if (res != ADCLI_SUCCESS)
return res;
assert (enroll->keytab_principals == NULL);
if (!enroll->service_principals) {
assert (enroll->service_names != NULL);
res = add_service_names_to_service_principals (enroll);
}
if (res == ADCLI_SUCCESS) {
res = add_and_remove_service_principals (enroll);
}
return res;
}
static void enroll_clear_keytab_principals (adcli_enroll *enroll)
{
krb5_context k5;
size_t c;
if (enroll->keytab_principals) {
k5 = adcli_conn_get_krb5_context (enroll->conn);
return_if_fail (k5 != NULL);
for (c = 0; enroll->keytab_principals[c] != NULL; c++)
krb5_free_principal (k5, enroll->keytab_principals[c]);
free (enroll->keytab_principals);
enroll->keytab_principals = NULL;
}
return;
}
static adcli_result
ensure_keytab_principals (adcli_result res,
adcli_enroll *enroll)
{
krb5_context k5;
krb5_error_code code;
int count = 0;
int at, i;
/* Prepare the principals we're going to add to the keytab */
if (!enroll->is_service) {
return_unexpected_if_fail (enroll->service_principals);
count = _adcli_strv_len (enroll->service_principals);
}
k5 = adcli_conn_get_krb5_context (enroll->conn);
return_unexpected_if_fail (k5 != NULL);
enroll_clear_keytab_principals (enroll);
enroll->keytab_principals = calloc (count + 3, sizeof (krb5_principal));
return_unexpected_if_fail (enroll->keytab_principals != NULL);
at = 0;
/* First add the principal for the computer account name */
code = krb5_copy_principal (k5, enroll->computer_principal,
&enroll->keytab_principals[at++]);
return_unexpected_if_fail (code == 0);
/* Next, optionally add the user principal */
if (enroll->user_principal) {
code = krb5_parse_name (k5, enroll->user_principal,
&enroll->keytab_principals[at++]);
if (code != 0) {
if (code != 0) {
_adcli_err ("Couldn't parse kerberos user principal: %s: %s",
enroll->user_principal,
krb5_get_error_message (k5, code));
return ADCLI_ERR_CONFIG;
}
}
}
/* Now add the principals for all the various services */
for (i = 0; i < count; i++) {
code = _adcli_krb5_build_principal (k5, enroll->service_principals[i],
adcli_conn_get_domain_realm (enroll->conn),
&enroll->keytab_principals[at++]);
if (code != 0) {
_adcli_err ("Couldn't parse kerberos service principal: %s: %s",
enroll->service_principals[i],
krb5_get_error_message (k5, code));
return ADCLI_ERR_CONFIG;
}
}
return ADCLI_SUCCESS;
}
static adcli_result
ensure_user_principal (adcli_result res,
adcli_enroll *enroll)
{
char *name;
if (res != ADCLI_SUCCESS)
return res;
if (enroll->user_princpal_generate) {
name = strdup (enroll->computer_name);
return_unexpected_if_fail (name != NULL);
_adcli_str_down (name);
assert (enroll->user_principal == NULL);
if (asprintf (&enroll->user_principal, "host/%s@%s",
name, adcli_conn_get_domain_realm (enroll->conn)) < 0)
return_unexpected_if_reached ();
free (name);
}
if (enroll->user_principal)
_adcli_info ("With user principal: %s", enroll->user_principal);
return ADCLI_SUCCESS;
}
static adcli_result
lookup_computer_container (adcli_enroll *enroll,
LDAP *ldap)
{
char *attrs[] = { enroll->is_service ? "otherWellKnownObjects"
: "wellKnownObjects", NULL };
const char *prefix = enroll->is_service ? "B:32:1EB93889E40C45DF9F0C64D23BBB6237:"
: "B:32:AA312825768811D1ADED00C04FD8D5CD:";
const char *filter = enroll->is_service ? "(&(objectClass=container)(cn=Managed Service Accounts))"
: "(&(objectClass=container)(cn=Computers))";
int prefix_len;
LDAPMessage *results;
const char *base;
char **values;
int ret;
int i;
if (enroll->computer_container)
return ADCLI_SUCCESS;
base = enroll->domain_ou;
if (base == NULL)
base = adcli_conn_get_default_naming_context (enroll->conn);
assert (base != NULL);
ret = ldap_search_ext_s (ldap, base, LDAP_SCOPE_BASE,
"(objectClass=*)", attrs, 0, NULL, NULL,
NULL, -1, &results);
if (ret == LDAP_NO_SUCH_OBJECT && enroll->domain_ou) {
_adcli_err ("The organizational unit does not exist: %s", enroll->domain_ou);
return enroll->domain_ou_explicit ? ADCLI_ERR_CONFIG : ADCLI_ERR_DIRECTORY;
} else if (ret != LDAP_SUCCESS) {
return _adcli_ldap_handle_failure (ldap, ADCLI_ERR_DIRECTORY,
"Couldn't lookup computer container: %s", base);
}
values = _adcli_ldap_parse_values (ldap, results, attrs[0]);
ldap_msgfree (results);
prefix_len = strlen (prefix);
for (i = 0; values && values[i]; i++) {
if (strncmp (values[i], prefix, prefix_len) == 0) {
enroll->computer_container = strdup (values[i] + prefix_len);
return_unexpected_if_fail (enroll->computer_container != NULL);
_adcli_info ("Found well known computer container at: %s",
enroll->computer_container);
break;
}
}
_adcli_strv_free (values);
/* Try harder */
if (!enroll->computer_container) {
ret = ldap_search_ext_s (ldap, base, LDAP_SCOPE_BASE, filter,
attrs, 0, NULL, NULL, NULL, -1, &results);
if (ret == LDAP_SUCCESS) {
enroll->computer_container = _adcli_ldap_parse_dn (ldap, results);
if (enroll->computer_container) {
_adcli_info ("Well known computer container not "
"found, but found suitable one at: %s",
enroll->computer_container);
}
}
ldap_msgfree (results);
}
if (!enroll->computer_container && enroll->domain_ou) {
_adcli_warn ("Couldn't find a computer container in the ou, "
"creating computer account directly in: %s", enroll->domain_ou);
enroll->computer_container = strdup (enroll->domain_ou);
return_unexpected_if_fail (enroll->computer_container != NULL);
}
if (!enroll->computer_container) {
_adcli_err ("Couldn't find location to create computer accounts");
return ADCLI_ERR_DIRECTORY;
}
return ADCLI_SUCCESS;
}
static adcli_result
calculate_computer_account (adcli_enroll *enroll,
LDAP *ldap)
{
adcli_result res;
assert (enroll->computer_dn == NULL);
/* Now need to find or validate the computer container */
res = lookup_computer_container (enroll, ldap);
if (res != ADCLI_SUCCESS)
return res;
assert (enroll->computer_container);
free (enroll->computer_dn);
enroll->computer_dn = NULL;
if (asprintf (&enroll->computer_dn, "CN=%s,%s", enroll->computer_name, enroll->computer_container) < 0)
return_unexpected_if_reached ();
_adcli_info ("Calculated computer account: %s", enroll->computer_dn);
return ADCLI_SUCCESS;
}
static adcli_result
calculate_enctypes (adcli_enroll *enroll, char **enctype)
{
char *value = NULL;
krb5_enctype *read_enctypes;
krb5_enctype *new_enctypes;
char *new_value = NULL;
int is_2008_or_later;
LDAP *ldap;
*enctype = NULL;
/*
* Because we're using a keytab we want the server to be aware of the
* encryption types supported on the client, because we can't dynamically
* use a new one that's thrown at us.
*
* If the encryption types are not explicitly set by the caller of this
* library, then see if the account already has some encryption types
* marked on it.
*
* If not, write our default set to the account.
*
* Note that Windows 2003 and earlier have a standard set of encryption
* types, and no msDS-supportedEncryptionTypes attribute.
*/
ldap = adcli_conn_get_ldap_connection (enroll->conn);
return_unexpected_if_fail (ldap != NULL);
is_2008_or_later = adcli_conn_server_has_capability (enroll->conn, ADCLI_CAP_V60_OID);
/* In 2008 or later, use the msDS-supportedEncryptionTypes attribute */
if (is_2008_or_later && enroll->computer_attributes != NULL) {
value = _adcli_ldap_parse_value (ldap, enroll->computer_attributes,
"msDS-supportedEncryptionTypes");
if (!enroll->keytab_enctypes_explicit && value != NULL) {
read_enctypes = _adcli_krb5_parse_enctypes (value);
if (read_enctypes == NULL) {
_adcli_warn ("Invalid or unsupported encryption types are set on "
"the computer account (%s).", value);
} else {
free (enroll->keytab_enctypes);
enroll->keytab_enctypes = read_enctypes;
}
}
/* In 2003 or earlier, standard set of enc types */
} else {
value = _adcli_krb5_format_enctypes (v51_earlier_enctypes);
}
new_enctypes = adcli_enroll_get_permitted_keytab_enctypes (enroll);
if (new_enctypes == NULL) {
_adcli_warn ("No permitted encryption type found.");
return ADCLI_ERR_UNEXPECTED;
}
new_value = _adcli_krb5_format_enctypes (new_enctypes);
krb5_free_enctypes (adcli_conn_get_krb5_context (enroll->conn), new_enctypes);
if (new_value == NULL) {
free (value);
_adcli_warn ("The encryption types desired are not available in active directory");
return ADCLI_ERR_CONFIG;
}
/* If we already have this value, then don't need to update */
if (value && strcmp (new_value, value) == 0) {
free (value);
free (new_value);
return ADCLI_SUCCESS;
}
free (value);
if (!is_2008_or_later) {
free (new_value);
_adcli_warn ("Server does not support setting encryption types");
return ADCLI_SUCCESS;
}
*enctype = new_value;
return ADCLI_SUCCESS;
}
static adcli_result
create_computer_account (adcli_enroll *enroll,
LDAP *ldap)
{
char *vals_objectClass[] = { enroll->is_service ? "msDS-ManagedServiceAccount" : "computer", NULL };
LDAPMod objectClass = { LDAP_MOD_ADD, "objectClass", { vals_objectClass, } };
char *vals_sAMAccountName[] = { enroll->computer_sam, NULL };
LDAPMod sAMAccountName = { LDAP_MOD_ADD, "sAMAccountName", { vals_sAMAccountName, } };
char *vals_userAccountControl[] = { "69632", NULL }; /* WORKSTATION_TRUST_ACCOUNT | DONT_EXPIRE_PASSWD */
LDAPMod userAccountControl = { LDAP_MOD_ADD, "userAccountControl", { vals_userAccountControl, } };
char *vals_supportedEncryptionTypes[] = { NULL, NULL };
LDAPMod encTypes = { LDAP_MOD_ADD, "msDS-supportedEncryptionTypes", { vals_supportedEncryptionTypes, } };
char *vals_dNSHostName[] = { enroll->host_fqdn, NULL };
LDAPMod dNSHostName = { LDAP_MOD_ADD, "dNSHostName", { vals_dNSHostName, } };
char *vals_operatingSystem[] = { enroll->os_name, NULL };
LDAPMod operatingSystem = { LDAP_MOD_ADD, "operatingSystem", { vals_operatingSystem, } };
char *vals_operatingSystemVersion[] = { enroll->os_version, NULL };
LDAPMod operatingSystemVersion = { LDAP_MOD_ADD, "operatingSystemVersion", { vals_operatingSystemVersion, } };
char *vals_operatingSystemServicePack[] = { enroll->os_service_pack, NULL };
LDAPMod operatingSystemServicePack = { LDAP_MOD_ADD, "operatingSystemServicePack", { vals_operatingSystemServicePack, } };
char *vals_userPrincipalName[] = { enroll->user_principal, NULL };
LDAPMod userPrincipalName = { LDAP_MOD_ADD, "userPrincipalName", { vals_userPrincipalName, }, };
LDAPMod servicePrincipalName = { LDAP_MOD_ADD, "servicePrincipalName", { enroll->service_principals, } };
char *vals_description[] = { enroll->description, NULL };
LDAPMod description = { LDAP_MOD_ADD, "description", { vals_description, }, };
char *val = NULL;
int ret;
size_t c;
size_t m;
LDAPMod *all_mods[] = {
&objectClass,
&sAMAccountName,
&userAccountControl,
&encTypes,
&dNSHostName,
&operatingSystem,
&operatingSystemVersion,
&operatingSystemServicePack,
&userPrincipalName,
&servicePrincipalName,
&description,
NULL
};
size_t mods_count = sizeof (all_mods) / sizeof (LDAPMod *);
LDAPMod *mods[mods_count];
if (adcli_enroll_get_trusted_for_delegation (enroll)) {
vals_userAccountControl[0] = "593920"; /* WORKSTATION_TRUST_ACCOUNT | DONT_EXPIRE_PASSWD | TRUSTED_FOR_DELEGATION */
}
ret = calculate_enctypes (enroll, &val);
if (ret != ADCLI_SUCCESS) {
return ret;
}
vals_supportedEncryptionTypes[0] = val;
m = 0;
for (c = 0; c < mods_count - 1; c++) {
/* Skip empty LDAP sttributes */
if (all_mods[c]->mod_vals.modv_strvals != NULL && all_mods[c]->mod_vals.modv_strvals[0] != NULL) {
mods[m++] = all_mods[c];
}
}
mods[m] = NULL;
ret = ldap_add_ext_s (ldap, enroll->computer_dn, mods, NULL, NULL);
free (val);
/*
* Hand to head. This is really dumb... AD returns
* OBJECT_CLASS_VIOLATION when the 'admin' account doesn't have
* enough permission to create this computer account.
*
* Additionally LDAP_UNWILLING_TO_PERFORM and LDAP_CONSTRAINT_VIOLATION
* are seen on various Windows Servers as responses to this case.
*
* TODO: Perhaps some missing attributes are auto-generated when
* the administrative credentials have sufficient permissions, and
* those missing attributes cause the object class violation. However
* I've tried to screw around with this, and can't find the missing
* attributes. They may be hidden, like unicodePwd.
*/
if (ret == LDAP_INSUFFICIENT_ACCESS || ret == LDAP_OBJECT_CLASS_VIOLATION ||
ret == LDAP_UNWILLING_TO_PERFORM || ret == LDAP_CONSTRAINT_VIOLATION) {
return _adcli_ldap_handle_failure (ldap, ADCLI_ERR_CREDENTIALS,
"Insufficient permissions to modify computer account: %s",
enroll->computer_dn);
} else if (ret != LDAP_SUCCESS) {
return _adcli_ldap_handle_failure (ldap, ADCLI_ERR_DIRECTORY,
"Couldn't create computer account: %s",
enroll->computer_dn);
}
_adcli_info ("Created computer account: %s", enroll->computer_dn);
return ADCLI_SUCCESS;
}
static int
filter_for_necessary_updates (adcli_enroll *enroll,
LDAP *ldap,
LDAPMessage *entry,
LDAPMod **mods)
{
struct berval **vals;
int match;
int out;
int in;
for (in = 0, out = 0; mods[in] != NULL; in++) {
match = 0;
/* Never update these attributes */
if (strcasecmp (mods[in]->mod_type, "objectClass") == 0)
continue;
/* If no entry, then no filtering */
if (entry != NULL) {
vals = ldap_get_values_len (ldap, entry, mods[in]->mod_type);
if (vals != NULL) {
match = _adcli_ldap_have_in_mod (mods[in], vals);
ldap_value_free_len (vals);
}
}
if (!match)
mods[out++] = mods[in];
}
mods[out] = NULL;
return out;
}
static adcli_result
validate_computer_account (adcli_enroll *enroll,
int allow_overwrite,
int already_exists)
{
assert (enroll->computer_dn != NULL);
if (already_exists && !allow_overwrite) {
_adcli_err ("The computer account %s already exists",
enroll->computer_name);
return ADCLI_ERR_CONFIG;
}
/* Do we have an explicitly requested ou? */
if (enroll->domain_ou && enroll->domain_ou_explicit && already_exists) {
if (!_adcli_ldap_dn_has_ancestor (enroll->computer_dn, enroll->domain_ou)) {
_adcli_err ("The computer account %s already exists, "
"but is not in the desired organizational unit.",
enroll->computer_name);
return ADCLI_ERR_CONFIG;
}
}
return ADCLI_SUCCESS;
}
static adcli_result
delete_computer_account (adcli_enroll *enroll,
LDAP *ldap)
{
int ret;
ret = ldap_delete_ext_s (ldap, enroll->computer_dn, NULL, NULL);
if (ret == LDAP_INSUFFICIENT_ACCESS) {
return _adcli_ldap_handle_failure (ldap, ADCLI_ERR_CREDENTIALS,
"Insufficient permissions to delete computer account: %s",
enroll->computer_dn);
} else if (ret != LDAP_SUCCESS) {
return _adcli_ldap_handle_failure (ldap, ADCLI_ERR_DIRECTORY,
"Couldn't delete computer account: %s",
enroll->computer_dn);
} else {
_adcli_info ("Deleted computer account at: %s", enroll->computer_dn);
}
return ADCLI_SUCCESS;
}
static adcli_result
locate_computer_account (adcli_enroll *enroll,
LDAP *ldap,
LDAPMessage **rresults,
LDAPMessage **rentry)
{
char *attrs[] = { "objectClass", NULL };
LDAPMessage *results = NULL;
LDAPMessage *entry = NULL;
const char *base;
char *value;
char *filter;
char *dn;
int ret = 0;
/* If we don't yet know our computer dn, then try and find it */
value = _adcli_ldap_escape_filter (enroll->computer_sam);
return_unexpected_if_fail (value != NULL);
if (asprintf (&filter, "(&(objectClass=%s)(sAMAccountName=%s))",
enroll->is_service ? "msDS-ManagedServiceAccount" : "computer",
value) < 0)
return_unexpected_if_reached ();
free (value);
base = adcli_conn_get_default_naming_context (enroll->conn);
ret = ldap_search_ext_s (ldap, base, LDAP_SCOPE_SUB, filter, attrs, 0,
NULL, NULL, NULL, 1, &results);
free (filter);
/* ldap_search_ext_s() can return results *and* an error. */
if (ret == LDAP_SUCCESS) {
entry = ldap_first_entry (ldap, results);
/* If we found a computer/service account, make note of dn */
if (entry) {
if (!enroll->is_service_explicit) {
check_if_service ( enroll, ldap, results);
}
dn = ldap_get_dn (ldap, entry);
free (enroll->computer_dn);
enroll->computer_dn = strdup (dn);
return_unexpected_if_fail (enroll->computer_dn != NULL);
_adcli_info ("Found computer account for %s at: %s",
enroll->computer_sam, dn);
ldap_memfree (dn);
} else {
ldap_msgfree (results);
results = NULL;
_adcli_info ("Computer account for %s does not exist",
enroll->computer_sam);
}
} else {
return _adcli_ldap_handle_failure (ldap, ADCLI_ERR_DIRECTORY,
"Couldn't lookup computer account: %s",
enroll->computer_sam);
}
if (rresults)
*rresults = results;
else
ldap_msgfree (results);
if (rentry) {
assert (rresults != NULL);
*rentry = entry;
}
return ADCLI_SUCCESS;
}
static adcli_result
load_computer_account (adcli_enroll *enroll,
LDAP *ldap,
LDAPMessage **rresults,
LDAPMessage **rentry)
{
char *attrs[] = { "objectClass", NULL };
LDAPMessage *results = NULL;
LDAPMessage *entry = NULL;
int ret;
ret = ldap_search_ext_s (ldap, enroll->computer_dn, LDAP_SCOPE_BASE,
"(objectClass=computer)", attrs, 0,
NULL, NULL, NULL, -1, &results);
if (ret == LDAP_SUCCESS) {
entry = ldap_first_entry (ldap, results);
if (entry) {
_adcli_info ("Found computer account for %s at: %s",
enroll->computer_sam, enroll->computer_dn);
}
} else if (ret == LDAP_NO_SUCH_OBJECT) {
results = entry = NULL;
} else {
return _adcli_ldap_handle_failure (ldap, ADCLI_ERR_DIRECTORY,
"Couldn't check computer account: %s",
enroll->computer_dn);
}
if (rresults)
*rresults = results;
else
ldap_msgfree (results);
if (rentry) {
assert (rresults != NULL);
*rentry = entry;
}
return ADCLI_SUCCESS;
}
static adcli_result
locate_or_create_computer_account (adcli_enroll *enroll,
int allow_overwrite)
{
LDAPMessage *results = NULL;
LDAPMessage *entry = NULL;
adcli_result res;
int searched = 0;
LDAP *ldap;
ldap = adcli_conn_get_ldap_connection (enroll->conn);
assert (ldap != NULL);
/* Try to find the computer account */
if (!enroll->computer_dn) {
res = locate_computer_account (enroll, ldap, &results, &entry);
if (res != ADCLI_SUCCESS)
return res;
searched = 1;
}
/* Next try and come up with where we think it should be */
if (enroll->computer_dn == NULL) {
res = calculate_computer_account (enroll, ldap);
if (res != ADCLI_SUCCESS)
return res;
}
assert (enroll->computer_dn != NULL);
/* Have we seen an account yet? */
if (!searched) {
res = load_computer_account (enroll, ldap, &results, &entry);
if (res != ADCLI_SUCCESS)
return res;
}
res = validate_computer_account (enroll, allow_overwrite, entry != NULL);
if (res == ADCLI_SUCCESS && entry == NULL)
res = create_computer_account (enroll, ldap);
/* Service account already exists, just continue and update the
* password */
if (enroll->is_service && entry != NULL) {
res = ADCLI_SUCCESS;
}
if (results)
ldap_msgfree (results);
return res;
}
static adcli_result
set_password_with_user_creds (adcli_enroll *enroll)
{
krb5_error_code code;
krb5_ccache ccache;
krb5_context k5;
krb5_data result_string = { 0, };
krb5_data result_code_string = { 0, };
adcli_result res;
int result_code;
char *message;
assert (enroll->computer_password != NULL);
assert (enroll->computer_principal != NULL);
k5 = adcli_conn_get_krb5_context (enroll->conn);
return_unexpected_if_fail (k5 != NULL);
ccache = adcli_conn_get_login_ccache (enroll->conn);
return_unexpected_if_fail (ccache != NULL);
memset (&result_string, 0, sizeof (result_string));
memset (&result_code_string, 0, sizeof (result_code_string));
code = krb5_set_password_using_ccache (k5, ccache, enroll->computer_password,
enroll->computer_principal, &result_code,
&result_code_string, &result_string);
if (code != 0) {
_adcli_err ("Couldn't set password for computer account: %s: %s",
enroll->computer_sam, krb5_get_error_message (k5, code));
/* TODO: Parse out these values */
res = ADCLI_ERR_DIRECTORY;
} else if (result_code != 0) {
#ifdef HAVE_KRB5_CHPW_MESSAGE
if (krb5_chpw_message (k5, &result_string, &message) != 0)
message = NULL;
#else
message = NULL;
if (result_string.length)
message = _adcli_str_dupn (result_string.data, result_string.length);
#endif
_adcli_err ("Cannot set computer password: %.*s%s%s",
(int)result_code_string.length, result_code_string.data,
message ? ": " : "", message ? message : "");
res = ADCLI_ERR_CREDENTIALS;
#ifdef HAVE_KRB5_CHPW_MESSAGE
krb5_free_string (k5, message);
#else
free (message);
#endif
} else {
_adcli_info ("Set computer password");
if (enroll->kvno > 0) {
enroll->kvno++;
_adcli_info ("kvno incremented to %d", enroll->kvno);
}
res = ADCLI_SUCCESS;
}
krb5_free_data_contents (k5, &result_string);
krb5_free_data_contents (k5, &result_code_string);
return res;
}
static adcli_result
set_password_with_computer_creds (adcli_enroll *enroll)
{
krb5_error_code code;
krb5_creds creds;
krb5_data result_string = { 0, };
krb5_data result_code_string = { 0, };
krb5_context k5;
int result_code;
adcli_result res;
char *message;
memset (&creds, 0, sizeof (creds));
k5 = adcli_conn_get_krb5_context (enroll->conn);
return_unexpected_if_fail (k5 != NULL);
code = _adcli_kinit_computer_creds (enroll->conn, "kadmin/changepw", NULL, &creds);
if (code != 0) {
_adcli_err ("Couldn't get change password ticket for computer account: %s: %s",
enroll->computer_sam, krb5_get_error_message (k5, code));
return ADCLI_ERR_DIRECTORY;
}
code = krb5_change_password (k5, &creds, enroll->computer_password,
&result_code, &result_code_string, &result_string);
krb5_free_cred_contents (k5, &creds);
if (code != 0) {
_adcli_err ("Couldn't change password for computer account: %s: %s",
enroll->computer_sam, krb5_get_error_message (k5, code));
/* TODO: Parse out these values */
res = ADCLI_ERR_DIRECTORY;
} else if (result_code != 0) {
#ifdef HAVE_KRB5_CHPW_MESSAGE
if (krb5_chpw_message (k5, &result_string, &message) != 0)
message = NULL;
#else
message = NULL;
if (result_string.length)
message = _adcli_str_dupn (result_string.data, result_string.length);
#endif
_adcli_err ("Cannot change computer password: %.*s%s%s",
(int)result_code_string.length, result_code_string.data,
message ? ": " : "", message ? message : "");
res = ADCLI_ERR_CREDENTIALS;
#ifdef HAVE_KRB5_CHPW_MESSAGE
krb5_free_string (k5, message);
#else
free (message);
#endif
} else {
_adcli_info ("Changed computer password");
if (enroll->kvno > 0) {
enroll->kvno++;
_adcli_info ("kvno incremented to %d", enroll->kvno);
}
res = ADCLI_SUCCESS;
}
krb5_free_data_contents (k5, &result_string);
krb5_free_data_contents (k5, &result_code_string);
return res;
}
static adcli_result
set_computer_password (adcli_enroll *enroll)
{
if (adcli_conn_get_login_type (enroll->conn) == ADCLI_LOGIN_COMPUTER_ACCOUNT)
return set_password_with_computer_creds (enroll);
else
return set_password_with_user_creds (enroll);
}
static adcli_result
retrieve_computer_account (adcli_enroll *enroll)
{
adcli_result res = ADCLI_SUCCESS;
unsigned long kvno;
char *value;
LDAP *ldap;
char *end;
int ret;
assert (enroll->computer_dn != NULL);
assert (enroll->computer_attributes == NULL);
ldap = adcli_conn_get_ldap_connection (enroll->conn);
assert (ldap != NULL);
ret = ldap_search_ext_s (ldap, enroll->computer_dn, LDAP_SCOPE_BASE,
"(objectClass=*)", default_ad_ldap_attrs,
0, NULL, NULL, NULL, -1,
&enroll->computer_attributes);
if (ret != LDAP_SUCCESS) {
return _adcli_ldap_handle_failure (ldap, ADCLI_ERR_DIRECTORY,
"Couldn't retrieve computer account info: %s",
enroll->computer_dn);
}
/* Update the kvno */
if (enroll->kvno == 0) {
value = _adcli_ldap_parse_value (ldap, enroll->computer_attributes, "msDS-KeyVersionNumber");
if (value != NULL) {
kvno = strtoul (value, &end, 10);
if (end == NULL || *end != '\0') {
_adcli_err ("Invalid kvno '%s' for computer account in directory: %s",
value, enroll->computer_dn);
res = ADCLI_ERR_DIRECTORY;
} else {
enroll->kvno = kvno;
_adcli_info ("Retrieved kvno '%s' for computer account in directory: %s",
value, enroll->computer_dn);
}
free (value);
} else {
/* Apparently old AD didn't have this attribute, use zero */
enroll->kvno = 0;
_adcli_info ("No kvno found for computer account in directory: %s",
enroll->computer_dn);
}
}
return res;
}
static adcli_result
update_and_calculate_enctypes (adcli_enroll *enroll)
{
char *vals_supportedEncryptionTypes[] = { NULL, NULL };
LDAPMod mod = { LDAP_MOD_REPLACE, "msDS-supportedEncryptionTypes", { vals_supportedEncryptionTypes, } };
LDAPMod *mods[2] = { &mod, NULL };
char *new_value;
LDAP *ldap;
int ret;
ldap = adcli_conn_get_ldap_connection (enroll->conn);
return_unexpected_if_fail (ldap != NULL);
ret = calculate_enctypes (enroll, &new_value);
if (ret != ADCLI_SUCCESS) {
free (new_value);
return ret;
}
if (new_value == NULL) {
return ADCLI_SUCCESS;
}
vals_supportedEncryptionTypes[0] = new_value;
if (filter_for_necessary_updates (enroll, ldap, enroll->computer_attributes, mods) == 0)
ret = 0;
else
ret = ldap_modify_ext_s (ldap, enroll->computer_dn, mods, NULL, NULL);
free (new_value);
if (ret == LDAP_INSUFFICIENT_ACCESS) {
return _adcli_ldap_handle_failure (ldap, ADCLI_ERR_CREDENTIALS,
"Insufficient permissions to set encryption types on computer account: %s",
enroll->computer_dn);
} else if (ret != LDAP_SUCCESS) {
return _adcli_ldap_handle_failure (ldap, ADCLI_ERR_DIRECTORY,
"Couldn't set encryption types on computer account: %s",
enroll->computer_dn);
}
return ADCLI_SUCCESS;
}
static adcli_result
update_computer_attribute (adcli_enroll *enroll,
LDAP *ldap,
LDAPMod **mods)
{
adcli_result res = ADCLI_SUCCESS;
char *string;
int ret;
/* See if there are any changes to be made? */
if (filter_for_necessary_updates (enroll, ldap, enroll->computer_attributes, mods) == 0)
return ADCLI_SUCCESS;
string = _adcli_ldap_mods_to_string (mods);
return_unexpected_if_fail (string != NULL);
_adcli_info ("Modifying computer account: %s", string);
ret = ldap_modify_ext_s (ldap, enroll->computer_dn, mods, NULL, NULL);
if (ret != LDAP_SUCCESS) {
_adcli_warn ("Couldn't set %s on computer account: %s: %s",
string, enroll->computer_dn, ldap_err2string (ret));
res = ADCLI_ERR_DIRECTORY;
}
free (string);
return res;
}
static char *get_user_account_control (adcli_enroll *enroll)
{
uint32_t uac = 0;
unsigned long attr_val;
char *uac_str;
LDAP *ldap;
char *end;
ldap = adcli_conn_get_ldap_connection (enroll->conn);
return_val_if_fail (ldap != NULL, NULL);
uac_str = _adcli_ldap_parse_value (ldap, enroll->computer_attributes, "userAccountControl");
if (uac_str != NULL) {
attr_val = strtoul (uac_str, &end, 10);
if (*end != '\0' || attr_val > UINT32_MAX) {
_adcli_warn ("Invalid userAccountControl '%s' for computer account in directory: %s, assuming 0",
uac_str, enroll->computer_dn);
} else {
uac = attr_val;
}
free (uac_str);
}
if (uac == 0) {
uac = UAC_WORKSTATION_TRUST_ACCOUNT | UAC_DONT_EXPIRE_PASSWORD;
}
if (adcli_enroll_get_trusted_for_delegation (enroll)) {
uac |= UAC_TRUSTED_FOR_DELEGATION;
} else {
uac &= ~(UAC_TRUSTED_FOR_DELEGATION);
}
if (asprintf (&uac_str, "%d", uac) < 0) {
return_val_if_reached (NULL);
}
return uac_str;
}
static void
update_computer_account (adcli_enroll *enroll)
{
int res = 0;
LDAP *ldap;
char *value = NULL;
/* No updates for service accounts */
if (enroll->is_service) {
return;
}
ldap = adcli_conn_get_ldap_connection (enroll->conn);
return_if_fail (ldap != NULL);
/* Only update attributes which are explicitly given on the command
* line or not set in the existing AD object. Otherwise 'adcli update'
* must be always called with the same set of options to make sure
* existing attributes are not deleted or overwritten with different
* values. */
if (enroll->computer_attributes != NULL) {
value = _adcli_ldap_parse_value (ldap,
enroll->computer_attributes,
"dNSHostName");
}
if (enroll->host_fqdn_explicit || value == NULL ) {
char *vals_dNSHostName[] = { enroll->host_fqdn, NULL };
LDAPMod dNSHostName = { LDAP_MOD_REPLACE, "dNSHostName", { vals_dNSHostName, } };
LDAPMod *mods[] = { &dNSHostName, NULL };
res |= update_computer_attribute (enroll, ldap, mods);
}
free (value);
if (res == ADCLI_SUCCESS && enroll->trusted_for_delegation_explicit) {
char *vals_userAccountControl[] = { NULL , NULL };
LDAPMod userAccountControl = { LDAP_MOD_REPLACE, "userAccountControl", { vals_userAccountControl, } };
LDAPMod *mods[] = { &userAccountControl, NULL };
vals_userAccountControl[0] = get_user_account_control (enroll);
if (vals_userAccountControl[0] != NULL) {
res |= update_computer_attribute (enroll, ldap, mods);
} else {
_adcli_warn ("Cannot update userAccountControl");
}
}
if (res == ADCLI_SUCCESS) {
char *vals_operatingSystem[] = { enroll->os_name, NULL };
LDAPMod operatingSystem = { LDAP_MOD_REPLACE, "operatingSystem", { vals_operatingSystem, } };
char *vals_operatingSystemVersion[] = { enroll->os_version, NULL };
LDAPMod operatingSystemVersion = { LDAP_MOD_REPLACE, "operatingSystemVersion", { vals_operatingSystemVersion, } };
char *vals_operatingSystemServicePack[] = { enroll->os_service_pack, NULL };
LDAPMod operatingSystemServicePack = { LDAP_MOD_REPLACE, "operatingSystemServicePack", { vals_operatingSystemServicePack, } };
LDAPMod *mods[] = { NULL, NULL, NULL, NULL };
size_t c = 0;
if (enroll->os_name_explicit) {
mods[c++] = &operatingSystem;
}
if (enroll->os_version_explicit) {
mods[c++] = &operatingSystemVersion;
}
if (enroll->os_service_pack_explicit) {
mods[c++] = &operatingSystemServicePack;
}
if (c != 0) {
res |= update_computer_attribute (enroll, ldap, mods);
}
}
if (res == ADCLI_SUCCESS && enroll->user_principal != NULL && !enroll->user_princpal_generate) {
char *vals_userPrincipalName[] = { enroll->user_principal, NULL };
LDAPMod userPrincipalName = { LDAP_MOD_REPLACE, "userPrincipalName", { vals_userPrincipalName, }, };
LDAPMod *mods[] = { &userPrincipalName, NULL, };
res |= update_computer_attribute (enroll, ldap, mods);
}
if (res == ADCLI_SUCCESS && enroll->description != NULL) {
char *vals_description[] = { enroll->description, NULL };
LDAPMod description = { LDAP_MOD_REPLACE, "description", { vals_description, }, };
LDAPMod *mods[] = { &description, NULL, };
res |= update_computer_attribute (enroll, ldap, mods);
}
if (res != 0)
_adcli_info ("Updated existing computer account: %s", enroll->computer_dn);
}
static adcli_result
update_service_principals (adcli_enroll *enroll)
{
LDAPMod servicePrincipalName = { LDAP_MOD_REPLACE, "servicePrincipalName", { enroll->service_principals, } };
LDAPMod *mods[] = { &servicePrincipalName, NULL, };
LDAP *ldap;
int ret;
/* No updates for service accounts */
if (enroll->is_service) {
return ADCLI_SUCCESS;
}
ldap = adcli_conn_get_ldap_connection (enroll->conn);
return_unexpected_if_fail (ldap != NULL);
/* See if there are any changes to be made? */
if (filter_for_necessary_updates (enroll, ldap, enroll->computer_attributes, mods) == 0)
return ADCLI_SUCCESS;
ret = ldap_modify_ext_s (ldap, enroll->computer_dn, mods, NULL, NULL);
if (ret == LDAP_INSUFFICIENT_ACCESS) {
return _adcli_ldap_handle_failure (ldap, ADCLI_ERR_CREDENTIALS,
"Insufficient permissions to set service principals on computer account: %s",
enroll->computer_dn);
} else if (ret != LDAP_SUCCESS) {
return _adcli_ldap_handle_failure (ldap, ADCLI_ERR_DIRECTORY,
"Couldn't set service principals on computer account %s",
enroll->computer_dn);
}
return ADCLI_SUCCESS;
}
static adcli_result
ensure_host_keytab (adcli_result res,
adcli_enroll *enroll)
{
krb5_context k5;
krb5_error_code code;
char *name;
if (res != ADCLI_SUCCESS)
return res;
if (enroll->keytab)
return ADCLI_SUCCESS;
k5 = adcli_conn_get_krb5_context (enroll->conn);
return_unexpected_if_fail (k5 != NULL);
res = _adcli_krb5_open_keytab (k5, enroll->keytab_name, &enroll->keytab);
if (res != ADCLI_SUCCESS)
return res;
if (!enroll->keytab_name) {
name = malloc (MAX_KEYTAB_NAME_LEN + 1);
return_unexpected_if_fail (name != NULL);
code = krb5_kt_get_name (k5, enroll->keytab, name, MAX_KEYTAB_NAME_LEN + 1);
return_unexpected_if_fail (code == 0);
enroll->keytab_name = name;
enroll->keytab_name_is_krb5 = 1;
}
_adcli_info ("Using keytab: %s", enroll->keytab_name);
return ADCLI_SUCCESS;
}
static krb5_boolean
load_keytab_entry (krb5_context k5,
krb5_keytab_entry *entry,
void *data)
{
adcli_enroll *enroll = data;
krb5_error_code code;
krb5_principal principal;
const char *realm;
size_t len;
char *value;
char *name;
/* Skip over any entry without a principal or realm */
principal = entry->principal;
if (!principal || !principal->realm.length)
return TRUE;
/* Use the first keytab entry as realm */
realm = adcli_conn_get_domain_realm (enroll->conn);
if (!realm) {
value = _adcli_str_dupn (principal->realm.data, principal->realm.length);
adcli_conn_set_domain_realm (enroll->conn, value);
_adcli_info ("Found realm in keytab: %s", value);
realm = adcli_conn_get_domain_realm (enroll->conn);
free (value);
}
/* Only look at entries that match the realm */
len = strlen (realm);
if (principal->realm.length != len && strncmp (realm, principal->realm.data, len) != 0)
return TRUE;
code = krb5_unparse_name_flags (k5, principal, KRB5_PRINCIPAL_UNPARSE_NO_REALM, &name);
return_val_if_fail (code == 0, FALSE);
len = strlen (name);
if (!enroll->service_principals_explicit) {
if (!_adcli_strv_has (enroll->service_principals, name) && strchr (name, '/')) {
value = strdup (name);
return_val_if_fail (value != NULL, FALSE);
_adcli_info ("Found service principal in keytab: %s", value);
enroll->service_principals = _adcli_strv_add_unique (enroll->service_principals, value, NULL, false);
}
}
if (!enroll->host_fqdn_explicit && !enroll->computer_name_explicit) {
/* Automatically use the netbios name */
if (!enroll->computer_name && len > 1 &&
_adcli_str_has_suffix (name, "$") && !strchr (name, '/')) {
enroll->computer_name = name;
name[len - 1] = '\0';
_adcli_info ("Found computer name in keytab: %s", name);
adcli_conn_set_computer_name (enroll->conn,
enroll->computer_name);
name = NULL;
} else if (!enroll->host_fqdn && _adcli_str_has_prefix (name, "host/") && strchr (name, '.')) {
/* Skip host/ prefix */
enroll->host_fqdn = strdup (name + 5);
return_val_if_fail (enroll->host_fqdn != NULL, FALSE);
_adcli_info ("Found host qualified name in keytab: %s", enroll->host_fqdn);
}
}
free (name);
return TRUE;
}
static adcli_result
load_host_keytab (adcli_enroll *enroll)
{
krb5_error_code code;
adcli_result res;
krb5_context k5;
krb5_keytab keytab;
res = _adcli_krb5_init_context (&k5);
if (res != ADCLI_SUCCESS)
return res;
res = _adcli_krb5_open_keytab (k5, enroll->keytab_name, &keytab);
if (res == ADCLI_SUCCESS) {
code = _adcli_krb5_keytab_enumerate (k5, keytab, load_keytab_entry, enroll);
if (code != 0) {
_adcli_err ("Couldn't enumerate keytab: %s: %s",
enroll->keytab_name, krb5_get_error_message (k5, code));
res = ADCLI_ERR_FAIL;
}
krb5_kt_close (k5, keytab);
}
krb5_free_context (k5);
return res;
}
typedef struct {
krb5_kvno kvno;
krb5_principal principal;
int matched;
} match_principal_kvno;
static krb5_boolean
match_principal_and_kvno (krb5_context k5,
krb5_keytab_entry *entry,
void *data)
{
match_principal_kvno *closure = data;
assert (closure->principal);
/*
* Don't match entries with kvno - 1 so that existing sessions
* will still work.
*/
if (entry->vno + 1 == closure->kvno)
return 0;
/* Is this the principal we're looking for? */
if (krb5_principal_compare (k5, entry->principal, closure->principal)) {
closure->matched = 1;
return 1;
}
return 0;
}
#define DEFAULT_SALT 1
static krb5_data *
build_principal_salts (adcli_enroll *enroll,
krb5_context k5,
krb5_principal principal)
{
krb5_error_code code;
krb5_data *salts;
const int count = 3;
int i = 0;
salts = calloc (count, sizeof (krb5_data));
return_val_if_fail (salts != NULL, NULL);
/* Build up the salts, first a standard kerberos salt */
code = krb5_principal2salt (k5, principal, &salts[i++]);
return_val_if_fail (code == 0, NULL);
/* Then a Windows 2003 computer account salt */
code = _adcli_krb5_w2k3_salt (k5, principal, enroll->computer_name, &salts[i++]);
return_val_if_fail (code == 0, NULL);
/* And lastly a null salt */
salts[i++].data = NULL;
assert (count == i);
return salts;
}
static void
free_principal_salts (krb5_context k5,
krb5_data *salts)
{
int i;
for (i = 0; salts[i].data != NULL; i++)
krb5_free_data_contents (k5, salts + i);
free (salts);
}
static adcli_result
remove_principal_from_keytab (adcli_enroll *enroll,
krb5_context k5,
const char *principal_name)
{
krb5_error_code code;
krb5_principal principal;
match_principal_kvno closure;
code = _adcli_krb5_build_principal (k5, principal_name,
adcli_conn_get_domain_realm (enroll->conn),
&principal);
if (code != 0) {
_adcli_err ("Couldn't parse principal: %s: %s",
principal_name, krb5_get_error_message (k5, code));
return ADCLI_ERR_FAIL;
}
closure.kvno = enroll->kvno;
closure.principal = principal;
closure.matched = 0;
code = _adcli_krb5_keytab_clear (k5, enroll->keytab,
match_principal_and_kvno, &closure);
krb5_free_principal (k5, principal);
if (code != 0) {
_adcli_err ("Couldn't update keytab: %s: %s",
enroll->keytab_name, krb5_get_error_message (k5, code));
return ADCLI_ERR_FAIL;
}
return ADCLI_SUCCESS;
}
static adcli_result
add_principal_to_keytab (adcli_enroll *enroll,
krb5_context k5,
krb5_principal principal,
const char *principal_name,
int *which_salt,
adcli_enroll_flags flags)
{
match_principal_kvno closure;
krb5_data password;
krb5_error_code code;
krb5_data *salts;
krb5_enctype *enctypes;
/* Remove old stuff from the keytab for this principal */
closure.kvno = enroll->kvno;
closure.principal = principal;
closure.matched = 0;
code = _adcli_krb5_keytab_clear (k5, enroll->keytab,
match_principal_and_kvno, &closure);
if (code != 0) {
_adcli_err ("Couldn't update keytab: %s: %s",
enroll->keytab_name, krb5_get_error_message (k5, code));
return ADCLI_ERR_FAIL;
}
if (closure.matched) {
_adcli_info ("Cleared old entries from keytab: %s",
enroll->keytab_name);
}
enctypes = adcli_enroll_get_permitted_keytab_enctypes (enroll);
if (enctypes == NULL) {
_adcli_warn ("No permitted encryption type found.");
return ADCLI_ERR_UNEXPECTED;
}
if (flags & ADCLI_ENROLL_PASSWORD_VALID) {
code = _adcli_krb5_keytab_copy_entries (k5, enroll->keytab, principal,
enroll->kvno, enctypes);
} else {
password.data = enroll->computer_password;
password.length = strlen (enroll->computer_password);
/*
* So we need to discover which salt to use. As a side effect we are
* also testing that our account works.
*/
salts = build_principal_salts (enroll, k5, principal);
if (salts == NULL) {
krb5_free_enctypes (k5, enctypes);
return ADCLI_ERR_UNEXPECTED;
}
if (*which_salt < 0) {
code = _adcli_krb5_keytab_discover_salt (k5, principal, enroll->kvno, &password,
enctypes, salts, which_salt);
if (code != 0) {
_adcli_warn ("Couldn't authenticate with keytab while discovering which salt to use: %s: %s",
principal_name, krb5_get_error_message (k5, code));
*which_salt = DEFAULT_SALT;
} else {
assert (*which_salt >= 0);
_adcli_info ("Discovered which keytab salt to use");
}
}
code = _adcli_krb5_keytab_add_entries (k5, enroll->keytab, principal,
enroll->kvno, &password, enctypes, &salts[*which_salt]);
free_principal_salts (k5, salts);
}
krb5_free_enctypes (k5, enctypes);
if (code != 0) {
_adcli_err ("Couldn't add keytab entries: %s: %s",
enroll->keytab_name, krb5_get_error_message (k5, code));
return ADCLI_ERR_FAIL;
}
_adcli_info ("Added the entries to the keytab: %s: %s",
principal_name, enroll->keytab_name);
return ADCLI_SUCCESS;
}
static adcli_result
update_keytab_for_principals (adcli_enroll *enroll,
adcli_enroll_flags flags)
{
krb5_context k5;
adcli_result res;
int which_salt = -1;
char *name;
int i;
assert (enroll->keytab_principals != NULL);
k5 = adcli_conn_get_krb5_context (enroll->conn);
return_unexpected_if_fail (k5 != NULL);
for (i = 0; enroll->keytab_principals[i] != 0; i++) {
if (krb5_unparse_name (k5, enroll->keytab_principals[i], &name) != 0)
name = "";
res = add_principal_to_keytab (enroll, k5, enroll->keytab_principals[i],
name, &which_salt, flags);
krb5_free_unparsed_name (k5, name);
if (res != ADCLI_SUCCESS)
return res;
}
if (enroll->service_principals_to_remove != NULL) {
for (i = 0; enroll->service_principals_to_remove[i] != NULL; i++) {
res = remove_principal_from_keytab (enroll, k5,
enroll->service_principals_to_remove[i]);
if (res != ADCLI_SUCCESS) {
_adcli_warn ("Failed to remove %s from keytab.",
enroll->service_principals_to_remove[i]);
}
}
}
return ADCLI_SUCCESS;
}
static adcli_result
update_samba_data (adcli_enroll *enroll)
{
int ret;
char *argv_pw[] = { NULL, "changesecretpw", "-i", "-f", NULL };
char *argv_sid[] = { NULL, "setdomainsid", NULL, NULL };
argv_pw[0] = (char *) adcli_enroll_get_samba_data_tool (enroll);
if (argv_pw[0] ==NULL) {
_adcli_err ("Samba data tool not available.");
return ADCLI_ERR_FAIL;
}
argv_sid[0] = argv_pw[0];
_adcli_info ("Trying to set Samba secret.");
ret = _adcli_call_external_program (argv_pw[0], argv_pw,
enroll->computer_password, NULL, NULL);
if (ret != ADCLI_SUCCESS) {
_adcli_err ("Failed to set Samba computer account password.");
}
argv_sid[2] = (char *) adcli_conn_get_domain_sid (enroll->conn);
if (argv_sid[2] == NULL) {
_adcli_err ("Domain SID not available.");
} else {
_adcli_info ("Trying to set domain SID %s for Samba.",
argv_sid[2]);
ret = _adcli_call_external_program (argv_sid[0], argv_sid,
NULL, NULL, NULL);
if (ret != ADCLI_SUCCESS) {
_adcli_err ("Failed to set Samba domain SID.");
}
}
return ret;
}
static void
enroll_clear_state (adcli_enroll *enroll)
{
krb5_context k5;
enroll_clear_keytab_principals (enroll);
if (enroll->keytab) {
k5 = adcli_conn_get_krb5_context (enroll->conn);
return_if_fail (k5 != NULL);
krb5_kt_close (k5, enroll->keytab);
enroll->keytab = NULL;
}
free (enroll->computer_sam);
enroll->computer_sam = NULL;
if (enroll->computer_principal) {
k5 = adcli_conn_get_krb5_context (enroll->conn);
return_if_fail (k5 != NULL);
krb5_free_principal (k5, enroll->computer_principal);
enroll->computer_principal = NULL;
}
if (!enroll->computer_password_explicit) {
free (enroll->computer_password);
enroll->computer_password = NULL;
}
free (enroll->computer_dn);
enroll->computer_dn = NULL;
free (enroll->computer_container);
enroll->computer_container = NULL;
if (!enroll->service_principals_explicit) {
_adcli_strv_free (enroll->service_principals);
enroll->service_principals = NULL;
}
if (enroll->user_princpal_generate) {
free (enroll->user_principal);
enroll->user_principal = NULL;
}
enroll->kvno = 0;
if (enroll->computer_attributes) {
ldap_msgfree (enroll->computer_attributes);
enroll->computer_attributes = NULL;
}
if (!enroll->domain_ou_explicit) {
free (enroll->domain_ou);
enroll->domain_ou = NULL;
}
}
adcli_result
adcli_enroll_prepare (adcli_enroll *enroll,
adcli_enroll_flags flags)
{
adcli_result res = ADCLI_SUCCESS;
return_unexpected_if_fail (enroll != NULL);
adcli_clear_last_error ();
if (enroll->is_service) {
/* Ensure basic params for service accounts */
res = ensure_computer_sam (res, enroll);
res = ensure_computer_password (res, enroll);
res = ensure_host_keytab (res, enroll);
res = ensure_keytab_principals (res, enroll);
} else {
/* Basic discovery and figuring out enroll params */
res = ensure_host_fqdn (res, enroll);
res = ensure_computer_name (res, enroll);
res = ensure_computer_sam (res, enroll);
res = ensure_user_principal (res, enroll);
res = ensure_computer_password (res, enroll);
if (!(flags & ADCLI_ENROLL_NO_KEYTAB))
res = ensure_host_keytab (res, enroll);
res = ensure_service_names (res, enroll);
res = ensure_service_principals (res, enroll);
res = ensure_keytab_principals (res, enroll);
}
return res;
}
static adcli_result
add_server_side_service_principals (adcli_enroll *enroll)
{
char **spn_list;
LDAP *ldap;
size_t c;
int length = 0;
adcli_result res;
ldap = adcli_conn_get_ldap_connection (enroll->conn);
assert (ldap != NULL);
spn_list = _adcli_ldap_parse_values (ldap, enroll->computer_attributes,
"servicePrincipalName");
if (spn_list == NULL) {
return ADCLI_SUCCESS;
}
if (enroll->service_principals != NULL) {
length = seq_count (enroll->service_principals);
}
for (c = 0; spn_list[c] != NULL; c++) {
_adcli_info ("Checking %s", spn_list[c]);
if (!_adcli_strv_has_ex (enroll->service_principals_to_remove, spn_list[c], strcasecmp)) {
enroll->service_principals = _adcli_strv_add_unique (enroll->service_principals,
strdup (spn_list[c]),
&length, false);
assert (enroll->service_principals != NULL);
_adcli_info (" Added %s", spn_list[c]);
}
}
_adcli_strv_free (spn_list);
res = ensure_keytab_principals (ADCLI_SUCCESS, enroll);
if (res != ADCLI_SUCCESS) {
return res;
}
return ADCLI_SUCCESS;
}
static adcli_result
enroll_join_or_update_tasks (adcli_enroll *enroll,
adcli_enroll_flags flags)
{
adcli_result res;
krb5_kvno old_kvno = -1;
if (!(flags & ADCLI_ENROLL_PASSWORD_VALID)) {
/* Handle kvno changes for read-only domain controllers
* (RODC). Since the actual password change does not happen on
* the RODC the kvno change has to be replicated back which
* might take some time. So we check the kvno before and after
* the change if we are connected to a RODC and increment the
* kvno if needed. */
if (!adcli_conn_is_writeable (enroll->conn)) {
if (enroll->computer_attributes == NULL) {
res = retrieve_computer_account (enroll);
if (res != ADCLI_SUCCESS)
return res;
}
old_kvno = adcli_enroll_get_kvno (enroll);
_adcli_info ("Found old kvno '%d'", old_kvno);
ldap_msgfree (enroll->computer_attributes);
enroll->computer_attributes = NULL;
adcli_enroll_set_kvno (enroll, 0);
}
res = set_computer_password (enroll);
if (res != ADCLI_SUCCESS)
return res;
}
/* kvno is not needed if no keytab */
if (flags & ADCLI_ENROLL_NO_KEYTAB)
enroll->kvno = -1;
/* Get information about the computer account if needed */
if (enroll->computer_attributes == NULL) {
res = retrieve_computer_account (enroll);
if (res != ADCLI_SUCCESS)
return res;
}
/* Handle kvno changes for read-only domain controllers (RODC) */
if (!adcli_conn_is_writeable (enroll->conn) && old_kvno != -1 &&
adcli_enroll_get_kvno (enroll) != 0 &&
adcli_enroll_get_kvno (enroll) == old_kvno) {
enroll->kvno++;
_adcli_info ("No kvno change detected on read-only DC, kvno "
"will be incremented by 1 to '%d'", enroll->kvno);
}
/* We ignore failures of setting these fields */
update_and_calculate_enctypes (enroll);
update_computer_account (enroll);
res = add_server_side_service_principals (enroll);
if (res != ADCLI_SUCCESS) {
return res;
}
/* service_names is only set from input on the command line, so no
* additional check for explicit is needed here */
if (enroll->service_names != NULL) {
res = add_service_names_to_service_principals (enroll);
if (res != ADCLI_SUCCESS) {
return res;
}
res = ensure_keytab_principals (res, enroll);
if (res != ADCLI_SUCCESS) {
return res;
}
}
update_service_principals (enroll);
if ( (flags & ADCLI_ENROLL_ADD_SAMBA_DATA) && ! (flags & ADCLI_ENROLL_PASSWORD_VALID)) {
res = update_samba_data (enroll);
if (res != ADCLI_SUCCESS) {
_adcli_info ("Failed to add Samba specific data, smbd "
"or winbindd might not work as "
"expected.\n");
}
}
if (flags & ADCLI_ENROLL_NO_KEYTAB)
return ADCLI_SUCCESS;
/*
* Salting in the keytab is wild, we need to autodetect the format
* that we use for salting.
*/
return update_keytab_for_principals (enroll, flags);
}
static adcli_result
adcli_enroll_add_description_for_service_account (adcli_enroll *enroll)
{
const char *fqdn;
char *desc;
fqdn = adcli_conn_get_host_fqdn (enroll->conn);
return_unexpected_if_fail (fqdn != NULL);
if (asprintf (&desc, "Please do not edit, Service account for %s, "
"managed by adcli.", fqdn) < 0) {
return_unexpected_if_reached ();
}
adcli_enroll_set_description (enroll, desc);
free (desc);
return ADCLI_SUCCESS;
}
static adcli_result
adcli_enroll_add_keytab_for_service_account (adcli_enroll *enroll)
{
krb5_context k5;
krb5_error_code code;
char def_keytab_name[MAX_KEYTAB_NAME_LEN];
char *lc_dom_name;
int ret;
if (adcli_enroll_get_keytab_name (enroll) == NULL) {
k5 = adcli_conn_get_krb5_context (enroll->conn);
return_unexpected_if_fail (k5 != NULL);
code = krb5_kt_default_name (k5, def_keytab_name,
sizeof (def_keytab_name));
return_unexpected_if_fail (code == 0);
lc_dom_name = strdup (adcli_conn_get_domain_name (enroll->conn));
return_unexpected_if_fail (lc_dom_name != NULL);
_adcli_str_down (lc_dom_name);
ret = asprintf (&enroll->keytab_name, "%s.%s", def_keytab_name,
lc_dom_name);
free (lc_dom_name);
return_unexpected_if_fail (ret > 0);
}
_adcli_info ("Using service account keytab: %s", enroll->keytab_name);
return ADCLI_SUCCESS;
}
adcli_result
adcli_enroll_join (adcli_enroll *enroll,
adcli_enroll_flags flags)
{
adcli_result res = ADCLI_SUCCESS;
return_unexpected_if_fail (enroll != NULL);
adcli_clear_last_error ();
enroll_clear_state (enroll);
res = adcli_conn_discover (enroll->conn);
if (res != ADCLI_SUCCESS)
return res;
if (enroll->is_service) {
res = adcli_enroll_add_description_for_service_account (enroll);
if (res == ADCLI_SUCCESS) {
res = adcli_enroll_add_keytab_for_service_account (enroll);
}
} else {
res = ensure_default_service_names (enroll);
}
if (res != ADCLI_SUCCESS)
return res;
res = adcli_enroll_prepare (enroll, flags);
if (res != ADCLI_SUCCESS)
return res;
/* This is where it really happens */
res = locate_or_create_computer_account (enroll, flags & ADCLI_ENROLL_ALLOW_OVERWRITE);
if (res != ADCLI_SUCCESS)
return res;
return enroll_join_or_update_tasks (enroll, flags);
}
adcli_result
adcli_enroll_load (adcli_enroll *enroll)
{
adcli_result res;
adcli_clear_last_error ();
/* Load default info from keytab */
res = load_host_keytab (enroll);
if (res != ADCLI_SUCCESS)
return res;
if (enroll->computer_name)
enroll->computer_name_explicit = 1;
if (enroll->host_fqdn)
enroll->host_fqdn_explicit = 1;
if (enroll->service_principals)
enroll->service_principals_explicit = 1;
return ADCLI_SUCCESS;
}
adcli_result
adcli_enroll_read_computer_account (adcli_enroll *enroll,
adcli_enroll_flags flags)
{
adcli_result res = ADCLI_SUCCESS;
LDAP *ldap;
return_unexpected_if_fail (enroll != NULL);
adcli_clear_last_error ();
enroll_clear_state (enroll);
res = adcli_conn_discover (enroll->conn);
if (res != ADCLI_SUCCESS)
return res;
res = adcli_enroll_prepare (enroll, flags);
if (res != ADCLI_SUCCESS)
return res;
ldap = adcli_conn_get_ldap_connection (enroll->conn);
assert (ldap != NULL);
/* Find the computer dn */
if (!enroll->computer_dn) {
res = locate_computer_account (enroll, ldap, NULL, NULL);
if (res != ADCLI_SUCCESS)
return res;
if (!enroll->computer_dn) {
_adcli_err ("No computer account for %s exists", enroll->computer_sam);
return ADCLI_ERR_CONFIG;
}
}
/* Get information about the computer account */
return retrieve_computer_account (enroll);
}
adcli_result
adcli_enroll_update (adcli_enroll *enroll,
adcli_enroll_flags flags)
{
adcli_result res = ADCLI_SUCCESS;
LDAP *ldap;
char *value;
res = adcli_enroll_read_computer_account (enroll, flags);
if (res != ADCLI_SUCCESS)
return res;
ldap = adcli_conn_get_ldap_connection (enroll->conn);
assert (ldap != NULL);
value = _adcli_ldap_parse_value (ldap,
enroll->computer_attributes,
"pwdLastSet");
if (_adcli_check_nt_time_string_lifetime (value,
adcli_enroll_get_computer_password_lifetime (enroll))) {
/* Do not update keytab if neither new service principals have
* to be added or deleted nor the user principal has to be changed. */
if (enroll->service_names == NULL
&& (enroll->user_principal == NULL || enroll->user_princpal_generate)
&& enroll->service_principals_to_add == NULL
&& enroll->service_principals_to_remove == NULL) {
flags |= ADCLI_ENROLL_NO_KEYTAB;
}
flags |= ADCLI_ENROLL_PASSWORD_VALID;
}
free (value);
/* We only support password changes for service accounts */
if (enroll->is_service && (flags & ADCLI_ENROLL_PASSWORD_VALID)) {
return ADCLI_SUCCESS;
}
return enroll_join_or_update_tasks (enroll, flags);
}
adcli_result
adcli_enroll_show_computer_attribute (adcli_enroll *enroll)
{
LDAP *ldap;
size_t c;
char **vals;
size_t v;
ldap = adcli_conn_get_ldap_connection (enroll->conn);
assert (ldap != NULL);
for (c = 0; default_ad_ldap_attrs[c] != NULL; c++) {
vals = _adcli_ldap_parse_values (ldap,
enroll->computer_attributes,
default_ad_ldap_attrs[c]);
printf ("%s:\n", default_ad_ldap_attrs[c]);
if (vals == NULL) {
printf (" - not set -\n");
} else {
for (v = 0; vals[v] != NULL; v++) {
printf (" %s\n", vals[v]);
}
}
_adcli_strv_free (vals);
}
return ADCLI_SUCCESS;
}
adcli_result
adcli_enroll_delete (adcli_enroll *enroll,
adcli_enroll_flags delete_flags)
{
adcli_result res = ADCLI_SUCCESS;
LDAP *ldap;
return_unexpected_if_fail (enroll != NULL);
adcli_clear_last_error ();
enroll_clear_state (enroll);
res = adcli_conn_discover (enroll->conn);
if (res != ADCLI_SUCCESS)
return res;
/* Basic discovery and figuring out enroll params */
res = ensure_host_fqdn (res, enroll);
res = ensure_computer_name (res, enroll);
res = ensure_computer_sam (res, enroll);
if (res != ADCLI_SUCCESS)
return res;
ldap = adcli_conn_get_ldap_connection (enroll->conn);
assert (ldap != NULL);
/* Find the computer dn */
if (!enroll->computer_dn) {
res = locate_computer_account (enroll, ldap, NULL, NULL);
if (res != ADCLI_SUCCESS)
return res;
if (!enroll->computer_dn) {
_adcli_err ("No computer account for %s exists",
enroll->computer_sam);
return ADCLI_ERR_CONFIG;
}
}
return delete_computer_account (enroll, ldap);
}
adcli_result
adcli_enroll_password (adcli_enroll *enroll,
adcli_enroll_flags password_flags)
{
adcli_result res = ADCLI_SUCCESS;
LDAP *ldap;
return_unexpected_if_fail (enroll != NULL);
adcli_clear_last_error ();
enroll_clear_state (enroll);
res = adcli_conn_discover (enroll->conn);
if (res != ADCLI_SUCCESS)
return res;
/* Basic discovery and figuring out enroll params */
res = ensure_host_fqdn (res, enroll);
res = ensure_computer_name (res, enroll);
res = ensure_computer_sam (res, enroll);
res = ensure_computer_password (res, enroll);
if (res != ADCLI_SUCCESS)
return res;
ldap = adcli_conn_get_ldap_connection (enroll->conn);
assert (ldap != NULL);
/* Find the computer dn */
if (!enroll->computer_dn) {
res = locate_computer_account (enroll, ldap, NULL, NULL);
if (res != ADCLI_SUCCESS)
return res;
if (!enroll->computer_dn) {
_adcli_err ("No computer account for %s exists",
enroll->computer_sam);
return ADCLI_ERR_CONFIG;
}
}
return set_computer_password (enroll);
}
adcli_enroll *
adcli_enroll_new (adcli_conn *conn)
{
adcli_enroll *enroll;
const char *value;
return_val_if_fail (conn != NULL, NULL);
enroll = calloc (1, sizeof (adcli_enroll));
return_val_if_fail (enroll != NULL, NULL);
enroll->conn = adcli_conn_ref (conn);
enroll->refs = 1;
/* Use the latter sections of host triple as OS name */
value = strchr (HOST_TRIPLET, '-');
if (value == NULL)
value = HOST_TRIPLET;
else
value++;
enroll->os_name = strdup (value);
return_val_if_fail (enroll->os_name != NULL, NULL);
enroll->samba_data_tool = strdup (SAMBA_DATA_TOOL);
return_val_if_fail (enroll->samba_data_tool != NULL, NULL);
return enroll;
}
adcli_enroll *
adcli_enroll_ref (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, NULL);
enroll->refs++;
return enroll;
}
static void
enroll_free (adcli_enroll *enroll)
{
if (enroll == NULL)
return;
enroll_clear_state (enroll);
free (enroll->computer_sam);
free (enroll->domain_ou);
free (enroll->computer_dn);
free (enroll->keytab_enctypes);
free (enroll->os_name);
free (enroll->os_version);
free (enroll->os_service_pack);
free (enroll->samba_data_tool);
free (enroll->user_principal);
_adcli_strv_free (enroll->service_names);
_adcli_strv_free (enroll->service_principals);
_adcli_password_free (enroll->computer_password);
adcli_enroll_set_keytab_name (enroll, NULL);
adcli_conn_unref (enroll->conn);
free (enroll);
}
void
adcli_enroll_unref (adcli_enroll *enroll)
{
if (enroll == NULL)
return;
if (--(enroll->refs) > 0)
return;
enroll_free (enroll);
}
const char *
adcli_enroll_get_host_fqdn (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, NULL);
return enroll->host_fqdn;
}
void
adcli_enroll_set_host_fqdn (adcli_enroll *enroll,
const char *value)
{
return_if_fail (enroll != NULL);
_adcli_str_set (&enroll->host_fqdn, value);
enroll->host_fqdn_explicit = 1;
}
const char *
adcli_enroll_get_computer_name (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, NULL);
return enroll->computer_name;
}
void
adcli_enroll_set_computer_name (adcli_enroll *enroll,
const char *value)
{
return_if_fail (enroll != NULL);
_adcli_str_set (&enroll->computer_name, value);
enroll->computer_name_explicit = (value != NULL);
}
const char *
adcli_enroll_get_domain_ou (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, NULL);
return enroll->domain_ou;
}
void
adcli_enroll_set_domain_ou (adcli_enroll *enroll,
const char *value)
{
return_if_fail (enroll != NULL);
enroll->domain_ou_validated = 0;
_adcli_str_set (&enroll->domain_ou, value);
enroll->domain_ou_explicit = (value != NULL);
}
const char *
adcli_enroll_get_computer_dn (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, NULL);
return enroll->computer_dn;
}
void
adcli_enroll_set_computer_dn (adcli_enroll *enroll,
const char *value)
{
return_if_fail (enroll != NULL);
_adcli_str_set (&enroll->computer_dn, value);
}
const char *
adcli_enroll_get_computer_password (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, NULL);
return enroll->computer_password;
}
void
adcli_enroll_set_computer_password (adcli_enroll *enroll,
const char *password)
{
char *newval = NULL;
return_if_fail (enroll != NULL);
if (password) {
newval = strdup (password);
return_if_fail (newval != NULL);
}
if (enroll->computer_password)
_adcli_password_free (enroll->computer_password);
enroll->computer_password = newval;
enroll->computer_password_explicit = (newval != NULL);
}
void
adcli_enroll_reset_computer_password (adcli_enroll *enroll)
{
return_if_fail (enroll != NULL);
_adcli_password_free (enroll->computer_password);
enroll->computer_password = NULL;
enroll->computer_password_explicit = 0;
enroll->reset_password = 1;
}
const char **
adcli_enroll_get_service_names (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, NULL);
if (ensure_service_names (ADCLI_SUCCESS, enroll) != ADCLI_SUCCESS)
return_val_if_reached (NULL);
return (const char **)enroll->service_names;
}
void
adcli_enroll_set_service_names (adcli_enroll *enroll,
const char **value)
{
return_if_fail (enroll != NULL);
_adcli_strv_set (&enroll->service_names, value);
}
void
adcli_enroll_add_service_name (adcli_enroll *enroll,
const char *value)
{
return_if_fail (enroll != NULL);
return_if_fail (value != NULL);
enroll->service_names = _adcli_strv_add (enroll->service_names, strdup (value), NULL);
return_if_fail (enroll->service_names != NULL);
}
const char **
adcli_enroll_get_service_principals (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, NULL);
return (const char **)enroll->service_principals;
}
void
adcli_enroll_set_service_principals (adcli_enroll *enroll,
const char **value)
{
return_if_fail (enroll != NULL);
_adcli_strv_set (&enroll->service_principals, value);
enroll->service_principals_explicit = (value != NULL);
}
krb5_kvno
adcli_enroll_get_kvno (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, 0);
return enroll->kvno;
}
void
adcli_enroll_set_kvno (adcli_enroll *enroll,
krb5_kvno value)
{
return_if_fail (enroll != NULL);
enroll->kvno = value;
}
krb5_keytab
adcli_enroll_get_keytab (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, NULL);
return enroll->keytab;
}
const char *
adcli_enroll_get_keytab_name (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, NULL);
return enroll->keytab_name;
}
void
adcli_enroll_set_keytab_name (adcli_enroll *enroll,
const char *value)
{
char *newval = NULL;
krb5_context k5;
return_if_fail (enroll != NULL);
if (enroll->keytab_name) {
if (enroll->keytab_name_is_krb5) {
k5 = adcli_conn_get_krb5_context (enroll->conn);
return_if_fail (k5 != NULL);
krb5_free_string (k5, enroll->keytab_name);
} else {
free (enroll->keytab_name);
}
}
if (enroll->keytab) {
k5 = adcli_conn_get_krb5_context (enroll->conn);
return_if_fail (k5 != NULL);
krb5_kt_close (k5, enroll->keytab);
enroll->keytab = NULL;
}
if (value) {
newval = strdup (value);
return_if_fail (newval != NULL);
}
enroll->keytab_name = newval;
enroll->keytab_name_is_krb5 = 0;
}
#define PROC_SYS_FIPS "/proc/sys/crypto/fips_enabled"
static bool adcli_fips_enabled (void)
{
int fd;
ssize_t len;
char buf[8];
fd = open (PROC_SYS_FIPS, O_RDONLY);
if (fd != -1) {
len = read (fd, buf, sizeof (buf));
close (fd);
/* Assume FIPS in enabled if PROC_SYS_FIPS contains a
* non-0 value. */
if ( ! (len == 2 && buf[0] == '0' && buf[1] == '\n')) {
return true;
}
}
return false;
}
krb5_enctype *
adcli_enroll_get_keytab_enctypes (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, NULL);
if (enroll->keytab_enctypes)
return enroll->keytab_enctypes;
if (adcli_conn_server_has_capability (enroll->conn, ADCLI_CAP_V60_OID))
if (adcli_fips_enabled ()) {
return v60_later_enctypes_fips;
} else {
return v60_later_enctypes;
}
else
return v51_earlier_enctypes;
}
krb5_enctype *
adcli_enroll_get_permitted_keytab_enctypes (adcli_enroll *enroll)
{
krb5_enctype *cur_enctypes;
krb5_enctype *permitted_enctypes;
krb5_enctype *new_enctypes;
krb5_error_code code;
krb5_context k5;
size_t c;
size_t p;
size_t n;
return_val_if_fail (enroll != NULL, NULL);
cur_enctypes = adcli_enroll_get_keytab_enctypes (enroll);
k5 = adcli_conn_get_krb5_context (enroll->conn);
return_val_if_fail (k5 != NULL, NULL);
code = krb5_get_permitted_enctypes (k5, &permitted_enctypes);
return_val_if_fail (code == 0, NULL);
for (c = 0; cur_enctypes[c] != 0; c++);
new_enctypes = calloc (c + 1, sizeof (krb5_enctype));
if (new_enctypes == NULL) {
krb5_free_enctypes (k5, permitted_enctypes);
return NULL;
}
n = 0;
for (c = 0; cur_enctypes[c] != 0; c++) {
for (p = 0; permitted_enctypes[p] != 0; p++) {
if (cur_enctypes[c] == permitted_enctypes[p]) {
new_enctypes[n++] = cur_enctypes[c];
break;
}
}
if (permitted_enctypes[p] == 0) {
_adcli_info ("Encryption type [%d] not permitted.", cur_enctypes[c]);
}
}
krb5_free_enctypes (k5, permitted_enctypes);
return new_enctypes;
}
void
adcli_enroll_set_keytab_enctypes (adcli_enroll *enroll,
krb5_enctype *value)
{
krb5_enctype *newval = NULL;
int len;
if (value) {
for (len = 0; value[len] != 0; len++);
newval = malloc (sizeof (krb5_enctype) * (len + 1));
return_if_fail (newval != NULL);
memcpy (newval, value, sizeof (krb5_enctype) * (len + 1));
}
free (enroll->keytab_enctypes);
enroll->keytab_enctypes = newval;
enroll->keytab_enctypes_explicit = (newval != NULL);
}
const char *
adcli_enroll_get_os_name (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, NULL);
return enroll->os_name;
}
void
adcli_enroll_set_os_name (adcli_enroll *enroll,
const char *value)
{
return_if_fail (enroll != NULL);
if (value && value[0] == '\0')
value = NULL;
_adcli_str_set (&enroll->os_name, value);
enroll->os_name_explicit = 1;
}
const char *
adcli_enroll_get_os_version (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, NULL);
return enroll->os_version;
}
void
adcli_enroll_set_os_version (adcli_enroll *enroll,
const char *value)
{
return_if_fail (enroll != NULL);
if (value && value[0] == '\0')
value = NULL;
_adcli_str_set (&enroll->os_version, value);
enroll->os_version_explicit = 1;
}
const char *
adcli_enroll_get_os_service_pack (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, NULL);
return enroll->os_service_pack;
}
void
adcli_enroll_set_os_service_pack (adcli_enroll *enroll,
const char *value)
{
return_if_fail (enroll != NULL);
if (value && value[0] == '\0')
value = NULL;
_adcli_str_set (&enroll->os_service_pack, value);
enroll->os_service_pack_explicit = 1;
}
const char *
adcli_enroll_get_user_principal (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, NULL);
return enroll->user_principal;
}
void
adcli_enroll_set_user_principal (adcli_enroll *enroll,
const char *value)
{
return_if_fail (enroll != NULL);
_adcli_str_set (&enroll->user_principal, value);
enroll->user_princpal_generate = 0;
}
void
adcli_enroll_auto_user_principal (adcli_enroll *enroll)
{
return_if_fail (enroll != NULL);
_adcli_str_set (&enroll->user_principal, NULL);
enroll->user_princpal_generate = 1;
}
#define DEFAULT_HOST_PW_LIFETIME 30
unsigned int
adcli_enroll_get_computer_password_lifetime (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, DEFAULT_HOST_PW_LIFETIME);
if (enroll->computer_password_lifetime_explicit) {
return enroll->computer_password_lifetime;
}
return DEFAULT_HOST_PW_LIFETIME;
}
void
adcli_enroll_set_computer_password_lifetime (adcli_enroll *enroll,
unsigned int lifetime)
{
return_if_fail (enroll != NULL);
enroll->computer_password_lifetime = lifetime;
enroll->computer_password_lifetime_explicit = 1;
}
void
adcli_enroll_set_samba_data_tool (adcli_enroll *enroll, const char *value)
{
return_if_fail (enroll != NULL);
if (value != NULL && value[0] != '\0') {
_adcli_str_set (&enroll->samba_data_tool, value);
}
}
const char *
adcli_enroll_get_samba_data_tool (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, NULL);
return enroll->samba_data_tool;
}
bool
adcli_enroll_get_trusted_for_delegation (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, false);
return enroll->trusted_for_delegation;
}
void
adcli_enroll_set_trusted_for_delegation (adcli_enroll *enroll,
bool value)
{
return_if_fail (enroll != NULL);
enroll->trusted_for_delegation = value;
enroll->trusted_for_delegation_explicit = 1;
}
void
adcli_enroll_set_description (adcli_enroll *enroll, const char *value)
{
return_if_fail (enroll != NULL);
if (value != NULL && value[0] != '\0') {
_adcli_str_set (&enroll->description, value);
}
}
const char *
adcli_enroll_get_desciption (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, NULL);
return enroll->description;
}
void
adcli_enroll_set_is_service (adcli_enroll *enroll, bool value)
{
return_if_fail (enroll != NULL);
enroll->is_service = value;
enroll->is_service_explicit = true;
}
bool
adcli_enroll_get_is_service (adcli_enroll *enroll)
{
return enroll->is_service;
}
const char **
adcli_enroll_get_service_principals_to_add (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, NULL);
return (const char **)enroll->service_principals_to_add;
}
void
adcli_enroll_add_service_principal_to_add (adcli_enroll *enroll,
const char *value)
{
return_if_fail (enroll != NULL);
return_if_fail (value != NULL);
enroll->service_principals_to_add = _adcli_strv_add (enroll->service_principals_to_add,
strdup (value), NULL);
return_if_fail (enroll->service_principals_to_add != NULL);
}
const char **
adcli_enroll_get_service_principals_to_remove (adcli_enroll *enroll)
{
return_val_if_fail (enroll != NULL, NULL);
return (const char **)enroll->service_principals_to_remove;
}
void
adcli_enroll_add_service_principal_to_remove (adcli_enroll *enroll,
const char *value)
{
return_if_fail (enroll != NULL);
return_if_fail (value != NULL);
enroll->service_principals_to_remove = _adcli_strv_add (enroll->service_principals_to_remove,
strdup (value), NULL);
return_if_fail (enroll->service_principals_to_remove != NULL);
}
#ifdef ADENROLL_TESTS
#include "test.h"
static void
test_adcli_enroll_get_permitted_keytab_enctypes (void)
{
krb5_enctype *enctypes;
krb5_error_code code;
krb5_enctype *permitted_enctypes;
krb5_enctype check_enctypes[3] = { 0 };
adcli_conn *conn;
adcli_enroll *enroll;
adcli_result res;
krb5_context k5;
size_t c;
conn = adcli_conn_new ("test.dom");
assert_ptr_not_null (conn);
enroll = adcli_enroll_new (conn);
assert_ptr_not_null (enroll);
enctypes = adcli_enroll_get_permitted_keytab_enctypes (NULL);
assert_ptr_eq (enctypes, NULL);
/* krb5 context missing */
enctypes = adcli_enroll_get_permitted_keytab_enctypes (enroll);
assert_ptr_eq (enctypes, NULL);
/* check that all permitted enctypes can pass */
res = _adcli_krb5_init_context (&k5);
assert_num_eq (res, ADCLI_SUCCESS);
adcli_conn_set_krb5_context (conn, k5);
code = krb5_get_permitted_enctypes (k5, &permitted_enctypes);
assert_num_eq (code, 0);
assert_ptr_not_null (permitted_enctypes);
assert_num_cmp (permitted_enctypes[0], !=, 0);
adcli_enroll_set_keytab_enctypes (enroll, permitted_enctypes);
enctypes = adcli_enroll_get_permitted_keytab_enctypes (enroll);
assert_ptr_not_null (enctypes);
for (c = 0; permitted_enctypes[c] != 0; c++) {
assert_num_eq (enctypes[c], permitted_enctypes[c]);
}
assert_num_eq (enctypes[c], 0);
krb5_free_enctypes (k5, enctypes);
/* check that ENCTYPE_UNKNOWN is filtered out */
check_enctypes[0] = permitted_enctypes[0];
check_enctypes[1] = ENCTYPE_UNKNOWN;
check_enctypes[2] = 0;
adcli_enroll_set_keytab_enctypes (enroll, check_enctypes);
enctypes = adcli_enroll_get_permitted_keytab_enctypes (enroll);
assert_ptr_not_null (enctypes);
assert_num_eq (enctypes[0], permitted_enctypes[0]);
assert_num_eq (enctypes[1], 0);
krb5_free_enctypes (k5, enctypes);
krb5_free_enctypes (k5, permitted_enctypes);
adcli_enroll_unref (enroll);
adcli_conn_unref (conn);
}
int
main (int argc,
char *argv[])
{
test_func (test_adcli_enroll_get_permitted_keytab_enctypes,
"/attrs/adcli_enroll_get_permitted_keytab_enctypes");
return test_run (argc, argv);
}
#endif /* ADENROLL_TESTS */
|