1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
|
/*
Unix SMB/CIFS implementation.
pdb glue module for direct access to the dsdb via LDB APIs
Copyright (C) Volker Lendecke 2009-2011
Copyright (C) Andrew Bartlett 2010-2012
Copyright (C) Matthias Dieter Wallnöfer 2009
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* This module, is a port of Volker's pdb_ads to ldb and DSDB APIs */
#include "includes.h"
#include "source3/include/passdb.h"
#include "source4/dsdb/samdb/samdb.h"
#include "ldb_errors.h"
#include "libcli/security/dom_sid.h"
#include "source4/winbind/idmap.h"
#include "librpc/gen_ndr/ndr_security.h"
#include "librpc/gen_ndr/ndr_drsblobs.h"
#include "librpc/gen_ndr/ndr_lsa.h"
#include "libds/common/flag_mapping.h"
#include "source4/lib/events/events.h"
#include "source4/auth/session.h"
#include "source4/auth/system_session_proto.h"
#include "lib/param/param.h"
#include "source4/dsdb/common/util.h"
#include "source3/include/secrets.h"
#include "source4/auth/auth_sam.h"
#include "auth/credentials/credentials.h"
#include "lib/util/base64.h"
#include "libcli/ldap/ldap_ndr.h"
#include "lib/util/util_ldb.h"
struct pdb_samba_dsdb_state {
struct tevent_context *ev;
struct ldb_context *ldb;
struct idmap_context *idmap_ctx;
struct loadparm_context *lp_ctx;
};
static NTSTATUS pdb_samba_dsdb_getsampwsid(struct pdb_methods *m,
struct samu *sam_acct,
const struct dom_sid *sid);
static NTSTATUS pdb_samba_dsdb_getsamupriv(struct pdb_samba_dsdb_state *state,
const char *filter,
TALLOC_CTX *mem_ctx,
struct ldb_message **pmsg);
static bool pdb_samba_dsdb_sid_to_id(struct pdb_methods *m, const struct dom_sid *sid,
struct unixid *id);
static bool pdb_samba_dsdb_pull_time(struct ldb_message *msg, const char *attr,
time_t *ptime)
{
uint64_t tmp;
if (! ldb_msg_find_element(msg, attr)) {
return false;
}
tmp = ldb_msg_find_attr_as_uint64(msg, attr, 0);
*ptime = nt_time_to_unix(tmp);
return true;
}
static struct pdb_domain_info *pdb_samba_dsdb_get_domain_info(
struct pdb_methods *m, TALLOC_CTX *mem_ctx)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
struct pdb_domain_info *info;
struct dom_sid *domain_sid;
struct ldb_dn *forest_dn, *domain_dn;
struct ldb_result *dom_res = NULL;
const char *dom_attrs[] = {
"objectSid",
"objectGUID",
"fSMORoleOwner",
NULL
};
char *p;
int ret;
info = talloc(mem_ctx, struct pdb_domain_info);
if (info == NULL) {
return NULL;
}
domain_dn = ldb_get_default_basedn(state->ldb);
ret = ldb_search(state->ldb, info, &dom_res,
domain_dn, LDB_SCOPE_BASE, dom_attrs, NULL);
if (ret != LDB_SUCCESS) {
goto fail;
}
if (dom_res->count != 1) {
goto fail;
}
info->guid = samdb_result_guid(dom_res->msgs[0], "objectGUID");
domain_sid = samdb_result_dom_sid(state, dom_res->msgs[0], "objectSid");
if (!domain_sid) {
goto fail;
}
info->sid = *domain_sid;
TALLOC_FREE(dom_res);
info->name = talloc_strdup(info, lpcfg_sam_name(state->lp_ctx));
info->dns_domain = ldb_dn_canonical_string(info, domain_dn);
if (!info->dns_domain) {
goto fail;
}
p = strchr(info->dns_domain, '/');
if (p) {
*p = '\0';
}
forest_dn = ldb_get_root_basedn(state->ldb);
if (!forest_dn) {
goto fail;
}
info->dns_forest = ldb_dn_canonical_string(info, forest_dn);
if (!info->dns_forest) {
goto fail;
}
p = strchr(info->dns_forest, '/');
if (p) {
*p = '\0';
}
return info;
fail:
TALLOC_FREE(dom_res);
TALLOC_FREE(info);
return NULL;
}
static struct ldb_message *pdb_samba_dsdb_get_samu_private(
struct pdb_methods *m, struct samu *sam)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
struct ldb_message *msg;
char *sidstr, *filter;
NTSTATUS status;
msg = (struct ldb_message *)
pdb_get_backend_private_data(sam, m);
if (msg != NULL) {
return talloc_get_type_abort(msg, struct ldb_message);
}
sidstr = dom_sid_string(talloc_tos(), pdb_get_user_sid(sam));
if (sidstr == NULL) {
return NULL;
}
filter = talloc_asprintf(
talloc_tos(), "(&(objectsid=%s)(objectclass=user))", sidstr);
TALLOC_FREE(sidstr);
if (filter == NULL) {
return NULL;
}
status = pdb_samba_dsdb_getsamupriv(state, filter, sam, &msg);
TALLOC_FREE(filter);
if (!NT_STATUS_IS_OK(status)) {
return NULL;
}
return msg;
}
static NTSTATUS pdb_samba_dsdb_init_sam_from_priv(struct pdb_methods *m,
struct samu *sam,
struct ldb_message *msg)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
TALLOC_CTX *frame = talloc_stackframe();
NTSTATUS status = NT_STATUS_INTERNAL_DB_CORRUPTION;
const char *str;
time_t tmp_time;
struct dom_sid *sid, group_sid;
uint64_t n;
const DATA_BLOB *blob;
str = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
if (str == NULL) {
DEBUG(10, ("no samAccountName\n"));
goto fail;
}
pdb_set_username(sam, str, PDB_SET);
if (pdb_samba_dsdb_pull_time(msg, "lastLogon", &tmp_time)) {
pdb_set_logon_time(sam, tmp_time, PDB_SET);
}
if (pdb_samba_dsdb_pull_time(msg, "lastLogoff", &tmp_time)) {
pdb_set_logoff_time(sam, tmp_time, PDB_SET);
}
if (pdb_samba_dsdb_pull_time(msg, "pwdLastSet", &tmp_time)) {
pdb_set_pass_last_set_time(sam, tmp_time, PDB_SET);
}
if (pdb_samba_dsdb_pull_time(msg, "accountExpires", &tmp_time)) {
pdb_set_kickoff_time(sam, tmp_time, PDB_SET);
}
str = ldb_msg_find_attr_as_string(msg, "displayName",
NULL);
if (str != NULL) {
pdb_set_fullname(sam, str, PDB_SET);
}
str = ldb_msg_find_attr_as_string(msg, "homeDirectory",
NULL);
if (str != NULL) {
pdb_set_homedir(sam, str, PDB_SET);
}
str = ldb_msg_find_attr_as_string(msg, "homeDrive", NULL);
if (str != NULL) {
pdb_set_dir_drive(sam, str, PDB_SET);
}
str = ldb_msg_find_attr_as_string(msg, "scriptPath", NULL);
if (str != NULL) {
pdb_set_logon_script(sam, str, PDB_SET);
}
str = ldb_msg_find_attr_as_string(msg, "profilePath",
NULL);
if (str != NULL) {
pdb_set_profile_path(sam, str, PDB_SET);
}
str = ldb_msg_find_attr_as_string(msg, "comment",
NULL);
if (str != NULL) {
pdb_set_comment(sam, str, PDB_SET);
}
str = ldb_msg_find_attr_as_string(msg, "description",
NULL);
if (str != NULL) {
pdb_set_acct_desc(sam, str, PDB_SET);
}
str = ldb_msg_find_attr_as_string(msg, "userWorkstations",
NULL);
if (str != NULL) {
pdb_set_workstations(sam, str, PDB_SET);
}
blob = ldb_msg_find_ldb_val(msg, "userParameters");
if (blob != NULL) {
str = base64_encode_data_blob(frame, *blob);
if (str == NULL) {
DEBUG(0, ("base64_encode_data_blob() failed\n"));
goto fail;
}
pdb_set_munged_dial(sam, str, PDB_SET);
}
sid = samdb_result_dom_sid(talloc_tos(), msg, "objectSid");
if (!sid) {
DEBUG(10, ("Could not pull SID\n"));
goto fail;
}
pdb_set_user_sid(sam, sid, PDB_SET);
n = samdb_result_acct_flags(msg, "msDS-User-Account-Control-Computed");
if (n == 0) {
DEBUG(10, ("Could not pull userAccountControl\n"));
goto fail;
}
pdb_set_acct_ctrl(sam, n, PDB_SET);
blob = ldb_msg_find_ldb_val(msg, "unicodePwd");
if (blob) {
if (blob->length != NT_HASH_LEN) {
DEBUG(0, ("Got NT hash of length %d, expected %d\n",
(int)blob->length, NT_HASH_LEN));
goto fail;
}
pdb_set_nt_passwd(sam, blob->data, PDB_SET);
}
blob = ldb_msg_find_ldb_val(msg, "dBCSPwd");
if (blob) {
if (blob->length != LM_HASH_LEN) {
DEBUG(0, ("Got LM hash of length %d, expected %d\n",
(int)blob->length, LM_HASH_LEN));
goto fail;
}
pdb_set_lanman_passwd(sam, blob->data, PDB_SET);
}
n = ldb_msg_find_attr_as_uint(msg, "primaryGroupID", 0);
if (n == 0) {
DEBUG(10, ("Could not pull primaryGroupID\n"));
goto fail;
}
sid_compose(&group_sid, samdb_domain_sid(state->ldb), n);
pdb_set_group_sid(sam, &group_sid, PDB_SET);
status = NT_STATUS_OK;
fail:
TALLOC_FREE(frame);
return status;
}
static bool pdb_samba_dsdb_add_time(struct ldb_message *msg,
const char *attrib, time_t t)
{
uint64_t nt_time;
unix_to_nt_time(&nt_time, t);
return ldb_msg_add_fmt(msg, attrib, "%llu", (unsigned long long) nt_time);
}
static int pdb_samba_dsdb_replace_by_sam(struct pdb_samba_dsdb_state *state,
bool (*need_update)(const struct samu *,
enum pdb_elements),
struct ldb_dn *dn,
struct samu *sam)
{
TALLOC_CTX *frame = talloc_stackframe();
int ret = LDB_SUCCESS;
const char *pw;
struct ldb_message *msg;
struct ldb_request *req;
uint32_t dsdb_flags = 0;
/* TODO: All fields :-) */
msg = ldb_msg_new(frame);
if (!msg) {
talloc_free(frame);
return false;
}
msg->dn = dn;
/* build modify request */
ret = ldb_build_mod_req(&req, state->ldb, frame, msg, NULL, NULL,
ldb_op_default_callback,
NULL);
if (ret != LDB_SUCCESS) {
talloc_free(frame);
return ret;
}
/* If we set a plaintext password, the system will
* force the pwdLastSet to now() */
if (need_update(sam, PDB_PASSLASTSET)) {
dsdb_flags |= DSDB_PASSWORD_BYPASS_LAST_SET;
ret |= pdb_samba_dsdb_add_time(msg, "pwdLastSet",
pdb_get_pass_last_set_time(sam));
}
pw = pdb_get_plaintext_passwd(sam);
if (need_update(sam, PDB_PLAINTEXT_PW)) {
struct ldb_val pw_utf16;
if (pw == NULL) {
talloc_free(frame);
return LDB_ERR_OPERATIONS_ERROR;
}
if (!convert_string_talloc(msg,
CH_UNIX, CH_UTF16,
pw, strlen(pw),
(void *)&pw_utf16.data,
&pw_utf16.length)) {
talloc_free(frame);
return LDB_ERR_OPERATIONS_ERROR;
}
ret |= ldb_msg_add_value(msg, "clearTextPassword", &pw_utf16, NULL);
} else {
bool changed_lm_pw = false;
bool changed_nt_pw = false;
bool changed_history = false;
if (need_update(sam, PDB_LMPASSWD)) {
struct ldb_val val;
val.data = discard_const_p(uint8_t, pdb_get_lanman_passwd(sam));
if (!val.data) {
samdb_msg_add_delete(state->ldb, msg, msg,
"dBCSPwd");
} else {
val.length = LM_HASH_LEN;
ret |= ldb_msg_add_value(msg, "dBCSPwd", &val, NULL);
}
changed_lm_pw = true;
}
if (need_update(sam, PDB_NTPASSWD)) {
struct ldb_val val;
val.data = discard_const_p(uint8_t, pdb_get_nt_passwd(sam));
if (!val.data) {
samdb_msg_add_delete(state->ldb, msg, msg,
"unicodePwd");
} else {
val.length = NT_HASH_LEN;
ret |= ldb_msg_add_value(msg, "unicodePwd", &val, NULL);
}
changed_nt_pw = true;
}
/* Try to ensure we don't get out of sync */
if (changed_lm_pw && !changed_nt_pw) {
samdb_msg_add_delete(state->ldb, msg, msg,
"unicodePwd");
} else if (changed_nt_pw && !changed_lm_pw) {
samdb_msg_add_delete(state->ldb, msg, msg,
"dBCSPwd");
}
if (changed_lm_pw || changed_nt_pw) {
samdb_msg_add_delete(state->ldb, msg, msg,
"supplementalCredentials");
}
if (need_update(sam, PDB_PWHISTORY)) {
uint32_t current_hist_len;
const uint8_t *history = pdb_get_pw_history(sam, ¤t_hist_len);
bool invalid_history = false;
struct samr_Password *history_hashes = talloc_array(talloc_tos(), struct samr_Password,
current_hist_len);
if (!history) {
invalid_history = true;
} else {
unsigned int i;
/* Parse the history into the correct format */
for (i = 0; i < current_hist_len; i++) {
if (!all_zero(&history[i*PW_HISTORY_ENTRY_LEN],
16)) {
/* If the history is in the old format, with a salted hash, then we can't migrate it to AD format */
invalid_history = true;
break;
}
/* Copy out the 2nd 16 bytes of the 32 byte password history, containing the NT hash */
memcpy(history_hashes[i].hash,
&history[(i*PW_HISTORY_ENTRY_LEN) + PW_HISTORY_SALT_LEN],
sizeof(history_hashes[i].hash));
}
}
if (invalid_history) {
ret |= samdb_msg_add_delete(state->ldb, msg, msg,
"ntPwdHistory");
ret |= samdb_msg_add_delete(state->ldb, msg, msg,
"lmPwdHistory");
} else {
ret |= samdb_msg_add_hashes(state->ldb, msg, msg,
"ntPwdHistory",
history_hashes,
current_hist_len);
}
changed_history = true;
}
if (changed_lm_pw || changed_nt_pw || changed_history) {
/* These attributes can only be modified directly by using a special control */
dsdb_flags |= DSDB_BYPASS_PASSWORD_HASH;
}
}
/* PDB_USERSID is only allowed on ADD, handled in caller */
if (need_update(sam, PDB_GROUPSID)) {
const struct dom_sid *sid = pdb_get_group_sid(sam);
uint32_t rid;
NTSTATUS status = dom_sid_split_rid(NULL, sid, NULL, &rid);
if (!NT_STATUS_IS_OK(status)) {
talloc_free(frame);
return LDB_ERR_OPERATIONS_ERROR;
}
if (!dom_sid_in_domain(samdb_domain_sid(state->ldb), sid)) {
talloc_free(frame);
return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
}
ret |= samdb_msg_add_uint(state->ldb, msg, msg, "primaryGroupID", rid);
}
if (need_update(sam, PDB_FULLNAME)) {
ret |= ldb_msg_add_string(msg, "displayName", pdb_get_fullname(sam));
}
if (need_update(sam, PDB_SMBHOME)) {
ret |= ldb_msg_add_string(msg, "homeDirectory",
pdb_get_homedir(sam));
}
if (need_update(sam, PDB_PROFILE)) {
ret |= ldb_msg_add_string(msg, "profilePath",
pdb_get_profile_path(sam));
}
if (need_update(sam, PDB_DRIVE)) {
ret |= ldb_msg_add_string(msg, "homeDrive",
pdb_get_dir_drive(sam));
}
if (need_update(sam, PDB_LOGONSCRIPT)) {
ret |= ldb_msg_add_string(msg, "scriptPath",
pdb_get_logon_script(sam));
}
if (need_update(sam, PDB_KICKOFFTIME)) {
ret |= pdb_samba_dsdb_add_time(msg, "accountExpires",
pdb_get_kickoff_time(sam));
}
if (need_update(sam, PDB_LOGONTIME)) {
ret |= pdb_samba_dsdb_add_time(msg, "lastLogon",
pdb_get_logon_time(sam));
}
if (need_update(sam, PDB_LOGOFFTIME)) {
ret |= pdb_samba_dsdb_add_time(msg, "lastLogoff",
pdb_get_logoff_time(sam));
}
if (need_update(sam, PDB_USERNAME)) {
ret |= ldb_msg_add_string(msg, "samAccountName",
pdb_get_username(sam));
}
if (need_update(sam, PDB_HOURSLEN) || need_update(sam, PDB_HOURS)) {
struct ldb_val hours = data_blob_const(pdb_get_hours(sam), pdb_get_hours_len(sam));
ret |= ldb_msg_add_value(msg, "logonHours",
&hours, NULL);
}
if (need_update(sam, PDB_ACCTCTRL)) {
ret |= samdb_msg_add_acct_flags(state->ldb, msg, msg,
"userAccountControl", pdb_get_acct_ctrl(sam));
}
if (need_update(sam, PDB_COMMENT)) {
ret |= ldb_msg_add_string(msg, "comment",
pdb_get_comment(sam));
}
if (need_update(sam, PDB_ACCTDESC)) {
ret |= ldb_msg_add_string(msg, "description",
pdb_get_acct_desc(sam));
}
if (need_update(sam, PDB_WORKSTATIONS)) {
ret |= ldb_msg_add_string(msg, "userWorkstations",
pdb_get_workstations(sam));
}
/* This will need work, it is actually a UTF8 'string' with internal NULLs, to handle TS parameters */
if (need_update(sam, PDB_MUNGEDDIAL)) {
const char *base64_munged_dial = NULL;
base64_munged_dial = pdb_get_munged_dial(sam);
if (base64_munged_dial != NULL && strlen(base64_munged_dial) > 0) {
struct ldb_val blob;
blob = base64_decode_data_blob_talloc(msg,
base64_munged_dial);
if (blob.data == NULL) {
DEBUG(0, ("Failed to decode userParameters from "
"munged dialback string[%s] for %s\n",
base64_munged_dial,
ldb_dn_get_linearized(msg->dn)));
talloc_free(frame);
return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
}
ret |= ldb_msg_add_steal_value(msg, "userParameters",
&blob);
}
}
if (need_update(sam, PDB_COUNTRY_CODE)) {
ret |= ldb_msg_add_fmt(msg, "countryCode",
"%i", (int)pdb_get_country_code(sam));
}
if (need_update(sam, PDB_CODE_PAGE)) {
ret |= ldb_msg_add_fmt(msg, "codePage",
"%i", (int)pdb_get_code_page(sam));
}
/* Not yet handled here or not meaningful for modifies on a Samba_Dsdb backend:
PDB_BAD_PASSWORD_TIME,
PDB_CANCHANGETIME, - these are calculated per policy, not stored
PDB_DOMAIN,
PDB_NTUSERNAME, - this makes no sense, and never really did
PDB_LOGONDIVS,
PDB_USERSID, - Handled in pdb_samba_dsdb_add_sam_account()
PDB_FIELDS_PRESENT,
PDB_BAD_PASSWORD_COUNT,
PDB_LOGON_COUNT,
PDB_UNKNOWN6,
PDB_BACKEND_PRIVATE_DATA,
*/
if (ret != LDB_SUCCESS) {
talloc_free(frame);
return LDB_ERR_OPERATIONS_ERROR;
}
if (msg->num_elements == 0) {
talloc_free(frame);
/* Nothing to do, just return success */
return LDB_SUCCESS;
}
ret = dsdb_replace(state->ldb, msg, dsdb_flags);
if (ret != LDB_SUCCESS) {
DEBUG(0,("Failed to modify account record %s to set user attributes: %s\n",
ldb_dn_get_linearized(msg->dn),
ldb_errstring(state->ldb)));
}
talloc_free(frame);
return ret;
}
static NTSTATUS pdb_samba_dsdb_getsamupriv(struct pdb_samba_dsdb_state *state,
const char *filter,
TALLOC_CTX *mem_ctx,
struct ldb_message **msg)
{
const char * attrs[] = {
"lastLogon", "lastLogoff", "pwdLastSet", "accountExpires",
"sAMAccountName", "displayName", "homeDirectory",
"homeDrive", "scriptPath", "profilePath", "description",
"userWorkstations", "comment", "userParameters", "objectSid",
"primaryGroupID", "userAccountControl",
"msDS-User-Account-Control-Computed", "logonHours",
"badPwdCount", "logonCount", "countryCode", "codePage",
"unicodePwd", "dBCSPwd", NULL };
int rc = dsdb_search_one(state->ldb, mem_ctx, msg, ldb_get_default_basedn(state->ldb), LDB_SCOPE_SUBTREE, attrs, 0, "%s", filter);
if (rc != LDB_SUCCESS) {
DEBUG(10, ("ldap_search failed %s\n",
ldb_errstring(state->ldb)));
return NT_STATUS_LDAP(rc);
}
return NT_STATUS_OK;
}
static NTSTATUS pdb_samba_dsdb_getsampwfilter(struct pdb_methods *m,
struct pdb_samba_dsdb_state *state,
struct samu *sam_acct,
const char *exp_fmt, ...)
PRINTF_ATTRIBUTE(4,5);
static NTSTATUS pdb_samba_dsdb_getsampwfilter(struct pdb_methods *m,
struct pdb_samba_dsdb_state *state,
struct samu *sam_acct,
const char *exp_fmt, ...)
{
struct ldb_message *priv;
NTSTATUS status;
va_list ap;
char *expression = NULL;
TALLOC_CTX *tmp_ctx = talloc_new(state);
NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
va_start(ap, exp_fmt);
expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
va_end(ap);
if (!expression) {
talloc_free(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
status = pdb_samba_dsdb_getsamupriv(state, expression, sam_acct, &priv);
talloc_free(tmp_ctx);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(10, ("pdb_samba_dsdb_getsamupriv failed: %s\n",
nt_errstr(status)));
return status;
}
status = pdb_samba_dsdb_init_sam_from_priv(m, sam_acct, priv);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(10, ("pdb_samba_dsdb_init_sam_from_priv failed: %s\n",
nt_errstr(status)));
TALLOC_FREE(priv);
return status;
}
pdb_set_backend_private_data(sam_acct, priv, NULL, m, PDB_SET);
return NT_STATUS_OK;
}
static NTSTATUS pdb_samba_dsdb_getsampwnam(struct pdb_methods *m,
struct samu *sam_acct,
const char *username)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
return pdb_samba_dsdb_getsampwfilter(m, state, sam_acct,
"(&(samaccountname=%s)(objectclass=user))",
username);
}
static NTSTATUS pdb_samba_dsdb_getsampwsid(struct pdb_methods *m,
struct samu *sam_acct,
const struct dom_sid *sid)
{
NTSTATUS status;
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
char *sidstr;
sidstr = dom_sid_string(talloc_tos(), sid);
NT_STATUS_HAVE_NO_MEMORY(sidstr);
status = pdb_samba_dsdb_getsampwfilter(m, state, sam_acct,
"(&(objectsid=%s)(objectclass=user))",
sidstr);
talloc_free(sidstr);
return status;
}
static NTSTATUS pdb_samba_dsdb_create_user(struct pdb_methods *m,
TALLOC_CTX *mem_ctx,
const char *name, uint32_t acct_flags,
uint32_t *rid)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
struct dom_sid *sid;
struct ldb_dn *dn;
NTSTATUS status;
TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
/* Internally this uses transactions to ensure all the steps
* happen or fail as one */
status = dsdb_add_user(state->ldb, tmp_ctx, name, acct_flags, NULL,
&sid, &dn);
if (!NT_STATUS_IS_OK(status)) {
talloc_free(tmp_ctx);
return status;
}
sid_peek_rid(sid, rid);
talloc_free(tmp_ctx);
return NT_STATUS_OK;
}
static NTSTATUS pdb_samba_dsdb_delete_user(struct pdb_methods *m,
TALLOC_CTX *mem_ctx,
struct samu *sam)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
struct ldb_dn *dn;
int rc;
TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
dn = ldb_dn_new_fmt(tmp_ctx, state->ldb, "<SID=%s>", dom_sid_string(tmp_ctx, pdb_get_user_sid(sam)));
if (!dn || !ldb_dn_validate(dn)) {
talloc_free(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
rc = ldb_delete(state->ldb, dn);
if (rc != LDB_SUCCESS) {
DEBUG(10, ("ldb_delete for %s failed: %s\n", ldb_dn_get_linearized(dn),
ldb_errstring(state->ldb)));
talloc_free(tmp_ctx);
return NT_STATUS_LDAP(rc);
}
talloc_free(tmp_ctx);
return NT_STATUS_OK;
}
/* This interface takes a fully populated struct samu and places it in
* the database. This is not implemented at this time as we need to
* be careful around the creation of arbitary SIDs (ie, we must ensrue
* they are not left in a RID pool */
static NTSTATUS pdb_samba_dsdb_add_sam_account(struct pdb_methods *m,
struct samu *sampass)
{
int ret;
NTSTATUS status;
struct ldb_dn *dn;
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
uint32_t acb_flags = pdb_get_acct_ctrl(sampass);
const char *username = pdb_get_username(sampass);
const struct dom_sid *user_sid = pdb_get_user_sid(sampass);
TALLOC_CTX *tframe = talloc_stackframe();
acb_flags &= (ACB_NORMAL|ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST);
ret = ldb_transaction_start(state->ldb);
if (ret != LDB_SUCCESS) {
talloc_free(tframe);
return NT_STATUS_LOCK_NOT_GRANTED;
}
status = dsdb_add_user(state->ldb, talloc_tos(), username,
acb_flags, user_sid, NULL, &dn);
if (!NT_STATUS_IS_OK(status)) {
ldb_transaction_cancel(state->ldb);
talloc_free(tframe);
return status;
}
ret = pdb_samba_dsdb_replace_by_sam(state, pdb_element_is_set_or_changed,
dn, sampass);
if (ret != LDB_SUCCESS) {
ldb_transaction_cancel(state->ldb);
talloc_free(tframe);
return dsdb_ldb_err_to_ntstatus(ret);
}
ret = ldb_transaction_commit(state->ldb);
if (ret != LDB_SUCCESS) {
DEBUG(0,("Failed to commit transaction to add and modify account record %s: %s\n",
ldb_dn_get_linearized(dn),
ldb_errstring(state->ldb)));
talloc_free(tframe);
return NT_STATUS_INTERNAL_DB_CORRUPTION;
}
talloc_free(tframe);
return NT_STATUS_OK;
}
/*
* Update the Samba_Dsdb LDB with the changes from a struct samu.
*
* This takes care not to update elements that have not been changed
* by the caller
*/
static NTSTATUS pdb_samba_dsdb_update_sam_account(struct pdb_methods *m,
struct samu *sam)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
struct ldb_message *msg = pdb_samba_dsdb_get_samu_private(
m, sam);
int ret;
ret = pdb_samba_dsdb_replace_by_sam(state, pdb_element_is_changed, msg->dn,
sam);
return dsdb_ldb_err_to_ntstatus(ret);
}
static NTSTATUS pdb_samba_dsdb_delete_sam_account(struct pdb_methods *m,
struct samu *username)
{
NTSTATUS status;
TALLOC_CTX *tmp_ctx = talloc_new(NULL);
NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
status = pdb_samba_dsdb_delete_user(m, tmp_ctx, username);
talloc_free(tmp_ctx);
return status;
}
static NTSTATUS pdb_samba_dsdb_rename_sam_account(struct pdb_methods *m,
struct samu *oldname,
const char *newname)
{
return NT_STATUS_NOT_IMPLEMENTED;
}
/* This is not implemented, as this module is exptected to be used
* with auth_samba_dsdb, and this is responible for login counters etc
*
*/
static NTSTATUS pdb_samba_dsdb_update_login_attempts(struct pdb_methods *m,
struct samu *sam_acct,
bool success)
{
return NT_STATUS_NOT_IMPLEMENTED;
}
static NTSTATUS pdb_samba_dsdb_getgrfilter(struct pdb_methods *m,
GROUP_MAP *map,
const char *exp_fmt, ...)
PRINTF_ATTRIBUTE(3,4);
static NTSTATUS pdb_samba_dsdb_getgrfilter(struct pdb_methods *m, GROUP_MAP *map,
const char *exp_fmt, ...)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
const char *attrs[] = { "objectClass", "objectSid", "description", "samAccountName", "groupType",
NULL };
struct ldb_message *msg;
va_list ap;
char *expression = NULL;
struct dom_sid *sid;
const char *str;
int rc;
struct id_map id_map;
struct id_map *id_maps[2];
TALLOC_CTX *tmp_ctx = talloc_stackframe();
NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
va_start(ap, exp_fmt);
expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
va_end(ap);
if (!expression) {
talloc_free(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
rc = dsdb_search_one(state->ldb, tmp_ctx, &msg, ldb_get_default_basedn(state->ldb), LDB_SCOPE_SUBTREE, attrs, 0, "%s", expression);
if (rc == LDB_ERR_NO_SUCH_OBJECT) {
talloc_free(tmp_ctx);
return NT_STATUS_NO_SUCH_GROUP;
} else if (rc != LDB_SUCCESS) {
talloc_free(tmp_ctx);
DEBUG(10, ("dsdb_search_one failed %s\n",
ldb_errstring(state->ldb)));
return NT_STATUS_LDAP(rc);
}
sid = samdb_result_dom_sid(tmp_ctx, msg, "objectSid");
if (!sid) {
talloc_free(tmp_ctx);
DEBUG(10, ("Could not pull SID\n"));
return NT_STATUS_INTERNAL_DB_CORRUPTION;
}
map->sid = *sid;
if (samdb_find_attribute(state->ldb, msg, "objectClass", "group")) {
NTSTATUS status;
uint32_t grouptype = ldb_msg_find_attr_as_uint(msg, "groupType", 0);
switch (grouptype) {
case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP:
case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP:
map->sid_name_use = SID_NAME_ALIAS;
break;
case GTYPE_SECURITY_GLOBAL_GROUP:
map->sid_name_use = SID_NAME_DOM_GRP;
break;
default:
talloc_free(tmp_ctx);
DEBUG(10, ("Could not pull groupType\n"));
return NT_STATUS_INTERNAL_DB_CORRUPTION;
}
ZERO_STRUCT(id_map);
id_map.sid = sid;
id_maps[0] = &id_map;
id_maps[1] = NULL;
status = idmap_sids_to_xids(state->idmap_ctx, tmp_ctx, id_maps);
if (!NT_STATUS_IS_OK(status)) {
talloc_free(tmp_ctx);
return status;
}
if (id_map.xid.type == ID_TYPE_GID || id_map.xid.type == ID_TYPE_BOTH) {
map->gid = id_map.xid.id;
} else {
DEBUG(1, (__location__ "Did not get GUID when mapping SID for %s", expression));
talloc_free(tmp_ctx);
return NT_STATUS_INTERNAL_DB_CORRUPTION;
}
} else if (samdb_find_attribute(state->ldb, msg, "objectClass", "user")) {
DEBUG(1, (__location__ "Got SID_NAME_USER when searching for a group with %s", expression));
talloc_free(tmp_ctx);
return NT_STATUS_INTERNAL_DB_CORRUPTION;
}
str = ldb_msg_find_attr_as_string(msg, "samAccountName",
NULL);
if (str == NULL) {
talloc_free(tmp_ctx);
return NT_STATUS_INTERNAL_DB_CORRUPTION;
}
map->nt_name = talloc_strdup(map, str);
if (!map->nt_name) {
talloc_free(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
str = ldb_msg_find_attr_as_string(msg, "description",
NULL);
if (str != NULL) {
map->comment = talloc_strdup(map, str);
} else {
map->comment = talloc_strdup(map, "");
}
if (!map->comment) {
talloc_free(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
talloc_free(tmp_ctx);
return NT_STATUS_OK;
}
static NTSTATUS pdb_samba_dsdb_getgrsid(struct pdb_methods *m, GROUP_MAP *map,
struct dom_sid sid)
{
char *filter;
NTSTATUS status;
filter = talloc_asprintf(talloc_tos(),
"(&(objectsid=%s)(objectclass=group))",
sid_string_talloc(talloc_tos(), &sid));
if (filter == NULL) {
return NT_STATUS_NO_MEMORY;
}
status = pdb_samba_dsdb_getgrfilter(m, map, "%s", filter);
TALLOC_FREE(filter);
return status;
}
static NTSTATUS pdb_samba_dsdb_getgrgid(struct pdb_methods *m, GROUP_MAP *map,
gid_t gid)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
NTSTATUS status;
struct id_map id_map;
struct id_map *id_maps[2];
TALLOC_CTX *tmp_ctx = talloc_stackframe();
NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
id_map.xid.id = gid;
id_map.xid.type = ID_TYPE_GID;
id_maps[0] = &id_map;
id_maps[1] = NULL;
status = idmap_xids_to_sids(state->idmap_ctx, tmp_ctx, id_maps);
if (!NT_STATUS_IS_OK(status)) {
talloc_free(tmp_ctx);
return status;
}
status = pdb_samba_dsdb_getgrsid(m, map, *id_map.sid);
talloc_free(tmp_ctx);
return status;
}
static NTSTATUS pdb_samba_dsdb_getgrnam(struct pdb_methods *m, GROUP_MAP *map,
const char *name)
{
char *filter;
NTSTATUS status;
filter = talloc_asprintf(talloc_tos(),
"(&(samaccountname=%s)(objectclass=group))",
name);
if (filter == NULL) {
return NT_STATUS_NO_MEMORY;
}
status = pdb_samba_dsdb_getgrfilter(m, map, "%s", filter);
TALLOC_FREE(filter);
return status;
}
static NTSTATUS pdb_samba_dsdb_create_dom_group(struct pdb_methods *m,
TALLOC_CTX *mem_ctx, const char *name,
uint32_t *rid)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
NTSTATUS status;
struct dom_sid *sid;
struct ldb_dn *dn;
TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
status = dsdb_add_domain_group(state->ldb, tmp_ctx, name, &sid, &dn);
if (!NT_STATUS_IS_OK(status)) {
talloc_free(tmp_ctx);
return status;
}
sid_peek_rid(sid, rid);
talloc_free(tmp_ctx);
return NT_STATUS_OK;
}
static NTSTATUS pdb_samba_dsdb_delete_dom_group(struct pdb_methods *m,
TALLOC_CTX *mem_ctx, uint32_t rid)
{
const char *attrs[] = { NULL };
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
struct dom_sid sid;
struct ldb_message *msg;
struct ldb_dn *dn;
int rc;
TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
sid_compose(&sid, samdb_domain_sid(state->ldb), rid);
if (ldb_transaction_start(state->ldb) != LDB_SUCCESS) {
DEBUG(0, ("Unable to start transaction in pdb_samba_dsdb_delete_dom_group()\n"));
return NT_STATUS_INTERNAL_ERROR;
}
dn = ldb_dn_new_fmt(tmp_ctx, state->ldb, "<SID=%s>", dom_sid_string(tmp_ctx, &sid));
if (!dn || !ldb_dn_validate(dn)) {
talloc_free(tmp_ctx);
ldb_transaction_cancel(state->ldb);
return NT_STATUS_NO_MEMORY;
}
rc = dsdb_search_one(state->ldb, tmp_ctx, &msg, dn, LDB_SCOPE_BASE, attrs, 0, "objectclass=group");
if (rc == LDB_ERR_NO_SUCH_OBJECT) {
talloc_free(tmp_ctx);
ldb_transaction_cancel(state->ldb);
return NT_STATUS_NO_SUCH_GROUP;
}
rc = ldb_delete(state->ldb, dn);
if (rc == LDB_ERR_NO_SUCH_OBJECT) {
talloc_free(tmp_ctx);
ldb_transaction_cancel(state->ldb);
return NT_STATUS_NO_SUCH_GROUP;
} else if (rc != LDB_SUCCESS) {
DEBUG(10, ("ldb_delete failed %s\n",
ldb_errstring(state->ldb)));
ldb_transaction_cancel(state->ldb);
return NT_STATUS_LDAP(rc);
}
if (ldb_transaction_commit(state->ldb) != LDB_SUCCESS) {
DEBUG(0, ("Unable to commit transaction in pdb_samba_dsdb_delete_dom_group()\n"));
return NT_STATUS_INTERNAL_ERROR;
}
return NT_STATUS_OK;
}
static NTSTATUS pdb_samba_dsdb_add_group_mapping_entry(struct pdb_methods *m,
GROUP_MAP *map)
{
return NT_STATUS_NOT_IMPLEMENTED;
}
static NTSTATUS pdb_samba_dsdb_update_group_mapping_entry(struct pdb_methods *m,
GROUP_MAP *map)
{
return NT_STATUS_NOT_IMPLEMENTED;
}
static NTSTATUS pdb_samba_dsdb_delete_group_mapping_entry(struct pdb_methods *m,
struct dom_sid sid)
{
return NT_STATUS_NOT_IMPLEMENTED;
}
static NTSTATUS pdb_samba_dsdb_enum_group_mapping(struct pdb_methods *m,
const struct dom_sid *sid,
enum lsa_SidType sid_name_use,
GROUP_MAP ***pp_rmap,
size_t *p_num_entries,
bool unix_only)
{
return NT_STATUS_NOT_IMPLEMENTED;
}
static NTSTATUS pdb_samba_dsdb_enum_group_members(struct pdb_methods *m,
TALLOC_CTX *mem_ctx,
const struct dom_sid *group,
uint32_t **pmembers,
size_t *pnum_members)
{
unsigned int i, num_sids, num_members;
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
struct dom_sid *members_as_sids;
struct dom_sid *dom_sid;
uint32_t *members;
struct ldb_dn *dn;
NTSTATUS status;
TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
dn = ldb_dn_new_fmt(tmp_ctx, state->ldb, "<SID=%s>", dom_sid_string(tmp_ctx, group));
if (!dn || !ldb_dn_validate(dn)) {
return NT_STATUS_NO_MEMORY;
}
status = dsdb_enum_group_mem(state->ldb, tmp_ctx, dn, &members_as_sids, &num_sids);
if (!NT_STATUS_IS_OK(status)) {
talloc_free(tmp_ctx);
return status;
}
status = dom_sid_split_rid(tmp_ctx, group, &dom_sid, NULL);
if (!NT_STATUS_IS_OK(status)) {
talloc_free(tmp_ctx);
return status;
}
*pmembers = members = talloc_array(mem_ctx, uint32_t, num_sids);
if (*pmembers == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
num_members = 0;
for (i = 0; i < num_sids; i++) {
if (!dom_sid_in_domain(dom_sid, &members_as_sids[i])) {
continue;
}
status = dom_sid_split_rid(NULL, &members_as_sids[i],
NULL, &members[num_members]);
if (!NT_STATUS_IS_OK(status)) {
talloc_free(tmp_ctx);
return status;
}
num_members++;
}
*pnum_members = num_members;
return NT_STATUS_OK;
}
/* Just convert the primary group SID into a group */
static NTSTATUS fake_enum_group_memberships(struct pdb_samba_dsdb_state *state,
TALLOC_CTX *mem_ctx,
struct samu *user,
struct dom_sid **pp_sids,
gid_t **pp_gids,
uint32_t *p_num_groups)
{
NTSTATUS status;
size_t num_groups = 0;
struct dom_sid *group_sids = NULL;
gid_t *gids = NULL;
TALLOC_CTX *tmp_ctx;
tmp_ctx = talloc_new(mem_ctx);
NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
if (user->group_sid) {
struct id_map *id_maps[2];
struct id_map id_map;
num_groups = 1;
group_sids = talloc_array(tmp_ctx, struct dom_sid, num_groups);
if (group_sids == NULL) {
talloc_free(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
gids = talloc_array(tmp_ctx, gid_t, num_groups);
if (gids == NULL) {
talloc_free(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
group_sids[0] = *user->group_sid;
ZERO_STRUCT(id_map);
id_map.sid = &group_sids[0];
id_maps[0] = &id_map;
id_maps[1] = NULL;
status = idmap_sids_to_xids(state->idmap_ctx, tmp_ctx, id_maps);
if (!NT_STATUS_IS_OK(status)) {
talloc_free(tmp_ctx);
return status;
}
if (id_map.xid.type == ID_TYPE_GID || id_map.xid.type == ID_TYPE_BOTH) {
gids[0] = id_map.xid.id;
} else {
DEBUG(1, (__location__
"Group %s, of which %s is a member, could not be converted to a GID\n",
dom_sid_string(tmp_ctx, &group_sids[0]),
dom_sid_string(tmp_ctx, &user->user_sid)));
talloc_free(tmp_ctx);
/* We must error out, otherwise a user might
* avoid a DENY acl based on a group they
* missed out on */
return NT_STATUS_NO_SUCH_GROUP;
}
}
*pp_sids = talloc_steal(mem_ctx, group_sids);
*pp_gids = talloc_steal(mem_ctx, gids);
*p_num_groups = num_groups;
talloc_free(tmp_ctx);
return NT_STATUS_OK;
}
static NTSTATUS pdb_samba_dsdb_enum_group_memberships(struct pdb_methods *m,
TALLOC_CTX *mem_ctx,
struct samu *user,
struct dom_sid **pp_sids,
gid_t **pp_gids,
uint32_t *p_num_groups)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
struct ldb_message *msg = pdb_samba_dsdb_get_samu_private(
m, user);
const char *attrs[] = { "tokenGroups", NULL};
struct ldb_message *tokengroups_msg;
struct ldb_message_element *tokengroups;
int i, rc;
NTSTATUS status;
unsigned int count = 0;
size_t num_groups;
struct dom_sid *group_sids;
gid_t *gids;
TALLOC_CTX *tmp_ctx;
if (msg == NULL) {
/* Fake up some things here */
return fake_enum_group_memberships(state,
mem_ctx,
user, pp_sids,
pp_gids, p_num_groups);
}
tmp_ctx = talloc_new(mem_ctx);
NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
rc = dsdb_search_one(state->ldb, tmp_ctx, &tokengroups_msg, msg->dn, LDB_SCOPE_BASE, attrs, 0, NULL);
if (rc == LDB_ERR_NO_SUCH_OBJECT) {
talloc_free(tmp_ctx);
return NT_STATUS_NO_SUCH_USER;
} else if (rc != LDB_SUCCESS) {
DEBUG(10, ("dsdb_search_one failed %s\n",
ldb_errstring(state->ldb)));
talloc_free(tmp_ctx);
return NT_STATUS_LDAP(rc);
}
tokengroups = ldb_msg_find_element(tokengroups_msg, "tokenGroups");
if (tokengroups) {
count = tokengroups->num_values;
}
group_sids = talloc_array(tmp_ctx, struct dom_sid, count);
if (group_sids == NULL) {
talloc_free(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
gids = talloc_array(tmp_ctx, gid_t, count);
if (gids == NULL) {
talloc_free(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
num_groups = 0;
for (i=0; i<count; i++) {
struct id_map *id_maps[2];
struct id_map id_map;
struct ldb_val *v = &tokengroups->values[i];
enum ndr_err_code ndr_err
= ndr_pull_struct_blob(v, group_sids, &group_sids[num_groups],
(ndr_pull_flags_fn_t)ndr_pull_dom_sid);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
talloc_free(tmp_ctx);
return NT_STATUS_INTERNAL_DB_CORRUPTION;
}
ZERO_STRUCT(id_map);
id_map.sid = &group_sids[num_groups];
id_maps[0] = &id_map;
id_maps[1] = NULL;
status = idmap_sids_to_xids(state->idmap_ctx, tmp_ctx, id_maps);
if (!NT_STATUS_IS_OK(status)) {
talloc_free(tmp_ctx);
return status;
}
if (id_map.xid.type == ID_TYPE_GID || id_map.xid.type == ID_TYPE_BOTH) {
gids[num_groups] = id_map.xid.id;
} else {
DEBUG(1, (__location__
"Group %s, of which %s is a member, could not be converted to a GID\n",
dom_sid_string(tmp_ctx, &group_sids[num_groups]),
ldb_dn_get_linearized(msg->dn)));
talloc_free(tmp_ctx);
/* We must error out, otherwise a user might
* avoid a DENY acl based on a group they
* missed out on */
return NT_STATUS_NO_SUCH_GROUP;
}
num_groups += 1;
if (num_groups == count) {
break;
}
}
*pp_sids = talloc_steal(mem_ctx, group_sids);
*pp_gids = talloc_steal(mem_ctx, gids);
*p_num_groups = num_groups;
talloc_free(tmp_ctx);
return NT_STATUS_OK;
}
static NTSTATUS pdb_samba_dsdb_set_unix_primary_group(struct pdb_methods *m,
TALLOC_CTX *mem_ctx,
struct samu *user)
{
return NT_STATUS_NOT_IMPLEMENTED;
}
static NTSTATUS pdb_samba_dsdb_mod_groupmem_by_sid(struct pdb_methods *m,
TALLOC_CTX *mem_ctx,
const struct dom_sid *groupsid,
const struct dom_sid *membersid,
int mod_op)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
struct ldb_message *msg;
int ret;
struct ldb_message_element *el;
TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
msg = ldb_msg_new(tmp_ctx);
if (msg == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
msg->dn = ldb_dn_new_fmt(msg, state->ldb, "<SID=%s>", dom_sid_string(tmp_ctx, groupsid));
if (!msg->dn || !ldb_dn_validate(msg->dn)) {
talloc_free(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
ret = ldb_msg_add_fmt(msg, "member", "<SID=%s>", dom_sid_string(tmp_ctx, membersid));
if (ret != LDB_SUCCESS) {
talloc_free(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
el = ldb_msg_find_element(msg, "member");
el->flags = mod_op;
/* No need for transactions here, the ldb auto-transaction
* code will handle things for the single operation */
ret = ldb_modify(state->ldb, msg);
talloc_free(tmp_ctx);
if (ret != LDB_SUCCESS) {
DEBUG(10, ("ldb_modify failed: %s\n",
ldb_errstring(state->ldb)));
if (ret == LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS) {
return NT_STATUS_MEMBER_IN_GROUP;
}
if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
return NT_STATUS_MEMBER_NOT_IN_GROUP;
}
return NT_STATUS_LDAP(ret);
}
return NT_STATUS_OK;
}
static NTSTATUS pdb_samba_dsdb_mod_groupmem(struct pdb_methods *m,
TALLOC_CTX *mem_ctx,
uint32_t grouprid, uint32_t memberrid,
int mod_op)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
const struct dom_sid *dom_sid, *groupsid, *membersid;
NTSTATUS status;
TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
dom_sid = samdb_domain_sid(state->ldb);
groupsid = dom_sid_add_rid(tmp_ctx, dom_sid, grouprid);
if (groupsid == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
membersid = dom_sid_add_rid(tmp_ctx, dom_sid, memberrid);
if (membersid == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
status = pdb_samba_dsdb_mod_groupmem_by_sid(m, tmp_ctx, groupsid, membersid, mod_op);
talloc_free(tmp_ctx);
return status;
}
static NTSTATUS pdb_samba_dsdb_add_groupmem(struct pdb_methods *m,
TALLOC_CTX *mem_ctx,
uint32_t group_rid, uint32_t member_rid)
{
return pdb_samba_dsdb_mod_groupmem(m, mem_ctx, group_rid, member_rid,
LDB_FLAG_MOD_ADD);
}
static NTSTATUS pdb_samba_dsdb_del_groupmem(struct pdb_methods *m,
TALLOC_CTX *mem_ctx,
uint32_t group_rid, uint32_t member_rid)
{
return pdb_samba_dsdb_mod_groupmem(m, mem_ctx, group_rid, member_rid,
LDB_FLAG_MOD_DELETE);
}
static NTSTATUS pdb_samba_dsdb_create_alias(struct pdb_methods *m,
const char *name, uint32_t *rid)
{
TALLOC_CTX *frame = talloc_stackframe();
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
struct dom_sid *sid;
struct ldb_dn *dn;
NTSTATUS status;
/* Internally this uses transactions to ensure all the steps
* happen or fail as one */
status = dsdb_add_domain_alias(state->ldb, frame, name, &sid, &dn);
if (!NT_STATUS_IS_OK(status)) {
TALLOC_FREE(frame);
}
sid_peek_rid(sid, rid);
TALLOC_FREE(frame);
return NT_STATUS_OK;
}
static NTSTATUS pdb_samba_dsdb_delete_alias(struct pdb_methods *m,
const struct dom_sid *sid)
{
const char *attrs[] = { NULL };
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
struct ldb_message *msg;
struct ldb_dn *dn;
int rc;
TALLOC_CTX *tmp_ctx = talloc_stackframe();
NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
dn = ldb_dn_new_fmt(tmp_ctx, state->ldb, "<SID=%s>", dom_sid_string(tmp_ctx, sid));
if (!dn || !ldb_dn_validate(dn)) {
talloc_free(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
if (ldb_transaction_start(state->ldb) != LDB_SUCCESS) {
DEBUG(0, ("Failed to start transaction in dsdb_add_domain_alias(): %s\n", ldb_errstring(state->ldb)));
talloc_free(tmp_ctx);
return NT_STATUS_INTERNAL_ERROR;
}
rc = dsdb_search_one(state->ldb, tmp_ctx, &msg, dn, LDB_SCOPE_BASE, attrs, 0, "(objectclass=group)"
"(|(grouptype=%d)(grouptype=%d)))",
GTYPE_SECURITY_BUILTIN_LOCAL_GROUP,
GTYPE_SECURITY_DOMAIN_LOCAL_GROUP);
if (rc == LDB_ERR_NO_SUCH_OBJECT) {
talloc_free(tmp_ctx);
ldb_transaction_cancel(state->ldb);
return NT_STATUS_NO_SUCH_ALIAS;
}
rc = ldb_delete(state->ldb, dn);
if (rc == LDB_ERR_NO_SUCH_OBJECT) {
talloc_free(tmp_ctx);
ldb_transaction_cancel(state->ldb);
return NT_STATUS_NO_SUCH_ALIAS;
} else if (rc != LDB_SUCCESS) {
DEBUG(10, ("ldb_delete failed %s\n",
ldb_errstring(state->ldb)));
ldb_transaction_cancel(state->ldb);
talloc_free(tmp_ctx);
return NT_STATUS_LDAP(rc);
}
if (ldb_transaction_commit(state->ldb) != LDB_SUCCESS) {
DEBUG(0, ("Failed to commit transaction in pdb_samba_dsdb_delete_alias(): %s\n",
ldb_errstring(state->ldb)));
talloc_free(tmp_ctx);
return NT_STATUS_INTERNAL_ERROR;
}
talloc_free(tmp_ctx);
return NT_STATUS_OK;
}
#if 0
static NTSTATUS pdb_samba_dsdb_set_aliasinfo(struct pdb_methods *m,
const struct dom_sid *sid,
struct acct_info *info)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
struct tldap_context *ld;
const char *attrs[3] = { "objectSid", "description",
"samAccountName" };
struct ldb_message **msg;
char *sidstr, *dn;
int rc;
struct tldap_mod *mods;
int num_mods;
bool ok;
ld = pdb_samba_dsdb_ld(state);
if (ld == NULL) {
return NT_STATUS_LDAP(TLDAP_SERVER_DOWN);
}
sidstr = sid_binstring(talloc_tos(), sid);
NT_STATUS_HAVE_NO_MEMORY(sidstr);
rc = pdb_samba_dsdb_search_fmt(state, state->domaindn, TLDAP_SCOPE_SUB,
attrs, ARRAY_SIZE(attrs), 0, talloc_tos(),
&msg, "(&(objectSid=%s)(objectclass=group)"
"(|(grouptype=%d)(grouptype=%d)))",
sidstr, GTYPE_SECURITY_BUILTIN_LOCAL_GROUP,
GTYPE_SECURITY_DOMAIN_LOCAL_GROUP);
TALLOC_FREE(sidstr)
if (rc != LDB_SUCCESS) {
DEBUG(10, ("ldap_search failed %s\n",
ldb_errstring(state->ldb)));
return NT_STATUS_LDAP(rc);
}
switch talloc_array_length(msg) {
case 0:
return NT_STATUS_NO_SUCH_ALIAS;
case 1:
break;
default:
return NT_STATUS_INTERNAL_DB_CORRUPTION;
}
if (!tldap_entry_dn(msg[0], &dn)) {
TALLOC_FREE(msg);
return NT_STATUS_INTERNAL_DB_CORRUPTION;
}
mods = NULL;
num_mods = 0;
ok = true;
ok &= tldap_make_mod_fmt(
msg[0], msg, &num_mods, &mods, "description",
"%s", info->acct_desc);
ok &= tldap_make_mod_fmt(
msg[0], msg, &num_mods, &mods, "samAccountName",
"%s", info->acct_name);
if (!ok) {
TALLOC_FREE(msg);
return NT_STATUS_NO_MEMORY;
}
if (num_mods == 0) {
/* no change */
TALLOC_FREE(msg);
return NT_STATUS_OK;
}
rc = tldap_modify(ld, dn, num_mods, mods, NULL, 0, NULL, 0);
TALLOC_FREE(msg);
if (rc != LDB_SUCCESS) {
DEBUG(10, ("ldap_modify failed: %s\n",
ldb_errstring(state->ldb)));
return NT_STATUS_LDAP(rc);
}
return NT_STATUS_OK;
}
#endif
static NTSTATUS pdb_samba_dsdb_add_aliasmem(struct pdb_methods *m,
const struct dom_sid *alias,
const struct dom_sid *member)
{
NTSTATUS status;
TALLOC_CTX *frame = talloc_stackframe();
status = pdb_samba_dsdb_mod_groupmem_by_sid(m, frame, alias, member, LDB_FLAG_MOD_ADD);
talloc_free(frame);
return status;
}
static NTSTATUS pdb_samba_dsdb_del_aliasmem(struct pdb_methods *m,
const struct dom_sid *alias,
const struct dom_sid *member)
{
NTSTATUS status;
TALLOC_CTX *frame = talloc_stackframe();
status = pdb_samba_dsdb_mod_groupmem_by_sid(m, frame, alias, member, LDB_FLAG_MOD_DELETE);
talloc_free(frame);
return status;
}
static NTSTATUS pdb_samba_dsdb_enum_aliasmem(struct pdb_methods *m,
const struct dom_sid *alias,
TALLOC_CTX *mem_ctx,
struct dom_sid **pmembers,
size_t *pnum_members)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
struct ldb_dn *dn;
unsigned int num_members;
NTSTATUS status;
TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
dn = ldb_dn_new_fmt(tmp_ctx, state->ldb, "<SID=%s>", dom_sid_string(tmp_ctx, alias));
if (!dn || !ldb_dn_validate(dn)) {
return NT_STATUS_NO_MEMORY;
}
status = dsdb_enum_group_mem(state->ldb, mem_ctx, dn, pmembers, &num_members);
*pnum_members = num_members;
if (NT_STATUS_IS_OK(status)) {
talloc_steal(mem_ctx, pmembers);
}
talloc_free(tmp_ctx);
return status;
}
static NTSTATUS pdb_samba_dsdb_enum_alias_memberships(struct pdb_methods *m,
TALLOC_CTX *mem_ctx,
const struct dom_sid *domain_sid,
const struct dom_sid *members,
size_t num_members,
uint32_t **palias_rids,
size_t *pnum_alias_rids)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
uint32_t *alias_rids = NULL;
size_t num_alias_rids = 0;
int i;
struct dom_sid *groupSIDs = NULL;
unsigned int num_groupSIDs = 0;
char *filter;
NTSTATUS status;
const char *sid_string;
const char *sid_dn;
DATA_BLOB sid_blob;
TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
/*
* TODO: Get the filter right so that we only get the aliases from
* either the SAM or BUILTIN
*/
filter = talloc_asprintf(tmp_ctx, "(&(objectClass=group)(groupType:1.2.840.113556.1.4.803:=%u))",
GROUP_TYPE_BUILTIN_LOCAL_GROUP);
if (filter == NULL) {
return NT_STATUS_NO_MEMORY;
}
for (i = 0; i < num_members; i++) {
sid_string = dom_sid_string(tmp_ctx, &members[i]);
if (sid_string == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
sid_dn = talloc_asprintf(tmp_ctx, "<SID=%s>", sid_string);
if (sid_dn == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
sid_blob = data_blob_string_const(sid_dn);
status = dsdb_expand_nested_groups(state->ldb, &sid_blob, true, filter,
tmp_ctx, &groupSIDs, &num_groupSIDs);
if (!NT_STATUS_IS_OK(status)) {
talloc_free(tmp_ctx);
return status;
}
}
alias_rids = talloc_array(mem_ctx, uint32_t, num_groupSIDs);
if (alias_rids == NULL) {
talloc_free(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
for (i=0; i<num_groupSIDs; i++) {
if (sid_peek_check_rid(domain_sid, &groupSIDs[i],
&alias_rids[num_alias_rids])) {
num_alias_rids++;;
}
}
*palias_rids = alias_rids;
*pnum_alias_rids = num_alias_rids;
return NT_STATUS_OK;
}
static NTSTATUS pdb_samba_dsdb_lookup_rids(struct pdb_methods *m,
const struct dom_sid *domain_sid,
int num_rids,
uint32_t *rids,
const char **names,
enum lsa_SidType *lsa_attrs)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
NTSTATUS status;
TALLOC_CTX *tmp_ctx;
if (num_rids == 0) {
return NT_STATUS_NONE_MAPPED;
}
tmp_ctx = talloc_stackframe();
NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
status = dsdb_lookup_rids(state->ldb, tmp_ctx, domain_sid, num_rids, rids, names, lsa_attrs);
talloc_free(tmp_ctx);
return status;
}
static NTSTATUS pdb_samba_dsdb_lookup_names(struct pdb_methods *m,
const struct dom_sid *domain_sid,
int num_names,
const char **pp_names,
uint32_t *rids,
enum lsa_SidType *attrs)
{
return NT_STATUS_NOT_IMPLEMENTED;
}
static NTSTATUS pdb_samba_dsdb_get_account_policy(struct pdb_methods *m,
enum pdb_policy_type type,
uint32_t *value)
{
return account_policy_get(type, value)
? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
}
static NTSTATUS pdb_samba_dsdb_set_account_policy(struct pdb_methods *m,
enum pdb_policy_type type,
uint32_t value)
{
return account_policy_set(type, value)
? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
}
static NTSTATUS pdb_samba_dsdb_get_seq_num(struct pdb_methods *m,
time_t *seq_num_out)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
uint64_t seq_num;
int ret = ldb_sequence_number(state->ldb, LDB_SEQ_HIGHEST_SEQ, &seq_num);
if (ret == LDB_SUCCESS) {
*seq_num_out = seq_num;
return NT_STATUS_OK;
} else {
return NT_STATUS_UNSUCCESSFUL;
}
}
struct pdb_samba_dsdb_search_state {
uint32_t acct_flags;
struct samr_displayentry *entries;
uint32_t num_entries;
ssize_t array_size;
uint32_t current;
};
static bool pdb_samba_dsdb_next_entry(struct pdb_search *search,
struct samr_displayentry *entry)
{
struct pdb_samba_dsdb_search_state *state = talloc_get_type_abort(
search->private_data, struct pdb_samba_dsdb_search_state);
if (state->current == state->num_entries) {
return false;
}
entry->idx = state->entries[state->current].idx;
entry->rid = state->entries[state->current].rid;
entry->acct_flags = state->entries[state->current].acct_flags;
entry->account_name = talloc_strdup(
search, state->entries[state->current].account_name);
entry->fullname = talloc_strdup(
search, state->entries[state->current].fullname);
entry->description = talloc_strdup(
search, state->entries[state->current].description);
state->current += 1;
return true;
}
static void pdb_samba_dsdb_search_end(struct pdb_search *search)
{
struct pdb_samba_dsdb_search_state *state = talloc_get_type_abort(
search->private_data, struct pdb_samba_dsdb_search_state);
talloc_free(state);
}
static bool pdb_samba_dsdb_search_filter(struct pdb_methods *m,
struct pdb_search *search,
struct pdb_samba_dsdb_search_state **pstate,
const char *exp_fmt, ...)
PRINTF_ATTRIBUTE(4, 5);
static bool pdb_samba_dsdb_search_filter(struct pdb_methods *m,
struct pdb_search *search,
struct pdb_samba_dsdb_search_state **pstate,
const char *exp_fmt, ...)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
struct pdb_samba_dsdb_search_state *sstate;
const char * attrs[] = { "objectSid", "sAMAccountName", "displayName",
"userAccountControl", "description", NULL };
struct ldb_result *res;
int i, rc, num_users;
va_list ap;
char *expression = NULL;
TALLOC_CTX *tmp_ctx = talloc_stackframe();
if (!tmp_ctx) {
return false;
}
va_start(ap, exp_fmt);
expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
va_end(ap);
if (!expression) {
talloc_free(tmp_ctx);
return LDB_ERR_OPERATIONS_ERROR;
}
sstate = talloc_zero(tmp_ctx, struct pdb_samba_dsdb_search_state);
if (sstate == NULL) {
talloc_free(tmp_ctx);
return false;
}
rc = dsdb_search(state->ldb, tmp_ctx, &res, ldb_get_default_basedn(state->ldb), LDB_SCOPE_SUBTREE, attrs, 0, "%s", expression);
if (rc != LDB_SUCCESS) {
talloc_free(tmp_ctx);
DEBUG(10, ("dsdb_search failed: %s\n",
ldb_errstring(state->ldb)));
return false;
}
num_users = res->count;
sstate->entries = talloc_array(sstate, struct samr_displayentry,
num_users);
if (sstate->entries == NULL) {
talloc_free(tmp_ctx);
DEBUG(10, ("talloc failed\n"));
return false;
}
sstate->num_entries = 0;
for (i=0; i<num_users; i++) {
struct samr_displayentry *e;
struct dom_sid *sid;
e = &sstate->entries[sstate->num_entries];
e->idx = sstate->num_entries;
sid = samdb_result_dom_sid(tmp_ctx, res->msgs[i], "objectSid");
if (!sid) {
talloc_free(tmp_ctx);
DEBUG(10, ("Could not pull SID\n"));
return false;
}
sid_peek_rid(sid, &e->rid);
e->acct_flags = samdb_result_acct_flags(res->msgs[i], "userAccountControl");
e->account_name = ldb_msg_find_attr_as_string(
res->msgs[i], "samAccountName", NULL);
if (e->account_name == NULL) {
talloc_free(tmp_ctx);
return false;
}
e->fullname = ldb_msg_find_attr_as_string(
res->msgs[i], "displayName", "");
e->description = ldb_msg_find_attr_as_string(
res->msgs[i], "description", "");
sstate->num_entries += 1;
if (sstate->num_entries >= num_users) {
break;
}
}
talloc_steal(sstate->entries, res->msgs);
search->private_data = talloc_steal(search, sstate);
search->next_entry = pdb_samba_dsdb_next_entry;
search->search_end = pdb_samba_dsdb_search_end;
*pstate = sstate;
talloc_free(tmp_ctx);
return true;
}
static bool pdb_samba_dsdb_search_users(struct pdb_methods *m,
struct pdb_search *search,
uint32_t acct_flags)
{
struct pdb_samba_dsdb_search_state *sstate;
bool ret;
ret = pdb_samba_dsdb_search_filter(m, search, &sstate, "(objectclass=user)");
if (!ret) {
return false;
}
sstate->acct_flags = acct_flags;
return true;
}
static bool pdb_samba_dsdb_search_groups(struct pdb_methods *m,
struct pdb_search *search)
{
struct pdb_samba_dsdb_search_state *sstate;
bool ret;
ret = pdb_samba_dsdb_search_filter(m, search, &sstate,
"(&(grouptype=%d)(objectclass=group))",
GTYPE_SECURITY_GLOBAL_GROUP);
if (!ret) {
return false;
}
sstate->acct_flags = 0;
return true;
}
static bool pdb_samba_dsdb_search_aliases(struct pdb_methods *m,
struct pdb_search *search,
const struct dom_sid *sid)
{
struct pdb_samba_dsdb_search_state *sstate;
bool ret;
ret = pdb_samba_dsdb_search_filter(m, search, &sstate,
"(&(grouptype=%d)(objectclass=group))",
sid_check_is_builtin(sid)
? GTYPE_SECURITY_BUILTIN_LOCAL_GROUP
: GTYPE_SECURITY_DOMAIN_LOCAL_GROUP);
if (!ret) {
return false;
}
sstate->acct_flags = 0;
return true;
}
/*
* Instead of taking a gid or uid, this function takes a pointer to a
* unixid.
*
* This acts as an in-out variable so that the idmap functions can correctly
* receive ID_TYPE_BOTH, and this function ensures cache details are filled
* correctly rather than forcing the cache to store ID_TYPE_UID or ID_TYPE_GID.
*/
static bool pdb_samba_dsdb_id_to_sid(struct pdb_methods *m, struct unixid *id,
struct dom_sid *sid)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
NTSTATUS status;
struct id_map id_map;
struct id_map *id_maps[2];
TALLOC_CTX *tmp_ctx = talloc_stackframe();
if (!tmp_ctx) {
return false;
}
id_map.xid = *id;
id_maps[0] = &id_map;
id_maps[1] = NULL;
status = idmap_xids_to_sids(state->idmap_ctx, tmp_ctx, id_maps);
if (!NT_STATUS_IS_OK(status)) {
talloc_free(tmp_ctx);
return false;
}
if (id_map.xid.type != ID_TYPE_NOT_SPECIFIED) {
id->type = id_map.xid.type;
}
*sid = *id_map.sid;
talloc_free(tmp_ctx);
return true;
}
static bool pdb_samba_dsdb_sid_to_id(struct pdb_methods *m, const struct dom_sid *sid,
struct unixid *id)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
struct id_map id_map;
struct id_map *id_maps[2];
NTSTATUS status;
TALLOC_CTX *tmp_ctx = talloc_stackframe();
if (!tmp_ctx) {
return false;
}
ZERO_STRUCT(id_map);
id_map.sid = discard_const_p(struct dom_sid, sid);
id_maps[0] = &id_map;
id_maps[1] = NULL;
status = idmap_sids_to_xids(state->idmap_ctx, tmp_ctx, id_maps);
talloc_free(tmp_ctx);
if (!NT_STATUS_IS_OK(status)) {
return false;
}
if (id_map.xid.type != ID_TYPE_NOT_SPECIFIED) {
*id = id_map.xid;
return true;
}
return false;
}
static uint32_t pdb_samba_dsdb_capabilities(struct pdb_methods *m)
{
return PDB_CAP_STORE_RIDS | PDB_CAP_ADS | PDB_CAP_TRUSTED_DOMAINS_EX;
}
static bool pdb_samba_dsdb_new_rid(struct pdb_methods *m, uint32_t *rid)
{
return false;
}
static bool pdb_samba_dsdb_get_trusteddom_pw(struct pdb_methods *m,
const char *domain, char** pwd,
struct dom_sid *sid,
time_t *pass_last_set_time)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
TALLOC_CTX *tmp_ctx = talloc_stackframe();
const char * const attrs[] = {
"securityIdentifier",
"flatName",
"trustPartner",
"trustAuthOutgoing",
"whenCreated",
"msDS-SupportedEncryptionTypes",
"trustAttributes",
"trustDirection",
"trustType",
NULL
};
struct ldb_message *msg;
const struct ldb_val *password_val;
int trust_direction_flags;
int trust_type;
int i;
DATA_BLOB password_utf16;
struct trustAuthInOutBlob password_blob;
struct AuthenticationInformationArray *auth_array;
char *password_talloc;
size_t password_len;
enum ndr_err_code ndr_err;
NTSTATUS status;
const char *netbios_domain = NULL;
const struct dom_sid *domain_sid = NULL;
status = dsdb_trust_search_tdo(state->ldb, domain, NULL,
attrs, tmp_ctx, &msg);
if (!NT_STATUS_IS_OK(status)) {
/*
* This can be called to work out of a domain is
* trusted, rather than just to get the password
*/
DEBUG(2, ("Failed to get trusted domain password for %s - %s. "
"It may not be a trusted domain.\n", domain,
nt_errstr(status)));
TALLOC_FREE(tmp_ctx);
return false;
}
netbios_domain = ldb_msg_find_attr_as_string(msg, "flatName", NULL);
if (netbios_domain == NULL) {
DEBUG(2, ("Trusted domain %s has to flatName defined.\n",
domain));
TALLOC_FREE(tmp_ctx);
return false;
}
domain_sid = samdb_result_dom_sid(tmp_ctx, msg, "securityIdentifier");
if (domain_sid == NULL) {
DEBUG(2, ("Trusted domain %s has no securityIdentifier defined.\n",
domain));
TALLOC_FREE(tmp_ctx);
return false;
}
trust_direction_flags = ldb_msg_find_attr_as_int(msg, "trustDirection", 0);
if (!(trust_direction_flags & LSA_TRUST_DIRECTION_OUTBOUND)) {
DEBUG(2, ("Trusted domain %s is is not an outbound trust.\n",
domain));
TALLOC_FREE(tmp_ctx);
return false;
}
trust_type = ldb_msg_find_attr_as_int(msg, "trustType", 0);
if (trust_type == LSA_TRUST_TYPE_MIT) {
DEBUG(1, ("Trusted domain %s is is not an AD trust "
"(trustType == LSA_TRUST_TYPE_MIT).\n",
domain));
TALLOC_FREE(tmp_ctx);
return false;
}
password_val = ldb_msg_find_ldb_val(msg, "trustAuthOutgoing");
if (password_val == NULL) {
DEBUG(2, ("Failed to get trusted domain password for %s, "
"attribute trustAuthOutgoing not returned.\n", domain));
TALLOC_FREE(tmp_ctx);
return false;
}
ndr_err = ndr_pull_struct_blob(password_val, tmp_ctx, &password_blob,
(ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
DEBUG(0, ("Failed to get trusted domain password for %s, "
"attribute trustAuthOutgoing could not be parsed %s.\n",
domain,
ndr_map_error2string(ndr_err)));
TALLOC_FREE(tmp_ctx);
return false;
}
auth_array = &password_blob.current;
for (i=0; i < auth_array->count; i++) {
if (auth_array->array[i].AuthType == TRUST_AUTH_TYPE_CLEAR) {
break;
}
}
if (i == auth_array->count) {
DEBUG(0, ("Trusted domain %s does not have a "
"clear-text password stored\n",
domain));
TALLOC_FREE(tmp_ctx);
return false;
}
password_utf16 = data_blob_const(auth_array->array[i].AuthInfo.clear.password,
auth_array->array[i].AuthInfo.clear.size);
/*
* In the future, make this function return a
* cli_credentials that can store a MD4 hash with cli_credential_set_nt_hash()
* but for now convert to UTF8 and fail if the string can not be converted.
*
* We can't safely convert the random strings windows uses into
* utf8.
*/
if (!convert_string_talloc(tmp_ctx,
CH_UTF16MUNGED, CH_UTF8,
password_utf16.data, password_utf16.length,
(void *)&password_talloc,
&password_len)) {
DEBUG(0, ("FIXME: Could not convert password for trusted domain %s"
" to UTF8. This may be a password set from Windows.\n",
domain));
TALLOC_FREE(tmp_ctx);
return false;
}
*pwd = SMB_STRNDUP(password_talloc, password_len);
if (pass_last_set_time) {
*pass_last_set_time = nt_time_to_unix(auth_array->array[i].LastUpdateTime);
}
if (sid != NULL) {
sid_copy(sid, domain_sid);
}
TALLOC_FREE(tmp_ctx);
return true;
}
static NTSTATUS pdb_samba_dsdb_get_trusteddom_creds(struct pdb_methods *m,
const char *domain,
TALLOC_CTX *mem_ctx,
struct cli_credentials **_creds)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
TALLOC_CTX *tmp_ctx = talloc_stackframe();
const char * const attrs[] = {
"securityIdentifier",
"flatName",
"trustPartner",
"trustAuthOutgoing",
"whenCreated",
"msDS-SupportedEncryptionTypes",
"trustAttributes",
"trustDirection",
"trustType",
NULL
};
struct ldb_message *msg;
const struct ldb_val *password_val;
int trust_direction_flags;
int trust_type;
int i;
DATA_BLOB password_utf16 = {};
struct samr_Password *password_nt = NULL;
uint32_t password_version = 0;
DATA_BLOB old_password_utf16 = {};
struct samr_Password *old_password_nt = NULL;
struct trustAuthInOutBlob password_blob;
enum ndr_err_code ndr_err;
NTSTATUS status;
time_t last_set_time = 0;
struct cli_credentials *creds = NULL;
bool ok;
const char *my_netbios_name = NULL;
const char *my_netbios_domain = NULL;
const char *my_dns_domain = NULL;
const char *netbios_domain = NULL;
char *account_name = NULL;
char *principal_name = NULL;
const char *dns_domain = NULL;
status = dsdb_trust_search_tdo(state->ldb, domain, NULL,
attrs, tmp_ctx, &msg);
if (!NT_STATUS_IS_OK(status)) {
/*
* This can be called to work out of a domain is
* trusted, rather than just to get the password
*/
DEBUG(2, ("Failed to get trusted domain password for %s - %s "
"It may not be a trusted domain.\n", domain,
nt_errstr(status)));
TALLOC_FREE(tmp_ctx);
return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
}
netbios_domain = ldb_msg_find_attr_as_string(msg, "flatName", NULL);
if (netbios_domain == NULL) {
DEBUG(2, ("Trusted domain %s has to flatName defined.\n",
domain));
TALLOC_FREE(tmp_ctx);
return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
}
dns_domain = ldb_msg_find_attr_as_string(msg, "trustPartner", NULL);
trust_direction_flags = ldb_msg_find_attr_as_int(msg, "trustDirection", 0);
if (!(trust_direction_flags & LSA_TRUST_DIRECTION_OUTBOUND)) {
DEBUG(2, ("Trusted domain %s is is not an outbound trust.\n",
domain));
TALLOC_FREE(tmp_ctx);
return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
}
trust_type = ldb_msg_find_attr_as_int(msg, "trustType", 0);
if (trust_type == LSA_TRUST_TYPE_MIT) {
DEBUG(1, ("Trusted domain %s is is not an AD trust "
"(trustType == LSA_TRUST_TYPE_MIT).\n",
domain));
TALLOC_FREE(tmp_ctx);
return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
}
password_val = ldb_msg_find_ldb_val(msg, "trustAuthOutgoing");
if (password_val == NULL) {
DEBUG(2, ("Failed to get trusted domain password for %s, "
"attribute trustAuthOutgoing not returned.\n", domain));
TALLOC_FREE(tmp_ctx);
return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
}
ndr_err = ndr_pull_struct_blob(password_val, tmp_ctx, &password_blob,
(ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
DEBUG(0, ("Failed to get trusted domain password for %s, "
"attribute trustAuthOutgoing could not be parsed %s.\n",
domain,
ndr_map_error2string(ndr_err)));
TALLOC_FREE(tmp_ctx);
return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
}
for (i=0; i < password_blob.current.count; i++) {
struct AuthenticationInformation *a =
&password_blob.current.array[i];
switch (a->AuthType) {
case TRUST_AUTH_TYPE_NONE:
break;
case TRUST_AUTH_TYPE_VERSION:
password_version = a->AuthInfo.version.version;
break;
case TRUST_AUTH_TYPE_CLEAR:
last_set_time = nt_time_to_unix(a->LastUpdateTime);
password_utf16 = data_blob_const(a->AuthInfo.clear.password,
a->AuthInfo.clear.size);
password_nt = NULL;
break;
case TRUST_AUTH_TYPE_NT4OWF:
if (password_utf16.length != 0) {
break;
}
last_set_time = nt_time_to_unix(a->LastUpdateTime);
password_nt = &a->AuthInfo.nt4owf.password;
break;
}
}
for (i=0; i < password_blob.previous.count; i++) {
struct AuthenticationInformation *a = &password_blob.previous.array[i];
switch (a->AuthType) {
case TRUST_AUTH_TYPE_NONE:
break;
case TRUST_AUTH_TYPE_VERSION:
break;
case TRUST_AUTH_TYPE_CLEAR:
old_password_utf16 = data_blob_const(a->AuthInfo.clear.password,
a->AuthInfo.clear.size);
old_password_nt = NULL;
break;
case TRUST_AUTH_TYPE_NT4OWF:
if (old_password_utf16.length != 0) {
break;
}
old_password_nt = &a->AuthInfo.nt4owf.password;
break;
}
}
if (password_utf16.length == 0 && password_nt == NULL) {
DEBUG(0, ("Trusted domain %s does not have a "
"clear-text nor nt password stored\n",
domain));
TALLOC_FREE(tmp_ctx);
return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
}
my_netbios_name = lpcfg_netbios_name(state->lp_ctx);
my_netbios_domain = lpcfg_workgroup(state->lp_ctx);
my_dns_domain = lpcfg_dnsdomain(state->lp_ctx);
creds = cli_credentials_init(tmp_ctx);
if (creds == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
ok = cli_credentials_set_workstation(creds, my_netbios_name, CRED_SPECIFIED);
if (!ok) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
ok = cli_credentials_set_domain(creds, netbios_domain, CRED_SPECIFIED);
if (!ok) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
ok = cli_credentials_set_realm(creds, dns_domain, CRED_SPECIFIED);
if (!ok) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
if (my_dns_domain != NULL && dns_domain != NULL) {
cli_credentials_set_secure_channel_type(creds, SEC_CHAN_DNS_DOMAIN);
account_name = talloc_asprintf(tmp_ctx, "%s.", my_dns_domain);
if (account_name == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
principal_name = talloc_asprintf(tmp_ctx, "%s$@%s", my_netbios_domain,
cli_credentials_get_realm(creds));
if (principal_name == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
} else {
cli_credentials_set_secure_channel_type(creds, SEC_CHAN_DOMAIN);
account_name = talloc_asprintf(tmp_ctx, "%s$", my_netbios_domain);
if (account_name == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
principal_name = NULL;
}
ok = cli_credentials_set_username(creds, account_name, CRED_SPECIFIED);
if (!ok) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
if (principal_name != NULL) {
ok = cli_credentials_set_principal(creds, principal_name,
CRED_SPECIFIED);
if (!ok) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
}
if (old_password_nt != NULL) {
ok = cli_credentials_set_old_nt_hash(creds, old_password_nt);
if (!ok) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
}
if (old_password_utf16.length > 0) {
ok = cli_credentials_set_old_utf16_password(creds,
&old_password_utf16);
if (!ok) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
}
if (password_nt != NULL) {
ok = cli_credentials_set_nt_hash(creds, password_nt,
CRED_SPECIFIED);
if (!ok) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
}
if (password_utf16.length > 0) {
ok = cli_credentials_set_utf16_password(creds,
&password_utf16,
CRED_SPECIFIED);
if (!ok) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
}
cli_credentials_set_password_last_changed_time(creds, last_set_time);
cli_credentials_set_kvno(creds, password_version);
if (password_utf16.length > 0 && dns_domain != NULL) {
/*
* Force kerberos if this is an active directory domain
*/
cli_credentials_set_kerberos_state(creds,
CRED_MUST_USE_KERBEROS);
} else {
/*
* TODO: we should allow krb5 with the raw nt hash.
*/
cli_credentials_set_kerberos_state(creds,
CRED_DONT_USE_KERBEROS);
}
*_creds = talloc_move(mem_ctx, &creds);
TALLOC_FREE(tmp_ctx);
return NT_STATUS_OK;
}
static bool pdb_samba_dsdb_set_trusteddom_pw(struct pdb_methods *m,
const char* domain, const char* pwd,
const struct dom_sid *sid)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
TALLOC_CTX *tmp_ctx = talloc_stackframe();
const char * const attrs[] = {
"trustAuthOutgoing",
"trustDirection",
"trustType",
NULL
};
struct ldb_message *msg = NULL;
int trust_direction_flags;
int trust_type;
uint32_t i; /* The same type as old_blob.current.count */
const struct ldb_val *old_val = NULL;
struct trustAuthInOutBlob old_blob = {};
uint32_t old_version = 0;
uint32_t new_version = 0;
DATA_BLOB new_utf16 = {};
struct trustAuthInOutBlob new_blob = {};
struct ldb_val new_val = {};
struct timeval tv = timeval_current();
NTTIME now = timeval_to_nttime(&tv);
enum ndr_err_code ndr_err;
NTSTATUS status;
bool ok;
int ret;
ret = ldb_transaction_start(state->ldb);
if (ret != LDB_SUCCESS) {
DEBUG(2, ("Failed to start transaction.\n"));
TALLOC_FREE(tmp_ctx);
return false;
}
ok = samdb_is_pdc(state->ldb);
if (!ok) {
DEBUG(2, ("Password changes for domain %s are only allowed on a PDC.\n",
domain));
TALLOC_FREE(tmp_ctx);
ldb_transaction_cancel(state->ldb);
return false;
}
status = dsdb_trust_search_tdo(state->ldb, domain, NULL,
attrs, tmp_ctx, &msg);
if (!NT_STATUS_IS_OK(status)) {
/*
* This can be called to work out of a domain is
* trusted, rather than just to get the password
*/
DEBUG(2, ("Failed to get trusted domain password for %s - %s. "
"It may not be a trusted domain.\n", domain,
nt_errstr(status)));
TALLOC_FREE(tmp_ctx);
ldb_transaction_cancel(state->ldb);
return false;
}
trust_direction_flags = ldb_msg_find_attr_as_int(msg, "trustDirection", 0);
if (!(trust_direction_flags & LSA_TRUST_DIRECTION_OUTBOUND)) {
DEBUG(2, ("Trusted domain %s is is not an outbound trust, can't set a password.\n",
domain));
TALLOC_FREE(tmp_ctx);
ldb_transaction_cancel(state->ldb);
return false;
}
trust_type = ldb_msg_find_attr_as_int(msg, "trustType", 0);
switch (trust_type) {
case LSA_TRUST_TYPE_DOWNLEVEL:
case LSA_TRUST_TYPE_UPLEVEL:
break;
default:
DEBUG(0, ("Trusted domain %s is of type 0x%X - "
"password changes are not supported\n",
domain, (unsigned)trust_type));
TALLOC_FREE(tmp_ctx);
ldb_transaction_cancel(state->ldb);
return false;
}
old_val = ldb_msg_find_ldb_val(msg, "trustAuthOutgoing");
if (old_val != NULL) {
ndr_err = ndr_pull_struct_blob(old_val, tmp_ctx, &old_blob,
(ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
DEBUG(0, ("Failed to get trusted domain password for %s, "
"attribute trustAuthOutgoing could not be parsed %s.\n",
domain,
ndr_map_error2string(ndr_err)));
TALLOC_FREE(tmp_ctx);
ldb_transaction_cancel(state->ldb);
return false;
}
}
for (i=0; i < old_blob.current.count; i++) {
struct AuthenticationInformation *a =
&old_blob.current.array[i];
switch (a->AuthType) {
case TRUST_AUTH_TYPE_NONE:
break;
case TRUST_AUTH_TYPE_VERSION:
old_version = a->AuthInfo.version.version;
break;
case TRUST_AUTH_TYPE_CLEAR:
break;
case TRUST_AUTH_TYPE_NT4OWF:
break;
}
}
new_version = old_version + 1;
ok = convert_string_talloc(tmp_ctx,
CH_UNIX, CH_UTF16,
pwd, strlen(pwd),
(void *)&new_utf16.data,
&new_utf16.length);
if (!ok) {
DEBUG(0, ("Failed to generate new_utf16 password for domain %s\n",
domain));
TALLOC_FREE(tmp_ctx);
ldb_transaction_cancel(state->ldb);
return false;
}
if (new_utf16.length < 28) {
DEBUG(0, ("new_utf16[%zu] version[%u] for domain %s to short.\n",
new_utf16.length,
(unsigned)new_version,
domain));
TALLOC_FREE(tmp_ctx);
ldb_transaction_cancel(state->ldb);
return false;
}
if (new_utf16.length > 498) {
DEBUG(0, ("new_utf16[%zu] version[%u] for domain %s to long.\n",
new_utf16.length,
(unsigned)new_version,
domain));
TALLOC_FREE(tmp_ctx);
ldb_transaction_cancel(state->ldb);
return false;
}
new_blob.count = MAX(old_blob.current.count, 2);
new_blob.current.array = talloc_zero_array(tmp_ctx,
struct AuthenticationInformation,
new_blob.count);
if (new_blob.current.array == NULL) {
DEBUG(0, ("talloc_zero_array(%u) failed\n",
(unsigned)new_blob.count));
TALLOC_FREE(tmp_ctx);
ldb_transaction_cancel(state->ldb);
return false;
}
new_blob.previous.array = talloc_zero_array(tmp_ctx,
struct AuthenticationInformation,
new_blob.count);
if (new_blob.current.array == NULL) {
DEBUG(0, ("talloc_zero_array(%u) failed\n",
(unsigned)new_blob.count));
TALLOC_FREE(tmp_ctx);
ldb_transaction_cancel(state->ldb);
return false;
}
for (i = 0; i < old_blob.current.count; i++) {
struct AuthenticationInformation *o =
&old_blob.current.array[i];
struct AuthenticationInformation *p =
&new_blob.previous.array[i];
*p = *o;
new_blob.previous.count++;
}
for (; i < new_blob.count; i++) {
struct AuthenticationInformation *pi =
&new_blob.previous.array[i];
if (i == 0) {
/*
* new_blob.previous is still empty so
* we'll do new_blob.previous = new_blob.current
* below.
*/
break;
}
pi->LastUpdateTime = now;
pi->AuthType = TRUST_AUTH_TYPE_NONE;
new_blob.previous.count++;
}
for (i = 0; i < new_blob.count; i++) {
struct AuthenticationInformation *ci =
&new_blob.current.array[i];
ci->LastUpdateTime = now;
switch (i) {
case 0:
ci->AuthType = TRUST_AUTH_TYPE_CLEAR;
ci->AuthInfo.clear.size = new_utf16.length;
ci->AuthInfo.clear.password = new_utf16.data;
break;
case 1:
ci->AuthType = TRUST_AUTH_TYPE_VERSION;
ci->AuthInfo.version.version = new_version;
break;
default:
ci->AuthType = TRUST_AUTH_TYPE_NONE;
break;
}
new_blob.current.count++;
}
if (new_blob.previous.count == 0) {
TALLOC_FREE(new_blob.previous.array);
new_blob.previous = new_blob.current;
}
ndr_err = ndr_push_struct_blob(&new_val, tmp_ctx, &new_blob,
(ndr_push_flags_fn_t)ndr_push_trustAuthInOutBlob);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
DEBUG(0, ("Failed to generate trustAuthOutgoing for "
"trusted domain password for %s: %s.\n",
domain, ndr_map_error2string(ndr_err)));
TALLOC_FREE(tmp_ctx);
ldb_transaction_cancel(state->ldb);
return false;
}
msg->num_elements = 0;
ret = ldb_msg_add_empty(msg, "trustAuthOutgoing",
LDB_FLAG_MOD_REPLACE, NULL);
if (ret != LDB_SUCCESS) {
DEBUG(0, ("ldb_msg_add_empty() failed\n"));
TALLOC_FREE(tmp_ctx);
ldb_transaction_cancel(state->ldb);
return false;
}
ret = ldb_msg_add_value(msg, "trustAuthOutgoing",
&new_val, NULL);
if (ret != LDB_SUCCESS) {
DEBUG(0, ("ldb_msg_add_value() failed\n"));
TALLOC_FREE(tmp_ctx);
ldb_transaction_cancel(state->ldb);
return false;
}
ret = ldb_modify(state->ldb, msg);
if (ret != LDB_SUCCESS) {
DEBUG(0, ("Failed to replace trustAuthOutgoing for "
"trusted domain password for %s: %s - %s\n",
domain, ldb_strerror(ret), ldb_errstring(state->ldb)));
TALLOC_FREE(tmp_ctx);
ldb_transaction_cancel(state->ldb);
return false;
}
ret = ldb_transaction_commit(state->ldb);
if (ret != LDB_SUCCESS) {
DEBUG(0, ("Failed to commit trustAuthOutgoing for "
"trusted domain password for %s: %s - %s\n",
domain, ldb_strerror(ret), ldb_errstring(state->ldb)));
TALLOC_FREE(tmp_ctx);
return false;
}
DEBUG(1, ("Added new_version[%u] to trustAuthOutgoing for "
"trusted domain password for %s.\n",
(unsigned)new_version, domain));
TALLOC_FREE(tmp_ctx);
return true;
}
static bool pdb_samba_dsdb_del_trusteddom_pw(struct pdb_methods *m,
const char *domain)
{
return false;
}
static NTSTATUS pdb_samba_dsdb_enum_trusteddoms(struct pdb_methods *m,
TALLOC_CTX *mem_ctx,
uint32_t *_num_domains,
struct trustdom_info ***_domains)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
TALLOC_CTX *tmp_ctx = talloc_stackframe();
const char * const attrs[] = {
"securityIdentifier",
"flatName",
"trustDirection",
NULL
};
struct ldb_result *res = NULL;
unsigned int i;
struct trustdom_info **domains = NULL;
NTSTATUS status;
uint32_t di = 0;
*_num_domains = 0;
*_domains = NULL;
status = dsdb_trust_search_tdos(state->ldb, NULL,
attrs, tmp_ctx, &res);
if (!NT_STATUS_IS_OK(status)) {
DBG_ERR("dsdb_trust_search_tdos() - %s ", nt_errstr(status));
TALLOC_FREE(tmp_ctx);
return status;
}
if (res->count == 0) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_OK;
}
domains = talloc_zero_array(tmp_ctx, struct trustdom_info *,
res->count);
if (domains == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
for (i = 0; i < res->count; i++) {
struct ldb_message *msg = res->msgs[i];
struct trustdom_info *d = NULL;
const char *name = NULL;
struct dom_sid *sid = NULL;
uint32_t direction;
d = talloc_zero(domains, struct trustdom_info);
if (d == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
name = ldb_msg_find_attr_as_string(msg, "flatName", NULL);
if (name == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_INTERNAL_DB_CORRUPTION;
}
sid = samdb_result_dom_sid(msg, msg, "securityIdentifier");
if (sid == NULL) {
continue;
}
direction = ldb_msg_find_attr_as_uint(msg, "trustDirection", 0);
if (!(direction & LSA_TRUST_DIRECTION_OUTBOUND)) {
continue;
}
d->name = talloc_strdup(d, name);
if (d->name == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
d->sid = *sid;
domains[di++] = d;
}
domains = talloc_realloc(domains, domains, struct trustdom_info *, di);
*_domains = talloc_move(mem_ctx, &domains);
*_num_domains = di;
TALLOC_FREE(tmp_ctx);
return NT_STATUS_OK;
}
static NTSTATUS pdb_samba_dsdb_msg_to_trusted_domain(const struct ldb_message *msg,
TALLOC_CTX *mem_ctx,
struct pdb_trusted_domain **_d)
{
struct pdb_trusted_domain *d = NULL;
const char *str = NULL;
struct dom_sid *sid = NULL;
const struct ldb_val *val = NULL;
uint64_t val64;
*_d = NULL;
d = talloc_zero(mem_ctx, struct pdb_trusted_domain);
if (d == NULL) {
return NT_STATUS_NO_MEMORY;
}
str = ldb_msg_find_attr_as_string(msg, "flatName", NULL);
if (str == NULL) {
TALLOC_FREE(d);
return NT_STATUS_INTERNAL_DB_CORRUPTION;
}
d->netbios_name = talloc_strdup(d, str);
if (d->netbios_name == NULL) {
TALLOC_FREE(d);
return NT_STATUS_NO_MEMORY;
}
str = ldb_msg_find_attr_as_string(msg, "trustPartner", NULL);
if (str != NULL) {
d->domain_name = talloc_strdup(d, str);
if (d->domain_name == NULL) {
TALLOC_FREE(d);
return NT_STATUS_NO_MEMORY;
}
}
sid = samdb_result_dom_sid(d, msg, "securityIdentifier");
if (sid != NULL) {
d->security_identifier = *sid;
TALLOC_FREE(sid);
}
val = ldb_msg_find_ldb_val(msg, "trustAuthOutgoing");
if (val != NULL) {
d->trust_auth_outgoing = data_blob_dup_talloc(d, *val);
if (d->trust_auth_outgoing.data == NULL) {
TALLOC_FREE(d);
return NT_STATUS_NO_MEMORY;
}
}
val = ldb_msg_find_ldb_val(msg, "trustAuthIncoming");
if (val != NULL) {
d->trust_auth_incoming = data_blob_dup_talloc(d, *val);
if (d->trust_auth_incoming.data == NULL) {
TALLOC_FREE(d);
return NT_STATUS_NO_MEMORY;
}
}
d->trust_direction = ldb_msg_find_attr_as_uint(msg, "trustDirection", 0);
d->trust_type = ldb_msg_find_attr_as_uint(msg, "trustType", 0);
d->trust_attributes = ldb_msg_find_attr_as_uint(msg, "trustAttributes", 0);
val64 = ldb_msg_find_attr_as_uint64(msg, "trustPosixOffset", UINT64_MAX);
if (val64 != UINT64_MAX) {
d->trust_posix_offset = talloc(d, uint32_t);
if (d->trust_posix_offset == NULL) {
TALLOC_FREE(d);
return NT_STATUS_NO_MEMORY;
}
*d->trust_posix_offset = (uint32_t)val64;
}
val64 = ldb_msg_find_attr_as_uint64(msg, "msDS-SupportedEncryptionTypes", UINT64_MAX);
if (val64 != UINT64_MAX) {
d->supported_enc_type = talloc(d, uint32_t);
if (d->supported_enc_type == NULL) {
TALLOC_FREE(d);
return NT_STATUS_NO_MEMORY;
}
*d->supported_enc_type = (uint32_t)val64;
}
val = ldb_msg_find_ldb_val(msg, "msDS-TrustForestTrustInfo");
if (val != NULL) {
d->trust_forest_trust_info = data_blob_dup_talloc(d, *val);
if (d->trust_forest_trust_info.data == NULL) {
TALLOC_FREE(d);
return NT_STATUS_NO_MEMORY;
}
}
*_d = d;
return NT_STATUS_OK;
}
static NTSTATUS pdb_samba_dsdb_get_trusted_domain(struct pdb_methods *m,
TALLOC_CTX *mem_ctx,
const char *domain,
struct pdb_trusted_domain **td)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
TALLOC_CTX *tmp_ctx = talloc_stackframe();
const char * const attrs[] = {
"securityIdentifier",
"flatName",
"trustPartner",
"trustAuthOutgoing",
"trustAuthIncoming",
"trustAttributes",
"trustDirection",
"trustType",
"trustPosixOffset",
"msDS-SupportedEncryptionTypes",
"msDS-TrustForestTrustInfo",
NULL
};
struct ldb_message *msg = NULL;
struct pdb_trusted_domain *d = NULL;
NTSTATUS status;
status = dsdb_trust_search_tdo(state->ldb, domain, NULL,
attrs, tmp_ctx, &msg);
if (!NT_STATUS_IS_OK(status)) {
DBG_ERR("dsdb_trust_search_tdo(%s) - %s ",
domain, nt_errstr(status));
TALLOC_FREE(tmp_ctx);
return status;
}
status = pdb_samba_dsdb_msg_to_trusted_domain(msg, mem_ctx, &d);
if (!NT_STATUS_IS_OK(status)) {
DBG_ERR("pdb_samba_dsdb_msg_to_trusted_domain(%s) - %s ",
domain, nt_errstr(status));
TALLOC_FREE(tmp_ctx);
return status;
}
*td = d;
TALLOC_FREE(tmp_ctx);
return NT_STATUS_OK;
}
static NTSTATUS pdb_samba_dsdb_get_trusted_domain_by_sid(struct pdb_methods *m,
TALLOC_CTX *mem_ctx,
struct dom_sid *sid,
struct pdb_trusted_domain **td)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
TALLOC_CTX *tmp_ctx = talloc_stackframe();
const char * const attrs[] = {
"securityIdentifier",
"flatName",
"trustPartner",
"trustAuthOutgoing",
"trustAuthIncoming",
"trustAttributes",
"trustDirection",
"trustType",
"trustPosixOffset",
"msDS-SupportedEncryptionTypes",
"msDS-TrustForestTrustInfo",
NULL
};
struct ldb_message *msg = NULL;
struct pdb_trusted_domain *d = NULL;
NTSTATUS status;
status = dsdb_trust_search_tdo_by_sid(state->ldb, sid,
attrs, tmp_ctx, &msg);
if (!NT_STATUS_IS_OK(status)) {
DBG_ERR("dsdb_trust_search_tdo_by_sid(%s) - %s ",
dom_sid_string(tmp_ctx, sid), nt_errstr(status));
TALLOC_FREE(tmp_ctx);
return status;
}
status = pdb_samba_dsdb_msg_to_trusted_domain(msg, mem_ctx, &d);
if (!NT_STATUS_IS_OK(status)) {
DBG_ERR("pdb_samba_dsdb_msg_to_trusted_domain(%s) - %s ",
dom_sid_string(tmp_ctx, sid), nt_errstr(status));
TALLOC_FREE(tmp_ctx);
return status;
}
*td = d;
TALLOC_FREE(tmp_ctx);
return NT_STATUS_OK;
}
static NTSTATUS add_trust_user(TALLOC_CTX *mem_ctx,
struct ldb_context *sam_ldb,
struct ldb_dn *base_dn,
const char *netbios_name,
struct trustAuthInOutBlob *taiob)
{
struct ldb_request *req = NULL;
struct ldb_message *msg = NULL;
struct ldb_dn *dn = NULL;
uint32_t i;
int ret;
bool ok;
dn = ldb_dn_copy(mem_ctx, base_dn);
if (dn == NULL) {
return NT_STATUS_NO_MEMORY;
}
ok = ldb_dn_add_child_fmt(dn, "cn=%s$,cn=users", netbios_name);
if (!ok) {
return NT_STATUS_NO_MEMORY;
}
msg = ldb_msg_new(mem_ctx);
if (msg == NULL) {
return NT_STATUS_NO_MEMORY;
}
msg->dn = dn;
ret = ldb_msg_add_string(msg, "objectClass", "user");
if (ret != LDB_SUCCESS) {
return NT_STATUS_NO_MEMORY;
}
ret = ldb_msg_add_fmt(msg, "samAccountName", "%s$", netbios_name);
if (ret != LDB_SUCCESS) {
return NT_STATUS_NO_MEMORY;
}
ret = samdb_msg_add_uint(sam_ldb, msg, msg, "userAccountControl",
UF_INTERDOMAIN_TRUST_ACCOUNT);
if (ret != LDB_SUCCESS) {
return NT_STATUS_NO_MEMORY;
}
for (i = 0; i < taiob->count; i++) {
struct AuthenticationInformation *auth_info =
&taiob->current.array[i];
const char *attribute = NULL;
struct ldb_val v;
switch (taiob->current.array[i].AuthType) {
case TRUST_AUTH_TYPE_NT4OWF:
attribute = "unicodePwd";
v.data = (uint8_t *)&auth_info->AuthInfo.nt4owf.password;
v.length = 16;
break;
case TRUST_AUTH_TYPE_CLEAR:
attribute = "clearTextPassword";
v.data = auth_info->AuthInfo.clear.password;
v.length = auth_info->AuthInfo.clear.size;
break;
default:
continue;
}
ret = ldb_msg_add_value(msg, attribute, &v, NULL);
if (ret != LDB_SUCCESS) {
return NT_STATUS_NO_MEMORY;
}
}
/* create the trusted_domain user account */
ret = ldb_build_add_req(&req, sam_ldb, mem_ctx, msg, NULL, NULL,
ldb_op_default_callback, NULL);
if (ret != LDB_SUCCESS) {
return NT_STATUS_NO_MEMORY;
}
ret = ldb_request_add_control(
req, DSDB_CONTROL_PERMIT_INTERDOMAIN_TRUST_UAC_OID,
false, NULL);
if (ret != LDB_SUCCESS) {
return NT_STATUS_NO_MEMORY;
}
ret = dsdb_autotransaction_request(sam_ldb, req);
if (ret != LDB_SUCCESS) {
DEBUG(0,("Failed to create user record %s: %s\n",
ldb_dn_get_linearized(msg->dn),
ldb_errstring(sam_ldb)));
switch (ret) {
case LDB_ERR_ENTRY_ALREADY_EXISTS:
return NT_STATUS_DOMAIN_EXISTS;
case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
return NT_STATUS_ACCESS_DENIED;
default:
return NT_STATUS_INTERNAL_DB_CORRUPTION;
}
}
return NT_STATUS_OK;
}
static NTSTATUS pdb_samba_dsdb_set_trusted_domain(struct pdb_methods *methods,
const char* domain,
const struct pdb_trusted_domain *td)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
methods->private_data, struct pdb_samba_dsdb_state);
TALLOC_CTX *tmp_ctx = talloc_stackframe();
bool in_txn = false;
struct ldb_dn *base_dn = NULL;
struct ldb_message *msg = NULL;
const char *attrs[] = {
NULL
};
char *netbios_encoded = NULL;
char *dns_encoded = NULL;
char *sid_encoded = NULL;
int ret;
struct trustAuthInOutBlob taiob;
enum ndr_err_code ndr_err;
NTSTATUS status;
bool ok;
base_dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->ldb));
if (base_dn == NULL) {
TALLOC_FREE(tmp_ctx);
status = NT_STATUS_NO_MEMORY;
goto out;
}
/*
* We expect S-1-5-21-A-B-C, but we don't
* allow S-1-5-21-0-0-0 as this is used
* for claims and compound identities.
*/
ok = dom_sid_is_valid_account_domain(&td->security_identifier);
if (!ok) {
status = NT_STATUS_INVALID_PARAMETER;
goto out;
}
if (strequal(td->netbios_name, "BUILTIN")) {
status = NT_STATUS_INVALID_PARAMETER;
goto out;
}
if (strequal(td->domain_name, "BUILTIN")) {
status = NT_STATUS_INVALID_PARAMETER;
goto out;
}
dns_encoded = ldb_binary_encode_string(tmp_ctx, td->domain_name);
if (dns_encoded == NULL) {
status = NT_STATUS_NO_MEMORY;
goto out;
}
netbios_encoded = ldb_binary_encode_string(tmp_ctx, td->netbios_name);
if (netbios_encoded == NULL) {
status =NT_STATUS_NO_MEMORY;
goto out;
}
sid_encoded = ldap_encode_ndr_dom_sid(tmp_ctx, &td->security_identifier);
if (sid_encoded == NULL) {
status = NT_STATUS_NO_MEMORY;
goto out;
}
ok = samdb_is_pdc(state->ldb);
if (!ok) {
DBG_ERR("Adding TDO is only allowed on a PDC.\n");
TALLOC_FREE(tmp_ctx);
status = NT_STATUS_INVALID_DOMAIN_ROLE;
goto out;
}
status = dsdb_trust_search_tdo(state->ldb,
td->netbios_name,
td->domain_name,
attrs,
tmp_ctx,
&msg);
if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
DBG_ERR("dsdb_trust_search_tdo returned %s\n",
nt_errstr(status));
status = NT_STATUS_INVALID_DOMAIN_STATE;
goto out;
}
ret = ldb_transaction_start(state->ldb);
if (ret != LDB_SUCCESS) {
status = NT_STATUS_INTERNAL_DB_CORRUPTION;
goto out;
}
in_txn = true;
msg = ldb_msg_new(tmp_ctx);
if (msg == NULL) {
status = NT_STATUS_NO_MEMORY;
goto out;
}
msg->dn = ldb_dn_copy(tmp_ctx, base_dn);
ok = ldb_dn_add_child_fmt(msg->dn, "cn=%s,cn=System", td->domain_name);
if (!ok) {
status = NT_STATUS_NO_MEMORY;
goto out;
}
ret = ldb_msg_add_string(msg, "objectClass", "trustedDomain");
if (ret != LDB_SUCCESS) {
status = NT_STATUS_NO_MEMORY;
goto out;
}
ret = ldb_msg_add_string(msg, "flatname", td->netbios_name);
if (ret != LDB_SUCCESS) {
status = NT_STATUS_NO_MEMORY;
goto out;
}
ret = ldb_msg_add_string(msg, "trustPartner", td->domain_name);
if (ret != LDB_SUCCESS) {
status = NT_STATUS_NO_MEMORY;
goto out;
}
ret = samdb_msg_add_dom_sid(state->ldb,
tmp_ctx,
msg,
"securityIdentifier",
&td->security_identifier);
if (ret != LDB_SUCCESS) {
status = NT_STATUS_NO_MEMORY;
goto out;
}
ret = samdb_msg_add_int(state->ldb,
tmp_ctx,
msg,
"trustType",
td->trust_type);
if (ret != LDB_SUCCESS) {
status = NT_STATUS_NO_MEMORY;
goto out;
}
ret = samdb_msg_add_int(state->ldb,
tmp_ctx,
msg,
"trustAttributes",
td->trust_attributes);
if (ret != LDB_SUCCESS) {
status =NT_STATUS_NO_MEMORY;
goto out;
}
ret = samdb_msg_add_int(state->ldb,
tmp_ctx,
msg,
"trustDirection",
td->trust_direction);
if (ret != LDB_SUCCESS) {
status = NT_STATUS_NO_MEMORY;
goto out;
}
if (td->trust_auth_incoming.data != NULL) {
ret = ldb_msg_add_value(msg,
"trustAuthIncoming",
&td->trust_auth_incoming,
NULL);
if (ret != LDB_SUCCESS) {
status = NT_STATUS_NO_MEMORY;
goto out;
}
}
if (td->trust_auth_outgoing.data != NULL) {
ret = ldb_msg_add_value(msg,
"trustAuthOutgoing",
&td->trust_auth_outgoing,
NULL);
if (ret != LDB_SUCCESS) {
status = NT_STATUS_NO_MEMORY;
goto out;
}
}
/* create the trusted_domain */
ret = ldb_add(state->ldb, msg);
switch (ret) {
case LDB_SUCCESS:
break;
case LDB_ERR_ENTRY_ALREADY_EXISTS:
DBG_ERR("Failed to create trusted domain record %s: %s\n",
ldb_dn_get_linearized(msg->dn),
ldb_errstring(state->ldb));
status = NT_STATUS_DOMAIN_EXISTS;
goto out;
case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
DBG_ERR("Failed to create trusted domain record %s: %s\n",
ldb_dn_get_linearized(msg->dn),
ldb_errstring(state->ldb));
status = NT_STATUS_ACCESS_DENIED;
goto out;
default:
DBG_ERR("Failed to create trusted domain record %s: %s\n",
ldb_dn_get_linearized(msg->dn),
ldb_errstring(state->ldb));
status = NT_STATUS_INTERNAL_DB_CORRUPTION;
goto out;
}
ndr_err = ndr_pull_struct_blob(
&td->trust_auth_outgoing,
tmp_ctx,
&taiob,
(ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
status = ndr_map_error2ntstatus(ndr_err);
goto out;
}
if (td->trust_direction == LSA_TRUST_DIRECTION_INBOUND) {
status = add_trust_user(tmp_ctx,
state->ldb,
base_dn,
td->netbios_name,
&taiob);
if (!NT_STATUS_IS_OK(status)) {
goto out;
}
}
ret = ldb_transaction_commit(state->ldb);
if (ret != LDB_SUCCESS) {
return NT_STATUS_INTERNAL_DB_CORRUPTION;
}
in_txn = false;
/*
* TODO: Notify winbindd that we have a new trust
*/
status = NT_STATUS_OK;
out:
if (in_txn) {
ldb_transaction_cancel(state->ldb);
}
TALLOC_FREE(tmp_ctx);
return status;
}
static NTSTATUS delete_trust_user(TALLOC_CTX *mem_ctx,
struct pdb_samba_dsdb_state *state,
const char *trust_user)
{
const char *attrs[] = { "userAccountControl", NULL };
struct ldb_message **msgs;
uint32_t uac;
int ret;
ret = gendb_search(state->ldb,
mem_ctx,
ldb_get_default_basedn(state->ldb),
&msgs,
attrs,
"samAccountName=%s$",
trust_user);
if (ret > 1) {
return NT_STATUS_INTERNAL_DB_CORRUPTION;
}
if (ret == 0) {
return NT_STATUS_OK;
}
uac = ldb_msg_find_attr_as_uint(msgs[0],
"userAccountControl",
0);
if (!(uac & UF_INTERDOMAIN_TRUST_ACCOUNT)) {
return NT_STATUS_OBJECT_NAME_COLLISION;
}
ret = ldb_delete(state->ldb, msgs[0]->dn);
switch (ret) {
case LDB_SUCCESS:
break;
case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
return NT_STATUS_ACCESS_DENIED;
default:
return NT_STATUS_INTERNAL_DB_CORRUPTION;
}
return NT_STATUS_OK;
}
static NTSTATUS pdb_samba_dsdb_del_trusted_domain(struct pdb_methods *methods,
const char *domain)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
methods->private_data, struct pdb_samba_dsdb_state);
TALLOC_CTX *tmp_ctx = talloc_stackframe();
struct pdb_trusted_domain *td = NULL;
struct ldb_dn *tdo_dn = NULL;
bool in_txn = false;
NTSTATUS status;
int ret;
bool ok;
status = pdb_samba_dsdb_get_trusted_domain(methods,
tmp_ctx,
domain,
&td);
if (!NT_STATUS_IS_OK(status)) {
if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
DBG_ERR("Searching TDO for %s returned %s\n",
domain, nt_errstr(status));
return status;
}
DBG_NOTICE("No TDO object for %s\n", domain);
return NT_STATUS_OK;
}
tdo_dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->ldb));
if (tdo_dn == NULL) {
status = NT_STATUS_NO_MEMORY;
goto out;
}
ok = ldb_dn_add_child_fmt(tdo_dn, "cn=%s,cn=System", domain);
if (!ok) {
TALLOC_FREE(tmp_ctx);
status = NT_STATUS_NO_MEMORY;
goto out;
}
ret = ldb_transaction_start(state->ldb);
if (ret != LDB_SUCCESS) {
status = NT_STATUS_INTERNAL_DB_CORRUPTION;
goto out;
}
in_txn = true;
ret = ldb_delete(state->ldb, tdo_dn);
if (ret != LDB_SUCCESS) {
status = NT_STATUS_INVALID_HANDLE;
goto out;
}
if (td->trust_direction == LSA_TRUST_DIRECTION_INBOUND) {
status = delete_trust_user(tmp_ctx, state, domain);
if (!NT_STATUS_IS_OK(status)) {
goto out;
}
}
ret = ldb_transaction_commit(state->ldb);
if (ret != LDB_SUCCESS) {
status = NT_STATUS_INTERNAL_DB_CORRUPTION;
goto out;
}
in_txn = false;
status = NT_STATUS_OK;
out:
if (in_txn) {
ldb_transaction_cancel(state->ldb);
}
TALLOC_FREE(tmp_ctx);
return status;
}
static NTSTATUS pdb_samba_dsdb_enum_trusted_domains(struct pdb_methods *m,
TALLOC_CTX *mem_ctx,
uint32_t *_num_domains,
struct pdb_trusted_domain ***_domains)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
m->private_data, struct pdb_samba_dsdb_state);
TALLOC_CTX *tmp_ctx = talloc_stackframe();
const char * const attrs[] = {
"securityIdentifier",
"flatName",
"trustPartner",
"trustAuthOutgoing",
"trustAuthIncoming",
"trustAttributes",
"trustDirection",
"trustType",
"trustPosixOffset",
"msDS-SupportedEncryptionTypes",
"msDS-TrustForestTrustInfo",
NULL
};
struct ldb_result *res = NULL;
unsigned int i;
struct pdb_trusted_domain **domains = NULL;
NTSTATUS status;
uint32_t di = 0;
*_num_domains = 0;
*_domains = NULL;
status = dsdb_trust_search_tdos(state->ldb, NULL,
attrs, tmp_ctx, &res);
if (!NT_STATUS_IS_OK(status)) {
DBG_ERR("dsdb_trust_search_tdos() - %s ", nt_errstr(status));
TALLOC_FREE(tmp_ctx);
return status;
}
if (res->count == 0) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_OK;
}
domains = talloc_zero_array(tmp_ctx, struct pdb_trusted_domain *,
res->count);
if (domains == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
for (i = 0; i < res->count; i++) {
struct ldb_message *msg = res->msgs[i];
struct pdb_trusted_domain *d = NULL;
status = pdb_samba_dsdb_msg_to_trusted_domain(msg, domains, &d);
if (!NT_STATUS_IS_OK(status)) {
DBG_ERR("pdb_samba_dsdb_msg_to_trusted_domain() - %s ",
nt_errstr(status));
TALLOC_FREE(tmp_ctx);
return status;
}
domains[di++] = d;
}
domains = talloc_realloc(domains, domains, struct pdb_trusted_domain *,
di);
*_domains = talloc_move(mem_ctx, &domains);
*_num_domains = di;
TALLOC_FREE(tmp_ctx);
return NT_STATUS_OK;
}
static bool pdb_samba_dsdb_is_responsible_for_wellknown(struct pdb_methods *m)
{
return true;
}
static bool pdb_samba_dsdb_is_responsible_for_everything_else(struct pdb_methods *m)
{
return true;
}
static void pdb_samba_dsdb_init_methods(struct pdb_methods *m)
{
m->name = "samba_dsdb";
m->get_domain_info = pdb_samba_dsdb_get_domain_info;
m->getsampwnam = pdb_samba_dsdb_getsampwnam;
m->getsampwsid = pdb_samba_dsdb_getsampwsid;
m->create_user = pdb_samba_dsdb_create_user;
m->delete_user = pdb_samba_dsdb_delete_user;
m->add_sam_account = pdb_samba_dsdb_add_sam_account;
m->update_sam_account = pdb_samba_dsdb_update_sam_account;
m->delete_sam_account = pdb_samba_dsdb_delete_sam_account;
m->rename_sam_account = pdb_samba_dsdb_rename_sam_account;
m->update_login_attempts = pdb_samba_dsdb_update_login_attempts;
m->getgrsid = pdb_samba_dsdb_getgrsid;
m->getgrgid = pdb_samba_dsdb_getgrgid;
m->getgrnam = pdb_samba_dsdb_getgrnam;
m->create_dom_group = pdb_samba_dsdb_create_dom_group;
m->delete_dom_group = pdb_samba_dsdb_delete_dom_group;
m->add_group_mapping_entry = pdb_samba_dsdb_add_group_mapping_entry;
m->update_group_mapping_entry = pdb_samba_dsdb_update_group_mapping_entry;
m->delete_group_mapping_entry = pdb_samba_dsdb_delete_group_mapping_entry;
m->enum_group_mapping = pdb_samba_dsdb_enum_group_mapping;
m->enum_group_members = pdb_samba_dsdb_enum_group_members;
m->enum_group_memberships = pdb_samba_dsdb_enum_group_memberships;
m->set_unix_primary_group = pdb_samba_dsdb_set_unix_primary_group;
m->add_groupmem = pdb_samba_dsdb_add_groupmem;
m->del_groupmem = pdb_samba_dsdb_del_groupmem;
m->create_alias = pdb_samba_dsdb_create_alias;
m->delete_alias = pdb_samba_dsdb_delete_alias;
m->get_aliasinfo = pdb_default_get_aliasinfo;
m->add_aliasmem = pdb_samba_dsdb_add_aliasmem;
m->del_aliasmem = pdb_samba_dsdb_del_aliasmem;
m->enum_aliasmem = pdb_samba_dsdb_enum_aliasmem;
m->enum_alias_memberships = pdb_samba_dsdb_enum_alias_memberships;
m->lookup_rids = pdb_samba_dsdb_lookup_rids;
m->lookup_names = pdb_samba_dsdb_lookup_names;
m->get_account_policy = pdb_samba_dsdb_get_account_policy;
m->set_account_policy = pdb_samba_dsdb_set_account_policy;
m->get_seq_num = pdb_samba_dsdb_get_seq_num;
m->search_users = pdb_samba_dsdb_search_users;
m->search_groups = pdb_samba_dsdb_search_groups;
m->search_aliases = pdb_samba_dsdb_search_aliases;
m->id_to_sid = pdb_samba_dsdb_id_to_sid;
m->sid_to_id = pdb_samba_dsdb_sid_to_id;
m->capabilities = pdb_samba_dsdb_capabilities;
m->new_rid = pdb_samba_dsdb_new_rid;
m->get_trusteddom_pw = pdb_samba_dsdb_get_trusteddom_pw;
m->get_trusteddom_creds = pdb_samba_dsdb_get_trusteddom_creds;
m->set_trusteddom_pw = pdb_samba_dsdb_set_trusteddom_pw;
m->del_trusteddom_pw = pdb_samba_dsdb_del_trusteddom_pw;
m->enum_trusteddoms = pdb_samba_dsdb_enum_trusteddoms;
m->get_trusted_domain = pdb_samba_dsdb_get_trusted_domain;
m->get_trusted_domain_by_sid = pdb_samba_dsdb_get_trusted_domain_by_sid;
m->set_trusted_domain = pdb_samba_dsdb_set_trusted_domain;
m->del_trusted_domain = pdb_samba_dsdb_del_trusted_domain;
m->enum_trusted_domains = pdb_samba_dsdb_enum_trusted_domains;
m->is_responsible_for_wellknown =
pdb_samba_dsdb_is_responsible_for_wellknown;
m->is_responsible_for_everything_else =
pdb_samba_dsdb_is_responsible_for_everything_else;
}
static void free_private_data(void **vp)
{
struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
*vp, struct pdb_samba_dsdb_state);
talloc_unlink(state, state->ldb);
return;
}
static NTSTATUS pdb_samba_dsdb_init_secrets(struct pdb_methods *m)
{
struct pdb_domain_info *dom_info;
struct dom_sid stored_sid;
struct GUID stored_guid;
bool sid_exists_and_matches = false;
bool guid_exists_and_matches = false;
bool ret;
dom_info = pdb_samba_dsdb_get_domain_info(m, m);
if (!dom_info) {
return NT_STATUS_UNSUCCESSFUL;
}
ret = secrets_fetch_domain_sid(dom_info->name, &stored_sid);
if (ret) {
if (dom_sid_equal(&stored_sid, &dom_info->sid)) {
sid_exists_and_matches = true;
}
}
if (sid_exists_and_matches == false) {
secrets_clear_domain_protection(dom_info->name);
ret = secrets_store_domain_sid(dom_info->name,
&dom_info->sid);
ret &= secrets_mark_domain_protected(dom_info->name);
if (!ret) {
goto done;
}
}
ret = secrets_fetch_domain_guid(dom_info->name, &stored_guid);
if (ret) {
if (GUID_equal(&stored_guid, &dom_info->guid)) {
guid_exists_and_matches = true;
}
}
if (guid_exists_and_matches == false) {
secrets_clear_domain_protection(dom_info->name);
ret = secrets_store_domain_guid(dom_info->name,
&dom_info->guid);
ret &= secrets_mark_domain_protected(dom_info->name);
if (!ret) {
goto done;
}
}
done:
TALLOC_FREE(dom_info);
if (!ret) {
return NT_STATUS_UNSUCCESSFUL;
}
return NT_STATUS_OK;
}
static NTSTATUS pdb_init_samba_dsdb(struct pdb_methods **pdb_method,
const char *location)
{
struct pdb_methods *m;
struct pdb_samba_dsdb_state *state;
NTSTATUS status;
char *errstring = NULL;
int ret;
if ( !NT_STATUS_IS_OK(status = make_pdb_method( &m )) ) {
return status;
}
state = talloc_zero(m, struct pdb_samba_dsdb_state);
if (state == NULL) {
goto nomem;
}
m->private_data = state;
m->free_private_data = free_private_data;
pdb_samba_dsdb_init_methods(m);
state->ev = s4_event_context_init(state);
if (!state->ev) {
DEBUG(0, ("s4_event_context_init failed\n"));
goto nomem;
}
state->lp_ctx = loadparm_init_s3(state, loadparm_s3_helpers());
if (state->lp_ctx == NULL) {
DEBUG(0, ("loadparm_init_s3 failed\n"));
goto nomem;
}
if (location == NULL) {
location = "sam.ldb";
}
ret = samdb_connect_url(state,
state->ev,
state->lp_ctx,
system_session(state->lp_ctx),
0,
location,
NULL,
&state->ldb,
&errstring);
if (!state->ldb) {
DEBUG(0, ("samdb_connect failed: %s: %s\n",
errstring, ldb_strerror(ret)));
status = NT_STATUS_INTERNAL_ERROR;
goto fail;
}
state->idmap_ctx = idmap_init(state, state->ev,
state->lp_ctx);
if (!state->idmap_ctx) {
DEBUG(0, ("idmap failed\n"));
status = NT_STATUS_INTERNAL_ERROR;
goto fail;
}
status = pdb_samba_dsdb_init_secrets(m);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(10, ("pdb_samba_dsdb_init_secrets failed!\n"));
goto fail;
}
*pdb_method = m;
return NT_STATUS_OK;
nomem:
status = NT_STATUS_NO_MEMORY;
fail:
TALLOC_FREE(m);
return status;
}
NTSTATUS pdb_samba_dsdb_init(TALLOC_CTX *);
NTSTATUS pdb_samba_dsdb_init(TALLOC_CTX *ctx)
{
NTSTATUS status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "samba_dsdb",
pdb_init_samba_dsdb);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
return smb_register_passdb(PASSDB_INTERFACE_VERSION, "samba4",
pdb_init_samba_dsdb);
}
|