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
|
/*
SSSD
LDAP Helper routines
Copyright (C) Simo Sorce <ssorce@redhat.com>
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/>.
*/
#include "util/util.h"
#include "util/crypto/sss_crypto.h"
#include "confdb/confdb.h"
#include "providers/ldap/ldap_common.h"
#include "providers/ldap/sdap.h"
#include "providers/ldap/sdap_range.h"
#include "util/probes.h"
/* =Retrieve-Options====================================================== */
errno_t sdap_copy_map_entry(const struct sdap_attr_map *src_map,
struct sdap_attr_map *dst_map,
int entry_index)
{
if (src_map[entry_index].name != NULL) {
dst_map[entry_index].name = talloc_strdup(dst_map,
src_map[entry_index].name);
if (dst_map[entry_index].name == NULL) {
return ENOMEM;
}
} else {
dst_map->name = NULL;
}
return EOK;
}
int sdap_copy_map(TALLOC_CTX *memctx,
struct sdap_attr_map *src_map,
int num_entries,
struct sdap_attr_map **_map)
{
struct sdap_attr_map *map;
int i;
map = talloc_array(memctx, struct sdap_attr_map, num_entries + 1);
if (!map) {
return ENOMEM;
}
for (i = 0; i < num_entries; i++) {
map[i].opt_name = talloc_strdup(map, src_map[i].opt_name);
map[i].sys_name = talloc_strdup(map, src_map[i].sys_name);
if (map[i].opt_name == NULL || map[i].sys_name == NULL) {
return ENOMEM;
}
if (src_map[i].def_name != NULL) {
map[i].def_name = talloc_strdup(map, src_map[i].def_name);
map[i].name = talloc_strdup(map, src_map[i].def_name);
if (map[i].def_name == NULL || map[i].name == NULL) {
return ENOMEM;
}
} else {
map[i].def_name = NULL;
map[i].name = NULL;
}
DEBUG(SSSDBG_TRACE_FUNC, "Option %s has%s value %s\n",
map[i].opt_name, map[i].name ? "" : " no",
map[i].name ? map[i].name : "");
}
/* Include the sentinel */
memset(&map[num_entries], 0, sizeof(struct sdap_attr_map));
*_map = map;
return EOK;
}
static errno_t split_extra_attr(TALLOC_CTX *mem_ctx,
const char *conf_attr,
char **_sysdb_attr,
char **_ldap_attr)
{
char *ldap_attr;
char *sysdb_attr;
char *sep;
sep = strchr(conf_attr, ':');
if (sep == NULL) {
sysdb_attr = talloc_strdup(mem_ctx, conf_attr);
ldap_attr = talloc_strdup(mem_ctx, conf_attr);
} else {
if (sep == conf_attr || *(sep + 1) == '\0') {
return ERR_INVALID_EXTRA_ATTR;
}
sysdb_attr = talloc_strndup(mem_ctx, conf_attr,
sep - conf_attr);
ldap_attr = talloc_strdup(mem_ctx, sep+1);
}
if (sysdb_attr == NULL || ldap_attr == NULL) {
return ENOMEM;
}
*_sysdb_attr = sysdb_attr;
*_ldap_attr = ldap_attr;
return EOK;
}
enum duplicate_t {
NOT_FOUND = 0,
ALREADY_IN_MAP, /* nothing to add */
CONFLICT_WITH_MAP /* attempt to redefine attribute */
};
static enum duplicate_t check_duplicate(struct sdap_attr_map *map,
int num_entries,
const char *sysdb_attr,
const char *ldap_attr)
{
int i;
for (i = 0; i < num_entries; i++) {
if (strcmp(map[i].sys_name, sysdb_attr) == 0) {
if (map[i].name != NULL && strcmp(map[i].name, ldap_attr) == 0) {
return ALREADY_IN_MAP;
} else {
return CONFLICT_WITH_MAP;
}
}
}
return NOT_FOUND;
}
int sdap_extend_map(TALLOC_CTX *memctx,
struct sdap_attr_map *src_map,
size_t num_entries,
char **extra_attrs,
struct sdap_attr_map **_map,
size_t *_new_size)
{
struct sdap_attr_map *map;
size_t nextra = 0;
size_t i;
char *ldap_attr;
char *sysdb_attr;
errno_t ret;
*_map = src_map;
if (extra_attrs == NULL) {
DEBUG(SSSDBG_FUNC_DATA, "No extra attributes\n");
*_new_size = num_entries;
return EOK;
}
for (nextra = 0; extra_attrs[nextra]; nextra++) ;
DEBUG(SSSDBG_FUNC_DATA, "%zu extra attributes\n", nextra);
map = talloc_realloc(memctx, src_map, struct sdap_attr_map,
num_entries + nextra + 1);
if (map == NULL) {
return ENOMEM;
}
*_map = map;
for (i = 0; *extra_attrs != NULL; extra_attrs++) {
ret = split_extra_attr(map, *extra_attrs, &sysdb_attr, &ldap_attr);
if (ret != EOK) {
DEBUG(SSSDBG_MINOR_FAILURE, "Cannot split %s\n", *extra_attrs);
continue;
}
ret = check_duplicate(map, num_entries, sysdb_attr, ldap_attr);
if (ret == ALREADY_IN_MAP) {
DEBUG(SSSDBG_TRACE_FUNC,
"Attribute %s (%s in LDAP) is already in map.\n",
sysdb_attr, ldap_attr);
continue;
} else if (ret == CONFLICT_WITH_MAP) {
DEBUG(SSSDBG_FATAL_FAILURE,
"Attribute %s (%s in LDAP) is already used by SSSD, please "
"choose a different cache name\n", sysdb_attr, ldap_attr);
return ERR_DUP_EXTRA_ATTR;
}
map[num_entries+i].name = ldap_attr;
map[num_entries+i].sys_name = sysdb_attr;
map[num_entries+i].opt_name = talloc_strdup(map,
map[num_entries+i].name);
map[num_entries+i].def_name = talloc_strdup(map,
map[num_entries+i].name);
if (map[num_entries+i].opt_name == NULL ||
map[num_entries+i].sys_name == NULL ||
map[num_entries+i].name == NULL ||
map[num_entries+i].def_name == NULL) {
return ENOMEM;
}
DEBUG(SSSDBG_TRACE_FUNC, "Extending map with %s\n", *extra_attrs);
/* index must be incremented only for appended entry. */
i++;
}
nextra = i;
/* Sentinel */
memset(&map[num_entries+nextra], 0, sizeof(struct sdap_attr_map));
*_new_size = num_entries + nextra;
return EOK;
}
int sdap_extend_map_with_list(TALLOC_CTX *mem_ctx,
const struct sdap_options *opts,
int extra_attr_index,
struct sdap_attr_map *src_map,
size_t num_entries,
struct sdap_attr_map **_map,
size_t *_new_size)
{
const char *extra_attrs;
char **extra_attrs_list;
errno_t ret;
*_map = src_map;
extra_attrs = dp_opt_get_string(opts->basic, extra_attr_index);
if (extra_attrs == NULL) {
*_new_size = num_entries;
return EOK;
}
/* split server parm into a list */
ret = split_on_separator(mem_ctx, extra_attrs, ',', true, true,
&extra_attrs_list, NULL);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, "Failed to parse server list!\n");
return ret;
}
ret = sdap_extend_map(mem_ctx, src_map,
num_entries, extra_attrs_list,
_map, _new_size);
talloc_free(extra_attrs_list);
if (ret != EOK) {
return ret;
}
return EOK;
}
static void sdap_inherit_basic_options(char **inherit_opt_list,
struct dp_option *parent_opts,
struct dp_option *subdom_opts)
{
int inherit_options[] = {
SDAP_SEARCH_TIMEOUT,
SDAP_NETWORK_TIMEOUT,
SDAP_OPT_TIMEOUT,
SDAP_OFFLINE_TIMEOUT,
SDAP_ENUM_REFRESH_TIMEOUT,
SDAP_ENUM_REFRESH_OFFSET,
SDAP_PURGE_CACHE_TIMEOUT,
SDAP_PURGE_CACHE_OFFSET,
SDAP_KRB5_KEYTAB,
SDAP_KRB5_TICKET_LIFETIME,
SDAP_ENUM_SEARCH_TIMEOUT,
SDAP_EXPIRE_TIMEOUT,
SDAP_EXPIRE_OFFSET,
SDAP_IDLE_TIMEOUT,
SDAP_AD_USE_TOKENGROUPS,
SDAP_OPTS_BASIC /* sentinel */
};
int i;
for (i = 0; inherit_options[i] != SDAP_OPTS_BASIC; i++) {
dp_option_inherit_match(inherit_opt_list,
inherit_options[i],
parent_opts,
subdom_opts);
}
}
static void sdap_inherit_user_options(char **inherit_opt_list,
struct sdap_attr_map *parent_user_map,
struct sdap_attr_map *child_user_map)
{
int inherit_options[] = {
SDAP_AT_USER_PRINC,
SDAP_OPTS_USER /* sentinel */
};
int i;
int opt_index;
bool inherit_option;
for (i = 0; inherit_options[i] != SDAP_OPTS_USER; i++) {
opt_index = inherit_options[i];
inherit_option = string_in_list(parent_user_map[opt_index].opt_name,
inherit_opt_list,
false);
if (inherit_option == false) {
continue;
}
sdap_copy_map_entry(parent_user_map, child_user_map, opt_index);
}
}
void sdap_inherit_options(char **inherit_opt_list,
struct sdap_options *parent_sdap_opts,
struct sdap_options *child_sdap_opts)
{
sdap_inherit_basic_options(inherit_opt_list,
parent_sdap_opts->basic,
child_sdap_opts->basic);
sdap_inherit_user_options(inherit_opt_list,
parent_sdap_opts->user_map,
child_sdap_opts->user_map);
}
int sdap_get_map(TALLOC_CTX *memctx,
struct confdb_ctx *cdb,
const char *conf_path,
struct sdap_attr_map *def_map,
int num_entries,
struct sdap_attr_map **_map)
{
struct sdap_attr_map *map;
char *name;
int i, ret;
map = talloc_zero_array(memctx, struct sdap_attr_map, num_entries + 1);
if (!map) {
return ENOMEM;
}
for (i = 0; i < num_entries; i++) {
map[i].opt_name = def_map[i].opt_name;
map[i].def_name = def_map[i].def_name;
map[i].sys_name = def_map[i].sys_name;
ret = confdb_get_string(cdb, map, conf_path,
map[i].opt_name,
map[i].def_name,
&name);
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE,
"Failed to retrieve value for %s\n", map[i].opt_name);
talloc_zfree(map);
return EINVAL;
}
if (name) {
ret = sss_filter_sanitize(map, name, &map[i].name);
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE,
"Could not sanitize attribute [%s]\n", name);
talloc_zfree(map);
return EINVAL;
}
talloc_zfree(name);
} else {
map[i].name = NULL;
}
if (map[i].def_name && !map[i].name) {
DEBUG(SSSDBG_CRIT_FAILURE,
"Failed to process value for %s\n", map[i].opt_name);
talloc_zfree(map);
return EINVAL;
}
DEBUG(SSSDBG_TRACE_FUNC, "Option %s has%s value %s\n",
map[i].opt_name, map[i].name ? "" : " no",
map[i].name ? map[i].name : "");
}
*_map = map;
return EOK;
}
/* =Parse-msg============================================================= */
static bool objectclass_matched(struct sdap_attr_map *map,
const char *objcl, int len);
int sdap_parse_entry(TALLOC_CTX *memctx,
struct sdap_handle *sh, struct sdap_msg *sm,
struct sdap_attr_map *map, int attrs_num,
struct sysdb_attrs **_attrs,
bool disable_range_retrieval)
{
struct sysdb_attrs *attrs;
BerElement *ber = NULL;
struct berval **vals;
struct ldb_val v;
char *str;
int lerrno;
int i, ret, ai;
int base_attr_idx = 0;
const char *name = NULL;
bool store;
bool base64;
char *base_attr;
uint32_t range_offset;
TALLOC_CTX *tmp_ctx = talloc_new(NULL);
if (!tmp_ctx) return ENOMEM;
lerrno = 0;
ret = ldap_set_option(sh->ldap, LDAP_OPT_RESULT_CODE, &lerrno);
if (ret != LDAP_OPT_SUCCESS) {
DEBUG(SSSDBG_MINOR_FAILURE, "ldap_set_option failed [%s], ignored.\n",
sss_ldap_err2string(ret));
}
attrs = sysdb_new_attrs(tmp_ctx);
if (!attrs) {
ret = ENOMEM;
goto done;
}
str = ldap_get_dn(sh->ldap, sm->msg);
if (!str) {
ldap_get_option(sh->ldap, LDAP_OPT_RESULT_CODE, &lerrno);
DEBUG(SSSDBG_CRIT_FAILURE, "ldap_get_dn failed: %d(%s)\n",
lerrno, sss_ldap_err2string(lerrno));
ret = EIO;
goto done;
}
DEBUG(SSSDBG_TRACE_LIBS, "OriginalDN: [%s].\n", str);
PROBE(SDAP_PARSE_ENTRY, "OriginalDN", str, strlen(str));
ret = sysdb_attrs_add_string(attrs, SYSDB_ORIG_DN, str);
ldap_memfree(str);
if (ret) goto done;
if (map) {
vals = ldap_get_values_len(sh->ldap, sm->msg, "objectClass");
if (!vals) {
DEBUG(SSSDBG_CRIT_FAILURE,
"Unknown entry type, no objectClasses found!\n");
ret = EINVAL;
goto done;
}
for (i = 0; vals[i]; i++) {
if (objectclass_matched(map, vals[i]->bv_val, vals[i]->bv_len)) {
/* ok it's an entry of the right type */
break;
}
}
if (!vals[i]) {
DEBUG(SSSDBG_CRIT_FAILURE, "objectClass not matching: %s\n",
map[0].name);
ldap_value_free_len(vals);
ret = EINVAL;
goto done;
}
ldap_value_free_len(vals);
}
str = ldap_first_attribute(sh->ldap, sm->msg, &ber);
if (!str) {
ldap_get_option(sh->ldap, LDAP_OPT_RESULT_CODE, &lerrno);
DEBUG(lerrno == LDAP_SUCCESS
? SSSDBG_TRACE_LIBS
: SSSDBG_MINOR_FAILURE,
"Entry has no attributes [%d(%s)]!?\n",
lerrno, sss_ldap_err2string(lerrno));
if (map) {
ret = EINVAL;
goto done;
}
}
while (str) {
base64 = false;
ret = sdap_parse_range(tmp_ctx, str, &base_attr, &range_offset,
disable_range_retrieval);
switch(ret) {
case EAGAIN:
/* This attribute contained range values and needs more to
* be retrieved
*/
/* TODO: return the set of attributes that need additional retrieval
* For now, we'll continue below and treat it as regular values.
*/
/* FALLTHROUGH */
case ECANCELED:
/* FALLTHROUGH */
case EOK:
break;
default:
DEBUG(SSSDBG_CRIT_FAILURE,
"Could not determine if attribute [%s] was ranged\n", str);
goto done;
}
if (ret == ECANCELED) {
store = false;
} else if (map) {
for (i = 1; i < attrs_num; i++) {
/* check if this attr is valid with the chosen schema */
if (!map[i].name) continue;
/* check if it is an attr we are interested in */
if (strcasecmp(base_attr, map[i].name) == 0) break;
}
/* interesting attr */
if (i < attrs_num) {
store = true;
name = map[i].sys_name;
base_attr_idx = i;
if (strcmp(name, SYSDB_SSH_PUBKEY) == 0) {
base64 = true;
}
} else {
store = false;
}
} else {
name = base_attr;
store = true;
}
if (store) {
vals = ldap_get_values_len(sh->ldap, sm->msg, str);
if (!vals) {
ldap_get_option(sh->ldap, LDAP_OPT_RESULT_CODE, &lerrno);
if (lerrno != LDAP_SUCCESS) {
DEBUG(SSSDBG_CRIT_FAILURE,
"ldap_get_values_len() failed: %d(%s)\n",
lerrno, sss_ldap_err2string(lerrno));
ret = EIO;
goto done;
}
DEBUG(SSSDBG_TRACE_LIBS,
"Attribute [%s] has no values, skipping.\n", str);
} else {
if (!vals[0]) {
DEBUG(SSSDBG_CRIT_FAILURE,
"Missing value after ldap_get_values() ??\n");
ldap_value_free_len(vals);
ret = EINVAL;
goto done;
}
for (i = 0; vals[i]; i++) {
if (vals[i]->bv_len == 0) {
DEBUG(SSSDBG_TRACE_LIBS,
"Value of attribute [%s] is empty. "
"Skipping this value.\n", str);
continue;
}
if (base64) {
v.data = (uint8_t *) sss_base64_encode(attrs,
(uint8_t *) vals[i]->bv_val, vals[i]->bv_len);
if (!v.data) {
ldap_value_free_len(vals);
ret = ENOMEM;
goto done;
}
v.length = strlen((const char *)v.data);
} else {
v.data = (uint8_t *)vals[i]->bv_val;
v.length = vals[i]->bv_len;
}
PROBE(SDAP_PARSE_ENTRY, str, v.data, v.length);
if (map) {
/* The same LDAP attr might be used for more sysdb
* attrs in case there is a map. Find all that match
* and copy the value
*/
for (ai = base_attr_idx; ai < attrs_num; ai++) {
/* check if this attr is valid with the chosen
* schema */
if (!map[ai].name) continue;
/* check if it is an attr we are interested in */
if (strcasecmp(base_attr, map[ai].name) == 0) {
ret = sysdb_attrs_add_val(attrs,
map[ai].sys_name,
&v);
if (ret) {
ldap_value_free_len(vals);
goto done;
}
}
}
} else {
/* No map, just store the attribute */
ret = sysdb_attrs_add_val(attrs, name, &v);
if (ret) {
ldap_value_free_len(vals);
goto done;
}
}
}
ldap_value_free_len(vals);
}
}
ldap_memfree(str);
str = ldap_next_attribute(sh->ldap, sm->msg, ber);
}
ber_free(ber, 0);
ber = NULL;
ldap_get_option(sh->ldap, LDAP_OPT_RESULT_CODE, &lerrno);
if (lerrno) {
DEBUG(SSSDBG_CRIT_FAILURE, "ldap_get_option() failed: %d(%s)\n",
lerrno, sss_ldap_err2string(lerrno));
ret = EIO;
goto done;
}
PROBE(SDAP_PARSE_ENTRY_DONE);
*_attrs = talloc_steal(memctx, attrs);
ret = EOK;
done:
if (ber) ber_free(ber, 0);
talloc_free(tmp_ctx);
return ret;
}
static bool objectclass_matched(struct sdap_attr_map *map,
const char *objcl, int len)
{
if (len == 0) {
len = strlen(objcl) + 1;
}
if (strncasecmp(map[SDAP_OC_GROUP].name, objcl, len) == 0) {
return true;
}
if (map[SDAP_OC_GROUP_ALT].name != NULL
&& strncasecmp(map[SDAP_OC_GROUP_ALT].name, objcl, len) == 0) {
return true;
}
return false;
}
/* Parses an LDAPDerefRes into sdap_deref_attrs structure */
errno_t sdap_parse_deref(TALLOC_CTX *mem_ctx,
struct sdap_attr_map_info *minfo,
size_t num_maps,
LDAPDerefRes *dref,
struct sdap_deref_attrs ***_deref_res)
{
TALLOC_CTX *tmp_ctx;
LDAPDerefVal *dval;
const char *orig_dn;
const char **ocs;
struct sdap_attr_map *map;
int num_attrs = 0;
int ret, i, a, mi;
const char *name;
size_t len;
struct sdap_deref_attrs **res;
if (!dref || !minfo) return EINVAL;
tmp_ctx = talloc_new(NULL);
if (!tmp_ctx) return ENOMEM;
res = talloc_array(tmp_ctx, struct sdap_deref_attrs *, num_maps);
if (!res) {
ret = ENOMEM;
goto done;
}
for (i=0; i < num_maps; i++) {
res[i] = talloc_zero(res, struct sdap_deref_attrs);
if (!res[i]) {
ret = ENOMEM;
goto done;
}
res[i]->map = minfo[i].map;
}
if (!dref->derefVal.bv_val) {
DEBUG(SSSDBG_OP_FAILURE, "Entry has no DN?\n");
ret = EINVAL;
goto done;
}
orig_dn = dref->derefVal.bv_val;
DEBUG(SSSDBG_TRACE_LIBS,
"Dereferenced DN: %s\n", orig_dn);
if (!dref->attrVals) {
DEBUG(SSSDBG_FUNC_DATA,
"Dereferenced entry [%s] has no attributes, skipping\n",
orig_dn);
*_deref_res = NULL;
ret = EOK;
goto done;
}
ocs = NULL;
for (dval = dref->attrVals; dval != NULL; dval = dval->next) {
if (strcasecmp("objectClass", dval->type) == 0) {
if (dval->vals == NULL) {
DEBUG(SSSDBG_CONF_SETTINGS,
"No value for objectClass, skipping\n");
continue;
}
for(len=0; dval->vals[len].bv_val; len++);
ocs = talloc_array(tmp_ctx, const char *, len+1);
if (!ocs) {
ret = ENOMEM;
goto done;
}
for (i=0; i<len; i++) {
DEBUG(SSSDBG_TRACE_ALL, "Dereferenced objectClass value: %s\n",
dval->vals[i].bv_val);
ocs[i] = talloc_strdup(ocs, dval->vals[i].bv_val);
if (!ocs[i]) {
ret = ENOMEM;
goto done;
}
}
ocs[i] = NULL;
break;
}
}
if (!ocs) {
DEBUG(SSSDBG_CRIT_FAILURE,
"Unknown entry type, no objectClasses found!\n");
ret = EINVAL;
goto done;
}
for (mi = 0; mi < num_maps; mi++) {
map = NULL;
for (i=0; ocs[i]; i++) {
/* the objectclass is always the first name in the map */
if (objectclass_matched(minfo[mi].map, ocs[i], 0)) {
DEBUG(SSSDBG_TRACE_ALL,
"Found map for objectclass '%s'\n", ocs[i]);
map = minfo[mi].map;
num_attrs = minfo[mi].num_attrs;
break;
}
}
if (!map) continue;
res[mi]->attrs = sysdb_new_attrs(res[mi]);
if (!res[mi]->attrs) {
ret = ENOMEM;
goto done;
}
ret = sysdb_attrs_add_string(res[mi]->attrs, SYSDB_ORIG_DN,
orig_dn);
if (ret) {
goto done;
}
/* The dereference control seems to return the DN from the dereference
* attribute (e.g. member) so we can use it as key for the hash table
* later. */
ret = sysdb_attrs_add_string(res[mi]->attrs,
SYSDB_DN_FOR_MEMBER_HASH_TABLE, orig_dn);
if (ret) {
DEBUG(SSSDBG_OP_FAILURE, "sysdb_attrs_add_string failed.\n");
goto done;
}
for (dval = dref->attrVals; dval != NULL; dval = dval->next) {
DEBUG(SSSDBG_TRACE_INTERNAL,
"Dereferenced attribute: %s\n", dval->type);
for (a = 1; a < num_attrs; a++) {
/* check if this attr is valid with the chosen schema */
if (!map[a].name) continue;
/* check if it is an attr we are interested in */
if (strcasecmp(dval->type, map[a].name) == 0) break;
}
/* interesting attr */
if (a < num_attrs) {
name = map[a].sys_name;
} else {
continue;
}
if (dval->vals == NULL) {
DEBUG(SSSDBG_CONF_SETTINGS,
"No value for attribute %s, skipping\n", name);
continue;
}
for (i=0; dval->vals[i].bv_val; i++) {
DEBUG(SSSDBG_TRACE_ALL, "Dereferenced attribute value: %s\n",
dval->vals[i].bv_val);
ret = sysdb_attrs_add_mem(res[mi]->attrs, name,
dval->vals[i].bv_val,
dval->vals[i].bv_len);
if (ret) goto done;
}
}
}
*_deref_res = talloc_steal(mem_ctx, res);
ret = EOK;
done:
talloc_zfree(tmp_ctx);
return ret;
}
static void sss_ldap_debug(const char *buf)
{
sss_debug_fn(__FILE__, __LINE__, __FUNCTION__, SSSDBG_TRACE_ALL,
"libldap: %s", buf);
}
void setup_ldap_debug(struct dp_option *basic_opts)
{
int ret;
int ldap_debug_level;
ldap_debug_level = dp_opt_get_int(basic_opts, SDAP_LIBRARY_DEBUG_LEVEL);
if (ldap_debug_level == 0) {
return;
}
DEBUG(SSSDBG_CONF_SETTINGS, "Setting LDAP library debug level [%d].\n",
ldap_debug_level);
ret = ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug_level);
if (ret != LBER_OPT_SUCCESS) {
DEBUG(SSSDBG_OP_FAILURE,
"Failed to set LBER_OPT_DEBUG_LEVEL, ignored .\n");
}
ret = ber_set_option(NULL, LBER_OPT_LOG_PRINT_FN, sss_ldap_debug);
if (ret != LBER_OPT_SUCCESS) {
DEBUG(SSSDBG_OP_FAILURE,
"Failed to set LBER_OPT_LOG_PRINT_FN, ignored .\n");
}
ret = ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &ldap_debug_level);
if (ret != LDAP_OPT_SUCCESS) {
DEBUG(SSSDBG_OP_FAILURE,
"Failed to set LDAP_OPT_DEBUG_LEVEL, ignored .\n");
}
}
errno_t setup_tls_config(struct dp_option *basic_opts)
{
int ret;
int ldap_opt_x_tls_require_cert;
const char *tls_opt;
tls_opt = dp_opt_get_string(basic_opts, SDAP_TLS_REQCERT);
if (tls_opt) {
if (strcasecmp(tls_opt, "never") == 0) {
ldap_opt_x_tls_require_cert = LDAP_OPT_X_TLS_NEVER;
}
else if (strcasecmp(tls_opt, "allow") == 0) {
ldap_opt_x_tls_require_cert = LDAP_OPT_X_TLS_ALLOW;
}
else if (strcasecmp(tls_opt, "try") == 0) {
ldap_opt_x_tls_require_cert = LDAP_OPT_X_TLS_TRY;
}
else if (strcasecmp(tls_opt, "demand") == 0) {
ldap_opt_x_tls_require_cert = LDAP_OPT_X_TLS_DEMAND;
}
else if (strcasecmp(tls_opt, "hard") == 0) {
ldap_opt_x_tls_require_cert = LDAP_OPT_X_TLS_HARD;
}
else {
DEBUG(SSSDBG_CRIT_FAILURE,
"Unknown value for tls_reqcert '%s'.\n", tls_opt);
return EINVAL;
}
/* LDAP_OPT_X_TLS_REQUIRE_CERT has to be set as a global option,
* because the SSL/TLS context is initialized from this value. */
ret = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT,
&ldap_opt_x_tls_require_cert);
if (ret != LDAP_OPT_SUCCESS) {
DEBUG(SSSDBG_CRIT_FAILURE,
"ldap_set_option(req_cert) failed: %s\n",
sss_ldap_err2string(ret));
return EIO;
}
}
tls_opt = dp_opt_get_string(basic_opts, SDAP_TLS_CACERT);
if (tls_opt) {
ret = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, tls_opt);
if (ret != LDAP_OPT_SUCCESS) {
DEBUG(SSSDBG_CRIT_FAILURE,
"ldap_set_option(cacertfile) failed: %s\n",
sss_ldap_err2string(ret));
return EIO;
}
}
tls_opt = dp_opt_get_string(basic_opts, SDAP_TLS_CACERTDIR);
if (tls_opt) {
ret = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTDIR, tls_opt);
if (ret != LDAP_OPT_SUCCESS) {
DEBUG(SSSDBG_CRIT_FAILURE,
"ldap_set_option(cacertdir) failed: %s\n",
sss_ldap_err2string(ret));
return EIO;
}
}
tls_opt = dp_opt_get_string(basic_opts, SDAP_TLS_CERT);
if (tls_opt) {
ret = ldap_set_option(NULL, LDAP_OPT_X_TLS_CERTFILE, tls_opt);
if (ret != LDAP_OPT_SUCCESS) {
DEBUG(SSSDBG_CRIT_FAILURE,
"ldap_set_option(certfile) failed: %s\n",
sss_ldap_err2string(ret));
return EIO;
}
}
tls_opt = dp_opt_get_string(basic_opts, SDAP_TLS_KEY);
if (tls_opt) {
ret = ldap_set_option(NULL, LDAP_OPT_X_TLS_KEYFILE, tls_opt);
if (ret != LDAP_OPT_SUCCESS) {
DEBUG(SSSDBG_CRIT_FAILURE,
"ldap_set_option(keyfile) failed: %s\n",
sss_ldap_err2string(ret));
return EIO;
}
}
tls_opt = dp_opt_get_string(basic_opts, SDAP_TLS_CIPHER_SUITE);
if (tls_opt) {
ret = ldap_set_option(NULL, LDAP_OPT_X_TLS_CIPHER_SUITE, tls_opt);
if (ret != LDAP_OPT_SUCCESS) {
DEBUG(SSSDBG_CRIT_FAILURE,
"ldap_set_option(cipher) failed: %s\n",
sss_ldap_err2string(ret));
return EIO;
}
}
return EOK;
}
bool sdap_sasl_mech_needs_kinit(const char *sasl_mech)
{
if (strcasecmp(sasl_mech, "GSSAPI") == 0
|| strcasecmp(sasl_mech, "GSS-SPNEGO") == 0) {
return true;
}
return false;
}
bool sdap_check_sup_list(struct sup_list *l, const char *val)
{
int i;
if (!val) {
return false;
}
for (i = 0; i < l->num_vals; i++) {
if (strcasecmp(val, (char *)l->vals[i])) {
continue;
}
return true;
}
return false;
}
static int sdap_init_sup_list(TALLOC_CTX *memctx,
struct sup_list *list,
int num, struct ldb_val *vals)
{
int i;
list->vals = talloc_array(memctx, char *, num);
if (!list->vals) {
return ENOMEM;
}
for (i = 0; i < num; i++) {
list->vals[i] = talloc_strndup(list->vals,
(char *)vals[i].data, vals[i].length);
if (!list->vals[i]) {
return ENOMEM;
}
}
list->num_vals = num;
return EOK;
}
int sdap_set_rootdse_supported_lists(struct sysdb_attrs *rootdse,
struct sdap_handle *sh)
{
struct ldb_message_element *el = NULL;
int ret;
int i;
for (i = 0; i < rootdse->num; i++) {
el = &rootdse->a[i];
if (strcasecmp(el->name, "supportedControl") == 0) {
ret = sdap_init_sup_list(sh, &sh->supported_controls,
el->num_values, el->values);
if (ret) {
return ret;
}
} else if (strcasecmp(el->name, "supportedExtension") == 0) {
ret = sdap_init_sup_list(sh, &sh->supported_extensions,
el->num_values, el->values);
if (ret) {
return ret;
}
} else if (strcasecmp(el->name, "supportedSASLMechanisms") == 0) {
ret = sdap_init_sup_list(sh, &sh->supported_saslmechs,
el->num_values, el->values);
if (ret) {
return ret;
}
}
}
return EOK;
}
static char *get_single_value_as_string(TALLOC_CTX *mem_ctx,
struct ldb_message_element *el)
{
char *str = NULL;
if (el->num_values == 0) {
DEBUG(SSSDBG_MINOR_FAILURE, "Missing value.\n");
} else if (el->num_values == 1) {
str = talloc_strndup(mem_ctx, (char *) el->values[0].data,
el->values[0].length);
if (str == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "talloc_strndup failed.\n");
}
} else {
DEBUG(SSSDBG_MINOR_FAILURE, "More than one value found.\n");
}
return str;
}
static char *get_naming_context(TALLOC_CTX *mem_ctx,
struct sysdb_attrs *rootdse)
{
struct ldb_message_element *nc = NULL;
struct ldb_message_element *dnc = NULL;
int i;
char *naming_context = NULL;
for (i = 0; i < rootdse->num; i++) {
if (strcasecmp(rootdse->a[i].name,
SDAP_ROOTDSE_ATTR_NAMING_CONTEXTS) == 0) {
nc = &rootdse->a[i];
} else if (strcasecmp(rootdse->a[i].name,
SDAP_ROOTDSE_ATTR_DEFAULT_NAMING_CONTEXT) == 0) {
dnc = &rootdse->a[i];
}
}
if (dnc == NULL && nc == NULL) {
DEBUG(SSSDBG_MINOR_FAILURE,
"No attributes [%s] or [%s] found in rootDSE.\n",
SDAP_ROOTDSE_ATTR_NAMING_CONTEXTS,
SDAP_ROOTDSE_ATTR_DEFAULT_NAMING_CONTEXT);
} else {
if (dnc != NULL) {
DEBUG(SSSDBG_FUNC_DATA,
"Using value from [%s] as naming context.\n",
SDAP_ROOTDSE_ATTR_DEFAULT_NAMING_CONTEXT);
naming_context = get_single_value_as_string(mem_ctx, dnc);
}
if (naming_context == NULL && nc != NULL) {
DEBUG(SSSDBG_FUNC_DATA,
"Using value from [%s] as naming context.\n",
SDAP_ROOTDSE_ATTR_NAMING_CONTEXTS);
naming_context = get_single_value_as_string(mem_ctx, nc);
}
}
/* Some directory servers such as Novell eDirectory will return
* a zero-length namingContexts value in some situations. In this
* case, we should return it as NULL so things fail gracefully.
*/
if (naming_context && naming_context[0] == '\0') {
talloc_zfree(naming_context);
}
return naming_context;
}
errno_t
sdap_create_search_base(TALLOC_CTX *mem_ctx,
struct ldb_context *ldb,
const char *unparsed_base,
int scope,
const char *filter,
struct sdap_search_base **_base)
{
struct sdap_search_base *base;
TALLOC_CTX *tmp_ctx;
errno_t ret;
struct ldb_dn *ldn;
tmp_ctx = talloc_new(NULL);
if (!tmp_ctx) {
ret = ENOMEM;
goto done;
}
base = talloc_zero(tmp_ctx, struct sdap_search_base);
if (base == NULL) {
ret = ENOMEM;
goto done;
}
base->basedn = talloc_strdup(base, unparsed_base);
if (base->basedn == NULL) {
ret = ENOMEM;
goto done;
}
/* Validate the basedn */
ldn = ldb_dn_new(base, ldb, unparsed_base);
if (!ldn) {
ret = ENOMEM;
goto done;
}
if (!ldb_dn_validate(ldn)) {
DEBUG(SSSDBG_CRIT_FAILURE, "Invalid base DN [%s]\n", unparsed_base);
ret = EINVAL;
goto done;
}
base->ldb = ldb;
base->ldb_basedn = ldn;
base->scope = scope;
base->filter = filter;
*_base = talloc_steal(mem_ctx, base);
ret = EOK;
done:
talloc_free(tmp_ctx);
return ret;
}
static errno_t sdap_set_search_base(struct sdap_options *opts,
struct sdap_domain *sdom,
enum sdap_basic_opt class,
char *naming_context)
{
errno_t ret;
struct sdap_search_base ***bases;
switch(class) {
case SDAP_SEARCH_BASE:
bases = &sdom->search_bases;
break;
case SDAP_USER_SEARCH_BASE:
bases = &sdom->user_search_bases;
break;
case SDAP_GROUP_SEARCH_BASE:
bases = &sdom->group_search_bases;
break;
case SDAP_NETGROUP_SEARCH_BASE:
bases = &sdom->netgroup_search_bases;
break;
case SDAP_HOST_SEARCH_BASE:
bases = &sdom->host_search_bases;
break;
case SDAP_SUDO_SEARCH_BASE:
bases = &sdom->sudo_search_bases;
break;
case SDAP_SERVICE_SEARCH_BASE:
bases = &sdom->service_search_bases;
break;
case SDAP_AUTOFS_SEARCH_BASE:
bases = &sdom->autofs_search_bases;
break;
case SDAP_IPHOST_SEARCH_BASE:
bases = &sdom->iphost_search_bases;
break;
case SDAP_IPNETWORK_SEARCH_BASE:
bases = &sdom->ipnetwork_search_bases;
break;
default:
return EINVAL;
}
DEBUG(SSSDBG_CONF_SETTINGS,
"Setting option [%s] to [%s].\n",
opts->basic[class].opt_name, naming_context);
ret = dp_opt_set_string(opts->basic, class, naming_context);
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "dp_opt_set_string failed.\n");
goto done;
}
ret = sdap_parse_search_base(opts, sysdb_ctx_get_ldb(sdom->dom->sysdb),
opts->basic, class, bases);
if (ret != EOK) goto done;
ret = EOK;
done:
return ret;
}
errno_t sdap_set_config_options_with_rootdse(struct sysdb_attrs *rootdse,
struct sdap_options *opts,
struct sdap_domain *sdom)
{
int ret;
if (!sdom->naming_context) {
sdom->naming_context = get_naming_context(sdom, rootdse);
if (sdom->naming_context == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "get_naming_context failed.\n");
/* This has to be non-fatal, since some servers offer
* multiple namingContexts entries. We will just
* add NULL checks for the search bases in the lookups.
*/
ret = EOK;
goto done;
}
}
/* Default */
if (!sdom->search_bases) {
ret = sdap_set_search_base(opts, sdom,
SDAP_SEARCH_BASE,
sdom->naming_context);
if (ret != EOK) goto done;
}
/* Users */
if (!sdom->user_search_bases) {
ret = sdap_set_search_base(opts, sdom,
SDAP_USER_SEARCH_BASE,
sdom->naming_context);
if (ret != EOK) goto done;
}
/* Groups */
if (!sdom->group_search_bases) {
ret = sdap_set_search_base(opts, sdom,
SDAP_GROUP_SEARCH_BASE,
sdom->naming_context);
if (ret != EOK) goto done;
}
/* Netgroups */
if (!sdom->netgroup_search_bases) {
ret = sdap_set_search_base(opts, sdom,
SDAP_NETGROUP_SEARCH_BASE,
sdom->naming_context);
if (ret != EOK) goto done;
}
/* Hosts */
if (!sdom->host_search_bases) {
ret = sdap_set_search_base(opts, sdom,
SDAP_HOST_SEARCH_BASE,
sdom->naming_context);
if (ret != EOK) goto done;
}
/* Sudo */
if (!sdom->sudo_search_bases) {
ret = sdap_set_search_base(opts, sdom,
SDAP_SUDO_SEARCH_BASE,
sdom->naming_context);
if (ret != EOK) goto done;
}
/* Services */
if (!sdom->service_search_bases) {
ret = sdap_set_search_base(opts, sdom,
SDAP_SERVICE_SEARCH_BASE,
sdom->naming_context);
if (ret != EOK) goto done;
}
/* autofs */
if (!sdom->autofs_search_bases) {
ret = sdap_set_search_base(opts, sdom,
SDAP_AUTOFS_SEARCH_BASE,
sdom->naming_context);
if (ret != EOK) goto done;
}
/* IP host */
if (!sdom->iphost_search_bases) {
ret = sdap_set_search_base(opts, sdom,
SDAP_IPHOST_SEARCH_BASE,
sdom->naming_context);
if (ret != EOK) goto done;
}
/* IP network */
if (!sdom->ipnetwork_search_bases) {
ret = sdap_set_search_base(opts, sdom,
SDAP_IPNETWORK_SEARCH_BASE,
sdom->naming_context);
if (ret != EOK) goto done;
}
ret = EOK;
done:
return ret;
}
int sdap_get_server_opts_from_rootdse(TALLOC_CTX *memctx,
const char *server,
struct sysdb_attrs *rootdse,
struct sdap_options *opts,
struct sdap_server_opts **srv_opts)
{
struct sdap_server_opts *so;
struct {
const char *last_name;
const char *entry_name;
} usn_attrs[] = { { SDAP_IPA_LAST_USN, SDAP_IPA_USN },
{ SDAP_AD_LAST_USN, SDAP_AD_USN },
{ NULL, NULL } };
const char *last_usn_name;
const char *last_usn_value;
const char *entry_usn_name;
const char *schema_nc = NULL;
char *endptr = NULL;
int ret;
int i;
uint32_t dc_level;
so = talloc_zero(memctx, struct sdap_server_opts);
if (!so) {
return ENOMEM;
}
so->server_id = talloc_strdup(so, server);
if (!so->server_id) {
talloc_zfree(so);
return ENOMEM;
}
last_usn_name = opts->gen_map[SDAP_AT_LAST_USN].name;
entry_usn_name = opts->gen_map[SDAP_AT_ENTRY_USN].name;
if (rootdse) {
if (last_usn_name) {
ret = sysdb_attrs_get_string(rootdse,
last_usn_name, &last_usn_value);
if (ret != EOK) {
switch (ret) {
case ENOENT:
DEBUG(SSSDBG_CRIT_FAILURE,
"%s configured but not found in rootdse!\n",
opts->gen_map[SDAP_AT_LAST_USN].opt_name);
break;
case ERANGE:
DEBUG(SSSDBG_CRIT_FAILURE,
"Multiple values of %s found in rootdse!\n",
opts->gen_map[SDAP_AT_LAST_USN].opt_name);
break;
default:
DEBUG(SSSDBG_CRIT_FAILURE,
"Unknown error (%d) checking rootdse!\n", ret);
}
} else {
if (!entry_usn_name) {
DEBUG(SSSDBG_CRIT_FAILURE,
"%s found in rootdse but %s is not set!\n",
last_usn_name,
opts->gen_map[SDAP_AT_ENTRY_USN].opt_name);
} else {
so->supports_usn = true;
errno = 0;
so->last_usn = strtoul(last_usn_value, &endptr, 10);
if (errno || !endptr || *endptr || (endptr == last_usn_value)) {
DEBUG(SSSDBG_MINOR_FAILURE,
"USN is not valid (value: %s)\n", last_usn_value);
so->last_usn = 0;
} else {
DEBUG(SSSDBG_TRACE_ALL,
"USN value: %s (int: %lu)\n", last_usn_value, so->last_usn);
}
}
}
} else {
/* no usn option configure, let's try to autodetect. */
for (i = 0; usn_attrs[i].last_name; i++) {
ret = sysdb_attrs_get_string(rootdse,
usn_attrs[i].last_name,
&last_usn_value);
if (ret == EOK) {
/* Fixate discovered configuration */
opts->gen_map[SDAP_AT_LAST_USN].name =
talloc_strdup(opts->gen_map, usn_attrs[i].last_name);
opts->gen_map[SDAP_AT_ENTRY_USN].name =
talloc_strdup(opts->gen_map, usn_attrs[i].entry_name);
so->supports_usn = true;
errno = 0;
so->last_usn = strtoul(last_usn_value, &endptr, 10);
if (errno || !endptr || *endptr || (endptr == last_usn_value)) {
DEBUG(SSSDBG_MINOR_FAILURE,
"USN is not valid (value: %s)\n", last_usn_value);
so->last_usn = 0;
} else {
DEBUG(SSSDBG_TRACE_ALL,
"USN value: %s (int: %lu)\n", last_usn_value, so->last_usn);
}
last_usn_name = usn_attrs[i].last_name;
break;
}
}
}
/* Detect Active Directory version if available */
ret = sysdb_attrs_get_uint32_t(rootdse,
SDAP_ROOTDSE_ATTR_AD_VERSION,
&dc_level);
if (ret == EOK) {
/* Validate that the DC level matches an expected value */
switch(dc_level) {
case DS_BEHAVIOR_WIN2000:
case DS_BEHAVIOR_WIN2003:
case DS_BEHAVIOR_WIN2008:
case DS_BEHAVIOR_WIN2008R2:
case DS_BEHAVIOR_WIN2012:
case DS_BEHAVIOR_WIN2012R2:
case DS_BEHAVIOR_WIN2016:
opts->dc_functional_level = dc_level;
DEBUG(SSSDBG_CONF_SETTINGS,
"Setting AD compatibility level to [%d]\n",
opts->dc_functional_level);
break;
default:
DEBUG(SSSDBG_MINOR_FAILURE,
"Received invalid value [%d] for AD compatibility level. "
"Using the lowest-common compatibility level\n",
dc_level);
opts->dc_functional_level = DS_BEHAVIOR_WIN2003;
}
} else if (ret != ENOENT) {
DEBUG(SSSDBG_MINOR_FAILURE,
"Error detecting Active Directory compatibility level "
"(%s). Continuing without AD performance enhancements\n",
strerror(ret));
}
ret = sysdb_attrs_get_string(rootdse,
SDAP_ROOTDSE_ATTR_AD_SCHEMA_NC,
&schema_nc);
if (ret == EOK) {
DEBUG(SSSDBG_CONF_SETTINGS,
"Will look for schema at [%s]\n", schema_nc);
opts->schema_basedn = talloc_strdup(opts, schema_nc);
}
}
if (!last_usn_name) {
DEBUG(SSSDBG_FUNC_DATA,
"No known USN scheme is supported by this server!\n");
if (!entry_usn_name) {
DEBUG(SSSDBG_FUNC_DATA,
"Will use modification timestamp as usn!\n");
opts->gen_map[SDAP_AT_ENTRY_USN].name =
talloc_strdup(opts->gen_map, "modifyTimestamp");
}
}
if (!opts->user_map[SDAP_AT_USER_USN].name) {
opts->user_map[SDAP_AT_USER_USN].name =
talloc_strdup(opts->user_map,
opts->gen_map[SDAP_AT_ENTRY_USN].name);
}
if (!opts->group_map[SDAP_AT_GROUP_USN].name) {
opts->group_map[SDAP_AT_GROUP_USN].name =
talloc_strdup(opts->group_map,
opts->gen_map[SDAP_AT_ENTRY_USN].name);
}
if (!opts->service_map[SDAP_AT_SERVICE_USN].name) {
opts->service_map[SDAP_AT_SERVICE_USN].name =
talloc_strdup(opts->service_map,
opts->gen_map[SDAP_AT_ENTRY_USN].name);
}
if (opts->sudorule_map &&
!opts->sudorule_map[SDAP_AT_SUDO_USN].name) {
opts->sudorule_map[SDAP_AT_SUDO_USN].name =
talloc_strdup(opts->sudorule_map,
opts->gen_map[SDAP_AT_ENTRY_USN].name);
}
if (opts->iphost_map &&
!opts->iphost_map[SDAP_AT_IPHOST_USN].name) {
opts->iphost_map[SDAP_AT_IPHOST_USN].name =
talloc_strdup(opts->iphost_map,
opts->gen_map[SDAP_AT_ENTRY_USN].name);
}
if (opts->ipnetwork_map &&
!opts->ipnetwork_map[SDAP_AT_IPNETWORK_USN].name) {
opts->ipnetwork_map[SDAP_AT_IPNETWORK_USN].name =
talloc_strdup(opts->ipnetwork_map,
opts->gen_map[SDAP_AT_ENTRY_USN].name);
}
*srv_opts = so;
return EOK;
}
void sdap_steal_server_opts(struct sdap_id_ctx *id_ctx,
struct sdap_server_opts **srv_opts)
{
if (!id_ctx || !srv_opts || !*srv_opts) {
return;
}
if (!id_ctx->srv_opts) {
id_ctx->srv_opts = talloc_move(id_ctx, srv_opts);
return;
}
/* discard if same as previous so we do not reset max usn values
* unnecessarily, only update last_usn. */
if (strcmp(id_ctx->srv_opts->server_id, (*srv_opts)->server_id) == 0) {
id_ctx->srv_opts->last_usn = (*srv_opts)->last_usn;
talloc_zfree(*srv_opts);
return;
}
talloc_zfree(id_ctx->srv_opts);
id_ctx->srv_opts = talloc_move(id_ctx, srv_opts);
}
static bool attr_is_filtered(const char *attr, const char **filter)
{
int i;
if (filter) {
i = 0;
while (filter[i]) {
if (filter[i] == attr ||
strcasecmp(filter[i], attr) == 0) {
return true;
}
i++;
}
}
return false;
}
int build_attrs_from_map(TALLOC_CTX *memctx,
struct sdap_attr_map *map,
size_t size,
const char **filter,
const char ***_attrs,
size_t *attr_count)
{
errno_t ret;
const char **attrs;
int i, j;
TALLOC_CTX *tmp_ctx = talloc_new(NULL);
if (!tmp_ctx) return ENOMEM;
/* Assume that all entries in the map have values */
attrs = talloc_zero_array(tmp_ctx, const char *, size + 1);
if (!attrs) {
ret = ENOMEM;
goto done;
}
/* first attribute is "objectclass" not the specific one */
attrs[0] = talloc_strdup(memctx, "objectClass");
if (!attrs[0]) return ENOMEM;
/* add the others */
for (i = j = 1; i < size; i++) {
if (map[i].name && !attr_is_filtered(map[i].name, filter)) {
attrs[j] = map[i].name;
j++;
}
}
attrs[j] = NULL;
/* Trim down the used memory if some attributes were NULL */
attrs = talloc_realloc(tmp_ctx, attrs, const char *, j + 1);
if (!attrs) {
ret = ENOMEM;
goto done;
}
*_attrs = talloc_steal(memctx, attrs);
if (attr_count) *attr_count = j;
ret = EOK;
done:
talloc_free(tmp_ctx);
return ret;
}
int sdap_control_create(struct sdap_handle *sh, const char *oid, int iscritical,
struct berval *value, int dupval, LDAPControl **ctrlp)
{
int ret;
if (sdap_is_control_supported(sh, oid)) {
ret = sss_ldap_control_create(oid, iscritical, value, dupval, ctrlp);
if (ret != LDAP_SUCCESS) {
DEBUG(SSSDBG_CRIT_FAILURE,
"sss_ldap_control_create failed [%d][%s].\n",
ret, sss_ldap_err2string(ret));
}
} else {
DEBUG(SSSDBG_MINOR_FAILURE,
"Server does not support the requested control [%s].\n", oid);
ret = LDAP_NOT_SUPPORTED;
}
return ret;
}
int sdap_replace_id(struct sysdb_attrs *entry, const char *attr, id_t val)
{
char *str;
errno_t ret;
struct ldb_message_element *el;
ret = sysdb_attrs_get_el_ext(entry, attr, false, &el);
if (ret == ENOENT) {
return sysdb_attrs_add_uint32(entry, attr, val);
} else if (ret) {
DEBUG(SSSDBG_OP_FAILURE, "Cannot get attribute [%s]\n", attr);
return ret;
}
if (el->num_values != 1) {
DEBUG(SSSDBG_OP_FAILURE,
"Expected 1 value for %s, got %d\n", attr, el->num_values);
return EINVAL;
}
str = talloc_asprintf(entry, "%llu", (unsigned long long) val);
if (!str) {
return ENOMEM;
}
el->values[0].data = (uint8_t *) str;
el->values[0].length = strlen(str);
return EOK;
}
static errno_t sdap_get_rdn_multi(TALLOC_CTX *mem_ctx, const char *dn,
const char *name, char **_val)
{
int ret;
size_t c;
LDAPDN ldapdn = NULL;
ret = ldap_str2dn(dn, &ldapdn, LDAP_DN_FORMAT_LDAPV3);
if (ret != LDAP_SUCCESS || ldapdn == NULL || ldapdn[0] == NULL) {
DEBUG(SSSDBG_OP_FAILURE, "Failed to parse DN [%s].\n", dn);
ret = EINVAL;
goto done;
}
ret = ENOENT;
for (c = 0; ldapdn[0][c] != NULL; c++) {
if (strncasecmp(name, ldapdn[0][c]->la_attr.bv_val,
ldapdn[0][c]->la_attr.bv_len) == 0) {
*_val = talloc_strndup(mem_ctx, ldapdn[0][c]->la_value.bv_val,
ldapdn[0][c]->la_value.bv_len);
if (*_val == NULL) {
DEBUG(SSSDBG_OP_FAILURE, "Failed to copy AVA value.\n");
ret = ENOMEM;
goto done;
}
ret = EOK;
break;
}
}
done:
ldap_dnfree(ldapdn);
return ret;
}
errno_t sdap_get_primary_name(const char *attr_name,
struct sysdb_attrs *attrs,
const char **_primary_name)
{
errno_t ret;
const char *orig_name = NULL;
char *rdn_val = NULL;
struct ldb_message_element *sysdb_name_el;
struct ldb_message_element *orig_dn_el;
size_t i;
TALLOC_CTX *tmp_ctx = NULL;
tmp_ctx = talloc_new(NULL);
if (!tmp_ctx) {
return ENOMEM;
}
ret = sysdb_attrs_get_el(attrs,
SYSDB_NAME,
&sysdb_name_el);
if (ret != EOK || sysdb_name_el->num_values == 0) {
ret = EINVAL;
goto done;
}
if (sysdb_name_el->num_values == 1) {
/* Entry contains only one name. Just return that */
orig_name = (const char *)sysdb_name_el->values[0].data;
ret = EOK;
goto done;
}
/* Multiple values for name. Check whether one matches the RDN */
ret = sysdb_attrs_get_el(attrs, SYSDB_ORIG_DN, &orig_dn_el);
if (ret) {
goto done;
}
if (orig_dn_el->num_values == 0) {
DEBUG(SSSDBG_CRIT_FAILURE, "Original DN is not available.\n");
ret = EINVAL;
goto done;
} else if (orig_dn_el->num_values == 1) {
ret = sdap_get_rdn_multi(tmp_ctx,
(const char *) orig_dn_el->values[0].data,
attr_name, &rdn_val);
if (ret == ENOENT) {
DEBUG(SSSDBG_MINOR_FAILURE,
"The entry has multiple names and the RDN attribute does "
"not match. Will use the first value [%s] as fallback.\n",
(const char *)sysdb_name_el->values[0].data);
orig_name = (const char *)sysdb_name_el->values[0].data;
ret = EOK;
goto done;
} else if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "Could not get rdn from [%s]\n",
(const char *) orig_dn_el->values[0].data);
goto done;
}
} else {
DEBUG(SSSDBG_CRIT_FAILURE, "Should not have more than one origDN\n");
ret = EINVAL;
goto done;
}
for (i = 0; i < sysdb_name_el->num_values; i++) {
if (strcasecmp(rdn_val,
(const char *)sysdb_name_el->values[i].data) == 0) {
/* This name matches the RDN. Use it */
break;
}
}
if (i < sysdb_name_el->num_values) {
/* Match was found */
orig_name = (const char *)sysdb_name_el->values[i].data;
} else {
/* If we can't match the name to the RDN, we just have to
* throw up our hands. There's no deterministic way to
* decide which name is correct.
*/
DEBUG(SSSDBG_CRIT_FAILURE,
"Can't match the name to the RDN\n");
ret = EINVAL;
goto done;
}
ret = EOK;
done:
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE,
"Could not determine primary name: [%d][%s]\n",
ret, strerror(ret));
}
talloc_free(tmp_ctx);
DEBUG(SSSDBG_TRACE_FUNC, "Processing object %s\n", orig_name);
*_primary_name = orig_name;
return ret;
}
static errno_t
sdap_get_primary_fqdn(TALLOC_CTX *mem_ctx,
struct sdap_idmap_ctx *idmap_ctx,
const char *attr_name,
const char *sid_attr_name,
struct sysdb_attrs *attrs,
struct sss_domain_info *dom,
const char **_primary_fqdn)
{
errno_t ret;
const char *shortname = NULL;
const char *primary_fqdn = NULL;
TALLOC_CTX *tmp_ctx;
char *sid_str = NULL;
struct sss_domain_info *subdomain = NULL;
tmp_ctx = talloc_new(NULL);
if (tmp_ctx == NULL) {
return ENOMEM;
}
ret = sdap_get_primary_name(attr_name, attrs, &shortname);
if (ret != EOK) {
goto done;
}
/* In AD scenarion, the object can be from subdomain - identify it by SID */
if (sid_attr_name != NULL) {
ret = sdap_attrs_get_sid_str(tmp_ctx,
idmap_ctx,
attrs,
sid_attr_name,
&sid_str);
if (ret == EOK) {
DEBUG(SSSDBG_TRACE_INTERNAL, "Group has objectSID [%s]\n", sid_str);
subdomain = find_domain_by_sid(dom, sid_str);
talloc_free(sid_str);
if (subdomain != NULL) {
dom = subdomain;
}
}
DEBUG(SSSDBG_TRACE_INTERNAL, "Group has name [%s]\n", dom->name);
}
primary_fqdn = sss_create_internal_fqname(tmp_ctx, shortname, dom->name);
if (primary_fqdn == NULL) {
ret = ENOMEM;
goto done;
}
ret = EOK;
*_primary_fqdn = talloc_steal(mem_ctx, primary_fqdn);
done:
talloc_free(tmp_ctx);
return ret;
}
errno_t sdap_get_user_primary_name(TALLOC_CTX *memctx,
struct sdap_options *opts,
struct sysdb_attrs *attrs,
struct sss_domain_info *dom,
const char **_user_name)
{
return sdap_get_primary_fqdn(memctx,
opts->idmap_ctx,
opts->user_map[SDAP_AT_USER_NAME].name,
opts->group_map[SDAP_AT_USER_OBJECTSID].name,
attrs, dom, _user_name);
}
errno_t sdap_get_group_primary_name(TALLOC_CTX *memctx,
struct sdap_options *opts,
struct sysdb_attrs *attrs,
struct sss_domain_info *dom,
const char **_group_name)
{
return sdap_get_primary_fqdn(memctx,
opts->idmap_ctx,
opts->group_map[SDAP_AT_GROUP_NAME].name,
opts->group_map[SDAP_AT_GROUP_OBJECTSID].name,
attrs, dom, _group_name);
}
errno_t sdap_get_netgroup_primary_name(struct sdap_options *opts,
struct sysdb_attrs *attrs,
const char **_netgroup_name)
{
return sdap_get_primary_name(opts->netgroup_map[SDAP_AT_NETGROUP_NAME].name,
attrs, _netgroup_name);
}
static errno_t
_sdap_get_primary_name_list(struct sss_domain_info *domain,
TALLOC_CTX *mem_ctx,
struct sysdb_attrs **attr_list,
size_t attr_count,
const char *ldap_attr,
bool qualify_names,
const char *sid_attr,
struct sdap_idmap_ctx *idmap_ctx,
char ***name_list)
{
errno_t ret;
size_t i, j;
char **list;
const char *name;
/* Assume that every entry has a primary name */
list = talloc_array(mem_ctx, char *, attr_count+1);
if (!list) {
return ENOMEM;
}
j = 0;
for (i = 0; i < attr_count; i++) {
if (qualify_names == false) {
ret = sdap_get_primary_name(ldap_attr, attr_list[i], &name);
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "Could not determine primary name\n");
/* Skip and continue. Don't advance 'j' */
continue;
}
list[j] = talloc_strdup(list, name);
} else {
ret = sdap_get_primary_fqdn(mem_ctx,
idmap_ctx,
ldap_attr,
sid_attr,
attr_list[i],
domain,
&name);
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "Could not determine primary fqdn name\n");
/* Skip and continue. Don't advance 'j' */
continue;
}
list[j] = talloc_strdup(list, name);
}
if (!list[j]) {
ret = ENOMEM;
goto done;
}
j++;
}
/* NULL-terminate the list */
list[j] = NULL;
*name_list = list;
ret = EOK;
done:
if (ret != EOK) {
talloc_free(list);
}
return ret;
}
errno_t sdap_get_primary_name_list(struct sss_domain_info *domain,
TALLOC_CTX *mem_ctx,
struct sysdb_attrs **attr_list,
size_t attr_count,
const char *ldap_attr,
char ***name_list)
{
return _sdap_get_primary_name_list(domain, mem_ctx, attr_list, attr_count,
ldap_attr, false, NULL, NULL, name_list);
}
errno_t sdap_get_primary_fqdn_list(struct sss_domain_info *domain,
TALLOC_CTX *mem_ctx,
struct sysdb_attrs **attr_list,
size_t attr_count,
const char *ldap_attr,
const char *sid_attr,
struct sdap_idmap_ctx *idmap_ctx,
char ***name_list)
{
return _sdap_get_primary_name_list(domain, mem_ctx, attr_list, attr_count,
ldap_attr, true, sid_attr, idmap_ctx, name_list);
}
char *sdap_make_oc_list(TALLOC_CTX *mem_ctx, struct sdap_attr_map *map)
{
if (map[SDAP_OC_GROUP_ALT].name == NULL) {
return talloc_asprintf(mem_ctx, "objectClass=%s",
map[SDAP_OC_GROUP].name);
} else {
return talloc_asprintf(mem_ctx,
"|(objectClass=%s)(objectClass=%s)",
map[SDAP_OC_GROUP].name,
map[SDAP_OC_GROUP_ALT].name);
}
}
struct sss_domain_info *sdap_get_object_domain(struct sdap_options *opts,
struct sysdb_attrs *obj,
struct sss_domain_info *dom)
{
errno_t ret;
const char *original_dn = NULL;
struct sdap_domain *sdmatch = NULL;
ret = sysdb_attrs_get_string(obj, SYSDB_ORIG_DN, &original_dn);
if (ret) {
DEBUG(SSSDBG_FUNC_DATA,
"The group has no original DN, assuming our domain\n");
return dom;
}
sdmatch = sdap_domain_get_by_dn(opts, original_dn);
if (sdmatch == NULL) {
DEBUG(SSSDBG_FUNC_DATA,
"The original DN of the group cannot "
"be related to any search base\n");
return dom;
}
return sdmatch->dom;
}
bool sdap_object_in_domain(struct sdap_options *opts,
struct sysdb_attrs *obj,
struct sss_domain_info *dom)
{
struct sss_domain_info *obj_dom;
obj_dom = sdap_get_object_domain(opts, obj, dom);
if (obj_dom == NULL) {
return false;
}
return (obj_dom == dom);
}
size_t sdap_steal_objects_in_dom(struct sdap_options *opts,
struct sysdb_attrs **dom_objects,
size_t offset,
struct sss_domain_info *dom,
struct sysdb_attrs **all_objects,
size_t count,
bool filter)
{
size_t copied = 0;
/* Own objects from all_objects by dom_objects in case they belong
* to domain dom.
*
* Don't copy objects from other domains in case
* the search was for parent domain but a child domain would match,
* too, such as:
* dc=example,dc=com
* dc=child,dc=example,dc=com
* while searching for an object from dc=example.
*/
for (size_t i = 0; i < count; i++) {
if (filter &&
sdap_object_in_domain(opts, all_objects[i], dom) == false) {
continue;
}
dom_objects[offset + copied] =
talloc_steal(dom_objects, all_objects[i]);
copied++;
}
return copied;
}
void sdap_domain_copy_search_bases(struct sdap_domain *to,
struct sdap_domain *from)
{
to->search_bases = from->search_bases;
to->user_search_bases = from->user_search_bases;
to->group_search_bases = from->group_search_bases;
to->netgroup_search_bases = from->netgroup_search_bases;
to->sudo_search_bases = from->sudo_search_bases;
to->service_search_bases = from->service_search_bases;
to->iphost_search_bases = from->iphost_search_bases;
to->ipnetwork_search_bases = from->ipnetwork_search_bases;
to->autofs_search_bases = from->autofs_search_bases;
}
|