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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2012 Milan Jurik. All rights reserved.
* Copyright 2012 Nexenta Systems, Inc. All rights reserved.
*/
#define __STANDALONE_MODULE__
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <libintl.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <syslog.h>
#include <locale.h>
#include <errno.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <strings.h>
#include <thread.h>
#include <nsswitch.h>
#include <nss_dbdefs.h>
#include <nss.h>
#include "ns_cache_door.h"
#include "ns_internal.h"
#include "ns_connmgmt.h"
typedef enum {
INFO_SERVER_JUST_INITED = -1,
INFO_SERVER_UNKNOWN = 0,
INFO_SERVER_CONNECTING = 1,
INFO_SERVER_UP = 2,
INFO_SERVER_ERROR = 3,
INFO_SERVER_REMOVED = 4
} dir_server_status_t;
typedef enum {
INFO_STATUS_NEW = 2,
INFO_STATUS_OLD = 3
} dir_server_info_t;
typedef struct dir_server {
char *ip;
char **controls;
char **saslMech;
dir_server_status_t status;
mutex_t updateStatus;
dir_server_info_t info;
} dir_server_t;
typedef struct dir_server_list {
dir_server_t **nsServers;
rwlock_t listDestroyLock;
} dir_server_list_t;
struct {
/* The local list of the directory servers' root DSEs. */
dir_server_list_t *list;
/* The flag indicating if libsldap is in the 'Standalone' mode. */
int standalone;
/*
* The mutex ensuring that only one thread performs
* the initialization of the list.
*/
mutex_t listReplaceLock;
/*
* A flag indicating that a particular thread is
* in the 'ldap_cachemgr' mode. It is stored by thread as
* a thread specific data.
*/
const int initFlag;
/*
* A thread specific key storing
* the the 'ldap_cachemgr' mode indicator.
*/
thread_key_t standaloneInitKey;
} dir_servers = {NULL, 0, DEFAULTMUTEX, '1'};
typedef struct switchDatabase {
char *conf;
uint32_t alloced;
} switch_database_t;
static thread_key_t switchConfigKey;
#pragma init(createStandaloneKey)
#define DONT_INCLUDE_ATTR_NAMES 0
#define INCLUDE_ATTR_NAMES 1
#define IS_PROFILE 1
#define NOT_PROFILE 0
/* INET6_ADDRSTRLEN + ":" + <5-digit port> + some round-up */
#define MAX_HOSTADDR_LEN (INET6_ADDRSTRLEN + 6 + 12)
static
void
switch_conf_disposer(void *data)
{
switch_database_t *localData = (switch_database_t *)data;
free(localData->conf);
free(localData);
}
/*
* This function initializes an indication that a thread obtaining a root DSE
* will be switched to the 'ldap_cachemgr' mode. Within the thread libsldap
* will not invoke the __s_api_requestServer function. Instead, the library
* will establish a connection to the server specified by
* the __ns_ldap_getRootDSE function.
* Since ldap_cachmgr can obtain a DUAProfile and root DSEs at the same time
* and we do not want to affect a thread obtaining a DUAProfile,
* the 'ldap_cachemgr' mode is thread private.
* In addition, this function creates a key holding temporary configuration
* for the "hosts" and "ipnodes" databases which is used by the "SKIPDB"
* mechanism (__s_api_ip2hostname() & _s_api_hostname2ip()).
*/
static
void
createStandaloneKey()
{
if (thr_keycreate(&dir_servers.standaloneInitKey, NULL) != 0) {
syslog(LOG_ERR, gettext("libsldap: unable to create a thread "
"key needed for sharing ldap connections"));
}
if (thr_keycreate(&switchConfigKey, switch_conf_disposer) != 0) {
syslog(LOG_ERR, gettext("libsldap: unable to create a thread "
"key containing current nsswitch configuration"));
}
}
/*
* This function sets the 'ldap_cachemgr' mode indication.
*/
void
__s_api_setInitMode()
{
(void) thr_setspecific(dir_servers.standaloneInitKey,
(void *) &dir_servers.initFlag);
}
/*
* This function unset the 'ldap_cachemgr' mode indication.
*/
void
__s_api_unsetInitMode()
{
(void) thr_setspecific(dir_servers.standaloneInitKey, NULL);
}
/*
* This function checks if the 'ldap_cachemgr' mode indication is set.
*/
int
__s_api_isInitializing() {
int *flag = NULL;
(void) thr_getspecific(dir_servers.standaloneInitKey, (void **) &flag);
return (flag != NULL && *flag == dir_servers.initFlag);
}
/*
* This function checks if the process runs in the 'Standalone' mode.
* In this mode libsldap will check the local, process private list of root DSEs
* instead of requesting them via a door call to ldap_cachemgr.
*/
int
__s_api_isStandalone()
{
int mode;
(void) mutex_lock(&dir_servers.listReplaceLock);
mode = dir_servers.standalone;
(void) mutex_unlock(&dir_servers.listReplaceLock);
return (mode);
}
static
int
remove_ldap(char *dst, char *src, int dst_buf_len)
{
int i = 0;
if (strlen(src) >= dst_buf_len)
return (0);
while (*src != '\0') {
/* Copy up to one space from source. */
if (isspace(*src)) {
dst[i++] = *src;
while (isspace(*src))
src++;
}
/* If not "ldap", just copy. */
if (strncmp(src, "ldap", 4) != 0) {
while (!isspace(*src)) {
dst[i++] = *src++;
/* At the end of string? */
if (dst[i-1] == '\0')
return (1);
}
/* Copy up to one space from source. */
if (isspace(*src)) {
dst[i++] = *src;
while (isspace(*src))
src++;
}
/* Copy also the criteria section */
if (*src == '[')
while (*src != ']') {
dst[i++] = *src++;
/* Shouln't happen if format is right */
if (dst[i-1] == '\0')
return (1);
}
}
/* If next part is ldap, skip over it ... */
if (strncmp(src, "ldap", 4) == 0) {
if (isspace(*(src+4)) || *(src+4) == '\0') {
src += 4;
while (isspace(*src))
src++;
if (*src == '[') {
while (*src++ != ']') {
/*
* See comment above about
* correct format.
*/
if (*src == '\0') {
dst[i++] = '\0';
return (1);
}
}
}
while (isspace(*src))
src++;
}
}
if (*src == '\0')
dst[i++] = '\0';
}
return (1);
}
static
char *
get_db(const char *db_name)
{
char *ptr;
switch_database_t *hostService = NULL;
FILE *fp = fopen(__NSW_CONFIG_FILE, "rF");
char *linep, line[NSS_BUFSIZ];
if (fp == NULL) {
syslog(LOG_WARNING, gettext("libsldap: can not read %s"),
__NSW_CONFIG_FILE);
return (NULL);
}
while ((linep = fgets(line, NSS_BUFSIZ, fp)) != NULL) {
while (isspace(*linep)) {
++linep;
}
if (*linep == '#') {
continue;
}
if (strncmp(linep, db_name, strlen(db_name)) != 0) {
continue;
}
if ((linep = strchr(linep, ':')) != NULL) {
if (linep[strlen(linep) - 1] == '\n') {
linep[strlen(linep) - 1] = '\0';
}
while (isspace(*++linep))
;
if ((ptr = strchr(linep, '#')) != NULL) {
while (--ptr >= linep && isspace(*ptr))
;
*(ptr + 1) = '\0';
}
if (strlen(linep) == 0) {
continue;
}
break;
}
}
(void) fclose(fp);
if (linep == NULL) {
syslog(LOG_WARNING,
gettext("libsldap: the %s database "
"is missing from %s"),
db_name,
__NSW_CONFIG_FILE);
return (NULL);
}
(void) thr_getspecific(switchConfigKey, (void **) &hostService);
if (hostService == NULL) {
hostService = calloc(1, sizeof (switch_database_t));
if (hostService == NULL) {
return (NULL);
}
(void) thr_setspecific(switchConfigKey, hostService);
}
/*
* In a long-living process threads can perform several
* getXbyY requests. And the windows between those requests
* can be long. The nsswitch configuration can change from time
* to time. So instead of allocating/freeing memory every time
* the API is called, reallocate memory only when the current
* configuration for the database being used is longer than
* the previous one.
*/
if (strlen(linep) >= hostService->alloced) {
ptr = (char *)realloc((void *)hostService->conf,
strlen(linep) + 1);
if (ptr == NULL) {
free((void *)hostService->conf);
hostService->conf = NULL;
hostService->alloced = 0;
return (NULL);
}
bzero(ptr, strlen(linep) + 1);
hostService->conf = ptr;
hostService->alloced = strlen(linep) + 1;
}
if (remove_ldap(hostService->conf, linep, hostService->alloced))
return (hostService->conf);
else
return (NULL);
}
static
void
_initf_ipnodes(nss_db_params_t *p)
{
char *services = get_db("ipnodes");
p->name = NSS_DBNAM_IPNODES;
p->flags |= NSS_USE_DEFAULT_CONFIG;
p->default_config = services == NULL ? "" : services;
}
static
void
_initf_hosts(nss_db_params_t *p)
{
char *services = get_db("hosts");
p->name = NSS_DBNAM_HOSTS;
p->flags |= NSS_USE_DEFAULT_CONFIG;
p->default_config = services == NULL ? "" : services;
}
/*
* This function is an analog of the standard gethostbyaddr_r()
* function with an exception that it removes the 'ldap' back-end
* (if any) from the host/ipnodes nsswitch's databases and then
* looks up using remaining back-ends.
*/
static
struct hostent *
_filter_gethostbyaddr_r(const char *addr, int len, int type,
struct hostent *result, char *buffer, int buflen,
int *h_errnop)
{
DEFINE_NSS_DB_ROOT(db_root_hosts);
DEFINE_NSS_DB_ROOT(db_root_ipnodes);
nss_XbyY_args_t arg;
nss_status_t res;
int (*str2ent)();
void (*nss_initf)();
nss_db_root_t *nss_db_root;
int dbop;
switch (type) {
case AF_INET:
str2ent = str2hostent;
nss_initf = _initf_hosts;
nss_db_root = &db_root_hosts;
dbop = NSS_DBOP_HOSTS_BYADDR;
break;
case AF_INET6:
str2ent = str2hostent6;
nss_initf = _initf_ipnodes;
nss_db_root = &db_root_ipnodes;
dbop = NSS_DBOP_IPNODES_BYADDR;
break;
default:
return (NULL);
}
NSS_XbyY_INIT(&arg, result, buffer, buflen, str2ent);
arg.key.hostaddr.addr = addr;
arg.key.hostaddr.len = len;
arg.key.hostaddr.type = type;
arg.stayopen = 0;
arg.h_errno = NETDB_SUCCESS;
res = nss_search(nss_db_root, nss_initf, dbop, &arg);
arg.status = res;
*h_errnop = arg.h_errno;
return (struct hostent *)NSS_XbyY_FINI(&arg);
}
/*
* This routine is an analog of gethostbyaddr_r().
* But in addition __s_api_hostname2ip() performs the "LDAP SKIPDB" activity
* prior to querying the name services.
* If the buffer is not big enough to accommodate a returning data,
* NULL is returned and h_errnop is set to TRY_AGAIN.
*/
struct hostent *
__s_api_hostname2ip(const char *name,
struct hostent *result, char *buffer, int buflen,
int *h_errnop)
{
DEFINE_NSS_DB_ROOT(db_root_ipnodes);
DEFINE_NSS_DB_ROOT(db_root_hosts);
nss_XbyY_args_t arg;
nss_status_t res;
struct in_addr addr;
struct in6_addr addr6;
if (inet_pton(AF_INET, name, &addr) > 0) {
if (buflen < strlen(name) + 1 +
sizeof (char *) * 2 + /* The h_aliases member */
sizeof (struct in_addr) +
sizeof (struct in_addr *) * 2) {
*h_errnop = TRY_AGAIN;
return (NULL);
}
result->h_addrtype = AF_INET;
result->h_length = sizeof (struct in_addr);
(void) strncpy(buffer, name, buflen);
result->h_addr_list = (char **)ROUND_UP(
buffer + strlen(name) + 1,
sizeof (char *));
result->h_aliases = (char **)ROUND_UP(result->h_addr_list,
sizeof (char *));
result->h_aliases[0] = buffer;
result->h_aliases[1] = NULL;
bcopy(&addr,
buffer + buflen - sizeof (struct in_addr),
sizeof (struct in_addr));
result->h_addr_list[0] = buffer + buflen -
sizeof (struct in_addr);
result->h_addr_list[1] = NULL;
result->h_aliases = result->h_addr_list;
result->h_name = buffer;
*h_errnop = NETDB_SUCCESS;
return (result);
}
if (inet_pton(AF_INET6, name, &addr6) > 0) {
if (buflen < strlen(name) + 1 +
sizeof (char *) * 2 + /* The h_aliases member */
sizeof (struct in6_addr) +
sizeof (struct in6_addr *) * 2) {
*h_errnop = TRY_AGAIN;
return (NULL);
}
result->h_addrtype = AF_INET6;
result->h_length = sizeof (struct in6_addr);
(void) strncpy(buffer, name, buflen);
result->h_addr_list = (char **)ROUND_UP(
buffer + strlen(name) + 1,
sizeof (char *));
result->h_aliases = (char **)ROUND_UP(result->h_addr_list,
sizeof (char *));
result->h_aliases[0] = buffer;
result->h_aliases[1] = NULL;
bcopy(&addr6,
buffer + buflen - sizeof (struct in6_addr),
sizeof (struct in6_addr));
result->h_addr_list[0] = buffer + buflen -
sizeof (struct in6_addr);
result->h_addr_list[1] = NULL;
result->h_aliases = result->h_addr_list;
result->h_name = buffer;
*h_errnop = NETDB_SUCCESS;
return (result);
}
NSS_XbyY_INIT(&arg, result, buffer, buflen, str2hostent);
arg.key.name = name;
arg.stayopen = 0;
arg.h_errno = NETDB_SUCCESS;
res = nss_search(&db_root_ipnodes, _initf_ipnodes,
NSS_DBOP_IPNODES_BYNAME, &arg);
if (res == NSS_NOTFOUND || res == NSS_UNAVAIL) {
arg.h_errno = NETDB_SUCCESS;
res = nss_search(&db_root_hosts, _initf_hosts,
NSS_DBOP_HOSTS_BYNAME, &arg);
}
arg.status = res;
*h_errnop = arg.h_errno;
return ((struct hostent *)NSS_XbyY_FINI(&arg));
}
/*
* Convert an IP to a host name.
*/
ns_ldap_return_code
__s_api_ip2hostname(char *ipaddr, char **hostname) {
struct in_addr in;
struct in6_addr in6;
struct hostent *hp = NULL, hostEnt;
char buffer[NSS_BUFLEN_HOSTS];
int buflen = NSS_BUFLEN_HOSTS;
char *start = NULL,
*end = NULL,
delim = '\0';
char *port = NULL,
*addr = NULL;
int errorNum = 0,
len = 0;
if (ipaddr == NULL || hostname == NULL)
return (NS_LDAP_INVALID_PARAM);
*hostname = NULL;
if ((addr = strdup(ipaddr)) == NULL)
return (NS_LDAP_MEMORY);
if (addr[0] == '[') {
/*
* Assume it's [ipv6]:port
* Extract ipv6 IP
*/
start = &addr[1];
if ((end = strchr(addr, ']')) != NULL) {
*end = '\0';
delim = ']';
if (*(end + 1) == ':')
/* extract port */
port = end + 2;
} else {
free(addr);
return (NS_LDAP_INVALID_PARAM);
}
} else if ((end = strchr(addr, ':')) != NULL) {
/* assume it's ipv4:port */
*end = '\0';
delim = ':';
start = addr;
port = end + 1;
} else
/* No port */
start = addr;
if (inet_pton(AF_INET, start, &in) == 1) {
/* IPv4 */
hp = _filter_gethostbyaddr_r((char *)&in,
sizeof (in.s_addr),
AF_INET,
&hostEnt,
buffer,
buflen,
&errorNum);
if (hp && hp->h_name) {
/* hostname + '\0' */
len = strlen(hp->h_name) + 1;
if (port)
/* ':' + port */
len += strlen(port) + 1;
if ((*hostname = malloc(len)) == NULL) {
free(addr);
return (NS_LDAP_MEMORY);
}
if (port)
(void) snprintf(*hostname, len, "%s:%s",
hp->h_name, port);
else
(void) strlcpy(*hostname, hp->h_name, len);
free(addr);
return (NS_LDAP_SUCCESS);
} else {
free(addr);
return (NS_LDAP_NOTFOUND);
}
} else if (inet_pton(AF_INET6, start, &in6) == 1) {
/* IPv6 */
hp = _filter_gethostbyaddr_r((char *)&in6,
sizeof (in6.s6_addr),
AF_INET6,
&hostEnt,
buffer,
buflen,
&errorNum);
if (hp && hp->h_name) {
/* hostname + '\0' */
len = strlen(hp->h_name) + 1;
if (port)
/* ':' + port */
len += strlen(port) + 1;
if ((*hostname = malloc(len)) == NULL) {
free(addr);
return (NS_LDAP_MEMORY);
}
if (port)
(void) snprintf(*hostname, len, "%s:%s",
hp->h_name, port);
else
(void) strlcpy(*hostname, hp->h_name, len);
free(addr);
return (NS_LDAP_SUCCESS);
} else {
free(addr);
return (NS_LDAP_NOTFOUND);
}
} else {
/*
* A hostname
* Return it as is
*/
if (end)
*end = delim;
*hostname = addr;
return (NS_LDAP_SUCCESS);
}
}
/*
* This function obtains data returned by an LDAP search request and puts it
* in a string in the ldap_cachmgr(1) door call format.
*
* INPUT:
* ld - a pointer to an LDAP structure used for a search operation,
* result_msg - a pointer to an LDAPMessage returned by the search,
* include_names - if set to INCLUDE_ATTR_NAMES, the output buffer will
* contain attribute names.
* Otherwise, only values will be return.
*
* OUTPUT:
* a buffer containing server info in the following format:
* [<attribute name>=]value [DOORLINESEP [<attribute name>=]value ]...]
* Should be free'ed by the caller.
*/
static
ns_ldap_return_code
convert_to_door_line(LDAP* ld,
LDAPMessage *result_msg,
int include_names,
int is_profile,
char **door_line)
{
uint32_t total_length = 0, attr_len = 0, i;
LDAPMessage *e;
char *a, **vals;
BerElement *ber;
int seen_objectclass = 0, rewind = 0;
if (!door_line) {
return (NS_LDAP_INVALID_PARAM);
}
*door_line = NULL;
if ((e = ldap_first_entry(ld, result_msg)) == NULL) {
return (NS_LDAP_NOTFOUND);
}
/* calculate length of received data */
for (a = ldap_first_attribute(ld, e, &ber);
a != NULL;
a = ldap_next_attribute(ld, e, ber)) {
if ((vals = ldap_get_values(ld, e, a)) != NULL) {
for (i = 0; vals[i] != NULL; i++) {
total_length += (include_names ?
strlen(a) : 0) +
strlen(vals[i]) +
strlen(DOORLINESEP) +1;
}
ldap_value_free(vals);
}
ldap_memfree(a);
}
if (ber != NULL) {
ber_free(ber, 0);
}
if (total_length == 0) {
return (NS_LDAP_NOTFOUND);
}
/* copy the data */
/* add 1 for the last '\0' */
*door_line = (char *)malloc(total_length + 1);
if (*door_line == NULL) {
return (NS_LDAP_MEMORY);
}
/* make it an empty string first */
**door_line = '\0';
a = ldap_first_attribute(ld, e, &ber);
while (a != NULL) {
if (is_profile) {
/*
* If we're processing DUAConfigProfile, we need to make
* sure we put objectclass attribute first.
* __s_api_create_config_door_str depends on that.
*/
if (seen_objectclass) {
if (strcasecmp(a, "objectclass") == 0) {
/* Skip objectclass now. */
a = ldap_next_attribute(ld, e, ber);
continue;
}
} else {
if (strcasecmp(a, "objectclass") == 0) {
seen_objectclass = 1;
rewind = 1;
} else {
/* Skip all but objectclass first. */
a = ldap_next_attribute(ld, e, ber);
continue;
}
}
}
if ((vals = ldap_get_values(ld, e, a)) != NULL) {
for (i = 0; vals[i] != NULL; i++) {
if (include_names) {
attr_len += strlen(a);
}
attr_len += strlen(vals[i]) +
strlen(DOORLINESEP) + 2;
if (include_names) {
(void) snprintf(*door_line +
strlen(*door_line),
attr_len,
"%s=%s%s",
a, vals[i],
DOORLINESEP);
} else {
(void) snprintf(*door_line +
strlen(*door_line),
attr_len,
"%s%s",
vals[i],
DOORLINESEP);
}
}
ldap_value_free(vals);
}
ldap_memfree(a);
/* Rewind */
if (rewind) {
if (ber != NULL) {
ber_free(ber, 0);
}
a = ldap_first_attribute(ld, e, &ber);
rewind = 0;
} else {
a = ldap_next_attribute(ld, e, ber);
}
}
if (ber != NULL) {
ber_free(ber, 0);
}
if (e != result_msg) {
(void) ldap_msgfree(e);
}
return (NS_LDAP_SUCCESS);
}
/*
* This function looks up the base DN of a directory serving
* a specified domain name.
*
* INPUT:
* ld - a pointer to an LDAP structure used for the search operation,
* domain_name - the name of a domain.
*
* OUTPUT:
* a buffer containing a directory's base DN found.
* Should be free'ed by the caller.
*/
static
ns_ldap_return_code
getDirBaseDN(LDAP *ld, const char *domain_name, char **dir_base_dn)
{
struct timeval tv = {NS_DEFAULT_SEARCH_TIMEOUT, 0};
char *attrs[2], *DNlist, *rest, *ptr;
char filter[BUFSIZ], *a = NULL;
int ldap_rc;
LDAPMessage *resultMsg = NULL;
ns_ldap_return_code ret_code;
/* Get the whole list of naming contexts residing on the server */
attrs[0] = "namingcontexts";
attrs[1] = NULL;
ldap_rc = ldap_search_ext_s(ld, "", LDAP_SCOPE_BASE, "(objectclass=*)",
attrs, 0, NULL, NULL, &tv, 0, &resultMsg);
switch (ldap_rc) {
/* If successful, the root DSE was found. */
case LDAP_SUCCESS:
break;
/*
* If the root DSE was not found, the server does
* not comply with the LDAP v3 protocol.
*/
default:
if (resultMsg) {
(void) ldap_msgfree(resultMsg);
resultMsg = NULL;
}
return (NS_LDAP_OP_FAILED);
}
if ((ret_code = convert_to_door_line(ld,
resultMsg,
DONT_INCLUDE_ATTR_NAMES,
NOT_PROFILE,
&DNlist)) != NS_LDAP_SUCCESS) {
if (resultMsg) {
(void) ldap_msgfree(resultMsg);
resultMsg = NULL;
}
return (ret_code);
}
if (resultMsg) {
(void) ldap_msgfree(resultMsg);
resultMsg = NULL;
}
if (DNlist == NULL ||
(ptr = strtok_r(DNlist, DOORLINESEP, &rest)) == NULL) {
return (NS_LDAP_NOTFOUND);
}
attrs[0] = "dn";
do {
/*
* For each context try to find a NIS domain object
* which 'nisdomain' attribute's value matches the domain name
*/
(void) snprintf(filter,
BUFSIZ,
"(&(objectclass=nisDomainObject)"
"(nisdomain=%s))",
domain_name);
ldap_rc = ldap_search_ext_s(ld,
ptr,
LDAP_SCOPE_SUBTREE,
filter,
attrs,
0,
NULL,
NULL,
&tv,
0,
&resultMsg);
if (ldap_rc != LDAP_SUCCESS) {
if (resultMsg) {
(void) ldap_msgfree(resultMsg);
resultMsg = NULL;
}
continue;
}
if ((a = ldap_get_dn(ld, resultMsg)) != NULL) {
*dir_base_dn = strdup(a);
ldap_memfree(a);
if (resultMsg) {
(void) ldap_msgfree(resultMsg);
resultMsg = NULL;
}
if (!*dir_base_dn) {
free(DNlist);
return (NS_LDAP_MEMORY);
}
break;
}
if (resultMsg) {
(void) ldap_msgfree(resultMsg);
resultMsg = NULL;
}
} while (ptr = strtok_r(NULL, DOORLINESEP, &rest));
free(DNlist);
if (!*dir_base_dn) {
return (NS_LDAP_NOTFOUND);
}
return (NS_LDAP_SUCCESS);
}
/*
* This function parses the results of a search operation
* requesting a DUAProfile.
*
* INPUT:
* ld - a pointer to an LDAP structure used for the search operation,
* dir_base_dn - the name of a directory's base DN,
* profile_name - the name of a DUAProfile to be obtained.
*
* OUTPUT:
* a buffer containing the DUAProfile in the following format:
* [<attribute name>=]value [DOORLINESEP [<attribute name>=]value ]...]
* Should be free'ed by the caller.
*/
static
ns_ldap_return_code
getDUAProfile(LDAP *ld,
const char *dir_base_dn,
const char *profile_name,
char **profile)
{
char searchBaseDN[BUFSIZ], filter[BUFSIZ];
LDAPMessage *resultMsg = NULL;
struct timeval tv = {NS_DEFAULT_SEARCH_TIMEOUT, 0};
int ldap_rc;
ns_ldap_return_code ret_code;
(void) snprintf(searchBaseDN, BUFSIZ, "ou=profile,%s", dir_base_dn);
(void) snprintf(filter,
BUFSIZ,
_PROFILE_FILTER,
_PROFILE1_OBJECTCLASS,
_PROFILE2_OBJECTCLASS,
profile_name);
ldap_rc = ldap_search_ext_s(ld,
searchBaseDN,
LDAP_SCOPE_SUBTREE,
filter,
NULL,
0,
NULL,
NULL,
&tv,
0,
&resultMsg);
switch (ldap_rc) {
/* If successful, the DUA profile was found. */
case LDAP_SUCCESS:
break;
/*
* If the root DSE was not found, the server does
* not comply with the LDAP v3 protocol.
*/
default:
if (resultMsg) {
(void) ldap_msgfree(resultMsg);
resultMsg = NULL;
}
return (NS_LDAP_OP_FAILED);
}
ret_code = convert_to_door_line(ld,
resultMsg,
INCLUDE_ATTR_NAMES,
IS_PROFILE,
profile);
if (resultMsg) {
(void) ldap_msgfree(resultMsg);
resultMsg = NULL;
}
return (ret_code);
}
/*
* This function derives the directory's base DN from a provided domain name.
*
* INPUT:
* domain_name - the name of a domain to be converted into a base DN,
* buffer - contains the derived base DN,
* buf_len - the length of the buffer.
*
* OUTPUT:
* The function returns the address of the buffer or NULL.
*/
static
char *
domainname2baseDN(char *domain_name, char *buffer, uint16_t buf_len)
{
char *nextDC, *chr;
uint16_t i, length;
if (!domain_name || !buffer || buf_len == 0) {
return (NULL);
}
buffer[0] = '\0';
nextDC = chr = domain_name;
length = strlen(domain_name);
for (i = 0; i < length + 1; ++i, ++chr) {
/* Simply replace dots with "dc=" */
if (*chr != '.' && *chr != '\0') {
continue;
}
*chr = '\0';
if (strlcat(buffer, "dc=", buf_len) >= buf_len)
return (NULL);
if (strlcat(buffer, nextDC, buf_len) >= buf_len)
return (NULL);
if (i < length) {
/*
* The end of the domain name
* has not been reached yet
*/
if (strlcat(buffer, ",", buf_len) >= buf_len)
return (NULL);
nextDC = chr + 1;
*chr = '.';
}
}
return (buffer);
}
/*
* This function obtains the directory's base DN and a DUAProfile
* from a specified server.
*
* INPUT:
* server - a structure describing a server to connect to and
* a DUAProfile to be obtained from the server,
* cred - credentials to be used during establishing connections to
* the server.
*
* OUTPUT:
* dua_profile - a buffer containing the DUAProfile in the following format:
* [<attribute name>=]value [DOORLINESEP [<attribute name>=]value ]...]
* dir_base_dn - a buffer containing the base DN,
* errorp - an error object describing an error, if any.
*
* All the output data structures should be free'ed by the caller.
*/
ns_ldap_return_code
__ns_ldap_getConnectionInfoFromDUA(const ns_dir_server_t *server,
const ns_cred_t *cred,
char **dua_profile,
char **dir_base_dn,
ns_ldap_error_t **errorp)
{
char serverAddr[MAX_HOSTADDR_LEN];
char *dirBaseDN = NULL, *duaProfile = NULL;
ns_cred_t default_cred;
ns_ldap_return_code ret_code;
ns_config_t *config_struct = __s_api_create_config();
ConnectionID sessionId = 0;
Connection *session = NULL;
char errmsg[MAXERROR];
char buffer[NSS_BUFLEN_HOSTS];
ns_conn_user_t *cu = NULL;
if (errorp == NULL) {
__s_api_destroy_config(config_struct);
return (NS_LDAP_INVALID_PARAM);
}
*errorp = NULL;
if (server == NULL) {
__s_api_destroy_config(config_struct);
return (NS_LDAP_INVALID_PARAM);
}
if (config_struct == NULL) {
return (NS_LDAP_MEMORY);
}
/*
* If no credentials are specified, try to establish a connection
* as anonymous.
*/
if (!cred) {
default_cred.cred.unix_cred.passwd = NULL;
default_cred.cred.unix_cred.userID = NULL;
default_cred.auth.type = NS_LDAP_AUTH_NONE;
}
/* Now create a default LDAP configuration */
(void) strncpy(buffer, server->server, sizeof (buffer));
if (__ns_ldap_setParamValue(config_struct, NS_LDAP_SERVERS_P, buffer,
errorp) != NS_LDAP_SUCCESS) {
__s_api_destroy_config(config_struct);
return (NS_LDAP_CONFIG);
}
/* Put together the address and the port specified by the user app. */
if (server->port > 0) {
(void) snprintf(serverAddr,
sizeof (serverAddr),
"%s:%hu",
buffer,
server->port);
} else {
(void) strncpy(serverAddr, buffer, sizeof (serverAddr));
}
/*
* There is no default value for the 'Default Search Base DN' attribute.
* Derive one from the domain name to make __s_api_crosscheck() happy.
*/
if (domainname2baseDN(server->domainName ?
server->domainName : config_struct->domainName,
buffer, NSS_BUFLEN_HOSTS) == NULL) {
(void) snprintf(errmsg,
sizeof (errmsg),
gettext("Can not convert %s into a base DN name"),
server->domainName ?
server->domainName : config_struct->domainName);
MKERROR(LOG_ERR,
*errorp,
NS_LDAP_INTERNAL,
strdup(errmsg),
NS_LDAP_MEMORY);
__s_api_destroy_config(config_struct);
return (NS_LDAP_INTERNAL);
}
if (__ns_ldap_setParamValue(config_struct, NS_LDAP_SEARCH_BASEDN_P,
buffer, errorp) != NS_LDAP_SUCCESS) {
__s_api_destroy_config(config_struct);
return (NS_LDAP_CONFIG);
}
if (__s_api_crosscheck(config_struct, errmsg, B_FALSE) != NS_SUCCESS) {
__s_api_destroy_config(config_struct);
return (NS_LDAP_CONFIG);
}
__s_api_init_config(config_struct);
__s_api_setInitMode();
cu = __s_api_conn_user_init(NS_CONN_USER_SEARCH, NULL, B_FALSE);
if (cu == NULL) {
return (NS_LDAP_INTERNAL);
}
if ((ret_code = __s_api_getConnection(serverAddr,
NS_LDAP_NEW_CONN,
cred ? cred : &default_cred,
&sessionId,
&session,
errorp,
0,
0,
cu)) != NS_LDAP_SUCCESS) {
__s_api_conn_user_free(cu);
__s_api_unsetInitMode();
return (ret_code);
}
__s_api_unsetInitMode();
if ((ret_code = getDirBaseDN(session->ld,
server->domainName ?
server->domainName :
config_struct->domainName,
&dirBaseDN)) != NS_LDAP_SUCCESS) {
(void) snprintf(errmsg,
sizeof (errmsg),
gettext("Can not find the "
"nisDomainObject for domain %s\n"),
server->domainName ?
server->domainName : config_struct->domainName);
MKERROR(LOG_ERR,
*errorp,
ret_code,
strdup(errmsg),
NS_LDAP_MEMORY);
__s_api_conn_user_free(cu);
DropConnection(sessionId, NS_LDAP_NEW_CONN);
return (ret_code);
}
/*
* And here obtain a DUAProfile which will be used
* as a real configuration.
*/
if ((ret_code = getDUAProfile(session->ld,
dirBaseDN,
server->profileName ?
server->profileName : "default",
&duaProfile)) != NS_LDAP_SUCCESS) {
(void) snprintf(errmsg,
sizeof (errmsg),
gettext("Can not find the "
"%s DUAProfile\n"),
server->profileName ?
server->profileName : "default");
MKERROR(LOG_ERR,
*errorp,
ret_code,
strdup(errmsg),
NS_LDAP_MEMORY);
__s_api_conn_user_free(cu);
DropConnection(sessionId, NS_LDAP_NEW_CONN);
return (ret_code);
}
if (dir_base_dn) {
*dir_base_dn = dirBaseDN;
} else {
free(dirBaseDN);
}
if (dua_profile) {
*dua_profile = duaProfile;
} else {
free(duaProfile);
}
__s_api_conn_user_free(cu);
DropConnection(sessionId, NS_LDAP_NEW_CONN);
return (NS_LDAP_SUCCESS);
}
/*
* This function obtains the root DSE from a specified server.
*
* INPUT:
* server_addr - an adress of a server to be connected to.
*
* OUTPUT:
* root_dse - a buffer containing the root DSE in the following format:
* [<attribute name>=]value [DOORLINESEP [<attribute name>=]value ]...]
* For example: ( here | used as DOORLINESEP for visual purposes)
* supportedControl=1.1.1.1|supportedSASLmechanisms=EXTERNAL
* Should be free'ed by the caller.
*/
ns_ldap_return_code
__ns_ldap_getRootDSE(const char *server_addr,
char **root_dse,
ns_ldap_error_t **errorp,
int anon_fallback)
{
char errmsg[MAXERROR];
ns_ldap_return_code ret_code;
ConnectionID sessionId = 0;
Connection *session = NULL;
struct timeval tv = {NS_DEFAULT_SEARCH_TIMEOUT, 0};
char *attrs[3];
int ldap_rc, ldaperrno = 0;
LDAPMessage *resultMsg = NULL;
void **paramVal = NULL;
ns_cred_t anon;
ns_conn_user_t *cu = NULL;
if (errorp == NULL) {
return (NS_LDAP_INVALID_PARAM);
}
*errorp = NULL;
if (!root_dse) {
return (NS_LDAP_INVALID_PARAM);
}
if (!server_addr) {
return (NS_LDAP_INVALID_PARAM);
}
__s_api_setInitMode();
cu = __s_api_conn_user_init(NS_CONN_USER_SEARCH, NULL, B_FALSE);
if (cu == NULL) {
return (NS_LDAP_INTERNAL);
}
/*
* All the credentials will be taken from the current
* libsldap configuration.
*/
if ((ret_code = __s_api_getConnection(server_addr,
NS_LDAP_NEW_CONN,
NULL,
&sessionId,
&session,
errorp,
0,
0,
cu)) != NS_LDAP_SUCCESS) {
/* Fallback to anonymous mode is disabled. Stop. */
if (anon_fallback == 0) {
syslog(LOG_WARNING,
gettext("libsldap: can not get the root DSE from "
" the %s server: %s. "
"Falling back to anonymous disabled.\n"),
server_addr,
errorp && *errorp && (*errorp)->message ?
(*errorp)->message : "");
if (errorp != NULL && *errorp != NULL) {
(void) __ns_ldap_freeError(errorp);
}
__s_api_unsetInitMode();
return (ret_code);
}
/*
* Fallback to anonymous, non-SSL mode for backward
* compatibility reasons. This mode should only be used when
* this function (__ns_ldap_getRootDSE) is called from
* ldap_cachemgr(1M).
*/
syslog(LOG_WARNING,
gettext("libsldap: Falling back to anonymous, non-SSL"
" mode for __ns_ldap_getRootDSE. %s\n"),
errorp && *errorp && (*errorp)->message ?
(*errorp)->message : "");
/* Setup the anon credential for anonymous connection. */
(void) memset(&anon, 0, sizeof (ns_cred_t));
anon.auth.type = NS_LDAP_AUTH_NONE;
if (*errorp != NULL) {
(void) __ns_ldap_freeError(errorp);
}
*errorp = NULL;
ret_code = __s_api_getConnection(server_addr,
NS_LDAP_NEW_CONN,
&anon,
&sessionId,
&session,
errorp,
0,
0,
cu);
if (ret_code != NS_LDAP_SUCCESS) {
__s_api_conn_user_free(cu);
__s_api_unsetInitMode();
return (ret_code);
}
}
__s_api_unsetInitMode();
/* get search timeout value */
(void) __ns_ldap_getParam(NS_LDAP_SEARCH_TIME_P, ¶mVal, errorp);
if (paramVal != NULL && *paramVal != NULL) {
tv.tv_sec = **((int **)paramVal);
(void) __ns_ldap_freeParam(¶mVal);
}
if (*errorp != NULL) {
(void) __ns_ldap_freeError(errorp);
}
/* Get root DSE from the server specified by the caller. */
attrs[0] = "supportedControl";
attrs[1] = "supportedsaslmechanisms";
attrs[2] = NULL;
ldap_rc = ldap_search_ext_s(session->ld,
"",
LDAP_SCOPE_BASE,
"(objectclass=*)",
attrs,
0,
NULL,
NULL,
&tv,
0,
&resultMsg);
if (ldap_rc != LDAP_SUCCESS) {
/*
* If the root DSE was not found, the server does
* not comply with the LDAP v3 protocol.
*/
(void) ldap_get_option(session->ld,
LDAP_OPT_ERROR_NUMBER,
&ldaperrno);
(void) snprintf(errmsg,
sizeof (errmsg),
gettext(ldap_err2string(ldaperrno)));
MKERROR(LOG_ERR,
*errorp,
NS_LDAP_OP_FAILED,
strdup(errmsg),
NS_LDAP_MEMORY);
if (resultMsg) {
(void) ldap_msgfree(resultMsg);
resultMsg = NULL;
}
__s_api_conn_user_free(cu);
DropConnection(sessionId, NS_LDAP_NEW_CONN);
return (NS_LDAP_OP_FAILED);
}
__s_api_conn_user_free(cu);
ret_code = convert_to_door_line(session->ld,
resultMsg,
INCLUDE_ATTR_NAMES,
NOT_PROFILE,
root_dse);
if (ret_code == NS_LDAP_NOTFOUND) {
(void) snprintf(errmsg,
sizeof (errmsg),
gettext("No root DSE data "
"for server %s returned."),
server_addr);
MKERROR(LOG_ERR,
*errorp,
NS_LDAP_NOTFOUND,
strdup(errmsg),
NS_LDAP_MEMORY);
}
if (resultMsg) {
(void) ldap_msgfree(resultMsg);
resultMsg = NULL;
}
DropConnection(sessionId, NS_LDAP_NEW_CONN);
return (ret_code);
}
/*
* This function destroys the local list of root DSEs. The input parameter is
* a pointer to the list to be erased.
* The type of the pointer passed to this function should be
* (dir_server_list_t *).
*/
static
void *
disposeOfOldList(void *param)
{
dir_server_list_t *old_list = (dir_server_list_t *)param;
long i = 0, j;
(void) rw_wrlock(&old_list->listDestroyLock);
/* Destroy the old list */
while (old_list->nsServers[i]) {
free(old_list->nsServers[i]->ip);
j = 0;
while (old_list->nsServers[i]->controls &&
old_list->nsServers[i]->controls[j]) {
free(old_list->nsServers[i]->controls[j]);
++j;
}
free(old_list->nsServers[i]->controls);
j = 0;
while (old_list->nsServers[i]->saslMech &&
old_list->nsServers[i]->saslMech[j]) {
free(old_list->nsServers[i]->saslMech[j]);
++j;
}
free(old_list->nsServers[i]->saslMech);
++i;
}
/*
* All the structures pointed by old_list->nsServers were allocated
* in one chunck. The nsServers[0] pointer points to the beginning
* of that chunck.
*/
free(old_list->nsServers[0]);
free(old_list->nsServers);
(void) rw_unlock(&old_list->listDestroyLock);
(void) rwlock_destroy(&old_list->listDestroyLock);
free(old_list);
return (NULL);
}
/*
* This function cancels the Standalone mode and destroys the list of root DSEs.
*/
void
__ns_ldap_cancelStandalone(void)
{
dir_server_list_t *old_list;
(void) mutex_lock(&dir_servers.listReplaceLock);
dir_servers.standalone = 0;
if (!dir_servers.list) {
(void) mutex_unlock(&dir_servers.listReplaceLock);
return;
}
old_list = dir_servers.list;
dir_servers.list = NULL;
(void) mutex_unlock(&dir_servers.listReplaceLock);
(void) disposeOfOldList(old_list);
}
static
void*
create_ns_servers_entry(void *param)
{
#define CHUNK_SIZE 16
dir_server_t *server = (dir_server_t *)param;
ns_ldap_return_code *retCode = calloc(1,
sizeof (ns_ldap_return_code));
uint32_t sc_counter = 0, sm_counter = 0;
uint32_t sc_mem_blocks = 1, sm_mem_blocks = 1;
char *rootDSE = NULL, *attr, *val, *rest, **ptr;
ns_ldap_error_t *error = NULL;
if (retCode == NULL) {
return (NULL);
}
/*
* We call this function in non anon-fallback mode because we
* want the whole procedure to fail as soon as possible to
* indicate there are problems with connecting to the server.
*/
*retCode = __ns_ldap_getRootDSE(server->ip,
&rootDSE,
&error,
SA_ALLOW_FALLBACK);
if (*retCode == NS_LDAP_MEMORY) {
free(retCode);
return (NULL);
}
/*
* If the root DSE can not be obtained, log an error and keep the
* server.
*/
if (*retCode != NS_LDAP_SUCCESS) {
server->status = INFO_SERVER_ERROR;
syslog(LOG_WARNING,
gettext("libsldap (\"standalone\" mode): "
"can not obtain the root DSE from %s. %s"),
server->ip,
error && error->message ? error->message : "");
if (error) {
(void) __ns_ldap_freeError(&error);
}
return (retCode);
}
/* Get the first attribute of the root DSE. */
attr = strtok_r(rootDSE, DOORLINESEP, &rest);
if (attr == NULL) {
free(rootDSE);
server->status = INFO_SERVER_ERROR;
syslog(LOG_WARNING,
gettext("libsldap (\"standalone\" mode): "
"the root DSE from %s is empty or corrupted."),
server->ip);
*retCode = NS_LDAP_INTERNAL;
return (retCode);
}
server->controls = (char **)calloc(CHUNK_SIZE, sizeof (char *));
server->saslMech = (char **)calloc(CHUNK_SIZE, sizeof (char *));
if (server->controls == NULL || server->saslMech == NULL) {
free(rootDSE);
free(retCode);
return (NULL);
}
do {
if ((val = strchr(attr, '=')) == NULL) {
continue;
}
++val;
if (strncasecmp(attr,
_SASLMECHANISM,
_SASLMECHANISM_LEN) == 0) {
if (sm_counter == CHUNK_SIZE * sm_mem_blocks - 1) {
ptr = (char **)realloc(server->saslMech,
CHUNK_SIZE *
++sm_mem_blocks *
sizeof (char *));
if (ptr == NULL) {
*retCode = NS_LDAP_MEMORY;
break;
}
bzero((char *)ptr +
(sm_counter + 1) *
sizeof (char *),
CHUNK_SIZE *
sm_mem_blocks *
sizeof (char *) -
(sm_counter + 1) *
sizeof (char *));
server->saslMech = ptr;
}
server->saslMech[sm_counter] = strdup(val);
if (server->saslMech[sm_counter] == NULL) {
*retCode = NS_LDAP_MEMORY;
break;
}
++sm_counter;
continue;
}
if (strncasecmp(attr,
_SUPPORTEDCONTROL,
_SUPPORTEDCONTROL_LEN) == 0) {
if (sc_counter == CHUNK_SIZE * sc_mem_blocks - 1) {
ptr = (char **)realloc(server->controls,
CHUNK_SIZE *
++sc_mem_blocks *
sizeof (char *));
if (ptr == NULL) {
*retCode = NS_LDAP_MEMORY;
break;
}
bzero((char *)ptr +
(sc_counter + 1) *
sizeof (char *),
CHUNK_SIZE *
sc_mem_blocks *
sizeof (char *) -
(sc_counter + 1) *
sizeof (char *));
server->controls = ptr;
}
server->controls[sc_counter] = strdup(val);
if (server->controls[sc_counter] == NULL) {
*retCode = NS_LDAP_MEMORY;
break;
}
++sc_counter;
continue;
}
} while (attr = strtok_r(NULL, DOORLINESEP, &rest));
free(rootDSE);
if (*retCode == NS_LDAP_MEMORY) {
free(retCode);
return (NULL);
}
server->controls[sc_counter] = NULL;
server->saslMech[sm_counter] = NULL;
server->status = INFO_SERVER_UP;
return (retCode);
#undef CHUNK_SIZE
}
/*
* This function creates a new local list of root DSEs from all the servers
* mentioned in the DUAProfile (or local NS BEC) and returns
* a pointer to the list.
*/
static
ns_ldap_return_code
createDirServerList(dir_server_list_t **new_list,
ns_ldap_error_t **errorp)
{
char **serverList;
ns_ldap_return_code retCode = NS_LDAP_SUCCESS;
dir_server_t *tmpSrvArray;
long srvListLength, i;
thread_t *thrPool, thrID;
void *status = NULL;
if (errorp == NULL) {
return (NS_LDAP_INVALID_PARAM);
}
*errorp = NULL;
if (new_list == NULL) {
return (NS_LDAP_INVALID_PARAM);
}
retCode = __s_api_getServers(&serverList, errorp);
if (retCode != NS_LDAP_SUCCESS || serverList == NULL) {
return (retCode);
}
for (i = 0; serverList[i]; ++i) {
;
}
srvListLength = i;
thrPool = calloc(srvListLength, sizeof (thread_t));
if (thrPool == NULL) {
__s_api_free2dArray(serverList);
return (NS_LDAP_MEMORY);
}
*new_list = (dir_server_list_t *)calloc(1,
sizeof (dir_server_list_t));
if (*new_list == NULL) {
__s_api_free2dArray(serverList);
free(thrPool);
return (NS_LDAP_MEMORY);
}
(void) rwlock_init(&(*new_list)->listDestroyLock, USYNC_THREAD, NULL);
(*new_list)->nsServers = (dir_server_t **)calloc(srvListLength + 1,
sizeof (dir_server_t *));
if ((*new_list)->nsServers == NULL) {
free(*new_list);
*new_list = NULL;
__s_api_free2dArray(serverList);
free(thrPool);
return (NS_LDAP_MEMORY);
}
/*
* Allocate a set of dir_server_t structures as an array,
* with one alloc call and then initialize the nsServers pointers
* with the addresses of the array's members.
*/
tmpSrvArray = (dir_server_t *)calloc(srvListLength,
sizeof (dir_server_t));
for (i = 0; i < srvListLength; ++i) {
(*new_list)->nsServers[i] = &tmpSrvArray[i];
(*new_list)->nsServers[i]->info = INFO_STATUS_NEW;
(void) mutex_init(&(*new_list)->nsServers[i]->updateStatus,
USYNC_THREAD,
NULL);
(*new_list)->nsServers[i]->ip = strdup(serverList[i]);
if ((*new_list)->nsServers[i]->ip == NULL) {
retCode = NS_LDAP_MEMORY;
break;
}
(*new_list)->nsServers[i]->status = INFO_SERVER_CONNECTING;
switch (thr_create(NULL,
0,
create_ns_servers_entry,
(*new_list)->nsServers[i],
0,
&thrID)) {
case EAGAIN:
(*new_list)->nsServers[i]->status =
INFO_SERVER_ERROR;
continue;
case ENOMEM:
(*new_list)->nsServers[i]->status =
INFO_SERVER_ERROR;
continue;
default:
thrPool[i] = thrID;
continue;
}
}
for (i = 0; i < srvListLength; ++i) {
if (thrPool[i] != 0 &&
thr_join(thrPool[i], NULL, &status) == 0) {
if (status == NULL) {
/*
* Some memory allocation problems occured. Just
* ignore the server and hope there will be some
* other good ones.
*/
(*new_list)->nsServers[i]->status =
INFO_SERVER_ERROR;
}
free(status);
}
}
__s_api_free2dArray(serverList);
free(thrPool);
if (retCode == NS_LDAP_MEMORY) {
(void) disposeOfOldList(*new_list);
return (NS_LDAP_MEMORY);
}
return (NS_LDAP_SUCCESS);
}
/*
* This functions replaces the local list of root DSEs with a new one and starts
* a thread destroying the old list. There is no need for other threads to wait
* until the old list will be destroyed.
* Since it is possible that more than one thread can start creating the list,
* this function should be protected by mutexes to be sure that only one thread
* performs the initialization.
*/
static
ns_ldap_return_code
initGlobalList(ns_ldap_error_t **error)
{
dir_server_list_t *new_list, *old_list;
ns_ldap_return_code ret_code;
thread_t tid;
ret_code = createDirServerList(&new_list, error);
if (ret_code != NS_LDAP_SUCCESS) {
return (ret_code);
}
old_list = dir_servers.list;
dir_servers.list = new_list;
if (old_list) {
(void) thr_create(NULL,
0,
disposeOfOldList,
old_list,
THR_DETACHED,
&tid);
}
return (NS_LDAP_SUCCESS);
}
static
struct {
char *authMech;
ns_auth_t auth;
} authArray[] = {{"none", {NS_LDAP_AUTH_NONE,
NS_LDAP_TLS_NONE,
NS_LDAP_SASL_NONE,
NS_LDAP_SASLOPT_NONE}},
{"simple", {NS_LDAP_AUTH_SIMPLE,
NS_LDAP_TLS_NONE,
NS_LDAP_SASL_NONE,
NS_LDAP_SASLOPT_NONE}},
{"tls:simple", {NS_LDAP_AUTH_TLS,
NS_LDAP_TLS_SIMPLE,
NS_LDAP_SASL_NONE,
NS_LDAP_SASLOPT_NONE}},
{"tls:sasl/CRAM-MD5", {NS_LDAP_AUTH_TLS,
NS_LDAP_TLS_SASL,
NS_LDAP_SASL_CRAM_MD5,
NS_LDAP_SASLOPT_NONE}},
{"tls:sasl/DIGEST-MD5", {NS_LDAP_AUTH_TLS,
NS_LDAP_TLS_SASL,
NS_LDAP_SASL_DIGEST_MD5,
NS_LDAP_SASLOPT_NONE}},
{"sasl/CRAM-MD5", {NS_LDAP_AUTH_SASL,
NS_LDAP_TLS_SASL,
NS_LDAP_SASL_CRAM_MD5,
NS_LDAP_SASLOPT_NONE}},
{"sasl/DIGEST-MD5", {NS_LDAP_AUTH_SASL,
NS_LDAP_TLS_SASL,
NS_LDAP_SASL_DIGEST_MD5,
NS_LDAP_SASLOPT_NONE}},
{"sasl/GSSAPI", {NS_LDAP_AUTH_SASL,
NS_LDAP_TLS_SASL,
NS_LDAP_SASL_GSSAPI,
NS_LDAP_SASLOPT_PRIV | NS_LDAP_SASLOPT_INT}},
{NULL, {NS_LDAP_AUTH_NONE,
NS_LDAP_TLS_NONE,
NS_LDAP_SASL_NONE,
NS_LDAP_SASLOPT_NONE}}};
ns_ldap_return_code
__ns_ldap_initAuth(const char *auth_mech,
ns_auth_t *auth,
ns_ldap_error_t **errorp)
{
uint32_t i;
char errmsg[MAXERROR];
if (auth_mech == NULL) {
(void) snprintf(errmsg,
sizeof (errmsg),
gettext("Invalid authentication method specified\n"));
MKERROR(LOG_WARNING,
*errorp,
NS_LDAP_INTERNAL,
strdup(errmsg),
NS_LDAP_MEMORY);
return (NS_LDAP_INTERNAL);
}
for (i = 0; authArray[i].authMech != NULL; ++i) {
if (strcasecmp(auth_mech, authArray[i].authMech) == 0) {
*auth = authArray[i].auth;
return (NS_LDAP_SUCCESS);
}
}
(void) snprintf(errmsg,
sizeof (errmsg),
gettext("Invalid authentication method specified\n"));
MKERROR(LOG_WARNING,
*errorp,
NS_LDAP_INTERNAL,
strdup(errmsg),
NS_LDAP_MEMORY);
return (NS_LDAP_INTERNAL);
}
/*
* This function "informs" libsldap that a client application has specified
* a directory to use. The function obtains a DUAProfile, credentials,
* and naming context. During all further operations on behalf
* of the application requested a standalone schema libsldap will use
* the information obtained by __ns_ldap_initStandalone() instead of
* door_call(3C)ing ldap_cachemgr(1M).
*
* INPUT:
* sa_conf - a structure describing where and in which way to obtain all
* the configuration describing how to communicate to
* a choosen LDAP directory,
* errorp - an error object describing an error occured.
*/
ns_ldap_return_code
__ns_ldap_initStandalone(const ns_standalone_conf_t *sa_conf,
ns_ldap_error_t **errorp) {
ns_cred_t user_cred = {{NS_LDAP_AUTH_NONE,
NS_LDAP_TLS_NONE,
NS_LDAP_SASL_NONE,
NS_LDAP_SASLOPT_NONE},
NULL,
{NULL, NULL}};
char *dua_profile = NULL;
char errmsg[MAXERROR];
ns_config_t *cfg;
int ret_code;
if (sa_conf->SA_BIND_DN == NULL && sa_conf->SA_BIND_PWD != NULL ||
sa_conf->SA_BIND_DN != NULL && sa_conf->SA_BIND_PWD == NULL) {
(void) snprintf(errmsg,
sizeof (errmsg),
gettext("Bind DN and bind password"
" must both be provided\n"));
MKERROR(LOG_ERR,
*errorp,
NS_CONFIG_NOTLOADED,
strdup(errmsg),
NS_LDAP_MEMORY);
return (NS_LDAP_INTERNAL);
}
switch (sa_conf->type) {
case NS_LDAP_SERVER:
if (sa_conf->SA_BIND_DN != NULL) {
user_cred.cred.unix_cred.userID = sa_conf->SA_BIND_DN;
user_cred.auth.type = NS_LDAP_AUTH_SIMPLE;
}
if (sa_conf->SA_BIND_PWD != NULL) {
user_cred.cred.unix_cred.passwd = sa_conf->SA_BIND_PWD;
}
if (sa_conf->SA_AUTH != NULL) {
user_cred.auth.type = sa_conf->SA_AUTH->type;
user_cred.auth.tlstype = sa_conf->SA_AUTH->tlstype;
user_cred.auth.saslmech = sa_conf->SA_AUTH->saslmech;
user_cred.auth.saslopt = sa_conf->SA_AUTH->saslopt;
}
if (sa_conf->SA_CERT_PATH != NULL) {
user_cred.hostcertpath = sa_conf->SA_CERT_PATH;
}
ret_code = __ns_ldap_getConnectionInfoFromDUA(
&sa_conf->ds_profile.server,
&user_cred,
&dua_profile,
NULL,
errorp);
if (ret_code != NS_LDAP_SUCCESS) {
return (ret_code);
}
cfg = __s_api_create_config_door_str(dua_profile, errorp);
if (cfg == NULL) {
free(dua_profile);
return (NS_LDAP_CONFIG);
}
if (sa_conf->SA_CERT_PATH != NULL) {
char *certPathAttr;
ParamIndexType type;
switch (cfg->version) {
case NS_LDAP_V1:
certPathAttr = "NS_LDAP_CERT_PATH";
break;
default: /* Version 2 */
certPathAttr = "NS_LDAP_HOST_CERTPATH";
break;
}
if (__s_api_get_versiontype(cfg,
certPathAttr,
&type) == 0 &&
(ret_code = __ns_ldap_setParamValue(cfg,
type,
sa_conf->SA_CERT_PATH,
errorp)) != NS_LDAP_SUCCESS) {
__s_api_destroy_config(cfg);
return (ret_code);
}
}
if (sa_conf->SA_BIND_DN != NULL &&
sa_conf->SA_BIND_PWD != NULL) {
char *authMethods;
authMethods = __s_api_strValue(cfg, NS_LDAP_AUTH_P,
NS_FILE_FMT);
if (authMethods != NULL &&
strstr(authMethods, "sasl/GSSAPI") != NULL) {
/*
* The received DUAProfile specifies
* sasl/GSSAPI as an auth. mechanism.
* The bind DN and password will be
* ignored.
*/
syslog(LOG_INFO, gettext("sasl/GSSAPI will be "
"used as an authentication method. "
"The bind DN and password will "
"be ignored.\n"));
free(authMethods);
break;
}
if (authMethods != NULL)
free(authMethods);
if (__ns_ldap_setParamValue(cfg,
NS_LDAP_BINDDN_P,
sa_conf->SA_BIND_DN,
errorp) != NS_LDAP_SUCCESS) {
__s_api_destroy_config(cfg);
return (NS_LDAP_CONFIG);
}
if (__ns_ldap_setParamValue(cfg,
NS_LDAP_BINDPASSWD_P,
sa_conf->SA_BIND_PWD,
errorp) != NS_LDAP_SUCCESS) {
__s_api_destroy_config(cfg);
return (NS_LDAP_CONFIG);
}
}
break;
default: /* NS_CACHEMGR */
return (NS_LDAP_SUCCESS);
}
__s_api_init_config(cfg);
/* Connection management should use the new config now. */
__s_api_reinit_conn_mgmt_new_config(cfg);
__ns_ldap_setServer(TRUE);
(void) mutex_lock(&dir_servers.listReplaceLock);
if ((ret_code = initGlobalList(errorp)) != NS_SUCCESS) {
(void) mutex_unlock(&dir_servers.listReplaceLock);
return (ret_code);
}
dir_servers.standalone = 1;
(void) mutex_unlock(&dir_servers.listReplaceLock);
return (NS_LDAP_SUCCESS);
}
/*
* INPUT:
* serverAddr is the address of a server and
* request is one of the following:
* NS_CACHE_NEW: get a new server address, addr is ignored.
* NS_CACHE_NORESP: get the next one, remove addr from list.
* NS_CACHE_NEXT: get the next one, keep addr on list.
* NS_CACHE_WRITE: get a non-replica server, if possible, if not, same
* as NS_CACHE_NEXT.
* addrType:
* NS_CACHE_ADDR_IP: return server address as is, this is default.
* NS_CACHE_ADDR_HOSTNAME: return server addess as FQDN format, only
* self credential case requires such format.
* OUTPUT:
* ret
*
* a structure of type ns_server_info_t containing the server address
* or name, server controls and supported SASL mechanisms.
* NOTE: Caller should allocate space for the structure and free
* all the space allocated by the function for the information contained
* in the structure.
*
* error - an error object describing an error, if any.
*/
ns_ldap_return_code
__s_api_findRootDSE(const char *request,
const char *serverAddr,
const char *addrType,
ns_server_info_t *ret,
ns_ldap_error_t **error)
{
dir_server_list_t *current_list = NULL;
ns_ldap_return_code ret_code;
long i = 0;
int matched = FALSE;
dir_server_t *server = NULL;
char errmsg[MAXERROR];
(void) mutex_lock(&dir_servers.listReplaceLock);
if (dir_servers.list == NULL) {
(void) mutex_unlock(&dir_servers.listReplaceLock);
(void) snprintf(errmsg,
sizeof (errmsg),
gettext("The list of root DSEs is empty: "
"the Standalone mode was not properly initialized"));
MKERROR(LOG_ERR,
*error,
NS_CONFIG_NOTLOADED,
strdup(errmsg),
NS_LDAP_MEMORY);
return (NS_LDAP_INTERNAL);
}
current_list = dir_servers.list;
(void) rw_rdlock(¤t_list->listDestroyLock);
(void) mutex_unlock(&dir_servers.listReplaceLock);
/*
* The code below is mostly the clone of the
* ldap_cachemgr::cachemgr_getldap.c::getldap_get_serverInfo() function.
* Currently we have two different server lists: one is maintained
* by libsldap ('standalone' mode), the other is in ldap_cachemgr
* (a part of its standard functionality).
*/
/*
* If NS_CACHE_NEW, or the server info is new,
* starts from the beginning of the list.
*/
(void) mutex_lock(¤t_list->nsServers[0]->updateStatus);
if (strcmp(request, NS_CACHE_NEW) == 0 ||
current_list->nsServers[0]->info == INFO_STATUS_NEW) {
matched = TRUE;
}
(void) mutex_unlock(¤t_list->nsServers[i]->updateStatus);
for (i = 0; current_list->nsServers[i]; ++i) {
/*
* Lock the updateStatus mutex to
* make sure the server status stays the same
* while the data is being processed.
*/
if (matched == FALSE &&
strcmp(current_list->nsServers[i]->ip,
serverAddr) == 0) {
matched = TRUE;
if (strcmp(request, NS_CACHE_NORESP) == 0) {
/*
* if the server has already been removed,
* don't bother.
*/
(void) mutex_lock(¤t_list->
nsServers[i]->updateStatus);
if (current_list->nsServers[i]->status ==
INFO_SERVER_REMOVED) {
(void) mutex_unlock(¤t_list->
nsServers[i]->
updateStatus);
continue;
}
(void) mutex_unlock(¤t_list->
nsServers[i]->
updateStatus);
/*
* if the information is new,
* give this server one more chance.
*/
(void) mutex_lock(¤t_list->
nsServers[i]->
updateStatus);
if (current_list->nsServers[i]->info ==
INFO_STATUS_NEW &&
current_list->nsServers[i]->status ==
INFO_SERVER_UP) {
server = current_list->nsServers[i];
(void) mutex_unlock(¤t_list->
nsServers[i]->
updateStatus);
break;
} else {
/*
* it is recommended that
* before removing the
* server from the list,
* the server should be
* contacted one more time
* to make sure that it is
* really unavailable.
* For now, just trust the client
* (i.e., the sldap library)
* that it knows what it is
* doing and would not try
* to mess up the server
* list.
*/
current_list->nsServers[i]->status =
INFO_SERVER_REMOVED;
(void) mutex_unlock(¤t_list->
nsServers[i]->
updateStatus);
continue;
}
} else {
/*
* req == NS_CACHE_NEXT or NS_CACHE_WRITE
*/
continue;
}
}
if (matched) {
if (strcmp(request, NS_CACHE_WRITE) == 0) {
/*
* ldap_cachemgr checks here if the server
* is not a non-replica server (a server
* of type INFO_RW_WRITEABLE). But currently
* it considers all the servers in its list
* as those.
*/
(void) mutex_lock(¤t_list->
nsServers[i]->
updateStatus);
if (current_list->nsServers[i]->status ==
INFO_SERVER_UP) {
(void) mutex_unlock(¤t_list->
nsServers[i]->
updateStatus);
server = current_list->nsServers[i];
break;
}
} else {
(void) mutex_lock(¤t_list->
nsServers[i]->
updateStatus);
if (current_list->nsServers[i]->status ==
INFO_SERVER_UP) {
(void) mutex_unlock(¤t_list->
nsServers[i]->
updateStatus);
server = current_list->nsServers[i];
break;
}
}
(void) mutex_unlock(¤t_list->
nsServers[i]->
updateStatus);
}
}
if (server == NULL) {
(void) rw_unlock(¤t_list->listDestroyLock);
(void) snprintf(errmsg,
sizeof (errmsg),
gettext("No servers are available"));
MKERROR(LOG_ERR,
*error,
NS_CONFIG_NOTLOADED,
strdup(errmsg),
NS_LDAP_MEMORY);
return (NS_LDAP_NOTFOUND);
}
(void) mutex_lock(&server->updateStatus);
server->info = INFO_STATUS_OLD;
(void) mutex_unlock(&server->updateStatus);
if (ret == NULL) {
(void) rw_unlock(¤t_list->listDestroyLock);
return (NS_LDAP_SUCCESS);
}
if (strcmp(addrType, NS_CACHE_ADDR_HOSTNAME) == 0) {
ret_code = __s_api_ip2hostname(server->ip, &ret->serverFQDN);
if (ret_code != NS_LDAP_SUCCESS) {
(void) snprintf(errmsg,
sizeof (errmsg),
gettext("The %s address "
"can not be resolved into "
"a host name. Returning "
"the address as it is."),
server->ip);
MKERROR(LOG_ERR,
*error,
NS_CONFIG_NOTLOADED,
strdup(errmsg),
NS_LDAP_MEMORY);
return (NS_LDAP_INTERNAL);
}
}
ret->server = strdup(server->ip);
ret->controls = __s_api_cp2dArray(server->controls);
ret->saslMechanisms = __s_api_cp2dArray(server->saslMech);
(void) rw_unlock(¤t_list->listDestroyLock);
return (NS_LDAP_SUCCESS);
}
/*
* This function iterates through the list of the configured LDAP servers
* and "pings" those which are marked as removed or if any error occurred
* during the previous receiving of the server's root DSE. If the
* function is able to reach such a server and get its root DSE, it
* marks the server as on-line. Otherwise, the server's status is set
* to "Error".
* For each server the function tries to connect to, it fires up
* a separate thread and then waits until all the treads finish.
* The function returns NS_LDAP_INTERNAL if the Standalone mode was not
* initialized or was canceled prior to an invocation of
* __ns_ldap_pingOfflineServers().
*/
ns_ldap_return_code
__ns_ldap_pingOfflineServers(void)
{
dir_server_list_t *current_list = NULL;
ns_ldap_return_code retCode = NS_LDAP_SUCCESS;
long srvListLength, i = 0;
thread_t *thrPool, thrID;
void *status = NULL;
(void) mutex_lock(&dir_servers.listReplaceLock);
if (dir_servers.list == NULL) {
(void) mutex_unlock(&dir_servers.listReplaceLock);
return (NS_LDAP_INTERNAL);
}
current_list = dir_servers.list;
(void) rw_wrlock(¤t_list->listDestroyLock);
(void) mutex_unlock(&dir_servers.listReplaceLock);
while (current_list->nsServers[i] != NULL) {
++i;
}
srvListLength = i;
thrPool = calloc(srvListLength, sizeof (thread_t));
if (thrPool == NULL) {
(void) rw_unlock(¤t_list->listDestroyLock);
return (NS_LDAP_MEMORY);
}
for (i = 0; i < srvListLength; ++i) {
if (current_list->nsServers[i]->status != INFO_SERVER_REMOVED &&
current_list->nsServers[i]->status != INFO_SERVER_ERROR) {
continue;
}
current_list->nsServers[i]->status = INFO_SERVER_CONNECTING;
current_list->nsServers[i]->info = INFO_STATUS_NEW;
__s_api_free2dArray(current_list->nsServers[i]->controls);
current_list->nsServers[i]->controls = NULL;
__s_api_free2dArray(current_list->nsServers[i]->saslMech);
current_list->nsServers[i]->saslMech = NULL;
switch (thr_create(NULL,
0,
create_ns_servers_entry,
current_list->nsServers[i],
0,
&thrID)) {
case EAGAIN:
current_list->nsServers[i]->status = INFO_SERVER_ERROR;
continue;
case ENOMEM:
current_list->nsServers[i]->status = INFO_SERVER_ERROR;
retCode = NS_LDAP_MEMORY;
break;
default:
thrPool[i] = thrID;
continue;
}
/* A memory allocation error has occured */
break;
}
for (i = 0; i < srvListLength; ++i) {
if (thrPool[i] != 0 &&
thr_join(thrPool[i], NULL, &status) == 0) {
if (status == NULL) {
current_list->nsServers[i]->status =
INFO_SERVER_ERROR;
retCode = NS_LDAP_MEMORY;
}
free(status);
}
}
(void) rw_unlock(¤t_list->listDestroyLock);
free(thrPool);
return (retCode);
}
|