This file is indexed.

/usr/lib/perl5/Net/LDAPapi.pm is in libnet-ldapapi-perl 3.0.3-7build2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
package Net::LDAPapi;

use strict;
use Carp;
use Convert::ASN1;
use vars qw($VERSION @ISA @EXPORT $AUTOLOAD);

require Exporter;
require DynaLoader;
require AutoLoader;

@ISA = qw(Exporter DynaLoader);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
@EXPORT =
    qw(
       ldap_create ldap_set_option ldap_get_option ldap_unbind_ext
       ldap_unbind_ext_s ldap_version ldap_abandon_ext ldap_add_ext ldap_add_ext_s
       ldap_set_rebind_proc
       ldap_rename ldap_rename_s
       ldap_compare_ext ldap_compare_ext_s ldap_delete_ext
       ldap_delete_ext_s ldap_search_ext ldap_search_ext_s ldap_result
       ldap_msgfree ldap_msg_free ldap_msgid ldap_msgtype
       ldap_get_lderrno ldap_set_lderrno ldap_parse_result ldap_err2string
       ldap_count_entries ldap_first_entry ldap_next_entry ldap_get_dn
       ldap_err2string ldap_dn2ufn ldap_str2dn ldap_str2rdn ldap_explode_rdn
       ldap_explode_dns ldap_first_attribute ldap_next_attribute
       ldap_get_values ldap_get_values_len ldap_sasl_bind ldap_sasl_bind_s
       ldapssl_client_init ldapssl_init ldapssl_install_routines
       ldap_get_all_entries ldap_multisort_entries
       ldap_is_ldap_url ldap_url_parse ldap_url_search ldap_url_search_s
       ldap_url_search_st ber_free ldap_init ldap_initialize ldap_start_tls_s
       ldap_sasl_interactive_bind_s
       ldap_create_control ldap_control_berval
       LDAP_RES_BIND
       LDAP_RES_SEARCH_ENTRY
       LDAP_RES_SEARCH_REFERENCE
       LDAP_RES_SEARCH_RESULT
       LDAP_RES_MODIFY
       LDAP_RES_ADD
       LDAP_RES_DELETE
       LDAP_RES_MODDN
       LDAP_RES_COMPARE
       LDAP_RES_EXTENDED
       LDAP_RES_INTERMEDIATE
       LDAP_RES_ANY
       LDAP_RES_UNSOLICITED
       LDAPS_PORT
       LDAP_ADMIN_LIMIT_EXCEEDED
       LDAP_AFFECTS_MULTIPLE_DSAS
       LDAP_ALIAS_DEREF_PROBLEM
       LDAP_ALIAS_PROBLEM
       LDAP_ALREADY_EXISTS
       LDAP_AUTH_KRBV4
       LDAP_AUTH_KRBV41
       LDAP_AUTH_KRBV41_30
       LDAP_AUTH_KRBV42
       LDAP_AUTH_KRBV42_30
       LDAP_AUTH_NONE
       LDAP_AUTH_SASL
       LDAP_AUTH_SIMPLE
       LDAP_AUTH_UNKNOWN
       LDAP_BUSY
       LDAP_CACHE_CHECK
       LDAP_CACHE_LOCALDB
       LDAP_CACHE_POPULATE
       LDAP_CALLBACK
       LDAP_COMPARE_FALSE
       LDAP_COMPARE_TRUE
       LDAP_CONNECT_ERROR
       LDAP_CONSTRAINT_VIOLATION
       LDAP_CONTROL_ASSERT
       LDAP_CONTROL_DUPENT
       LDAP_CONTROL_DUPENT_ENTRY
       LDAP_CONTROL_DUPENT_REQUEST
       LDAP_CONTROL_DUPENT_RESPONSE
       LDAP_CONTROL_GROUPING
       LDAP_CONTROL_MANAGEDIT
       LDAP_CONTROL_MANAGEDSAIT
       LDAP_CONTROL_NOOP
       LDAP_CONTROL_NO_SUBORDINATES
       LDAP_CONTROL_PAGEDRESULTS
       LDAP_CONTROL_PASSWORDPOLICYREQUEST
       LDAP_CONTROL_PASSWORDPOLICYRESPONSE
       LDAP_CONTROL_PERSIST_ENTRY_CHANGE_NOTICE
       LDAP_CONTROL_PERSIST_REQUEST
       LDAP_CONTROL_POST_READ
       LDAP_CONTROL_PRE_READ
       LDAP_CONTROL_PROXY_AUTHZ
       LDAP_CONTROL_SLURP
       LDAP_CONTROL_SORTREQUEST
       LDAP_CONTROL_SORTRESPONSE
       LDAP_CONTROL_SUBENTRIES
       LDAP_CONTROL_SYNC
       LDAP_CONTROL_SYNC_DONE
       LDAP_CONTROL_SYNC_STATE
       LDAP_CONTROL_VALSORT
       LDAP_CONTROL_VALUESRETURNFILTER
       LDAP_CONTROL_VLVREQUEST
       LDAP_CONTROL_VLVRESPONSE
       LDAP_CONTROL_X_CHAINING_BEHAVIOR
       LDAP_CONTROL_X_DOMAIN_SCOPE
       LDAP_CONTROL_X_EXTENDED_DN
       LDAP_CONTROL_X_INCREMENTAL_VALUES
       LDAP_CONTROL_X_PERMISSIVE_MODIFY
       LDAP_CONTROL_X_SEARCH_OPTIONS
       LDAP_CONTROL_X_TREE_DELETE
       LDAP_CONTROL_X_VALUESRETURNFILTER
       LDAP_CUP_INVALID_DATA
       LDAP_DECODING_ERROR
       LDAP_DEREF_ALWAYS
       LDAP_DEREF_FINDING
       LDAP_DEREF_NEVER
       LDAP_DEREF_SEARCHING
       LDAP_ENCODING_ERROR
       LDAP_FILTER_ERROR
       LDAP_FILT_MAXSIZ
       LDAP_INAPPROPRIATE_AUTH
       LDAP_INAPPROPRIATE_MATCHING
       LDAP_INSUFFICIENT_ACCESS
       LDAP_INVALID_CREDENTIALS
       LDAP_INVALID_DN_SYNTAX
       LDAP_INVALID_SYNTAX
       LDAP_IS_LEAF
       LDAP_LOCAL_ERROR
       LDAP_LOOP_DETECT
       LDAP_MOD_ADD
       LDAP_MOD_BVALUES
       LDAP_MOD_DELETE
       LDAP_MOD_REPLACE
       LDAP_NAMING_VIOLATION
       LDAP_NOT_ALLOWED_ON_NONLEAF
       LDAP_NOT_ALLOWED_ON_RDN
       LDAP_NO_LIMIT
       LDAP_NO_MEMORY
       LDAP_NO_OBJECT_CLASS_MODS
       LDAP_NO_SUCH_ATTRIBUTE
       LDAP_NO_SUCH_OBJECT
       LDAP_OBJECT_CLASS_VIOLATION
       LDAP_OPERATIONS_ERROR
       LDAP_OPT_CACHE_ENABLE
       LDAP_OPT_CACHE_FN_PTRS
       LDAP_OPT_CACHE_STRATEGY
       LDAP_OPT_DEBUG_LEVEL
       LDAP_OPT_DEREF
       LDAP_OPT_DESC
       LDAP_OPT_DNS
       LDAP_OPT_IO_FN_PTRS
       LDAP_OPT_OFF
       LDAP_OPT_ON
       LDAP_OPT_PROTOCOL_VERSION
       LDAP_OPT_REBIND_ARG
       LDAP_OPT_REBIND_FN
       LDAP_OPT_REFERRALS
       LDAP_OPT_REFERRAL_HOP_LIMIT
       LDAP_OPT_RESTART
       LDAP_OPT_SIZELIMIT
       LDAP_OPT_SSL
       LDAP_OPT_THREAD_FN_PTRS
       LDAP_OPT_TIMELIMIT
       LDAP_OTHER
       LDAP_PARAM_ERROR
       LDAP_PARTIAL_RESULTS
       LDAP_PORT
       LDAP_PORT_MAX
       LDAP_PROTOCOL_ERROR
       LDAP_REFERRAL
       LDAP_RESULTS_TOO_LARGE
       LDAP_SASL_AUTOMATIC
       LDAP_SASL_INTERACTIVE
       LDAP_SASL_NULL
       LDAP_SASL_QUIET
       LDAP_SASL_SIMPLE
       LDAP_SCOPE_BASE
       LDAP_SCOPE_ONELEVEL
       LDAP_SCOPE_SUBTREE
       LDAP_SECURITY_NONE
       LDAP_SERVER_DOWN
       LDAP_SIZELIMIT_EXCEEDED
       LDAP_STRONG_AUTH_NOT_SUPPORTED
       LDAP_STRONG_AUTH_REQUIRED
       LDAP_SUCCESS
       LDAP_SYNC_INFO
       LDAP_TIMELIMIT_EXCEEDED
       LDAP_TIMEOUT
       LDAP_TYPE_OR_VALUE_EXISTS
       LDAP_UNAVAILABLE
       LDAP_UNAVAILABLE_CRITICAL_EXTN
       LDAP_UNDEFINED_TYPE
       LDAP_UNWILLING_TO_PERFORM
       LDAP_URL_ERR_BADSCOPE
       LDAP_URL_ERR_MEM
       LDAP_URL_ERR_NODN
       LDAP_URL_ERR_NOTLDAP
       LDAP_URL_ERR_PARAM
       LDAP_URL_OPT_SECURE
       LDAP_USER_CANCELLED
       LDAP_VERSION
       LDAP_VERSION1
       LDAP_VERSION2
       LDAP_VERSION3
       LDAP_TAG_SYNC_NEW_COOKIE
       LDAP_TAG_SYNC_REFRESH_DELETE
       LDAP_TAG_SYNC_REFRESH_PRESENT
       LDAP_TAG_SYNC_ID_SET
       LDAP_TAG_SYNC_COOKIE
       LDAP_TAG_REFRESHDELETES
       LDAP_TAG_REFRESHDONE
       LDAP_TAG_RELOAD_HINT
       LDAP_TAG_EXOP_MODIFY_PASSWD_ID
       LDAP_TAG_EXOP_MODIFY_PASSWD_OLD
       LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
       LDAP_TAG_EXOP_MODIFY_PASSWD_GEN
       LDAP_TAG_MESSAGE
       LDAP_TAG_MSGID
       LDAP_TAG_LDAPDN
       LDAP_TAG_LDAPCRED
       LDAP_TAG_CONTROLS
       LDAP_TAG_REFERRAL
       LDAP_TAG_NEWSUPERIOR
       LDAP_TAG_EXOP_REQ_OID
       LDAP_TAG_EXOP_REQ_VALUE
       LDAP_TAG_EXOP_RES_OID
       LDAP_TAG_EXOP_RES_VALUE
       LDAP_TAG_IM_RES_OID
       LDAP_TAG_IM_RES_VALUE
       LDAP_TAG_SASL_RES_CREDS
       );
$VERSION = '3.0.3';

sub AUTOLOAD {
    # This AUTOLOAD is used to 'autoload' constants from the constant()
    # XS function.  If a constant is not found then control is passed
    # to the AUTOLOAD in AutoLoader.

    my $constname;
    ($constname = $AUTOLOAD) =~ s/.*:://;
    my $val = constant($constname, @_ ? $_[0] : 0);
    if ($! != 0) {
        if ($! =~ /Invalid/) {

            # try constants_h
            $val = '"'.constant_s($constname).'"';
            goto SUBDEF if ($! == 0);

            $AutoLoader::AUTOLOAD = $AUTOLOAD;
            goto &AutoLoader::AUTOLOAD;
        } else {
            croak "Your vendor has not defined LDAP macro $constname";
        }
    }
SUBDEF:
    eval "sub $AUTOLOAD { $val }";
    goto &$AUTOLOAD;
}

bootstrap Net::LDAPapi $VERSION;


# creats blessed ldap object.
# accepts following arguments '-host', '-port', '-url', '-debug'
# if '-url' is given then then '-host' and '-port' are not used
sub new
{
    my ($this, @args) = @_;
    my $class = ref($this) || $this;
    my $self  = {};
    my $ld;
    bless $self, $class;

    my ($host, $port, $url, $debug) =
        $self->rearrange(['HOST','PORT','URL', 'DEBUG'],@args);

    if ( defined($url) ) {
        return -1 unless (ldap_initialize($ld, $url) == $self->LDAP_SUCCESS );

    } else {
        $host = "localhost"      unless $host;
        $port = $self->LDAP_PORT unless $port;

        return -1 unless ( ldap_initialize($ld, "ldap://$host:$port") == $self-> LDAP_SUCCESS);
    }

    # Following ASN.1 contains definitions for synrepl API
    my $asn = Convert::ASN1->new;
    $asn->prepare(<<ASN) or die "prepare: ", $asn->error;

    syncUUID ::= OCTET STRING

    syncCookie ::= OCTET STRING

    syncRequestValue ::= SEQUENCE {
        mode       ENUMERATED,
        cookie     syncCookie OPTIONAL,
        reloadHint BOOLEAN
        }

    syncStateValue ::= SEQUENCE {
        state     ENUMERATED,
        entryUUID syncUUID,
        cookie    syncCookie OPTIONAL
        }

    refresh_Delete ::= SEQUENCE {
        cookie         syncCookie OPTIONAL,
        refreshDone    BOOLEAN OPTIONAL
        }

    refresh_Present ::= SEQUENCE {
        cookie         syncCookie OPTIONAL,
        refreshDone    BOOLEAN OPTIONAL
        }

    syncId_Set      ::= SEQUENCE {
        cookie         syncCookie OPTIONAL,
        refreshDeletes BOOLEAN OPTIONAL,
        syncUUIDs      SET OF syncUUID
        }

    syncInfoValue ::= CHOICE {
        newcookie      [0] syncCookie,
        refreshDelete  [1] refresh_Delete,
        refreshPresent [2] refresh_Present,
        syncIdSet      [3] syncId_Set
        }
ASN

    $self->{"asn"}       = $asn;
    $self->{"ld"}        = $ld;
    $self->{"errno"}     = 0;
    $self->{"errstring"} = undef;
    $self->{"debug"}     = $debug;
    ldap_set_option($ld, $self->LDAP_OPT_PROTOCOL_VERSION, $self->LDAP_VERSION3);
    return $self;
} # end of new


sub DESTROY {};


sub abandon
{
    my ($self, @args) = @_;

    my ($status, $sctrls, $cctrls);

    my ($msgid, $serverctrls, $clientctrls) =
        $self->rearrange(['MSGID', 'SCTRLS', 'CCTRLS'], @args);

    croak("Invalid MSGID") if ($msgid < 0);

    $sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
    $cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;

    $status = ldap_abandon_ext($self->{"ld"}, $msgid, $sctrls, $cctrls);

    $self->errorize($status);

    ldap_controls_array_free($sctrls) if $sctrls;
    ldap_controls_array_free($cctrls) if $cctrls;

    return $status;
} # end of abandon


# synonim for abandon(...)
sub abandon_ext {
    my ($self, @args) = @_;

    return $self->abandon(@args);
} # end of abandon_ext


sub add
{
    my ($self,@args) = @_;

    my ($msgid, $sctrls, $cctrls, $status);

    my ($dn, $mod, $serverctrls, $clientctrls) =
        $self->rearrange(['DN', 'MOD', 'SCTRLS', 'CCTRLS'], @args);

    croak("No DN Specified") if ($dn eq "");
    croak("LDAPMod structure is not a hash reference.") if( ref($mod) ne "HASH" );

    $sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
    $cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;

    $status = ldap_add_ext($self->{"ld"}, $dn, $mod, $sctrls, $cctrls, $msgid);

    ldap_controls_array_free($sctrls) if $sctrls;
    ldap_controls_array_free($cctrls) if $cctrls;

    $self->errorize($status);
    if( $status != $self->LDAP_SUCCESS ) {
        return undef;
    }

    return $msgid;
} # end of add

# synonym for add
sub add_ext
{
    my ($self, @args) = @_;

    return $self->add(@args);
} # end of add_ext


sub add_s
{
    my ($self,@args) = @_;

    my ($sctrls, $cctrls, $status);

    my ($dn, $mod, $serverctrls, $clientctrls) =
        $self->rearrange(['DN', 'MOD', 'SCTRLS', 'CCTRLS'], @args);

    croak("No DN Specified") if ($dn eq "");
    croak("LDAP Modify Structure Not a HASH Reference") if (ref($mod) ne "HASH");

    $sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
    $cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;

    $status = ldap_add_ext_s($self->{"ld"}, $dn, $mod, $sctrls, $cctrls);

    ldap_controls_array_free($sctrls) if $sctrls;
    ldap_controls_array_free($cctrls) if $cctrls;

    $self->errorize($status);

    return $status;
} # end of add_s


# synonym for add_s
sub add_ext_s
{
    my ($self, @args) = @_;

    return $self->add_s(@args);
} # end of add_ext_s


sub bind
{
    my ($self,@args) = @_;

    my ($msgid, $sctrls, $cctrls, $status);

    my ($dn, $pass, $authtype, $serverctrls, $clientctrls) =
        $self->rearrange(['DN', 'PASSWORD', 'TYPE', 'SCTRLS', 'CCTRLS'],@args);

    $dn       = "" unless $dn;
    $pass     = "" unless $pass;
    $authtype = $authtype || $self->LDAP_AUTH_SIMPLE;

    croak("bind supports only LDAP_AUTH_SIMPLE auth type")
        unless $authtype == $self->LDAP_AUTH_SIMPLE;

    $sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
    $cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;

    $status = ldap_sasl_bind($self->{"ld"}, $dn,     $pass,
                             $sctrls,       $cctrls, $msgid);

    ldap_controls_array_free($sctrls) if $sctrls;
    ldap_controls_array_free($cctrls) if $cctrls;

    $self->errorize($status);
    if( $status != $self->LDAP_SUCCESS ) {
        return undef;
    }

    return $msgid;
} # end of bind


sub bind_s
{
    my ($self, @args) = @_;

    my ($saslmech, $status, $servercredp, $sctrls, $cctrls);

    my ($dn, $pass, $authtype, $serverctrls, $clientctrls) =
        $self->rearrange(['DN', 'PASSWORD', 'TYPE', 'SCTRLS', 'CCTRLS'], @args);

    $dn       = "" unless $dn;
    $pass     = "" unless $pass;
    $sctrls   = 0 unless $sctrls;
    $cctrls   = 0 unless $cctrls;
    $authtype = $authtype || $self->LDAP_AUTH_SIMPLE;

    $sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
    $cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;

    if ($authtype == $self->LDAP_AUTH_SASL) {
        $status =
            ldap_sasl_interactive_bind_s($self->{"ld"}, $dn, $pass,
                                         $sctrls, $cctrls, $saslmech,
                                         $self->{"saslrealm"},
                                         $self->{"saslauthzid"},
                                         $self->{"saslsecprops"},
                                         $self->{"saslflags"});

    } else {
        # not sure here what to do with $servercredp
        $status = ldap_sasl_bind_s($self->{"ld"}, $dn, $pass,
                                   $sctrls, $cctrls, \$servercredp);
    }

    ldap_controls_array_free($sctrls) if $sctrls;
    ldap_controls_array_free($cctrls) if $cctrls;

    $self->errorize($status);

    return $status;
} # end of bind_s


sub sasl_parms
{
    my ($self,@args) = @_;
    my ($mech,  $realm, $authzid, $secprops, $flags) =
        $self->rearrange(['MECH', 'REALM', 'AUTHZID', 'SECPROPS', 'FLAGS'],
                         @args);

    $mech     = ""                     unless $mech;
    $realm    = ""                     unless $realm;
    $authzid  = ""                     unless $authzid;
    $secprops = ""                     unless $secprops;
    $flags    = $self->LDAP_SASL_QUIET unless defined($flags);

    $self->{"saslmech"}     = $mech;
    $self->{"saslrealm"}    = $realm;
    $self->{"saslauthzid"}  = $authzid;
    $self->{"saslsecprops"} = $secprops;
    $self->{"saslflags"}    = $flags;
} # end of sasl_parms


sub compare
{
    my ($self, @args) = @_;

    my ($status, $msgid, $sctrls, $cctrls);

    my ($dn, $attr, $value, $serverctrls, $clientctrls) =
        $self->rearrange(['DN','ATTR', ['VALUE', 'VALUES'], 'SCTRLS', 'CCTRLS'],  @args);

    croak("No DN Specified") if ($dn eq "");
    $value = "" unless $value;

    $sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
    $cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;

    $status =
        ldap_compare_ext($self->{"ld"}, $dn, $attr, $value, $sctrls, $cctrls, $msgid);

    ldap_controls_array_free($sctrls) if $sctrls;
    ldap_controls_array_free($cctrls) if $cctrls;

    $self->errorize($status);
    if( $status != $self->LDAP_SUCCESS ) {
        return undef;
    }

    return $msgid;
} # end of compare


# synonym for compare
sub compare_ext {
    my ($self, @args) = @_;

    return $self->compare(@args);
} # end of compare_ext


# needs to use ldap_compare_ext_s
sub compare_s
{
    my ($self, @args) = @_;

    my ($status, $sctrls, $cctrls);

    my ($dn, $attr, $value, $serverctrls, $clientctrls) =
        $self->rearrange(['DN', 'ATTR' , ['VALUE', 'VALUES'], 'SCTRLS', 'CCTRLS'], @args);

    croak("No DN Specified") if ($dn eq "");
    $value = "" unless $value;

    $sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
    $cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;

    $status = ldap_compare_ext_s($self->{"ld"}, $dn, $attr, $value, $sctrls, $cctrls);

    ldap_controls_array_free($sctrls) if $sctrls;
    ldap_controls_array_free($cctrls) if $cctrls;

    $self->errorize($status);
    return $status;
} # end of compare_s


# synonym for compare
sub compare_ext_s {
    my ($self, @args) = @_;

    return $self->compare_s(@args);
} # end of compare_ext


# needs DOC in POD bellow. XXX
sub start_tls
{
    my ($self, @args) = @_;

    my ($msgid, $status, $sctrls, $cctrls);

    my ($serverctrls, $clientctrls) =
        $self->rearrange(['SCTRLS', 'CCTRLS'], @args);

    $sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
    $cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;

    $status = ldap_start_tls($self->{"ld"}, $sctrls, $cctrls, $msgid);

    ldap_controls_array_free($sctrls) if $sctrls;
    ldap_controls_array_free($cctrls) if $cctrls;

    $self->errorize($status);
    if( $status != $self->LDAP_SUCCESS ) {
        return undef;
    }

    return $msgid;
} # end of start_tls


# needs DOC in POD bellow. XXX
sub start_tls_s
{
    my ($self, @args) = @_;

    my ($status, $sctrls, $cctrls);
    $sctrls=0;
    $cctrls=0;

    my ($serverctrls, $clientctrls) = $self->rearrange(['SCTRLS', 'CCTRLS'], @args);

    $sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
    $cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;

    $status = ldap_start_tls_s($self->{"ld"}, $sctrls, $cctrls);

    ldap_controls_array_free($sctrls) if $sctrls;
    ldap_controls_array_free($cctrls) if $cctrls;

    $self->errorize($status);

    return $status;
} # end of start_tls_s


sub count_entries
{
    my ($self, @args) = @_;

    my ($result) = $self->rearrange(['RESULT'], @args);

    $result = $self->{"result"} unless $result;

    croak("No result is given") unless $result;

    return ldap_count_entries($self->{"ld"}, $result);
} # end of count_entries


sub delete
{
    my ($self,@args) = @_;

    my ($msgid, $status, $sctrls, $cctrls);

    my ($dn, $serverctrls, $clientctrls) =
        $self->rearrange(['DN', 'SCTRLS', 'CCTRLS'], @args);

    croak("No DN Specified") if ($dn eq "");

    $sctrls = 0;
    $cctrls = 0;

    $sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
    $cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;

    $status = ldap_delete_ext($self->{"ld"}, $dn, $sctrls, $cctrls, $msgid);

    ldap_controls_array_free($sctrls) if $sctrls;
    ldap_controls_array_free($cctrls) if $cctrls;

    $self->errorize($status);
    if( $status != $self->LDAP_SUCCESS ) {
        return undef;
    }

    return $msgid;
} # end of delete

sub delete_s
{
    my ($self,@args) = @_;

    my ($status, $sctrls, $cctrls);

    my ($dn, $serverctrls, $clientctrls) =
        $self->rearrange(['DN', 'SCTRLS', 'CCTRLS'], @args);

    croak("No DN Specified") if ($dn eq "");

    $sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
    $cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;

    $status = ldap_delete_ext_s($self->{"ld"}, $dn, $sctrls, $cctrls);

    ldap_controls_array_free($sctrls) if $sctrls;
    ldap_controls_array_free($cctrls) if $cctrls;

    $self->errorize($status);

    return $status;
} # end of delete_s

sub dn2ufn
{
    my ($self, @args) = @_;

    my ($dn) = $self->rearrange(['DN'], @args);

    return ldap_dn2ufn($dn);
} # end of dn2ufn


sub explode_dn
{
    my ($self, @args) = @_;

    my ($dn, $notypes) = $self->rearrange(['DN', 'NOTYPES'],@args);

    return ldap_explode_dn($dn, $notypes);
} # end of explode_dn


sub explode_rdn
{
    my ($self, @args) = @_;

    my (@components);

    my ($rdn, $notypes) = $self->rearrange(['RDN', 'NOTYPES'], @args);

    return ldap_explode_rdn($rdn, $notypes);
} # end of explode_rdn


sub first_message
{
    my ($self, @args) = @_;

    my ($result) = $self->rearrange(['RESULT'], @args);

    $result = $self->{"result"} unless $result;

    croak("No Current Result") unless $result;

    $self->{"msg"} = ldap_first_message($self->{"ld"}, $self->{"result"});

    return $self->{"msg"};
} # end of first_message


sub next_message
{
    my ($self, @args) = @_;

    my ($msg) = $self->rearrange(['MSG'], @args);

    $msg = $self->{"msg"} unless $msg;

    croak("No Current Message") unless $msg;

    $self->{"msg"} = ldap_next_message($self->{"ld"}, $msg);

    return $self->{"msg"};
} # end of next_message


# using this function you don't have to call fist_message and next_message
# here is an example:
#
# print "message = $message\n" while( $msg = $ld->result_message );
#
sub result_message
{
    my ($self, @args) = @_;

    my ($result) = $self->rearrange(['RESULT'], @args);

    $result = $self->{"result"} unless $result;

    croak("No Current Result") unless $result;

    if( $self->{"msg"} == 0 ) {
        $self->{"msg"} = ldap_first_message($self->{"ld"}, $self->{"result"});
    } else {
        $self->{"msg"} = ldap_next_message($self->{"ld"},  $self->{"msg"});
    }

    return $self->{"msg"};
} # end of result_message


sub next_changed_entries {
    my ($self, @args) = @_;

    my ($msgid, $allnone, $timeout) =
        $self->rearrange(['MSGID', 'ALL', 'TIMEOUT'], @args);

    my ($rc,             $msg,            $msgtype, $asn,            $syncInfoValue,
        $syncInfoValues, $refreshPresent, $ctrl,    $oid,            %parsed,
        $retdatap,       $retoidp,        @entries, $syncStateValue, $syncStateValues,
        $state,          $berval,         $cookie);

    $rc = $self->result($msgid, $allnone, $timeout);

    @entries = ();

    $asn = $self->{"asn"};

    while( $msg = $self->result_message ) {
        $msgtype = $self->msgtype($msg);

        if( $msgtype eq $self->LDAP_RES_SEARCH_ENTRY ) {
            my %entr =  ('entry' => $msg);
            push(@entries, \%entr);
            $self->{"entry"} = $msg;

            # extract controls if any
            my @sctrls = $self->get_entry_controls($msg);
            foreach $ctrl (@sctrls) {
                $oid = $self->get_control_oid($ctrl);
                if( $oid eq $self->LDAP_CONTROL_SYNC_STATE ) {
                    $berval = $self->get_control_berval($ctrl);
                    $syncStateValue = $asn->find('syncStateValue');
                    $syncStateValues = $syncStateValue->decode($berval);
                    $state = $syncStateValues->{'state'};
                    if(      $state == 0 ) {
                        $entr{'state'} = "present";
                    } elsif( $state == 1 ) {
                        $entr{'state'} = "add";
                    } elsif( $state == 2 ) {
                        $entr{'state'} = "modify";
                    } elsif( $state == 3 ) {
                        $entr{'state'} = "delete";
                    } else {
                        $entr{'state'} = "unknown";
                    }
                }

                $cookie = $syncStateValues->{'cookie'};
                if( $cookie ) {
                    # save the cookie
                    open(COOKIE_FILE,">".$self->{"cookie"}) ||
                        die("Cannot open file '".$self->{"cookie"}."' for writing.");
                    print COOKIE_FILE $cookie;
                    close(COOKIE_FILE);
                }
            }

        } elsif( $msgtype eq $self->LDAP_RES_INTERMEDIATE ) {
            %parsed = $self->parse_intermediate($msg);
            $retdatap = $parsed{'retdatap'};
            $retoidp  = $parsed{'retoidp'};

            if( $retoidp eq $self->LDAP_SYNC_INFO ) {
                my $cookie;

                $asn->configure(encoding => "DER");
                $syncInfoValue = $asn->find('syncInfoValue');
                $syncInfoValues = $syncInfoValue->decode($retdatap);

                # trying to get the cookie from one of the foolowing choices.
                $cookie = $syncInfoValues->{'newcookie'};

                my $refreshPresent = $syncInfoValues->{'refreshPresent'};
                $cookie = $refreshPresent->{'cookie'} if( $refreshPresent );

                my $refreshDelete = $syncInfoValues->{'refreshDelete'};
                $cookie = $refreshDelete->{'cookie'} if( $refreshDelete );

                my $syncIdSet = $syncInfoValues->{'syncIdSet'};
                $cookie = $syncIdSet->{'cookie'} if( $syncIdSet );

                $asn->configure(encoding => "BER");

                # see if we got any and save it.
                if( $cookie ) {
                    open(COOKIE_FILE,">".$self->{"cookie"}) ||
                        die("Cannot open file '".$self->{"cookie"}."' for writing.");
                    print COOKIE_FILE $cookie;
                    close(COOKIE_FILE);
                }
            }
        }
    }

    return @entries;
} # next_changed_entries


sub first_entry
{
    my ($self) = @_;

    croak("No Current Result") if ($self->{"result"} == 0);

    $self->{"entry"} = ldap_first_entry($self->{"ld"}, $self->{"result"});

    return $self->{"entry"};
} # end of first_entry

sub next_entry
{
    my ($self) = @_;

    croak("No Current Entry") if ($self->{"entry"} == 0);

    $self->{"entry"} = ldap_next_entry($self->{"ld"}, $self->{"entry"});

    return $self->{"entry"};
} # end of next_entry


# using this function you don't have to call fist_entry and next_entry
# here is an example:
#
# print "entry = $entry\n" while( $entry = $ld->result_entry );
#
sub result_entry
{
    my ($self) = @_;

    croak("No Current Result") if ($self->{"result"} == 0);

    if( $self->{"entry"} == 0 ) {
        $self->{"entry"} = ldap_first_entry($self->{"ld"}, $self->{"result"});
    } else {
        $self->{"entry"} = ldap_next_entry($self->{"ld"},  $self->{"entry"});
    }

    return $self->{"entry"};
} # end of result_entry


sub get_entry_controls
{
    my ($self, @args) = @_;

    my ($msg) = $self->rearrange(['MSG'], @args);

    $msg = $self->{"msg"} unless $msg;

    croak("No Current Message/Entry") unless $msg;

    my @serverctrls = ();
    my $serverctrls_ref = \@serverctrls;

    ldap_get_entry_controls($self->{"ld"}, $msg, $serverctrls_ref);

    return @serverctrls;
} # end of get_entry_controls


sub get_control_oid {
    my ($self, @args) = @_;

    my ($ctrl) = $self->rearrange(['CTRL'], @args);

    return ldap_control_oid($ctrl);
} # end of get_control_oid


sub get_control_berval {
    my ($self, @args) = @_;

    my ($ctrl) = $self->rearrange(['CTRL'], @args);

    return ldap_control_berval($ctrl);
} # end of get_control_berval


sub get_control_critical {
    my ($self, @args) = @_;

    my ($ctrl) = $self->rearrange(['CTRL'], @args);

    return ldap_control_critical($ctrl);
} # end of get_control_critical


sub first_attribute
{
    my ($self) = @_;

    my ($attr, $ber);

    croak("No Current Entry") if ($self->{"entry"} == 0);

    $attr = ldap_first_attribute($self->{"ld"}, $self->{"entry"}, $ber);

    $self->{"ber"} = $ber;

    return $attr;
} # end of first_attribute


sub next_attribute
{
    my ($self) = @_;

    my ($attr);

    croak("No Current Entry") if ($self->{"entry"} == 0);
    croak("Empty Ber Value")  if ($self->{"ber"}   == 0);

    $attr = ldap_next_attribute($self->{"ld"}, $self->{"entry"}, $self->{"ber"});

    ber_free($self->{"ber"}, 0) if (!$attr);

    return $attr;
} # end of next_attribute


# using this function you don't have to call fist_attribute and next_attribute
# as in the following example:
#
# print "<$attr>\n" while( $attr = $ld->entry_attribute );
#
sub entry_attribute {

    my ($self, @args) = @_;

    my ($msg) = $self->rearrange(['MSG'], @args);

    my ($attr, $ber);

    $msg = $self->{"entry"} unless $msg;

    croak("No Current Entry") unless $msg;

    if ($self->{"ber"} == 0) {
        $attr = ldap_first_attribute($self->{"ld"}, $msg, $ber);
        $self->{"ber"} = $ber;

    } else {
        croak("Empty Ber Value") if ($self->{"ber"} == 0);
        $attr = ldap_next_attribute($self->{"ld"}, $msg, $self->{"ber"});
        if (!$attr) {
            ber_free($self->{"ber"}, 0);
            $self->{"ber"} = undef;
        }
    }

    return $attr;
} # end of entry_attribute


sub parse_result {
    my ($self, @args) = @_;

    my ($msg, $freeMsg) = $self->rearrange(['MSG', 'FREEMSG'], @args);

    my ($status, %result);

    $freeMsg = 0            unless $freeMsg;
    $msg = $self->{"entry"} unless $msg;

    my ($errcode, $matcheddn, $errmsg, @referrals, @serverctrls);

    @serverctrls = ();
    my $serverctrls_ref = \@serverctrls;

    @referrals = ();
    my $referrals_ref = \@referrals;

    $status =
        ldap_parse_result($self->{"ld"}, $msg,           $errcode,         $matcheddn,
                          $errmsg,       $referrals_ref, $serverctrls_ref, $freeMsg);


    $self->errorize($status);
    if( $status != $self->LDAP_SUCCESS ) {
        return undef;
    }

    $result{"errcode"}     = $errcode;
    $result{"matcheddn"}   = $matcheddn;
    $result{"errmsg"}      = $errmsg;
    $result{"referrals"}   = $referrals_ref;
    $result{"serverctrls"} = $serverctrls_ref;

    return %result;
} # end of parse_result(...)


# needs docs bellow in POD. XXX
sub parse_intermediate {
    my ($self, @args) = @_;

    my ($msg, $freeMsg) = $self->rearrange(['MSG', 'FREEMSG'], @args);

    my ($status, %result);

    $freeMsg = 0          unless $freeMsg;
    $msg = $self->{"msg"} unless $msg;

    my ($retoidp, $retdatap, @serverctrls);

    @serverctrls = ();
    my $serverctrls_ref = \@serverctrls;

    $status =
        ldap_parse_intermediate($self->{"ld"}, $msg,             $retoidp,
                                $retdatap,     $serverctrls_ref, $freeMsg);

    $self->errorize($status);
    if( $status != $self->LDAP_SUCCESS ) {
        return undef;
    }

    $result{"retoidp"}     = $retoidp;
    $result{"retdatap"}    = $retdatap;
    $result{"serverctrls"} = $serverctrls_ref;

    return %result;
} # end of parse_result(...)


sub perror
{
    my ($self, @args) = @_;

    my ($msg) = $self->rearrange(['MSG'], @args);

    ldap_perror($self->{"ld"}, $msg);
}

# get dn for current entry
sub get_dn
{
    my ($self, @args) = @_;

    my ($entry) = $self->rearrange(['MSG'], @args);

    $entry = $self->{"entry"} unless $entry;

    croak("No Current Entry") unless $entry;

    my $dn = ldap_get_dn($self->{"ld"}, $entry);

    return $dn;
} # end of get_dn


# get array of values for current entry and a given attribute
sub get_values
{
    my ($self, @args) = @_;

    my ($attr) = $self->rearrange(['ATTR'], @args);

    croak("No Current Entry")       if ($self->{"entry"} == 0);
    croak("No Attribute Specified") if ($attr eq "");

    my @vals = ldap_get_values_len($self->{"ld"}, $self->{"entry"}, $attr);

    return @vals;
} # end of get_values


# synonym for get_values(...)
sub get_values_len {
    my ($self, @args) = @_;

    return $self->get_values(@args);
} # end of get_values_len


sub msgfree
{
    my ($self, @args) = @_;

    my ($result) = $self->rearrange(['RESULT'], @args);

    $result = $self->{"result"} unless $result;

    return ldap_msgfree($self->{"result"});
} # end of msgfree


sub modify
{
    my ($self, @args) = @_;

    my ($msgid, $sctrls, $cctrls, $status);

    my ($dn, $mod, $serverctrls, $clientctrls) =
        $self->rearrange(['DN', 'MOD', 'SCTRLS', 'CCTRLS'], @args);

    croak("No DN Specified") if ($dn eq "");
    croak("LDAP Modify Structure Not a Reference") if (ref($mod) ne "HASH");

    $sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
    $cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;

    $status = ldap_modify_ext($self->{"ld"}, $dn, $mod, $sctrls, $cctrls, $msgid);

    $self->errorize($status);
    if( $status != $self->LDAP_SUCCESS ) {
        return undef;
    }

    ldap_controls_array_free($sctrls) if $sctrls;
    ldap_controls_array_free($cctrls) if $cctrls;

    return $msgid;
} # end of modify


# synonym for modify
sub modify_ext
{
    my ($self, @args) = @_;

    return $self->modify(@args);
} # end of modify_ext


sub modify_s
{
    my ($self,@args) = @_;

    my ($status, $sctrls, $cctrls);

    my ($dn, $mod, $serverctrls, $clientctrls) =
        $self->rearrange(['DN', 'MOD', 'SCTRLS', 'CCTRLS'], @args);

    croak("No DN Specified") if ($dn eq "");
    croak("LDAP Modify Structure Not a Reference") if (ref($mod) ne "HASH");

    $sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
    $cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;

    $status = ldap_modify_ext_s($self->{"ld"}, $dn, $mod, $sctrls, $cctrls);

    ldap_controls_array_free($sctrls) if $sctrls;
    ldap_controls_array_free($cctrls) if $cctrls;

    $self->errorize($status);
    return $status;
} # end of modify_s


# synonym for modify
sub modify_ext_s
{
    my ($self, @args) = @_;

    return $self->modify_s(@args);
} # end of modify_ext


# needs updated docs in POD bellow
sub rename {
    my ($self, @args) = @_;

    my ($sctrls, $cctrls, $msgid, $status);

    my ($dn, $newrdn, $newsuper, $delete, $serverctrls, $clientctrls) =
        $self->rearrange(['DN', 'NEWRDN', 'NEWSUPER', 'DELETE', 'SCTRLS', 'CCTRLS'],
                         @args);

    $sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
    $cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;

    $status =
        ldap_rename($self->{"ld"}, $dn,     $newrdn, $newsuper,
                    $delete,       $sctrls, $cctrls, $msgid);

    ldap_controls_array_free($sctrls) if $sctrls;
    ldap_controls_array_free($cctrls) if $cctrls;

    $self->errorize($status);
    if( $status != $self->LDAP_SUCCESS ) {
        return undef;
    }

    return $msgid;
} # end of rename


# needs updated docs in POD bellow
sub rename_s {
    my ($self, @args) = @_;

    my ($sctrls, $cctrls, $status);

    my ($dn, $newrdn, $newsuper, $delete, $serverctrls, $clientctrls) =
        $self->rearrange(['DN', 'NEWRDN', 'NEWSUPER', 'DELETE', 'SCTRLS', 'CCTRLS'],
                         @args);

    $sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
    $cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;

    $status =
        ldap_rename_s($self->{"ld"}, $dn,     $newrdn, $newsuper,
                      $delete,       $sctrls, $cctrls);

    ldap_controls_array_free($sctrls) if $sctrls;
    ldap_controls_array_free($cctrls) if $cctrls;

    $self->errorize($status);

    return $status;
} # end of rename_s


# this function is used to retrieve results of asynchronous search operation
# it returns LDAPMesage which is to be processed by functions first_entry,
# result_entry, first_message, result_message. To find message type one
# should use function msgtype(...)
sub result
{
    my ($self, @args) = @_;
    my ($result, $status, $err) = (undef, undef, undef);

    my ($msgid, $allnone, $timeout) =
        $self->rearrange(['MSGID', 'ALL', 'TIMEOUT'], @args);

    croak("Invalid MSGID") if ($msgid < 0);

    $status = ldap_result($self->{"ld"}, $msgid, $allnone, $timeout, $result);
    $self->{"result"} = $result;
    $self->{"status"} = $status;

    $self->errorize($status);
    if( $status == -1 || $status == 0 ) {
        return undef;
    }

    return $result;
} # end of result


sub is_ldap_url
{
    my ($self,@args) = @_;

    my ($url) = $self->rearrange(['URL'],@args);

    return ldap_is_ldap_url($url);
} # end of is_ldap_url


sub url_parse
{
    my ($self,@args) = @_;
    my ($url) = $self->rearrange(['URL'],@args);

    return ldap_url_parse($url);
} # end of url_parse


# needs testing XXX. present only in Mozilla SDK
sub url_search
{
    my ($self,@args) = @_;
    my ($msgid,$errdn,$extramsg);

    my ($url,$attrsonly) = $self->rearrange(['URL','ATTRSONLY'],@args);

    if (($msgid = ldap_url_search($self->{"ld"},$url,$attrsonly)) < 0)
    {
        $self->{"errno"} = ldap_get_lderrno($self->{"ld"},$errdn,$extramsg);
        $self->{"extramsg"} = undef;
    } else {
        $self->{"errno"} = 0;
        $self->{"extramsg"} = "";
    }
    return $msgid;
} # end of url_search


# needs testing XXX. present only in Mozilla SDK
sub url_search_s
{
    my ($self, @args) = @_;
    my ($result, $status, $errdn, $extramsg);

    my ($url,$attrsonly) = $self->rearrange(['URL', 'ATTRSONLY'], @args);

    if( ($status = ldap_url_search_s($self->{"ld"}, $url, $attrsonly, $result)) !=
        $self->LDAP_SUCCESS )
    {
        $self->{"errno"} = ldap_get_lderrno($self->{"ld"},$errdn,$extramsg);
        $self->{"extramsg"} = $extramsg;
    } else {
        $self->{"errno"} = 0;
        $self->{"extramsg"} = undef;
    }
    $self->{"result"} = $result;
    return $status;
} # end of url_search_s


# needs testing XXX. present only in Mozilla SDK
sub url_search_st
{
    my ($self,@args) = @_;
    my ($result,$status,$errdn,$extramsg);

    my ($url,$attrsonly,$timeout) = $self->rearrange(['URL','ATTRSONLY',
                                                      'TIMEOUT'],@args);

    if (($status = ldap_url_search_st($self->{"ld"},$url,$attrsonly,$timeout,
                                      $result)) != $self->LDAP_SUCCESS)
    {
        $self->{"errno"} = ldap_get_lderrno($self->{"ld"},$errdn,$extramsg);
        $self->{"extramsg"} = $extramsg;
    } else {
        $self->{"errno"} = 0;
        $self->{"extramsg"} = undef;
    }
    $self->{"result"} = $result;
    return $status;
} # end of url_search_st


# needs testing XXX. present only in Mozilla SDK
sub multisort_entries
{
    my ($self,@args) = @_;
    my ($status,$errdn,$extramsg);

    my ($attr) = $self->rearrange(['ATTR'],@args);

    if (!$self->{"result"})
    {
        croak("No Current Result");
    }

    $status = ldap_multisort_entries($self->{"ld"},$self->{"result"},$attr);
    $self->errorize($status);
    return $status;
} # end of multisort_entries


sub listen_for_changes
{
    my ($self, @args) = @_;

    my ($msgid, $status, $sctrls, $the_cookie, $syncRequestBerval);

    my ($basedn,    $scope,   $filter,    $attrs,
        $attrsonly, $timeout, $sizelimit, $cookie) =
            $self->rearrange(['BASEDN',    'SCOPE',   'FILTER',    'ATTRS',
                              'ATTRSONLY', 'TIMEOUT', 'SIZELIMIT', 'COOKIE'], @args);

    croak("No Filter Specified") if ($filter eq "");
    croak("No cookie file specified") unless $cookie;

    $self->{"cookie"} = $cookie;

    if( $attrs == undef ) {
        my @null_array = ();
        $attrs = \@null_array;
    }

    # load cookie from the file
    if( open(COOKIE, $cookie) ) {
        read COOKIE, $the_cookie, 1024, 0;
    } else {
        warn "Failed to open file '".$cookie."' for reading.\n";
    }

    my $asn = $self->{"asn"};
    my $syncRequestValue  = $asn->find('syncRequestValue');

    # refreshAndPersist mode
    if( $the_cookie ) { # we have the cookie
        $syncRequestBerval = $asn->encode(mode => 3, cookie => $the_cookie, reloadHint => 1);
    } else {
        $syncRequestBerval = $asn->encode(mode => 3, reloadHint => 1);
    }

    my $ctrl_persistent =
        $self->create_control(-oid      => $self->LDAP_CONTROL_SYNC,
                              -berval   => $syncRequestBerval,
                              -critical => $self->CRITICAL);

    my @controls = ($ctrl_persistent);
    $sctrls = $self->create_controls_array(@controls);

    $status =
        ldap_search_ext($self->{"ld"}, $basedn,    $scope,  $filter,
                        $attrs,        $attrsonly, $sctrls, undef,
                        $timeout,      $sizelimit, $msgid);

    ldap_controls_array_free($sctrls);
    ldap_control_free($ctrl_persistent);

    $self->errorize($status);
    if( $status != $self->LDAP_SUCCESS ) {
        return undef;
    }

    return $msgid;
} # listen_for_changes


sub search
{
    my ($self, @args) = @_;
    my ($msgid, $status, $sctrls, $cctrls);

    my ($basedn,      $scope,       $filter,  $attrs,    $attrsonly,
        $serverctrls, $clientctrls, $timeout, $sizelimit) =
            $self->rearrange(['BASEDN',    'SCOPE',  'FILTER', 'ATTRS',
                              'ATTRSONLY', 'SCTRLS', 'CCTRLS', 'TIMEOUT',
                              'SIZELIMIT'],
                             @args);

    croak("No Filter Specified") if ($filter eq "");

    if( $attrs == undef ) {
        my @null_array = ();
        $attrs = \@null_array;
    }

    $sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
    $cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;

    $status =
        ldap_search_ext($self->{"ld"}, $basedn,    $scope,  $filter,
                        $attrs,        $attrsonly, $sctrls, $cctrls,
                        $timeout,      $sizelimit, $msgid);

    ldap_controls_array_free($sctrls) if $sctrls;
    ldap_controls_array_free($cctrls) if $cctrls;

    $self->errorize($status);
    if( $status != $self->LDAP_SUCCESS ) {
        return undef;
    }

    return $msgid;
} # end of search


# synonym for search
sub search_ext
{
    my ($self, @args) = @_;

    return $self->search(@args);
} # end of search_ext


sub search_s
{
    my ($self, @args) = @_;

    my ($result, $status, $sctrls, $cctrls);

    my ($basedn,      $scope,       $filter,  $attrs,    $attrsonly,
        $serverctrls, $clientctrls, $timeout, $sizelimit) =
            $self->rearrange(['BASEDN',    'SCOPE',  'FILTER', 'ATTRS',
                              'ATTRSONLY', 'SCTRLS', 'CCTRLS', 'TIMEOUT',
                              'SIZELIMIT' ], @args);

    croak("No Filter Passed as Argument 3") if ($filter eq "");

    if( $attrs == undef ) {
        my @null_array = ();
        $attrs = \@null_array;
    }

    $sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
    $cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;

    $status =
        ldap_search_ext_s($self->{"ld"}, $basedn,    $scope,   $filter,
                          $attrs,        $attrsonly, $sctrls,  $cctrls,
                          $timeout,      $sizelimit, $result);


    ldap_controls_array_free($sctrls) if $sctrls;
    ldap_controls_array_free($cctrls) if $cctrls;

    $self->errorize($status);
    $self->{"result"} = $result;
    return $status;
} # end of search_s


# synonym for search_s(...)
sub search_ext_s
{
    my ($self, @args) = @_;

    return $self->search_s(@args);
} # end of search_ext_s


sub count_references
{
    my ($self, @args) = @_;

    my ($msg) = $self->rearrange(['MSG'], @args);

    $msg = $self->{"entry"} unless $msg;

    return ldap_count_references($self->{"ld"}, $msg);
} # end of count_references


sub get_option
{
    my ($self, @args) = @_;
    my ($status);

    my ($option, $optdata) = $self->rearrange(['OPTION', 'OPTDATA'], @args);

    $status = ldap_get_option($self->{"ld"}, $option, $optdata);

    return $status;
} # end of get_option


sub set_option
{
    my ($self,@args) = @_;
    my ($status);

    my ($option,$optdata) = $self->rearrange(['OPTION','OPTDATA'],@args);

    $status = ldap_set_option($self->{"ld"},$option,$optdata);

    return $status;
} # end of set_option


# needs testing more XXX
sub set_rebind_proc
{
    my ($self, @args) = @_;
    my ($status);

    my ($rebindproc) = $self->rearrange(['REBINDPROC'], @args);

    if( ref($rebindproc) eq "CODE" ) {
        $status = ldap_set_rebind_proc($self->{"ld"}, $rebindproc);
    } else {
        croak("REBINDPROC is not a CODE Reference");
    }

    return $status;
} # end of set_rebind_proc


# needs docs in a POD bellow. XXX
sub get_all_entries
{
    my ($self, @args) = shift;

    my ($result) = $self->rearrange(['RESULT'], @args);

    $result = $self->{"result"} unless $result;

    croak("NULL Result") unless $result;

    return ldap_get_all_entries($self->{"ld"}, $result);
} # end of get_all_entries


sub unbind
{
    my ($self, @args) = @_;

    my ($status, $sctrls, $cctrls);

    my ($serverctrls, $clientctrls) =
        $self->rearrange(['SCTRLS', 'CCTRLS'], @args);

    $sctrls = 0;
    $cctrls = 0;

    $sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
    $cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;

    $status = ldap_unbind_ext_s($self->{"ld"}, $sctrls, $cctrls);

    ldap_controls_array_free($sctrls) if $sctrls;
    ldap_controls_array_free($cctrls) if $cctrls;

    $self->errorize($status);
    return $status;
} # end of unbind


# do we need these ssl function
sub ssl_client_init
{
    my ($self,@args) = @_;
    my ($status);

    my ($certdbpath,$certdbhandle) = $self->rearrange(['DBPATH','DBHANDLE'],
                                                      @args);

    $status = ldapssl_client_init($certdbpath,$certdbhandle);
    return($status);
} # end of ssl_client_init


# do we need these ssl function
sub ssl
{
    my ($self) = @_;
    my ($status);

    $status = ldapssl_install_routines($self->{"ld"});
    return $status;
} # end of ssl


sub entry
{
    my ($self) = @_;
    return $self->{"entry"};
} # end of entry


sub err
{
    my ($self) = @_;
    return $self->{"errno"};
} # end of err


sub errno
{
    my ($self) = @_;
    return $self->{"errno"};
} # end of errno


sub errstring
{
    my ($self) = @_;
    return ldap_err2string($self->{"errno"});
} # end of errstring


sub extramsg
{
    my ($self) = @_;
    return $self->{"extramsg"};
} # end of extramsg


sub ld
{
    my ($self) = @_;
    return $self->{"ld"};
} # end of ld


sub msgtype
{
    my ($self, @args) = @_;

    my ($msg) = $self->rearrange(['MSG'], @args);

    $msg = $self->{"msg"} unless $msg;

    return ldap_msgtype($msg);
} # end of msgtype

sub msgtype2str
{
    my ($self, @args) = @_;

    my ($type) = $self->rearrange(['TYPE'], @args);

    if(      $type == $self->LDAP_RES_BIND ) {
        return "LDAP_RES_BIND";
    } elsif( $type == $self->LDAP_RES_SEARCH_ENTRY ) {
        return "LDAP_RES_SEARCH_ENTRY";
    } elsif( $type == $self->LDAP_RES_SEARCH_REFERENCE ) {
        return "LDAP_RES_SEARCH_REFERENCE";
    } elsif( $type == $self->LDAP_RES_SEARCH_RESULT ) {
        return "LDAP_RES_SEARCH_RESULT";
    } elsif( $type == $self->LDAP_RES_MODIFY ) {
        return "LDAP_RES_MODIFY";
    } elsif( $type == $self->LDAP_RES_ADD ) {
        return "LDAP_RES_ADD";
    } elsif( $type == $self->LDAP_RES_DELETE ) {
        return "LDAP_RES_DELETE";
    } elsif( $type == $self->LDAP_RES_MODDN ) {
        return "LDAP_RES_MODDN";
    } elsif( $type == $self->LDAP_RES_COMPARE ) {
        return "LDAP_RES_COMPARE";
    } elsif( $type == $self->LDAP_RES_EXTENDED ) {
        return "LDAP_RES_EXTENDED";
    } elsif( $type == $self->LDAP_RES_INTERMEDIATE ) {
        return "LDAP_RES_INTERMEDIATE";
    } elsif( $type == $self->LDAP_RES_ANY ) {
        return "LDAP_RES_ANY";
    } elsif( $type == $self->LDAP_RES_UNSOLICITED ) {
        return "LDAP_RES_UNSOLICITED";
    } else {
        return "UNKNOWN";
    }
} # end of msgtype2str


sub msgid
{
    my ($self, @args) = @_;

    my ($result) = $self->rearrange(['RESULT'], @args);

    $result = $self->{"result"} unless $result;

    return ldap_msgid($self->{"ld"}, $result);
} # end of msgid

# Given array of elements of type Net::LDAP::Control
# array of controls sutable for passing to C-calls is created.
# It is to be freed by calling ldap_controls_array_free(...)
# Note that this method is *NOT* to be used by the end user of
# this library.
sub create_controls_array
{
    my ($self, @args) = @_;

    my ($location, $status, $ctrlp);

    my $ctrls = ldap_controls_array_init($#args + 2);
    for( $location = 0; $location < $#args + 1; $location++ ) {
        ldap_control_set($ctrls, $args[$location], $location);
    }
    ldap_control_set($ctrls, undef, $#args + 1);

    return $ctrls;
} # create_controls_array


# Creates control given its OID and berval. Default value of criticality is true.
sub create_control
{
    my ($self, @args) = @_;

    my ($oid, $berval, $critical) = $self->rearrange(['OID', 'BERVAL', 'CRITICAL'], @args);

    croak("No OID of controls is passed") unless $oid;
    croak("No BerVal is passed")          unless $berval;
    $critical = 1                         if $critical == undef;

    my ($ctrl) = undef;
    my $status = ldap_create_control($oid, $berval, length($berval), $critical, $ctrl);

    $self->errorize($status);
    return $ctrl;
} # end of create_control


sub free_control
{
    my ($self, @args) = @_;

    my ($control) = $self->rearrange(['CONTROL'], @args);

    ldap_control_free($control);
} # end of free_control


# This subroutine was borrowed from CGI.pm.  It does a wonderful job and
# is much better than anything I created in my first attempt at named
# arguments.  I may replace it later.
sub make_attributes
{
    my $attr = shift;
    return () unless $attr && ref($attr) && ref($attr) eq 'HASH';
    my $escape = shift || 0;
    my(@att);
    foreach (keys %{$attr}) {
        my($key) = $_;
        $key=~s/^\-//;     # get rid of initial - if present

        # old way: breaks EBCDIC!
        # $key=~tr/A-Z_/a-z-/; # parameters are lower case, use dashes

        ($key="\L$key") =~ tr/_/-/; # parameters are lower case, use dashes

        my $value = $escape ? simple_escape($attr->{$_}) : $attr->{$_};
        push(@att, defined($attr->{$_}) ? qq/$key="$value"/ : qq/$key/);
    }
    return @att;
} # end of make_attributes


sub rearrange
{
    my($self, $order, @param) = @_;
    return () unless @param;

    return @param unless (defined($param[0]) && substr($param[0],0,1) eq '-');

    my $i;
    for ($i=0;$i<@param;$i+=2) {
        $param[$i]=~s/^\-//;     # get rid of initial - if present
        $param[$i]=~tr/a-z/A-Z/; # parameters are upper case
    }

    my(%param) = @param;                # convert into associative array
    my(@return_array);

    my($key)='';
    foreach $key (@$order) {
        my($value);
        # this is an awful hack to fix spurious warnings when the
        # -w switch is set.
        if (ref($key) && ref($key) eq 'ARRAY') {
            foreach (@$key) {
                last if defined($value);
                $value = $param{$_};
                delete $param{$_};
            }
        } else {
            $value = $param{$key};
            delete $param{$key};
        }
        push(@return_array,$value);
    }
    push (@return_array, $self->make_attributes(\%param)) if %param;
    return (@return_array);
} # end of rearrange


# places internal ldap errors into $self under keys "errno" and "extramsg"
sub errorize {
    my ($self, $status) = @_;

    my ($errdn, $extramsg);

    if ($status != $self->LDAP_SUCCESS) {
        $self->{"errno"}    = ldap_get_lderrno($self->{"ld"}, $errdn, $extramsg);
        $self->{"extramsg"} = $extramsg;

        if( $self->{"debug"} ) {
            print  "LDAP ERROR STATUS: $status ".ldap_err2string($status)."\n";
            printf("LDAP ERROR CODE:   %x\n", $self->{"errno"});
            print  "LDAP ERROR MESSAGE: $extramsg\n";
        }
    } else {
        $self->{"errno"}=0;
        $self->{"errstring"}=undef;
    }
} # end of errorize


sub CRITICAL {
    1;
}


sub NONCRITICAL {
    0;
}

# Preloaded methods go here.

# Autoload methods go after =cut, and are processed by the autosplit program.

1;
__END__

=head1 NAME

Net::LDAPapi - Perl5 Module Supporting LDAP API

=head1 SYNOPSIS

  use Net::LDAPapi;

  See individual items and Example Programs for Usage

=head1 DESCRIPTION

  This module allows Perl programmers to access and manipulate an LDAP
  based Directory.

  Versions beginning with 1.40 support both the original "C API" and
  new "Perl OO" style interface methods.  With version 1.42, I've added
  named arguments.

=head1 THE INTIAL CONNECTION

  All connections to the LDAP server are started by creating a new
  "blessed object" in the Net::LDAPapi class.  This can be done quite
  easily by the following type of statement.

  $ld = new Net::LDAPapi($hostname);

  Where $hostname is the name of your LDAP server.  If you are not using
  the standard LDAP port (389), you will also need to supply the
  portnumber.

  $ld = new Net::LDAPapi($hostname, 15555);

  The new method can also be called with named arguments.

  $ld = new Net::LDAPapi(-host=>$hostname, -port=>15389);

  Instead of the above mentioned argumens -url can be used in the
  following form

  $ld = new Net::LDAPapi(-url=>"ldap://host:port");

  Setting -debug=>"TRUE" will enable more verbose error messages.

  Note that with named arguments, the order of the arguments is
  insignificant.

=head1 CONTROLS

  In LDAP v3 controls are an additional piece of data, which can be
  submitted with most of the requests to the server and returned back
  attached to the result.  Controls, passed to the call, are separated
  in two types.  The client side controls, which are not passed to the
  server and are of not much use.  They are denoted by -cctrls named
  parameter.  The server side controls, denoted by -sctrls named
  parameter are actually passed to the server and may affect its
  operation or returned results.  Each entry of the result may have
  controls attached to it as well ( see parse_entry(...) call ).

  -cctrls and -sctrls must be reference to array of controls.

  To create control call create_control(...) method. Bellow is an
  example of creating valsort control.

  my $asn = Convert::ASN1->new;
  $asn->prepare('SEQUENCE { b BOOLEAN }');
  my $berval = $asn->encode(b=>1); # or 1

  my $ctrl =
    $ld->create_control(-oid=>Net::LDAPapi::LDAP_CONTROL_VALSORT,
                        -berval=>$berval,
                        -critical=>Net::LDAPapi::CRITICAL);

  The control is to be freed by calling free_control($ctrl).

  If contol is attached to results entry, it can be retrieved by
  calling parse_result($entry). If no entry is passed to
  parse_result(...) then current entry is used. It returns hash
  with following keys

  Key           Value
  -------------------
  matcheddn     string
  errmsg        string
  referrals     array reference
  serverctrls   array reference

  You can look into content of the control by using get_contol_XXX
  functions like this:

  local %parsed = $ld->parse_result($entry);
  local $serverctrls = $parsed{"serverctrls"};
  local @sctrls = @$serverctrls;
  if( scalar(@sctrls) > 0 ) {
    foreach $ctrl (@sctrls) {
      print "\nreceived control\n";
      print "oid = ".$ld->get_control_oid($ctrl)."\n";
      print "berval = ".$ld->get_control_berval($ctrl)."\n";
      print "critical = ".$ld->get_control_critical($ctrl)."\n";
    }
  }

=head1 BINDING

  After creating a connection to the LDAP server, you may need to
  bind to the server prior to performing any LDAP related functions.
  This can be done with the 'bind' methods.

  An anonymous bind can be performed without arguments:

  $status = $ld->bind_s;

  A simple bind can be performed by specifying the DN and PASSWORD of
  the user you are authenticating as:

  $status = $ld->bind_s($dn, $password);

  Note that if $password above was "", you would be doing a reference
  bind, which would return success even if the password in the
  directory was non-null.  Thus if you were using the bind to check a
  password entered with one in the directory, you should first check
  to see if $password was NULL.

  To perform SASL bind fill in appropriate parameters calling
  sasl_params(...) and call

  $status = $ld->bind_s(-type=>LDAP_AUTH_SASL)

  Bellow is an example of GSSAPI K5 bind parameters.

  $ld->sasl_parms(-mech=>"GSSAPI", -realm=>"domain.name.com",
                  -authzid=>"",    -secprops=>"",
                  -flags=>LDAP_SASL_QUIET);

  For all of the above operations, you could compare $status to
  LDAP_SUCCESS to see if the operation was successful.

  Additionally, you could use 'bind' rather than 'bind_s' if you wanted
  to use the Asynchronous LDAP routines.  The asynchronous routines
  would return a MSGID rather than a status.  To find the status of an
  Asynchronous bind, you would need to first obtain the result with a
  call to $ld->result.  See the entry for result later in the man page,
  as well as the 'ldapwalk.pl' example for further information on
  obtaining results from Asynchronous operations.

  The bind operations can also accept named arguments.

  $status = $ld->bind_s(-dn=>$dn,
                        -password=>$password,
                        -type=>LDAP_AUTH_SIMPLE);

  As with all other commands that support named arguments, the order of
  the arguments makes no difference.

=head1 GENERATING AN ADD/MODIFY HASH

  For the add and modify routines you will need to generate
  a list of attributes and values.

  You will do this by creating a HASH table.  Each attribute in the
  hash contains associated values.  These values can be one of three
  things.

    - SCALAR VALUE    (ex. "Clayton Donley")
    - ARRAY REFERENCE (ex. ["Clayton Donley","Clay Donley"])
    - HASH REFERENCE  (ex. {"r",["Clayton Donley"]}
         note:  the value inside the HASH REFERENCE must currently
             be an ARRAY REFERENCE.

  The key inside the HASH REFERENCE must be one of the following for a
  modify operation:
    - "a" for LDAP_MOD_ADD (Add these values to the attribute)
    - "r" for LDAP_MOD_REPLACE (Replace these values in the attribute)
    - "d" for LDAP_MOD_DELETE (Delete these values from the attribute)

  Additionally, in add and modify operations, you may specify "b" if the
  attributes you are adding are BINARY (ex. "rb" to replace binary).

  Currently, it is only possible to do one operation per add/modify
  operation, meaning you can't do something like:

     {"d",["Clayton"],"a",["Clay"]}   <-- WRONG!

  Using any combination of the above value types, you can do things like:

  %ldap_modifications = (
     "cn", "Clayton Donley",                    # Replace 'cn' values
     "givenname", ["Clayton","Clay"],           # Replace 'givenname' values
     "mail", {"a",["donley\@cig.mcel.mot.com"],  #Add 'mail' values
     "jpegphoto", {"rb",[$jpegphotodata]},      # Replace Binary jpegPhoto
  );

  Then remember to call the add or modify operations with a REFERENCE to
  this HASH.  Something like:

  $ld->modify_s($modify_dn,\%ldap_modifications);

=head1 GETTING/SETTING LDAP INTERNAL VALUES

  The following methods exist to obtain internal values within a
  Net::LDAPapi object:

  o errno - The last error-number returned by the LDAP library for this
    connection.
          ex:  print "Error Number: " . $ld->errno . "\n";

  o errstring - The string equivalent of 'errno'.
          ex:  print "Error: " . $ld->errstring . "\n";

  o ld - Reference to the actual internal LDAP structure.  Only useful if
    you needed to obtain this pointer for use in non-OO routines.
          ex:  $ldptr = $ld->ld;

  o entry - Reference to the current entry.  Not typically needed, but method
    supplied, just in case.
          ex:  $entry = $ld->entry;

  o msgid - Get msgid from an LDAP Result.
          ex:  $msgid = $ld->msgid;  #  msgid of current result
          ex:  $msgid = $ld->msgid($result) # msgid of $result

  o msgtype - Get msgtype from an LDAP Result.
      ex:  $msgtype = $ld->msgtype;  # msgtype of current result
          ex:  $msgtype = $ld->msgtype($result) # msgtype of $result

  These methods are only useful for GETTING internal information, not setting
  it.  No methods are currently available for SETTING these internal values.

=head1 GETTING AND SETTING LDAP SESSION OPTIONS

  The get_option and set_option methods can be used to get and set LDAP
  session options.

  The following LDAP options can be set or gotten with these methods:
    LDAP_OPT_DEREF - Dereference
    LDAP_OPT_SIZELIMIT - Maximum Number of Entries to Return
    LDAP_OPT_TIMELIMIT - Timeout for LDAP Operations
    LDAP_OPT_REFERRALS - Follow Referrals

  For both get and set operations, the first argument is the relivant
  option.  In get, the second argument is a reference to a scalar variable
  that will contain the current value of the option.  In set, the second
  argument is the value at which to set this option.

  Examples:
    $ld->set_option(LDAP_OPT_SIZELIMIT,50);
    $ld->get_option(LDAP_OPT_SIZELIMIT,\$size);

  When setting LDAP_OPT_REFERRALS, the second argument is either LDAP_OPT_ON
  or LDAP_OPT_OFF.  Other options require a number.

  Both get_option and set_option return 0 on success and non-zero otherwise.

=head1 SSL SUPPORT

  When compiled with the Mozilla SDK, this module now supports SSL.
  I do not have an SSL capable server, but I'm told this works.  The
  functions available are:

  o ssl - Turn on SSL for this connection.
    Install I/O routines to make SSL over LDAP possible
  o ssl_client_init($certdbpath,$certdbhandle)
    Initialize the secure parts (called only once)

  Example:
    $ld = new Net::LDAPapi("host",LDAPS_PORT);
    $ld->ssl_client_init($certdbpath,$certdbhandle);
    $ld->ssl;

=head1 SETTING REBIND PROCESS

  As of version 1.42, rebinding now works properly.

  The set_rebind_proc method is used to set a PERL function to supply DN,
  PASSWORD, and AUTHTYPE for use when the server rebinds (for referals,
  etc...).

  Usage should be something like:
    $rebind_ref = \&my_rebind_proc;
    $ld->set_rebind_proc($rebind_ref);

  You can then create the procedure specified.  It should return 3 values.

  Example:
    sub my_rebind_proc
    {
       return($dn,$pass,LDAP_AUTH_SIMPLE);
    }


=head1 SUPPORTED METHODS

=over 4

=item abandon MSGID SCTRLS CCTRLS

  This cancels an asynchronous LDAP operation that has not completed.  It
  returns an LDAP STATUS code upon completion.

  Example:

    $status = ldap_abandon($ld, $msgid); # XXX fix this

=item add DN ATTR SCTRLS CCTRLS

  Begins an an asynchronous LDAP Add operation.  It returns a MSGID or undef
  upon completion.

  Example:

    %attributes = (
       "cn", ["Clayton Donley","Clay Donley"] #Add Multivalue cn
       "sn", "Donley",                #Add sn
       "telephoneNumber", "+86-10-65551234",  #Add telephoneNumber
       "objectClass", ["person","organizationalPerson"],
                        # Add Multivalue objectClass
       "jpegphoto", {"b",[$jpegphoto]},  # Add Binary jpegphoto
    );

    $entrydn = "cn=Clayton Donley, o=Motorola, c=US";

    $msgid = $ld->add($entrydn, \%attributes);

  Note that in most cases, you will need to be bound to the LDAP server
  as an administrator in order to add users.

=item add_s DN ATTR SCTRLS CCTRLS

  Synchronous version of the 'add' method.  Arguments are identical
  to the 'add' method, but this operation returns an LDAP STATUS,
  not a MSGID.

  Example:

    $ld->add_s($entrydn, \%attributes);

  See the section on creating the modify structure for more information
  on populating the ATTRIBUTES field for Add and Modify operations.

=item bind DN PASSWORD TYPE SCTRLS CCTRLS

  Asynchronous method for binding to the LDAP server.  It returns a
  MSGID.

  Examples:

    $msgid = $ld->bind;
    $msgid = $ld->bind("cn=Clayton Donley, o=Motorola, c=US", "abc123");


=item bind_s DN PASSWORD TYPE SCTRLS CCTRLS

  Synchronous method for binding to the LDAP server.  It returns
  an LDAP STATUS.

  Examples:

    $status = $ld->bind_s;
    $status = $ld->bind_s("cn=Clayton Donley, o=Motorola, c=US", "abc123");


=item compare DN ATTR VALUE SCTRLS CCTRLS

  Asynchronous method for comparing a value with the value contained
  within DN.  Returns a MSGID or undef.

  Example:

    $msgid = $ld->compare("cn=Clayton Donley, o=Motorola, c=US", \
        $type, $value);

=item compare_s DN ATTR VALUE SCTRLS CCTRLS

  Synchronous method for comparing a value with the value contained
  within DN.  Returns an LDAP_COMPARE_TRUE, LDAP_COMPARE_FALSE or an error code.

  Example:

    $status = $ld->compare_s("cn=Clayton Donley, o=Motorola, c=US", \
        $type, $value);

=item count_entries

  Returns the number of entries in an LDAP result chain.

  Example:

    $number = $ld->count_entries;

=item count_references MSG

  Return number of references in a given/current message.

  Example:

    $number = $ld->count_references

=item delete DN

  Asynchronous method to delete DN.  Returns a MSGID or -1 if error.

  Example:

    $msgid = $ld->delete("cn=Clayton Donley, o=Motorola, c=US");

=item delete_s DN

  Synchronous method to delete DN.  Returns an LDAP STATUS.

  Example:

    $status = $ld->delete_s("cn=Clayton Donley, o=Motorola, c=US");

=item dn2ufn DN

  Converts a Distinguished Name (DN) to a User Friendly Name (UFN).
  Returns a string with the UFN.

  Since this operation doesn't require an LDAP object to work, you
  could technically access the function directly as 'ldap_dn2ufn' rather
  that the object oriented form.

  Example:

    $ufn = $ld->dn2ufn("cn=Clayton Donley, o=Motorola, c=US");

=item explode_dn DN NOTYPES

  Splits the DN into an array comtaining the separate components of
  the DN.  Returns an Array.  NOTYPES is a 1 to remove attribute
  types and 0 to retain attribute types.

  Can also be accessed directly as 'ldap_explode_dn' if no session is
  initialized and you don't want the object oriented form.

  In OpenLDAP this call is depricated.

  Example:

    @components = $ld->explode_dn($dn, 0);

=item explode_rdn RDN NOTYPES

  Same as explode_dn, except that the first argument is a
  Relative Distinguished Name.  NOTYPES is a 1 to remove attribute
  types and 0 to retain attribute types.  Returns an array with
  each component.

  Can also be accessed directly as 'ldap_explode_rdn' if no session is
  initialized and you don't want the object oriented form.

  In OpenLDAP this call is depricated.

  Example:

    @components = $ld->explode_rdn($rdn, 0);

=item first_attribute

  Returns pointer to first attribute name found in the current entry.
  Note that this only returning attribute names (ex: cn, mail, etc...).
  Returns a string with the attribute name.

  Returns an empty string when no attributes are available.

  Example:

    $attr = $ld->first_attribute;

=item first_entry

  Sets internal pointer to the first entry in a chain of results.  Returns
  an empty string when no entries are available.

  Example:

    $entry = $ld->first_entry;

=item first_message

   Return the first message in a chain of result returned by the search
   operation. LDAP search operations return LDAPMessage, which is a head
   in chain of messages accessable to the user. Not all all of them are
   entries though. Type of the message can be obtained by calling
   msgtype(...) function.

=item get_all_entries RESULT

  Returns result of the search operation in the following format
    (HASH)
    dn -> (HASH)
          key -> (ARRAY)

  Example:
    my $all_entries_ref = $ld->get_all_entries;
    my %all_entries = %$all_entries_ref;

    foreach (keys %all_entries) {
        print "<$_> -> <".$all_entries{$_}.">\n";
        $entry = $all_entries{$_};

        local %entry_h = %$entry;
        foreach $k (keys %entry_h) {
            $values = $entry_h{$k};

            print "  <$k> ->\n";
            foreach $val (@$values) {
                print "     <$val>\n";
            }
        }
    }

=item get_dn MSG

  Returns a string containing the DN for the specified message or an
  empty string if an error occurs. If no message is specified then
  then default entry is used.

  Example:

    $dn = $ld->get_dn;

=item get_entry_controls MSG

  Returns an array of controls returned with the given entry. If not MSG
  is given as a paramater then current message/entry is used.

  Example:

    my @sctrls = $ld->get_entry_controls($msg);
    foreach $ctrl (@sctrls) {
        print "control oid is ".$self->get_control_oid($ctrl)."\n";
    }

=item get_values ATTR

  Obtain a list of all values associated with a given attribute.
  Returns an empty list if none are available.

  Example:

    @values = $ld->get_values("cn");

  This would put all the 'cn' values for $entry into the array @values.

=item get_values_len ATTR

  Retrieves a set of binary values for the specified attribute.

  Example:

    @values = $ld->get_values_len("jpegphoto");

  This would put all the 'jpegphoto' values for $entry into the array @values.
  These could then be written to a file, or further processed.

=item is_ldap_url URL

  Checks to see if a specified URL is a valid LDAP Url.  Returns 0 on false
  and 1 on true.

  Example:

    $isurl = $ld->is_ldap_url("ldap://x500.my.org/o=Org,c=US");

=item listen_for_changes BASEDN SCOPE FILTER ATTRS ATTRSONLY TIMEOUT SIZELIMIT COOKIE

  Experimental function which implements syncrepl API in
  refreshAndPersist mode. All but one arguments are the same as in search
  function. Argument 'cookie' is the special one here. It must be specified
  and is a file name in which cookie is to be stored. On a subsequent
  restart of the seach only the newer results will be returned than those
  indicated by the stored cookie. To refresh all entries, one would have to
  remove that file.

  This function is to be used in conjunction with next_changed_entries(...),
  there you will also find example of its usage.

=item msgfree

  Frees the current LDAP result.  Returns the type of message freed.

  Example:

    $type = $ld->msgfree;

=item msgtype MSG

  Returns the numeric id of a given message. If no MSG is given as a parameter
  then current message is used. Following types are recognized: LDAP_RES_BIND,
  LDAP_RES_SEARCH_ENTRY, LDAP_RES_SEARCH_REFERENCE, LDAP_RES_SEARCH_RESULT,
  LDAP_RES_MODIFY, LDAP_RES_ADD, LDAP_RES_DELETE, LDAP_RES_MODDN,
  LDAP_RES_COMPARE, LDAP_RES_EXTENDED, LDAP_RES_INTERMEDIATE, LDAP_RES_ANY,
  LDAP_RES_UNSOLICITED.

  Example:

    $type = $ld->msgtype

=item msgtype2str TYPE

  Returns string representation of a given numeric message type.

  Example:
    print "type = ".$ld->msgtype2str($ld->msgtype)."\n";

=item modify DN MOD

  Asynchronous method to modify an LDAP entry.  DN is the DN to
  modify and MOD contains a hash-table of attributes and values.  If
  multiple values need to be passed for a specific attribute, a
  reference to an array must be passed.

  Returns the MSGID of the modify operation.

  Example:

    %mods = (
      "telephoneNumber", "",     #remove telephoneNumber
      "sn", "Test",              #set SN to TEST
      "mail", ["me\@abc123.com","me\@second-home.com"],  #set multivalue 'mail'
      "pager", {"a",["1234567"]},  #Add a Pager Value
      "jpegphoto", {"rb",[$jpegphoto]},  # Replace Binary jpegphoto
    );

    $msgid = $ld->modify($entrydn,\%mods);

  The above would remove the telephoneNumber attribute from the entry
  and replace the "sn" attribute with "Test".  The value in the "mail"
  attribute for this entry would be replaced with both addresses
  specified in @mail.  The "jpegphoto" attribute would be replaced with
  the binary data in $jpegphoto.

=item modify_s DN MOD

  Synchronous version of modify method.  Returns an LDAP STATUS.  See the
  modify method for notes and examples of populating the MOD
  parameter.

  Example:

    $status = $ld->modify_s($entrydn,\%mods);

=item modrdn2 DN NEWRDN DELETE

  No longer available. Use function 'rename'.

=item modrdn2_s DN NEWRDN DELETE

  No longer available. Use function 'rename_s'.

=item next_attribute

  Similar to first_attribute, but obtains next attribute.
  Returns a string comtaining the attribute name.  An empty string
  is returned when no further attributes exist.

  Example:

    $attr = $ld->next_attribute;

=item next_changed_entries MSGID ALL TIMEOUT

 This function is too be used together with listen_for_changes(...) (see above).
 It returns an array of Entries, which has just changed. Each element in this
 array is a hash reference with two key value pairs, 'entry' which contains usual
 entry and 'state' which contain one of the following strings 'present', 'add',
 'modify' or 'delete'.

 Example:

    my $msgid = $ld->listen_for_changes('', LDAP_SCOPE_SUBTREE, "(cn=Dm*)", NULL, NULL,
                                    NULL, NULL, $cookie);

    while(1) {
        while( @entries = $ld->next_changed_entries($msgid, 0, -1) ) {
            foreach $entry (@entries) {
                print "entry dn is <".$ld->get_dn($entry->{'entry'})."> ".
                    $entry->{'state'}."\n";
            }
        }
    }

=item next_entry

  Moves internal pointer to the next entry in a chain of search results.

  Example:

    $entry = $ld->next_entry;

=item next_message

  Moves internal pointer to the next message in a chain of search results.

  Example:

    $msg = $ld->next_message;

=item parse_result MSG FREEMSG

  This function is used to retrieve auxiliary data associated with the
  message. The return value is a hashtable containing following kevalue
  pairs.
    'errcode'     -> numeric
    'matcheddn'   -> string
    'errmsg'      -> string
    'referrals'   -> array reference
    'serverctrls' -> array reference

  The FREEMSG parameter determines whether the parsed message is freed
  or not after the extraction. Any non-zero value will make it free the
  message. The msgfree() routine can also be used to free the message
  later.

=item perror MSG

  If an error occurs while performing an LDAP function, this procedure
  will display it.  You can also use the err and errstring methods to
  manipulate the error number and error string in other ways.

  Note that this function does NOT terminate your program.  You would
  need to do any cleanup work on your own.

  Example:

    $ld->perror("add_s");

=item rename DN NEWRDN NEWSUPER DELETE SCTRLS CCTRLS

  Asynchronous method to change the name of an entry. NEWSUPER is a new
  parent (superior entry).  If set to NULL then only the RDN is changed.
  Set DELETE to non-zero if you wish to remove the attribute values from the
  old name.  Returns a MSGID.

  Example:

    $msgid = $ld->rename("cn=Clayton Donley, o=Motorola, c=US", \
        "cn=Clay Donley", NULL, 0);

=item rename_s DN NEWRDN NEWSUPER DELETE SCTRLS CCTRLS

  Synchronous method to change the name of an entry. NEWSUPER is a new
  parent (superior entry).  If set to NULL then only the RDN is changed.
  Set DELETE to non-zero if you wish to remove the attribute values from the
  old name.  Returns a LDAP STATUS.

  Example:

    $status = $ld->rename("cn=Clayton Donley, o=Motorola, c=US", \
        "cn=Clay Donley", NULL, 0);

=item result MSGID ALL TIMEOUT

  Retrieves the result of an operation initiated using an asynchronous LDAP
  call.  It calls internally ldap_result function.  Returns LDAP message or
  undef if error. Return value of ldap_result call stored in $ld->{"status"}
  and is set -1 if something wrong happened, 0 if specified timeout was
  exceeded or type of the returned message.

  MSGID is the MSGID returned by the Asynchronous LDAP call.  Set ALL to
  0 to receive entries as they arrive, or non-zero to receive all entries
  before returning.  Set TIMEOUT to the number of seconds to wait for the
  result, or -1 for no timeout.

  Example:

    $entry = $ld->result($msgid, 0, 1);
    print "msgtype = ".$ld->msgtype2str($ld->{"status"})."\n";

=item result_entry

  This function is a shortcut for moving pointer along the chain of entries
  in the result. It is used instead of first_entry and next_entry functions.

  Example
    while( $entry = $ld->result_entry ) {
        print "dn = ".$ld->get_dn($entry)."\n";
    }

=item result_message

  This function is a shortcut for moving pointer along the chain of messages
  in the result. It is used instead of first_message and next_message functions.

  Example
    while( $msg = $ld->result_message ) {
        $msgtype = $self->msgtype($msg);
    }

=item search BASE SCOPE FILTER ATTRS ATTRSONLY

  Begins an asynchronous LDAP search.  Returns a MSGID or -1 if an
  error occurs.  BASE is the base object for the search operation.
  FILTER is a string containing an LDAP search filter.  ATTRS is a
  reference to an array containing the attributes to return.  An
  empty array would return all attributes.  ATTRSONLY set to non-zero
  will only obtain the attribute types without values.

  SCOPE is one of the following:
        LDAP_SCOPE_BASE
        LDAP_SCOPE_ONELEVEL
        LDAP_SCOPE_SUBTREE

  Example:

    @attrs = ("cn","sn");    # Return specific attributes
    @attrs = ();             # Return all Attributes

    $msgid = $ld->search("o=Motorola, c=US", LDAP_SCOPE_SUBTREE, \
        "(sn=Donley), \@attrs, 0);

=item search_s BASE SCOPE FILTER ATTRS ATTRSONLY (rewrite XXX)

  Performs a synchronous LDAP search.  Returns an LDAP STATUS.  BASE
  is the base object for the search operation.  FILTER is a string
  containing an LDAP search filter.  ATTRS is a reference to an array
  containing the attributes to return.  An empty array would return all
  attributes.  ATTRSONLY set to non-zero will only obtain the attribute
  types without values.

  SCOPE is one of the following:
        LDAP_SCOPE_BASE
        LDAP_SCOPE_ONELEVEL
        LDAP_SCOPE_SUBTREE

  Example:

    @attrs = ("cn","sn");    # Return specific attributes
    @attrs = ();             # Return all attributes

    $status = $ld->search_s("o=Motorola, c=US",LDAP_SCOPE_SUBTREE, \
        "(sn=Donley)",\@attrs,0);

=item search_st BASE SCOPE FILTER ATTRS ATTRSONLY TIMEOUT (rewrite/remove XXX)

  Performs a synchronous LDAP search with a TIMEOUT.  See search_s
  for a description of parameters.  Returns an LDAP STATUS.  Results are
  put into RESULTS.  TIMEOUT is a number of seconds to wait before giving
  up, or -1 for no timeout.

  Example:

    $status = $ld->search_st("o=Motorola, c=US",LDAP_SCOPE_SUBTREE, \
        "(sn=Donley),[],0,3);

=item unbind SCTRLS CCTRLS

  Unbind LDAP connection with specified SESSION handler.

  Example:

    $ld->unbind;

=item url_parse URL

  Parses an LDAP URL into separate components.  Returns a HASH reference
  with the following keys, if they exist in the URL:

  host      - LDAP Host
  port      - LDAP Port
  dn        - LDAP Base DN
  attr      - LDAP Attributes to Return (ARRAY Reference)
  filter    - LDAP Search Filter
  scope     - LDAP Search Scope
  options   - Mozilla key specifying LDAP over SSL

  Example:

    $urlref = $ld->url_parse("ldap://ldap.my.org/o=My,c=US");

=item url_search URL ATTRSONLY

  Perform an asynchronous search using an LDAP URL.  URL is the LDAP
  URL to search on.  ATTRSONLY determines whether we are returning
  the values for each attribute (0) or only returning the attribute
  names (1).  Results are retrieved and parsed identically to a call
  to the search method.

  Returns a non-negative MSGID upon success.

  Example:

    $msgid = $ld->url_search($my_ldap_url, 0);

=item url_search_s URL ATTRSONLY

  Synchronous version of the url_search method.  Results are retrieved
  and parsed identically to a call to the search_s method.

  Returns LDAP_SUCCESS upon success.

  Example:

    $status = $ld->url_search_s($my_ldap_url, 0);

=item url_search_st URL ATTRSONLY TIMEOUT

  Similar to the url_search_s method, except that it allows a timeout
  to be specified.  The timeout is specified as seconds.  A timeout of
  0 specifies an unlimited timeout.  Results are retrieved and parsed
  identically to a call to the search_st method.

  Returns LDAP_SUCCESS upon success.

  Example:

    $status = $ld->url_search_s($my_ldap_url,0,2);

=back

=head1 AUTHOR

Clayton Donley, donley@wwa.com
http://miso.wwa.com/~donley/

=head1 SEE ALSO

perl(1).

=cut