summaryrefslogtreecommitdiffstats
path: root/lib/Utils/Observer.h
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2009-09-15 19:14:50 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2009-09-15 19:14:50 +0200
commitb60ce4965bee80f44db8a7e76382ecbd2947e4d4 (patch)
tree4e9d265577aa34e8f5595b3d4c91627f0b261bc2 /lib/Utils/Observer.h
parentfbe358995978f60ad23ed86e71cfa8ba425b70eb (diff)
parent670a6e1741242096ee0fdd4ae98953493c1b743b (diff)
Merge branch 'master' of ssh://vda@git.fedorahosted.org/git/abrt
Diffstat (limited to 'lib/Utils/Observer.h')
0 files changed, 0 insertions, 0 deletions
/a> 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
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

/*
	Virtual Attributes 
	
	This file implements the "virtual attribute API", which is the interface
	by which any joe servercode gets the values of attributes associated with an
	entry which are _not_ stored with the entry. Attributes which _are_ stored
	with the entry will also be returned by this interface, unless the caller
	specifies the SLAPI_REALATTRS_ONLY flag. This means that the casual caller
	looking for attribute values should probably call the virtual attribute interface
	rather than the regular Slapi_Entry_Attr_Find... and so on calls.
	This interface is different from that one in that the data returned is
	a copy, to be freed by the caller. Details on how to free the returned data is 
	given individually for each function.

	The thing is implemented with a service provider model. The code in here
	does NOT know how to generate computed attr values etc, it just calls the
	registered providers of such information. It also takes care of any crusty
	special cases. Everything above the line here is crispy clean.

	Implicit in this interface is the assumption that the value of an attribute
	can only depend on the entry and some independent stored state. For example,
	the value can't depend upon who is asking, since we don't pass that information
	over the interface.

  More design: we'd like to have the regular result returning code in result.c call this
  interface. However, as it stands, his would incur a malloc, a copy and a free for every
  value. Too expensive. So, it would be good to modify the interface such that when we 
  retrieve values from inside the entry, we just fetch a pointer like before. When we
  retrieve values which are generated, we get copies of the data (or can we get pointers
  there too ?? --- no). 

  One way to achieve this: allow the caller to say that they perfer to receive pointers
  and not copies. They are then informed by the function whether they did receive pointers
  or copies. They then call the free function only when needed. The implicit lifetime of
  the returned pointers is the lifetime of the entry object passed into the function.
  Nasty, but one has to do these things in the name of the god performance.

  DBDB: remember to rename the structures mark complained weren't compliant with the slapi naming scheme.
	
*/

#include "slap.h"

#include "vattr_spi.h"
#include "statechange.h"

#ifdef SOURCEFILE 
#undef SOURCEFILE
#endif
#define SOURCEFILE "vattr.c"
static char *sourcefile = SOURCEFILE;

/* Define only for module test code */
/* #define VATTR_TEST_CODE */

/* Loop context structure */
struct _vattr_context {
	Slapi_PBlock *pb;
	unsigned int vattr_context_loop_count;
	unsigned int error_displayed;
};
#define VATTR_LOOP_COUNT_MAX 50

typedef  vattr_sp_handle vattr_sp_handle_list;

/* Local prototypes */
static int vattr_map_create();
static void vattr_map_destroy();
int vattr_map_sp_insert(char *type_to_add, vattr_sp_handle *sp, void *hint);
vattr_sp_handle_list *vattr_map_sp_getlist(char *type_to_find);
vattr_sp_handle_list *vattr_map_namespace_sp_getlist(Slapi_DN *dn, const char *type_to_find);
vattr_sp_handle_list *vattr_map_sp_get_complete_list();
vattr_sp_handle *vattr_map_sp_first(vattr_sp_handle_list *list,void **hint);
vattr_sp_handle *vattr_map_sp_next(vattr_sp_handle_list *list,void **hint);
vattr_sp_handle *vattr_list_sp_first(vattr_sp_handle_list *list);
vattr_sp_handle *vattr_list_sp_next(vattr_sp_handle_list *list);
int vattr_call_sp_get_value(vattr_sp_handle *handle, vattr_context *c, Slapi_Entry *e, vattr_get_thang *my_get, char *type, Slapi_ValueSet** results,int *type_name_disposition, char** actual_type_name, int flags, int *buffer_flags, void *hint);
int vattr_call_sp_get_batch_values(vattr_sp_handle *handle, vattr_context *c, Slapi_Entry *e, vattr_get_thang *my_get, char **type, Slapi_ValueSet*** results,int **type_name_disposition, char*** actual_type_name, int flags, int *buffer_flags, void** hint);
int vattr_call_sp_compare_value(vattr_sp_handle *handle, vattr_context *c, Slapi_Entry *e, vattr_get_thang *my_get, const char *type, Slapi_Value* test_this,int *result, int flags, void* hint);
int vattr_call_sp_get_types(vattr_sp_handle *handle,Slapi_Entry *e,vattr_type_list_context *type_context,int flags);
void schema_changed_callback(Slapi_Entry *e, char *dn, int modtype, Slapi_PBlock *pb, void *caller_data);
int slapi_vattrspi_register_internal(vattr_sp_handle **h, vattr_get_fn_type get_fn, vattr_get_ex_fn_type get_ex_fn, vattr_compare_fn_type compare_fn, vattr_types_fn_type types_fn, void *options);

#ifdef VATTR_TEST_CODE
int vattr_basic_sp_init();
#endif

void **statechange_api;

/* Housekeeping Functions, called by server startup/shutdown code */

/* Called on server startup, init all structures etc */
void vattr_init()
{
	statechange_api = 0;
	vattr_map_create();

#ifdef VATTR_TEST_CODE
	vattr_basic_sp_init(); 
#endif
}

/* Called on server shutdown, free all structures, inform service providers that we're going down etc */
void vattr_cleanup()
{
	vattr_map_destroy();
}

/* The public interface functions start here */

/* Function which returns the value(s) of an attribute, given an entry and the attribute type name */
/* Return 0 if OK, ?? if attr doesn't exist, ?? if some error condition ?? if result doesn't need to be free'ed */

int slapi_vattr_values_get(/* Entry we're interested in */ Slapi_Entry *e, /* attr type name */ char *type, /* pointer to result set */ Slapi_ValueSet** results,int *type_name_disposition, char** actual_type_name, int flags, int *buffer_flags) 
{
	vattr_context *c = NULL;
	return slapi_vattr_values_get_sp(c,e,type,results,type_name_disposition,actual_type_name,flags, buffer_flags);
}

int slapi_vattr_values_get_ex(/* Entry we're interested in */ Slapi_Entry *e,/* attr type name */ char *type, /* pointer to result set */ Slapi_ValueSet*** results,int **type_name_disposition, char*** actual_type_name, int flags, int *buffer_flags, int *subtype_count) 
{
	vattr_context *c = NULL;
	return slapi_vattr_values_get_sp_ex(c,e,type,results,type_name_disposition,actual_type_name,flags, buffer_flags, subtype_count);
}

int slapi_vattr_namespace_values_get(/* Entry we're interested in */ Slapi_Entry *e, /* backend namespace dn*/ Slapi_DN *namespace_dn, /* attr type name */ char *type, /* pointer to result set */ Slapi_ValueSet*** results,int **type_name_disposition, char*** actual_type_name, int flags, int *buffer_flags, int *subtype_count) 
{
	vattr_context *c = NULL;
	return slapi_vattr_namespace_values_get_sp(c,e,namespace_dn,type,results,type_name_disposition,actual_type_name,flags, buffer_flags, subtype_count);
}


/*
 * If pointers into the entry were requested then we might have
 * a stashed pointer to the entry values, otherwise will be null.
 */
Slapi_ValueSet *vattr_typethang_get_values(vattr_type_thang *t)
{
	return t->type_values;
}

/*
 * This can be much faster than using slapi_vattr_values_get()
 * when you have a vattr_type_thang list returned from slapi_vattr_list_types().
 *
 * We call the vattr SPs to get values for an attribute type in the list only
 * if the results field for that attribute type is null.
 * If the type list comes from slapi_vattr_list_types() then the value is null
 * only for attributes for which an SP wishes to provide a value, so fo the other
 * ones don't bother calling the SPs again--just use the real value we picked up
 * from the entry.
 * 
 */
int slapi_vattr_values_type_thang_get(
    Slapi_Entry *e,
    vattr_type_thang *type_thang,
    Slapi_ValueSet **results,
    int *type_name_disposition,
    char **actual_type_name,
    int flags,
    int *buffer_flags
)
{
    int rc = 0;
    char *type = NULL;

    type = vattr_typethang_get_name(type_thang);
    *results = vattr_typethang_get_values(type_thang);

    if (*results != NULL) {
        /* we already have a pointer directly into the entry */
        *actual_type_name = type;
		*type_name_disposition = 
	    			SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_EXACTLY_OR_ALIAS;
		*buffer_flags = SLAPI_VIRTUALATTRS_RETURNED_POINTERS;
    } else {
        /* fall back to usual implementation */
        rc = slapi_vattr_values_get(e,type,results,
				    type_name_disposition, 
				    actual_type_name,
				    flags,buffer_flags);
    }

    return rc;
}

static void vattr_helper_get_entry_conts(Slapi_Entry *e,char *type, vattr_get_thang *my_get)
{
	Slapi_Attr *a = NULL;
	void *dummy = 0;

	a = attrlist_find_ex(e->e_attrs,type,&(my_get->get_name_disposition), &(my_get->get_type_name), &dummy);
	if (a) {
		my_get->get_present = 1;
		my_get->get_present_values = &(a->a_present_values);
		my_get->get_attr = a;
	}
}

static int vattr_helper_get_entry_conts_with_subtypes(Slapi_Entry *e,const char *type, vattr_get_thang **my_get)
{
	Slapi_Attr *a = NULL;
	void *hint = 0;
	int counter = 0;
	int attr_count = attrlist_count_subtypes(e->e_attrs,type);

	if(attr_count > 0)
	{
		*my_get = (vattr_get_thang *)slapi_ch_calloc(attr_count, sizeof(vattr_get_thang));

		/* pick up attributes with sub-types and slip into the get_thang list */
		for(counter = 0; counter < attr_count; counter++)
		{
			a = attrlist_find_ex(e->e_attrs,type,&((*my_get)[counter].get_name_disposition), &((*my_get)[counter].get_type_name), &hint);
			if (a) {
				(*my_get)[counter].get_present = 1;
				(*my_get)[counter].get_present_values = &(a->a_present_values);
				(*my_get)[counter].get_attr = a;
			}
		}
	}

	return attr_count;
}

static int vattr_helper_get_entry_conts_no_subtypes(Slapi_Entry *e,const char *type, vattr_get_thang **my_get)
{
        int                     attr_count = 0;
        Slapi_Attr *a = attrlist_find(e->e_attrs,type);

        if (a) {
                attr_count = 1;
                *my_get = (vattr_get_thang *)slapi_ch_calloc(1, sizeof(vattr_get_thang));
                (*my_get)[0].get_present = 1;
                (*my_get)[0].get_name_disposition = SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_EXACTLY_OR_ALIAS;
                (*my_get)[0].get_type_name = a->a_type;
                (*my_get)[0].get_present_values = &(a->a_present_values);
                (*my_get)[0].get_attr = a;
        }

        return attr_count;
}

static int vattr_helper_get_entry_conts_ex(Slapi_Entry *e,const char *type, vattr_get_thang **my_get, int suppress_subtypes)
{
        int             rc;

        if ( suppress_subtypes ) {
                rc = vattr_helper_get_entry_conts_no_subtypes(e, type, my_get); 
        } else {
                rc = vattr_helper_get_entry_conts_with_subtypes(e, type, my_get); 
        }

        return rc;
}


vattr_context *vattr_context_new( Slapi_PBlock *pb )
{
	vattr_context *c = (vattr_context *)slapi_ch_calloc(1, sizeof(vattr_context));
	/* The payload is zero, which is what we want */
	if ( c ) {
		c->pb = pb;
	}

	return c;
}

static int vattr_context_check(vattr_context *c)
{
	/* Observe the loop count and see if it's higher than is allowed */
	if (c->vattr_context_loop_count > VATTR_LOOP_COUNT_MAX) {
		return SLAPI_VIRTUALATTRS_LOOP_DETECTED;
	} else return 0;
}

static void vattr_context_mark(vattr_context *c)
{
	c->vattr_context_loop_count += 1;
}

static int vattr_context_unmark(vattr_context *c)
{
	return (c->vattr_context_loop_count -= 1);
}

/* modify the context structure on exit from a vattr sp function */
static void  vattr_context_ungrok(vattr_context **c)
{
	/* Decrement the loop count */
	if (0 == vattr_context_unmark(*c)) {
		/* If necessary, delete the structure */
		slapi_ch_free((void **)c);
	}
}

/* Check and mess with the context structure on entry to a vattr sp function */
static int vattr_context_grok(vattr_context **c)
{
	int rc = 0;
		/* First check that we've not got into an infinite loop.
	   We do so by means of the vattr_context structure.
	 */

	/* Do we have a context at all ?? */
	if (NULL == *c) {
		/* No, so let's make one */
		*c = vattr_context_new( NULL );
		if (NULL == *c) {
			return ENOMEM;
		}
	} else {
		/* Yes, so let's check its contents */
		rc = vattr_context_check(*c);
	}
	/* mark the context as having been used once */
	vattr_context_mark(*c);
	return rc;
}



/* keep track of error messages so we don't spam the error log */
static void vattr_context_set_loop_msg_displayed(vattr_context **c)
{
	(*c)->error_displayed = 1;
}

static int vattr_context_is_loop_msg_displayed(vattr_context **c)
{
	return (*c)->error_displayed;
}

/*
 * vattr_test_filter:
 * 
 * . tests an ava, presence or substring filter against e.
 * . group these filter types together to avoid having to duplicate the
 * . vattr specific code in three seperate routines.
 * . handles virtual attrs in the filter.
 * . does update the vattrcache if a call to this calculates vattrs
 *
 * returns: 0	filter matched
 *			-1	filter did not match
 *			>0	an ldap error code 
 *
*/
int vattr_test_filter( /* Entry we're interested in */ Slapi_Entry *e,    					
						Slapi_Filter *f,
						filter_type_t filter_type,
						char * type) {	
	int rc = -1;
	int sp_bit = 0; /* Set if an SP supplied an answer */
	vattr_sp_handle_list *list = NULL;
	Slapi_DN *sdn;
	Slapi_Backend *be;
	Slapi_DN *namespace_dn;

	/* get the namespace this entry belongs to */
	sdn = slapi_entry_get_sdn( e );
	be = slapi_be_select( sdn );
	namespace_dn = (Slapi_DN*)slapi_be_getsuffix(be, 0);
		
	/* Look for attribute in the map */
	
	if(namespace_dn)
	{
		list = vattr_map_namespace_sp_getlist(namespace_dn, type);
	}
	else
	{
		list = vattr_map_namespace_sp_getlist(NULL, type);
	}

	if (list) {
		vattr_sp_handle *current_handle = NULL;
		/* first lets consult the cache to save work */
		int cache_status;
					
		cache_status = slapi_entry_vattrcache_findAndTest(e, type,
														f,
														filter_type,
														&rc);
		switch(cache_status)
		{
			case SLAPI_ENTRY_VATTR_RESOLVED_EXISTS: /* cached vattr */
			{
				sp_bit = 1;				
				break;
			}

			case SLAPI_ENTRY_VATTR_RESOLVED_ABSENT: /* does not exist */
				break; /* look in entry */

			case SLAPI_ENTRY_VATTR_NOT_RESOLVED: /* not resolved */
			default: /* any other result, resolve */
			{
				int flags = SLAPI_VIRTUALATTRS_REQUEST_POINTERS;
				void *hint = NULL;
				Slapi_ValueSet **results = NULL; /* pointer to result set */
				int  *type_name_disposition;
				char **actual_type_name;
				int buffer_flags;
				vattr_get_thang my_get = {0};
				vattr_context ctx;
				/* bit cacky, but need to make a null terminated lists for now
				 * for the (unimplemented and so fake) batch attribute request
				 */
				char *types[2];
				void *hint_list[2];

				types[0] = type;
				types[1] = 0;
				hint_list[1] = 0;

				/* set up some local context */
				ctx.vattr_context_loop_count=1;
				ctx.error_displayed = 0;

				for (current_handle = vattr_map_sp_first(list,&hint); current_handle; current_handle = vattr_map_sp_next(current_handle,&hint))
				{			
					hint_list[0] = hint;

					rc = vattr_call_sp_get_batch_values(current_handle,&ctx,e,
							&my_get,types,&results,&type_name_disposition,
								&actual_type_name,flags,&buffer_flags, hint_list);
						
					if (0 == rc)
					{
						sp_bit = 1;
						break;
					}
				}

				if(!sp_bit)
				{
					/*
					 * No vattr sp supplied an answer so will look in the
					 * entry itself.
					 * but first lets cache the no result
					*/			
					slapi_entry_vattrcache_merge_sv(e, type, NULL );

				}
				else
				{
					/*
					 * A vattr sp supplied an answer.
					 * so turn the value into a Slapi_Attr, pass
					 *  to the syntax plugin for comparison.
					*/

					if ( filter_type == FILTER_TYPE_AVA ||
							filter_type == FILTER_TYPE_SUBSTRING ) {				
						Slapi_Attr *a = NULL;
						int i=0;
						/* may need this when we have a passin interface for set_valueset */
						/*Slapi_ValueSet null_valueset = {0};*/

						rc=-1;

						if(results && actual_type_name && type_name_disposition)
						{
							while(results[i] && rc)
							{
    	    					a = slapi_attr_new();
        						slapi_attr_init(a, type);
								/* a now contains a *copy* of results */
        						slapi_attr_set_valueset( a, results[i]);

								if ( filter_type == FILTER_TYPE_AVA ) {
									rc = plugin_call_syntax_filter_ava( a,
															f->f_choice, &f->f_ava );
								} else if ( filter_type == FILTER_TYPE_SUBSTRING) {
									rc = plugin_call_syntax_filter_sub( a,
																		&f->f_sub);
								}

								/*
								 * Cache stuff: dups results
								*/
								slapi_entry_vattrcache_merge_sv(e, actual_type_name[i],
																results[i] );
								/*
								 * Free stuff, just in case we did not
								 * get pointers.					 
								*/
								slapi_vattr_values_free( &(results[i]),
														&(actual_type_name[i]),
														buffer_flags);
								/* may need this when we support a passin set_valueset */
								/*slapi_attr_set_valueset( a, &null_valueset);*/
								/* since a contains a copy of results, we must free it */
								slapi_attr_free(&a);
								i++;
							}
						}

						slapi_ch_free((void**)&results);
						slapi_ch_free((void**)&actual_type_name);
						slapi_ch_free((void**)&type_name_disposition);

					} else if ( filter_type == FILTER_TYPE_PRES ) {
						/*
						 * Cache stuff: dups results
						*/
						int i=0;

						while(results[i])
						{
							slapi_entry_vattrcache_merge_sv(e, actual_type_name[i],
															results[i] );
							/*
							 * Free stuff, just in case we did not
							 * get pointers.					 
							*/
							slapi_vattr_values_free( &results[i],
													&actual_type_name[i],
													buffer_flags);
						}
						slapi_ch_free((void**)&results);
						slapi_ch_free((void**)&actual_type_name);
						slapi_ch_free((void**)&type_name_disposition);
					}
				}

				break;
			}			
		}/* switch */		
	}
	/* If no SP supplied the answer, take it from the entry */
	if (!sp_bit) 
	{		
		int acl_test_done;

		if ( filter_type == FILTER_TYPE_AVA ) {
			
			rc = test_ava_filter( NULL /* pb not needed */,
									e, e->e_attrs, &f->f_ava,
									f->f_choice,
									0 /* no access check */,
									0 /* do test filter */,
									&acl_test_done);
								    		
    	} else if ( filter_type == FILTER_TYPE_SUBSTRING ) {
			
			rc = test_substring_filter( NULL, e, f, 0 /* no access check */,
									0 /* do test filter */, &acl_test_done);
				
		} else if ( filter_type == FILTER_TYPE_PRES ) {
	
			rc = test_presence_filter( NULL, e, f->f_type,
										0 /* no access check */,
										0 /* do test filter */,
										&acl_test_done);
		}
	} 
	return rc;
}
/*
 * deprecated in favour of slapi_vattr_values_get_sp_ex() which
 * returns subtypes too.
*/
SLAPI_DEPRECATED int
slapi_vattr_values_get_sp(vattr_context *c,
							/* Entry we're interested in */ Slapi_Entry *e,
							/* attr type name */ char *type,
							/* pointer to result set */ Slapi_ValueSet** results,
							int *type_name_disposition,
							char** actual_type_name, int flags,
							int *buffer_flags) 
{

  PRBool use_local_ctx=PR_FALSE;
  vattr_context ctx;
	int rc = 0;
	int sp_bit = 0; /* Set if an SP supplied an answer */
	vattr_sp_handle_list *list = NULL;

	vattr_get_thang my_get = {0};

	if (c != NULL) {
	  rc = vattr_context_grok(&c);
	  if (0 != rc) {
		if(!vattr_context_is_loop_msg_displayed(&c))
		{
			/* Print a handy error log message */
			LDAPDebug(LDAP_DEBUG_ANY,"Detected virtual attribute loop in get on entry %s, attribute %s\n", slapi_entry_get_dn_const(e), type, 0);
			vattr_context_set_loop_msg_displayed(&c);
		}
	    return rc;
	  }
	} else {
	  use_local_ctx=PR_TRUE;
	  ctx.vattr_context_loop_count=1;
	  ctx.error_displayed = 0;
	}

	/* For attributes which are in the entry, we just need to get to the Slapi_Attr structure and yank out the slapi_value_set 
	   structure. We either return a pointer directly to it, or we copy it, depending upon whether the caller asked us to try to 
	   avoid copying.
	*/

	/* First grok the entry, and remember what we saw. This call does no more than walk down the entry attribute list, do some string compares and copy pointers. */
	vattr_helper_get_entry_conts(e,type, &my_get);
	/* Having done that, we now consult the attribute map to find service providers who are interested */
	/* Look for attribute in the map */
	if(!(flags & SLAPI_REALATTRS_ONLY))
	{
		list = vattr_map_sp_getlist(type);
		if (list) {
			vattr_sp_handle *current_handle = NULL;
			void *hint = NULL;
			/* first lets consult the cache to save work */
			int cache_status;
						
			cache_status =
				slapi_entry_vattrcache_find_values_and_type(e, type,
															results,
															actual_type_name);															
			switch(cache_status)
			{
				case SLAPI_ENTRY_VATTR_RESOLVED_EXISTS: /* cached vattr */
				{
					sp_bit = 1;					

					/* Complete analysis of type matching */
					if ( 0 == slapi_attr_type_cmp( type , *actual_type_name, SLAPI_TYPE_CMP_EXACT) )
					{
						*type_name_disposition = SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_EXACTLY_OR_ALIAS;
					} else {
						*type_name_disposition = SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_SUBTYPE;
					}

					break;
				}

				case SLAPI_ENTRY_VATTR_RESOLVED_ABSENT: /* does not exist */
					break; /* look in entry */

				case SLAPI_ENTRY_VATTR_NOT_RESOLVED: /* not resolved */
				default: /* any other result, resolve */
				{
					for (current_handle = vattr_map_sp_first(list,&hint); current_handle; current_handle = vattr_map_sp_next(current_handle,&hint))
					{
						if (use_local_ctx)
						{
							rc = vattr_call_sp_get_value(current_handle,&ctx,e,&my_get,type,results,type_name_disposition,actual_type_name,flags,buffer_flags, hint);
						}
						else
						{
							/* call this SP */
							rc = vattr_call_sp_get_value(current_handle,c,e,&my_get,type,results,type_name_disposition,actual_type_name,flags,buffer_flags, hint);
						}

						if (0 == rc)
						{
							sp_bit = 1;
							break;
						}
					}

					if(!sp_bit)
					{
						/* clean up, we have failed and must now examine the
						 * entry itself
						 * But first lets cache the no result
						 * Creates the type (if necessary).
						*/
						slapi_entry_vattrcache_merge_sv(e, type, NULL );

					}
					else
					{
						/*
						 * we need to cache the virtual attribute
						 * creates the type (if necessary) and dups
						 * results.
						*/
						slapi_entry_vattrcache_merge_sv(e, *actual_type_name,
														*results );
					}

					break;
				}
			}
		}
	}
	/* If no SP supplied the answer, take it from the entry */
	if (!sp_bit && !(flags & SLAPI_VIRTUALATTRS_ONLY)) 
	{
		rc = 0; /* reset return code (cause an sp must have failed) */
		*type_name_disposition = my_get.get_name_disposition;

		if (my_get.get_present) {
			if (flags & SLAPI_VIRTUALATTRS_REQUEST_POINTERS) {
				*results = my_get.get_present_values;
				*actual_type_name = my_get.get_type_name;
			} else {
				*results = valueset_dup(my_get.get_present_values);
				if (NULL == *results) {
					rc = ENOMEM;
				} else {
					*actual_type_name = slapi_ch_strdup(my_get.get_type_name);
					if (NULL == *actual_type_name) {
						rc = ENOMEM;
					}
				}
			}
			if (flags & SLAPI_VIRTUALATTRS_REQUEST_POINTERS) {
					*buffer_flags = SLAPI_VIRTUALATTRS_RETURNED_POINTERS;
			} else {
				*buffer_flags = SLAPI_VIRTUALATTRS_RETURNED_COPIES;
			}
		} else {
			rc = SLAPI_VIRTUALATTRS_NOT_FOUND;
		}
	}
	if (!use_local_ctx) {
	  vattr_context_ungrok(&c);
	}
	return rc;
}

/*
 *
 * returns 0: (no_error && type found ) in which case:
 *              results: contains the current values for type and
 *						all it's subtypes in e
 *              type_name_disposition: how each type was matched
 *                      SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_EXACTLY_OR_ALIAS
 *                      SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_SUBTYPE
 *              actual_type_name: type name as found
 *              flags: bit mask of options:
 *                      SLAPI_REALATTRS_ONLY
 *                      SLAPI_VIRTUALATTRS_ONLY
 *                      SLAPI_VIRTUALATTRS_REQUEST_POINTERS
 *                      SLAPI_VIRTUALATTRS_LIST_OPERATIONAL_ATTRS
 *              buffer_flags: bit mask to be used as input flags for
 *                              slapi_values_free()
 *                  SLAPI_VIRTUALATTRS_RETURNED_POINTERS
 *                  SLAPI_VIRTUALATTRS_RETURNED_COPIES
 *                  SLAPI_VIRTUALATTRS_REALATTRS_ONLY
 *				item_count: number of subtypes matched
 * otherwise:
 *          SLAPI_VIRTUALATTRS_LOOP_DETECTED (failed to eval a vattr)
 *          SLAPI_VIRTUALATTRS_NOT_FOUND     (type not recognised by any vattr
 *                                              sp && not a real attr in entry )
 *          ENOMEM                           (memory error)
 *
 *	Note:
 *  . modifes the virtual cache in the entry.
 *  . for cached vattrs you always get a copy, so it will need to be
 * 	freed via slapi_values_free().
 *
 */

int slapi_vattr_values_get_sp_ex(vattr_context *c,
							/* Entry we're interested in */ Slapi_Entry *e,
							/* attr type name */ char *type,
							/* pointer to result set */ Slapi_ValueSet*** results,
							int **type_name_disposition,
							char*** actual_type_name, int flags,
							int *buffer_flags, int *item_count) 
{
	return slapi_vattr_namespace_values_get_sp(
		c, e, NULL, type, results, type_name_disposition,
		actual_type_name, flags, buffer_flags, item_count
		);
}

int slapi_vattr_namespace_values_get_sp(vattr_context *c,
							/* Entry we're interested in */ Slapi_Entry *e,
							/* DN denoting backend namespace */ Slapi_DN *namespace_dn,
							/* attr type name */ char *type,
							/* pointer to result set */ Slapi_ValueSet*** results,
							int **type_name_disposition,
							char*** actual_type_name, int flags,
							int *buffer_flags, int *item_count) 
{

	int rc = 0;
	int sp_bit = 0; /* Set if an SP supplied an answer */
	vattr_sp_handle_list *list = NULL;
	vattr_get_thang *my_get = NULL;
	int attr_count = 0;

	rc = vattr_context_grok(&c);
	if (0 != rc) {
		/* Print a handy error log message */
		if(!vattr_context_is_loop_msg_displayed(&c))
		{
			LDAPDebug(LDAP_DEBUG_ANY,"Detected virtual attribute loop in get on entry %s, attribute %s\n", slapi_entry_get_dn_const(e), type, 0);
			vattr_context_set_loop_msg_displayed(&c);
		}
		return rc;
	}

	/* Having done that, we now consult the attribute map to find service providers who are interested */
	/* Look for attribute in the map */
	if(!(flags & SLAPI_REALATTRS_ONLY))
	{
		/* we use the vattr namespace aware version of this */
		list = vattr_map_namespace_sp_getlist(namespace_dn, type);
		if (list) {
			vattr_sp_handle *current_handle = NULL;
			void *hint = NULL;
			/* first lets consult the cache to save work */
			int cache_status;
			
			cache_status =
					slapi_entry_vattrcache_find_values_and_type_ex(e, type,
															results,
															actual_type_name);
			switch(cache_status)
			{
				case SLAPI_ENTRY_VATTR_RESOLVED_EXISTS: /* cached vattr */
				{
					sp_bit = 1;					

					/* Complete analysis of type matching */
					*type_name_disposition =
							(int *)slapi_ch_malloc(sizeof(*type_name_disposition));
					if ( 0 == slapi_attr_type_cmp( type , **actual_type_name, SLAPI_TYPE_CMP_EXACT) )
					{
						**type_name_disposition =
							SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_EXACTLY_OR_ALIAS;
					} else {
						**type_name_disposition =
							SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_SUBTYPE;
					}

					*item_count = 1;

					break;
				}

				case SLAPI_ENTRY_VATTR_RESOLVED_ABSENT: /* does not exist */
					break; /* look in entry */

				case SLAPI_ENTRY_VATTR_NOT_RESOLVED: /* not resolved */
				default: /* any other result, resolve */
				{
					/* bit cacky, but need to make a null terminated lists for now
					 * for the (unimplemented and so fake) batch attribute request
					 */
					char **type_list = (char**)slapi_ch_calloc(2,sizeof(char*));
					void **hint_list = (void**)slapi_ch_calloc(2,sizeof(void*));

					type_list[0] = type;

					for (current_handle = vattr_map_sp_first(list,&hint); current_handle; current_handle = vattr_map_sp_next(current_handle,&hint)) {
						/* call this SP */

						hint_list[0] = hint;

						rc = vattr_call_sp_get_batch_values(current_handle,c,e,my_get,
								type_list,results,type_name_disposition,
								actual_type_name, flags,buffer_flags, hint_list);
						if (0 == rc) {
							sp_bit = 1; /* this sp provided an answer, we are done */

							/* count the items in the (null terminated) result list */
							*item_count = 0;

							while((*results)[*item_count])
							{
								(*item_count)++;
							}

							break;
						}
					}

					slapi_ch_free((void**)&type_list);
					slapi_ch_free((void**)&hint_list);

					if(!sp_bit)
					{
						/* we have failed and must now examine the entry itself
						 *
						 * But first lets cache the no result
						 * dups the type (if necessary).
						*/
						slapi_entry_vattrcache_merge_sv(e, type, NULL );

					}
					else
					{
						/*
						 * we need to cache the virtual attribute
						 * dups the type (if necessary) and results.
						*/
						/*
						 * this (and above) will of course need to get updated
						 * when we do real batched attributes
						 */
						slapi_entry_vattrcache_merge_sv(e, **actual_type_name,
															**results );
					}
				}
			}
		}
	}

	/* If no SP supplied the answer, take it from the entry */
	if (!sp_bit && !(flags & SLAPI_VIRTUALATTRS_ONLY)) 
	{
		int counter;

		/* First grok the entry - allocates memory for list */
		attr_count = vattr_helper_get_entry_conts_ex(e,type, &my_get,
				(0 != (flags & SLAPI_VIRTUALATTRS_SUPPRESS_SUBTYPES)));
		*item_count = attr_count;
		rc = 0; /* reset return code (cause an sp must have failed) */

		if(attr_count > 0)
		{	

			*results = (Slapi_ValueSet**)slapi_ch_calloc(1, sizeof(*results) * attr_count);
			*type_name_disposition = (int *)slapi_ch_malloc(sizeof(*type_name_disposition)  * attr_count);
			*actual_type_name = (char**)slapi_ch_malloc(sizeof(*actual_type_name)  * attr_count);

			/* For attributes which are in the entry, we just need to get to the Slapi_Attr structure and yank out the slapi_value_set 
			   structure. We either return a pointer directly to it, or we copy it, depending upon whether the caller asked us to try to 
			   avoid copying.
			*/
			for(counter = 0; counter < attr_count; counter++)
			{
				(*type_name_disposition)[counter] = my_get[counter].get_name_disposition;

				if (my_get[counter].get_present) {
					if (flags & SLAPI_VIRTUALATTRS_REQUEST_POINTERS) {
						/* pointers will do, good */
						(*results)[counter] = my_get[counter].get_present_values;
						(*actual_type_name)[counter] = my_get[counter].get_type_name;
					} else {
						/* need to copy the values */
						(*results)[counter] = valueset_dup(my_get[counter].get_present_values);
						if (NULL == (*results)[counter]) {
							rc = ENOMEM;
						} else {
							(*actual_type_name)[counter] = slapi_ch_strdup(my_get[counter].get_type_name);
							if (NULL == (*actual_type_name)[counter]) {
								rc = ENOMEM;
							}
						}
					}
					if (flags & SLAPI_VIRTUALATTRS_REQUEST_POINTERS) {
							*buffer_flags = SLAPI_VIRTUALATTRS_RETURNED_POINTERS;
					} else {
						*buffer_flags = SLAPI_VIRTUALATTRS_RETURNED_COPIES;
					}
				} else {
					rc = SLAPI_VIRTUALATTRS_NOT_FOUND;
				}
			}

			slapi_ch_free((void **)&my_get);
		}
	}
	vattr_context_ungrok(&c);
	return rc;
}

/* Do we need a call to free the results from the above ? */

void slapi_vattr_values_free(Slapi_ValueSet **value, char** actual_type_name,
								int flags)
{
	/* Check whether we need to free the strings */
	if (flags & SLAPI_VIRTUALATTRS_RETURNED_POINTERS) {
		/* We don't need to free the values */
	} else {
		/* Free the valueset */
		if (*value) {
			slapi_valueset_free(*value);
		}
		if (*actual_type_name) {
			slapi_ch_free((void **)actual_type_name);
		}
	}
	*actual_type_name = NULL;
	*value = NULL;
}

/* Function like the one above but doing a compare operation */
/* Same return codes as above. Compare result value returned in "result": 0 if compare true, 1 if compare false */

int slapi_vattr_value_compare(Slapi_Entry *e, char *type, Slapi_Value *test_this, int *result, int flags)
{
	return slapi_vattr_namespace_value_compare_sp(NULL,e,NULL,type,test_this,result,flags);
}

/* namespace version of above */
int slapi_vattr_namespace_value_compare(Slapi_Entry *e, Slapi_DN *namespace_dn, const char *type, Slapi_Value *test_this, int *result, int flags)
{
	return slapi_vattr_namespace_value_compare_sp(NULL,e,namespace_dn,type,test_this,result,flags);
}

int slapi_vattr_value_compare_sp(vattr_context *c,/* Entry we're interested in */ Slapi_Entry *e,/* attr type name */ char *type, Slapi_Value *test_this,/* pointer to result */ int *result, int flags)
{
	return slapi_vattr_namespace_value_compare_sp(c,e,NULL,type,test_this,result,flags);
}

int slapi_vattr_namespace_value_compare_sp(vattr_context *c,/* Entry we're interested in */ Slapi_Entry *e, /* backend namespace dn*/Slapi_DN *namespace_dn, /* attr type name */ const char *type, Slapi_Value *test_this,/* pointer to result */ int *result, int flags)
{
	int rc = 0;

	int sp_bit = 0; /* Set if an SP supplied an answer */
	vattr_sp_handle_list *list = NULL;

	vattr_get_thang *my_get = 0;

	*result = 0;	/* return "compare false" by default */

	rc = vattr_context_grok(&c);
	if (0 != rc) {
		/* Print a handy error log message */
		LDAPDebug(LDAP_DEBUG_ANY,"Detected virtual attribute loop in compare on entry %s, attribute %s\n", slapi_entry_get_dn_const(e), type, 0);
		return rc;
	}

	/* Having done that, we now consult the attribute map to find service providers who are interested */
	/* Look for attribute in the map */
	list = vattr_map_namespace_sp_getlist(namespace_dn, type);
	if (list) {
		vattr_sp_handle *current_handle = NULL;
		void *hint = NULL;
		for (current_handle = vattr_map_sp_first(list,&hint); current_handle; current_handle = vattr_map_sp_next(list,&hint)) {
			/* call this SP */
			rc = vattr_call_sp_compare_value(current_handle,c,e,my_get,type,test_this,result,flags, hint);
			if (0 == rc) {
				sp_bit = 1;
				break;
			}
		}
	}
	/* If no SP supplied the answer, take it from the entry */
	if (!sp_bit) {
		/* Grok the entry, and remember what we saw. This call does no more than walk down the entry attribute list, do some string compares and copy pointers. */
		int attr_count = vattr_helper_get_entry_conts_ex(e,type, &my_get,
			(0 != (flags & SLAPI_VIRTUALATTRS_SUPPRESS_SUBTYPES)));

		if (my_get && my_get->get_present) {
			int i;
			Slapi_Value *Dummy_value = NULL;
	
			/* Put the required stuff in the fake attr */

			for(i=0;i<attr_count;i++)
			{
				Dummy_value = slapi_valueset_find( my_get[i].get_attr, my_get[i].get_present_values, test_this );

				if (Dummy_value) {
					*result = 1;	/* return "compare true" */

					break;
				}
			}
		} else {
			rc = SLAPI_VIRTUALATTRS_NOT_FOUND;
		}
	}
	vattr_context_ungrok(&c);
	slapi_ch_free((void **)&my_get);
	return rc;
}

/* Function to obtain the list of attribute types which are available on this entry */

/*
	Need to call service providers here:
	We know only the entry's DN and so we could restrict our choice of SPs based on that.
	However, we don't yet implement such fancyness.
	For now, we just crawl through all the SPs, asking them in turn to contribute.
	This is pretty inefficient because we will trawl the list  for each wildcard attribute type search operation.
	Service providers should therefore handle the call as fast as they can.
 */

struct _vattr_type_list_context {
	 unsigned int vattr_context_loop_count;
	 vattr_type_thang *types;
	 int realattrs_only;	/* TRUE implies list contains only real attrs */
	 int list_is_copies;
	 int flags;
	 size_t list_length;
	 size_t block_length;
};

/* Helper function which converts a type list from pointers to copies */
static int vattr_convert_type_list(vattr_type_list_context *thang)
{
	vattr_type_thang *old_list = NULL;
	vattr_type_thang *new_list = NULL;
	size_t index = 0;

	old_list = thang->types;
	/* Make a new list */
	new_list = (vattr_type_thang*)slapi_ch_calloc(thang->block_length, sizeof(vattr_type_thang));
	if (NULL == new_list) {
		return ENOMEM;
	}
	/* Walk the list strdup'ing the type name and copying the rest of the structure contents */
	for (index = 0; index < thang->list_length; index++) {
		new_list[index].type_flags = old_list[index].type_flags;
		new_list[index].type_name = slapi_ch_strdup(old_list[index].type_name);
		/*
		 * list_is_copies does not affect type_values as it
		 * is always a pointer never a copy.
		 */
		new_list[index].type_values = old_list[index].type_values;
	}
	/* Mark it a copy list */
	thang->list_is_copies = 1;
	/* Free the original list */
	slapi_ch_free((void **)&(thang->types));
	/* swap lists */
	thang->types = new_list;

	return 0;
}

/*
 * Returns all the attribute types from e, both real and virtual.
 *
 * Any of the attributes found in the entry to start with, for
 * whom no vattr SP volunteered, will also have a pointer to the
 * original valueset in the entry--this fact can be used by
 * calling slapi_vattr_values_type_thang_get(), which will
 * take the values present in the vattr_type_thang list
 * rather than calling slapi_vattr_values_get() to retrieve the value.
*/ 

#define TYPE_LIST_EXTRA_SPACE 5 /* Number of extra slots we allow for SP's to insert types */

int slapi_vattr_list_attrs(/* Entry we're interested in */ Slapi_Entry *e,
					/* pointer to receive the list */ vattr_type_thang **types,
					int flags, int *buffer_flags)
{
	int rc = 0;
	size_t attr_count = 0;
	vattr_type_thang *result_array = NULL	;
	Slapi_Attr *current_attr = NULL;
	size_t i = 0;
	int list_is_copies = 0;
	vattr_sp_handle_list *list = NULL;
	size_t list_length = 0;
	size_t block_length = 0;
	vattr_type_list_context type_context = {0};

	block_length  = 1 + TYPE_LIST_EXTRA_SPACE;

	if(!(flags & SLAPI_VIRTUALATTRS_ONLY))
	{
		/* First find what's in the entry itself*/
		/* Count the attributes */
		for (current_attr = e->e_attrs; current_attr != NULL; current_attr = current_attr->a_next, attr_count++) ;
		block_length  += attr_count;
		/* Allocate the pointer array */
		result_array = (vattr_type_thang*)slapi_ch_calloc(block_length,sizeof(vattr_type_thang));
		if (NULL == result_array) {
			return ENOMEM;
		}
		list_length = attr_count;

		/* Now copy the pointers into the array */
		i  = 0;
		for (current_attr = e->e_attrs; current_attr != NULL; current_attr = current_attr->a_next) {
			char *mypointer = NULL;
			if (flags & SLAPI_VIRTUALATTRS_REQUEST_POINTERS) {
				mypointer = current_attr->a_type;
				result_array[i].type_values = &(current_attr->a_present_values);
			} else {
				mypointer = slapi_ch_strdup(current_attr->a_type);
				list_is_copies = 1;
			}
			result_array[i].type_flags = current_attr->a_flags;
			result_array[i++].type_name = mypointer;
		}
		PR_ASSERT(i == attr_count);
	}

	/* if we didnt send real attrs we need to allocate result array */
	if(NULL == result_array)
	{
		result_array = (vattr_type_thang*)slapi_ch_calloc(block_length,sizeof(vattr_type_thang));
		if (NULL == result_array) {
			return ENOMEM;
		}
	}

	/* Then ask the service providers for their input */
	type_context.flags = flags;
	type_context.realattrs_only = 1; /* until we know otherwise */
	type_context.list_is_copies = list_is_copies;
	type_context.types = result_array;
	type_context.list_length = list_length;
	type_context.block_length = block_length;

	/*
	 * At this point type_context.types is a list of all
	 * the attributetypes (copies or pointers) and values (copies, if present)
	 * found in the entry.
	*/

	if(!(flags & SLAPI_REALATTRS_ONLY))
	{
		list = vattr_map_sp_get_complete_list();
		if (list) {
			vattr_sp_handle *current_handle = NULL;
			
			for (current_handle = vattr_list_sp_first(list); current_handle; current_handle = vattr_list_sp_next((vattr_sp_handle_list *)current_handle)) {
				/* call this SP */
				rc = vattr_call_sp_get_types(current_handle,e,&type_context,
											flags);
				if (0 != rc) {
					/* DBDB do what on error condition ? */
				}
			}
			/* assert enough space for the null terminator */
			PR_ASSERT( type_context.list_length < type_context.block_length);
			i = type_context.list_length;
		}
	}

	/*
	 * Now type_context.types is a list of all the types in this entry--
	 * real and virtual.  For real ones, the values field is filled in.
	 * For virtual ones (including virtual ones which will overwrite a real
	 * value) the values field is null--this fact may used
	 * subsequently by callers of slapi_vattr_list_attrs()
	 * by calling slapi_vattr_values_type_thang_get() which
	 * will only calculate the values for attributetypes with
	 * non-null values.
	*/
		
	flags = type_context.flags;
	list_is_copies = type_context.list_is_copies;
	result_array = type_context.types;
	list_length = type_context.list_length;
	block_length = type_context.block_length;

	if (list_is_copies) {
		*buffer_flags = SLAPI_VIRTUALATTRS_RETURNED_COPIES;
	} else {
		*buffer_flags = SLAPI_VIRTUALATTRS_RETURNED_POINTERS;
	}

	/* Let the caller know if the list contains only real attrs */
	if (type_context.realattrs_only) {
		*buffer_flags |= SLAPI_VIRTUALATTRS_REALATTRS_ONLY;
	}

	result_array[i].type_name = NULL;

	if (types && list_length) {
		*types = result_array;
	}
	else
		*types = 0;

	return rc;
}

static int vattr_add_thang_to_list(vattr_type_list_context *c, vattr_type_thang *thang)
{
	vattr_type_thang *new_list = NULL;

	/* Will it fit in the current block ? */
	if (c->list_length == c->block_length - 1) {
		c->block_length *= 2;

		new_list = (vattr_type_thang*)slapi_ch_realloc((char *)c->types,
													   c->block_length * (int)sizeof(vattr_type_thang));
		if (NULL == new_list) {
			return ENOMEM;
		}

		c->types = new_list;
	} 
	/* Add to the end of the list */
	c->types[c->list_length ] = *thang;
	c->list_length += 1;
	return 0;
}

/* Called by SP's during their get_types call, to have the server add a
 * type to the type list.
 *
 * c: the vattr_type_list_context passed from the original caller of
 *		slapi_vattr_list_attrs()
 * thang: the new type to be added to the type list.
 * flags: tells the routine whether to copy the thang or not.
 *			SLAPI_VIRTUALATTRS_REQUEST_POINTERS--no need to copy it.
 *			!(SLAPI_VIRTUALATTRS_REQUEST_POINTERS)--need to copy it.
 *
 * Checks to see if the requested type is already in the list,
 * and if it is, if the value of the attribute is
 * non-null, it resets the value to null.  This means that when used
 * subsequently, the type list indicates whether, for a given attribute type,
 * the SP's need to be called to retrieve the value. 
 *
*/
int slapi_vattrspi_add_type(vattr_type_list_context *c,
								vattr_type_thang *thang, int flags)
{
	int rc = 0;
	unsigned int index = 0;
	vattr_type_thang thang_to_add = {0};

	int found_it = 0;

	PR_ASSERT(c);

	/* We are no longer sure that the list contains only real attrs */
	c->realattrs_only = 0;

	/* Check to see if the type is in the list already */
	for (index = 0; index < c->list_length; index++) {
		if (0 == slapi_UTF8CASECMP(c->types[index].type_name,thang->type_name)) {
			found_it = 1;
			break;
		}
	}
	/*
	 * If found and there are values specified in the vattr_type_thang, then
	 * that means it's a real attribute--because SP's do not add values
	 * when called via slapi_vattr_list_attrs()/vattr_call_sp_get_types().
	 * However, an SP is willing to claim responsibility for this attribute
	 * type, so to ensure that that virtual value will get calculated, set the
	 * original real value to NULL here.
	 * The guiding rule here is "if an SP is prepared
	 * to provide a value for an attribute type, then that is the one
	 * that must be returned to the user".  Note, this does not prevent
	 * the SP implementing a "merge real with virtual policy", if they
	 * wish.
	*/
	
	if (found_it) {
	
		if ( c->types[index].type_values != NULL ) {			
				/*
				 * Any values in this list are always pointers.
				 * See slapi_vattr_list_types() and vattr_convert_type_list()
				*/
				c->types[index].type_values = NULL;			
		}
	
		return 0;
	}
	/*
	 * If it's not in the list then we need to add it.
	 * If the list is already copies, then we need to make a copy of what the
	 * SP passed us.
	 * If the SP indicated that the type name it passed us is volatile,
	 * !(flags & SLAPI_VIRTUALATTRS_REQUEST_POINTERS)
	 * then we need to copy it anyway.
	*/

	/* rbyrneXXX optimisation for COS: if each type_thang could have a
	 * free_flag
	 * then we would not have to convert all the real attrs to copies
	 * just because an SP returned a copy (note: only COS rrequires copies;
	 * roles returns a pointer to a static string "nsRole").
	*/
	if (!c->list_is_copies &&
						(0 == (flags & SLAPI_VIRTUALATTRS_REQUEST_POINTERS))) {
		/* Means we need to turn the existing list into a list of copies */
		vattr_convert_type_list(c);
	}
	if (c->list_is_copies) {
		thang_to_add.type_flags = thang->type_flags;
		thang_to_add.type_name = slapi_ch_strdup(thang->type_name);
	} else {
		thang_to_add = *thang;
	}
	/* Lastly, we add to the list, using a function which hides the complexity of extending the list if it fills up */
	vattr_add_thang_to_list(c, &thang_to_add);

	return rc;
}

/* Function to free the list returned in the function above */ 
/* Written, reviewed, debug stepped */

void slapi_vattr_attrs_free(vattr_type_thang **types, int flags)
{
	if (NULL == *types) {
		return;
	}
	/* Check whether we need to free the strings */
	if (flags & SLAPI_VIRTUALATTRS_RETURNED_POINTERS) {
		/* We don't need to free the values */
	} else {
		char *attr_name_to_free = NULL;
		size_t i = 0;
		/* Walk down the set of values, fr eeing each one in turn */
		for (; (attr_name_to_free = (*types)[i].type_name) != NULL; i++) {
			slapi_ch_free((void **)&attr_name_to_free);
		}
	}
	/* We always need to free the pointer block */
	slapi_ch_free((void **)types);
}

char *vattr_typethang_get_name(vattr_type_thang *t)
{
	return t->type_name;
}

unsigned long vattr_typethang_get_flags(vattr_type_thang *t)
{
	return t->type_flags;
}

vattr_type_thang *vattr_typethang_next(vattr_type_thang *t)
{
	t++;
	if (t->type_name) {
		return t;
	} else {
		return NULL;
	}
}

vattr_type_thang *vattr_typethang_first(vattr_type_thang *t)
{
	return t;
}

vattr_get_thang *slapi_vattr_getthang_first(vattr_get_thang *t)
{
	return t;
}

vattr_get_thang *slapi_vattr_getthang_next(vattr_get_thang *t)
{
	t++;
	if (t->get_present) {
		return t;
	} else {
		return NULL;
	}
}




/* End of the public interface functions */

/* Everything below here is the SPI interface, only callable by vattr service providers.
   Currently this interface is not public. Interface definitions are in vattr_spi.h
 */

/* Structures used in the SPI interface */

/* Service provider object */


struct _vattr_sp {
	/* vtbl */
	vattr_get_fn_type sp_get_fn;
	vattr_get_ex_fn_type sp_get_ex_fn;
	vattr_compare_fn_type sp_compare_fn;
	vattr_types_fn_type sp_types_fn;
	void *sp_data; /* Pointer for use by the Service Provider */
};
typedef struct _vattr_sp vattr_sp;

/* Service provider handle */
struct _vattr_sp_handle {
	vattr_sp *sp;
	struct _vattr_sp_handle *next; /* So we can link them together in the map */
	void *hint; /* Hint to the SP */
};

/* Calls made by Service Providers */
/* Call to register as a service provider */
/* This function needs to do the following: 
	o Let the provider say "hey, I'm here". It should probably get some handle back which it can use in future calls.
	o Say whether it wants to be called to resolve every single query, or whether it will say in advance what attrs it services.
 */

static vattr_sp_handle *vattr_sp_list = NULL;

int slapi_vattrspi_register(vattr_sp_handle **h, vattr_get_fn_type get_fn, vattr_compare_fn_type compare_fn, vattr_types_fn_type types_fn)
{
	return slapi_vattrspi_register_internal(h, get_fn, 0, compare_fn, types_fn, 0);
}

int slapi_vattrspi_register_ex(vattr_sp_handle **h, vattr_get_ex_fn_type get_fn, vattr_compare_fn_type compare_fn, vattr_types_fn_type types_fn, void *options)
{
	return slapi_vattrspi_register_internal(h, 0, get_fn, compare_fn, types_fn, options);
}

/* options not used yet - for future expansion */
int slapi_vattrspi_register_internal(vattr_sp_handle **h, vattr_get_fn_type get_fn, vattr_get_ex_fn_type get_ex_fn, vattr_compare_fn_type compare_fn, vattr_types_fn_type types_fn, void *options)
{
	vattr_sp_handle *return_to_caller = NULL;
	vattr_sp_handle *list_handle = NULL;
	vattr_sp *new_sp = NULL;
	/* Make a service provider handle */
	new_sp = (vattr_sp*)slapi_ch_calloc(1,sizeof(vattr_sp));
	if (NULL == new_sp) {
		slapd_nasty(sourcefile,7,0);
		return ENOMEM;
	}
	return_to_caller = (vattr_sp_handle*)slapi_ch_calloc(1,sizeof(vattr_sp_handle));
	if (NULL == return_to_caller) {
		slapd_nasty(sourcefile,8,0);
		return ENOMEM;
	}
	new_sp->sp_get_fn = get_fn;
	new_sp->sp_get_ex_fn = get_ex_fn;
	new_sp->sp_compare_fn = compare_fn;
	new_sp->sp_types_fn = types_fn;
	return_to_caller->sp = new_sp;
	/* Add to the service provider list */
	/* Make a handle for the list */
	list_handle = (vattr_sp_handle*)slapi_ch_calloc(1, sizeof (vattr_sp_handle));
	if (NULL == list_handle) {
		return ENOMEM;
	}
	*list_handle = *return_to_caller;
	list_handle->next = vattr_sp_list;
	vattr_sp_list = list_handle;
	/* Return the handle to the caller */
	*h = return_to_caller;
	return 0;
}

vattr_sp_handle_list *vattr_map_sp_get_complete_list()
{
	return vattr_sp_list;
}

int slapi_vattrspi_regattr(vattr_sp_handle *h,char *type_name_to_register, char* DN , void *hint)
{
	int ret = 0;
	char *type_to_add;
	int free_type_to_add = 0;

	/* Supplying a DN means that the plugin requires to be called
	 * only when the considering attributes in relevant entries - almost
	 *
	 * Actually the smallest namespace is the backend.
	 *
	 * atttributes that are tied to backends have the format DN::attr
	 * this essentially makes the attribute have split namespaces
	 * that are treated as separate attributes in the vattr code
	 */
	if(DN)
	{
		/* as a coutesy we accept any old DN and will convert
		 * to a namespace DN, this helps to hide details
		 * (that we might decide to change) anyway
		 */
		Slapi_DN original_dn;
		Slapi_Backend *be;
		Slapi_DN *namespace_dn;
		
		slapi_sdn_init(&original_dn);
		slapi_sdn_set_dn_byref(&original_dn,DN);
		be = slapi_be_select( &original_dn );
		namespace_dn = (Slapi_DN*)slapi_be_getsuffix(be, 0);

		if(namespace_dn && be != defbackend_get_backend()) /* just in case someone thinks "" is a good namespace */
		{
			type_to_add = (char*)PR_smprintf("%s::%s",
				(char*)slapi_sdn_get_dn(namespace_dn), 
				type_name_to_register);

			if(!type_to_add)
			{
				ret = -1;
			}

			free_type_to_add = 1;
		}
		else
		{
			type_to_add = type_name_to_register;
		}

		slapi_sdn_done(&original_dn);
	}
	else
	{
		type_to_add = type_name_to_register;
	}

	ret = vattr_map_sp_insert(type_to_add,h,hint);

	if(free_type_to_add)
	{
		PR_smprintf_free(type_to_add);
	}

	return ret;
}


/* Call to advertise or refute that you generate the value for some attribute, and to signal that the definition for this attribute has changed */
/* This call needs to do the following:
	o Let the SP say "you know, I will generate the value of attribute "foo", within subtree "bar". (we might ignore the subtree for now to keep things simple
	o Same as above, but say "you know, I'm not doing that any more". (we'll do that with a addordel flag).
	o Say, "you know that definition I said I did, well it's changed".
*/

/* Functions to handle the context stucture */

int slapi_vattr_context_create(vattr_context **c)
{
	return 0;
}

void slapi_vattr_context_destroy(vattr_context *c)
{
}

int vattr_call_sp_get_value(vattr_sp_handle *handle, vattr_context *c, Slapi_Entry *e, vattr_get_thang *my_get, char *type, Slapi_ValueSet** results,int *type_name_disposition, char** actual_type_name, int flags, int *buffer_flags, void* hint)
{
	int ret = -1;

	if(handle->sp->sp_get_fn)
	{
		ret = ((handle->sp->sp_get_fn)(handle,c,e,type,results,type_name_disposition,actual_type_name,flags,buffer_flags, hint)); 
	}

	return ret;
}

int vattr_call_sp_get_batch_values(vattr_sp_handle *handle, vattr_context *c, Slapi_Entry *e, vattr_get_thang *my_get, char **type, Slapi_ValueSet*** results,int **type_name_disposition, char*** actual_type_name, int flags, int *buffer_flags, void** hint)
{
	int ret = 0;

	if(handle->sp->sp_get_ex_fn)
	{
		ret = ((handle->sp->sp_get_ex_fn)(handle,c,e,type,results,type_name_disposition,actual_type_name,flags,buffer_flags, hint)); 
	}
	else
	{
		/* make our args look like the simple non-batched case */
		*results = (Slapi_ValueSet**)slapi_ch_calloc(2, sizeof(*results)); /* 2 for null terminated list */
		*type_name_disposition = (int *)slapi_ch_calloc(2, sizeof(*type_name_disposition));
		*actual_type_name = (char**)slapi_ch_calloc(2, sizeof(*actual_type_name));

		ret =((handle->sp->sp_get_fn)(handle,c,e,*type,*results,*type_name_disposition,*actual_type_name,flags,buffer_flags, hint)); 
		if(ret)
		{
			slapi_ch_free((void**)results );
			slapi_ch_free((void**)type_name_disposition );
			slapi_ch_free((void**)actual_type_name );
		}
	}

	return ret;
}

int vattr_call_sp_compare_value(vattr_sp_handle *handle, vattr_context *c, Slapi_Entry *e, vattr_get_thang *my_get, const char *type, Slapi_Value* test_this,int *result, int flags, void* hint)
{
	return ((handle->sp->sp_compare_fn)(handle,c,e,(char*)type,test_this,result,flags, hint)); 
}

int vattr_call_sp_get_types(vattr_sp_handle *handle,Slapi_Entry *e,vattr_type_list_context *type_context,int flags)
{
		return ((handle->sp->sp_types_fn)(handle,e,type_context,flags)); 
}

/* Service provider entry point prototypes */

/* Call which allows a SP to say what attributes can be used to define a given attribute, used for loop detection */
/* Not implementing this yet */
/* typedef int (*vattrspi_explain_definition) (); */

/* Function called to indicate to the SP that the game is over, we free the handle, not the SP */
typedef int (*vattrspi_deregister) (vattr_sp_handle *h);

/* End of the SPI functions */

/* Implementation functions only after here */

/* The attribute map */

/* We need a structure which can map attribute type names to SP's which might tell us
something useful. We need a function which, given an attr type name, can return
a set of SP's to ask about said attr. SP's which don't tell us which attrs they are
handling always get asked. The first SP which tells us the answer wins, we don't
attempt to arbitrate between them. One might imaging doing this stuff in a unified
way with other stuff pertaining to types (schema, syntax). Someday. For now this
is a separate thing in the insterests of stability.
*/

/* Virtual Attribute map implementation */

/* The virtual attribute type map is implemented as a hash table.
   The single key is the attribute type base name (not an alias)
   (what about subtypes??). The hash table takes care of translating
   this key to a list of probable service providers for the type.

   Other than the obvious lookup function, we need functions to
   allow entries to be added and removed from the map.

   Because the map is likely to be consulted at least once for every search
   operation performed by the server, it's important that it is free from
   performance deficiencies such as lock contention (is the NSPR hash table
   implementation even thread safe ?

 */

#define VARRT_MAP_HASHTABLE_SIZE 10

/* Attribute map oject */
/* Needs to contain: a linked list of pointers to provider handles handles,
		The type name,
		other stuff ?

   Access to the entire map will be controlled via a single RW lock.
 */

struct _objAttrValue
{
	struct _objAttrValue *pNext;
	Slapi_Value *val;
};
typedef struct _objAttrValue objAttrValue;

struct _vattr_map_entry {
	char * /* currect ? */ type_name;
	vattr_sp_handle *sp_list;
	objAttrValue *objectclasses; /* objectclasses for this type to check schema with*/
};
typedef struct _vattr_map_entry vattr_map_entry;


vattr_map_entry test_entry = {NULL};

struct _vattr_map {
	PRRWLock  *lock;
	PLHashTable *hashtable; /* Hash table */  
};
typedef struct _vattr_map vattr_map;

static vattr_map *the_map = NULL;

static PRIntn vattr_hash_compare_keys(const void *v1, const void *v2)
{
	/* Should this be subtype aware, etc ? */
    return ( (0 == strcasecmp(v1,v2)) ? 1 : 0) ;
}

static PRIntn vattr_hash_compare_values(const void *v1, const void *v2)
{
    return( ((char *)v1 == (char *)v2 ) ? 1 : 0);
}

static PLHashNumber vattr_hash_fn(const void *type_name)
{
	/* need a hash function here */
	/* Sum the bytes for now */
	PLHashNumber result = 0;
	char * current_position = NULL;
	char current_char = 0;
	for (current_position = (char*) type_name; *current_position; current_position++) {
		current_char = tolower(*current_position);
		result += current_char;
	}
    return result;
}

static int vattr_map_create()
{
	the_map = (vattr_map*)slapi_ch_calloc(1, sizeof(vattr_map));
	if (NULL == the_map) {
		slapd_nasty(sourcefile,1,0);
		return ENOMEM;
	}

	the_map->hashtable = PL_NewHashTable(VARRT_MAP_HASHTABLE_SIZE,
	vattr_hash_fn, vattr_hash_compare_keys,
	vattr_hash_compare_values, NULL, NULL);

	if (NULL == the_map->hashtable) {
		slapd_nasty(sourcefile,2,0);
		return ENOMEM;
	}

	the_map->lock = PR_NewRWLock(0,SOURCEFILE "1");
	if (NULL == the_map) {
		slapd_nasty(sourcefile,3,0);
		return ENOMEM;
	}
	return 0;
}

static void vattr_map_destroy()
{
	if (the_map) {
		if (the_map->hashtable) {
			PL_HashTableDestroy(the_map->hashtable);
		}
		if (the_map->lock) {
			PR_DestroyRWLock(the_map->lock);
		}
	}
	slapi_ch_free ((void**)&the_map);
}

/* Returns 0 if present, entry returned in result. Returns SLAPI_VIRTUALATTRS_NOT_FOUND if not found */
static int vattr_map_lookup(const char *type_to_find, vattr_map_entry **result)
{
	char *basetype = 0;
	char *tmp = 0;
	char buf[SLAPD_TYPICAL_ATTRIBUTE_NAME_MAX_LENGTH];

	*result = NULL;
	PR_ASSERT(the_map);

	/* vattrs need to support subtypes
	 * we insist that one service provider
	 * will support all subtypes for a
	 * given superior, hence we look for
	 * superiors only here
	 */

	tmp = slapi_attr_basetype( type_to_find, buf, sizeof(buf));
	if(tmp)
	{
		basetype = tmp;
	}
	else
	{
		basetype = buf;
	}

	/* Get the reader lock */
	PR_RWLock_Rlock(the_map->lock);
    *result = (vattr_map_entry*)PL_HashTableLookupConst(the_map->hashtable,
					      (void*)basetype);
	/* Release ze lock */
	PR_RWLock_Unlock(the_map->lock);

	if(tmp)
	{
		slapi_ch_free_string(&tmp);
	}

	if (*result) {
		return 0;
	} else {
		return SLAPI_VIRTUALATTRS_NOT_FOUND;
	}
}

/* Insert an entry into the attribute map */
int vattr_map_insert(vattr_map_entry *vae)
{
	char *copy_of_type_name = NULL;
	PR_ASSERT(the_map);
	copy_of_type_name = slapi_ch_strdup(vae->type_name);
	if (NULL == copy_of_type_name) {
		slapd_nasty(sourcefile,6,0);
		return ENOMEM;
	}
	/* Get the writer lock */
	PR_RWLock_Wlock(the_map->lock);
	/* Insert the thing */
	/* It's illegal to call this function if the entry is already there */
	PR_ASSERT(NULL == PL_HashTableLookupConst(the_map->hashtable,(void*)copy_of_type_name));
    PL_HashTableAdd(the_map->hashtable,(void*)copy_of_type_name,(void*)vae);
	/* Unlock and we're done */
	PR_RWLock_Unlock(the_map->lock);
	return 0;
}

/* 
	vattr_delete_attrvals
	---------------------
	deletes a value list
*/
void vattr_delete_attrvals(objAttrValue **attrval)
{
	objAttrValue *val = *attrval;

	while(val)
	{
		objAttrValue *next = val->pNext;
		slapi_value_free(&val->val);
		slapi_ch_free((void**)&val);
		val = next;
	}
}

/* 
	vattr_add_attrval
	-----------------
	adds a value to an attribute value list
*/
int vattr_add_attrval(objAttrValue **attrval, char *val)
{
	int ret = 0;
	objAttrValue *theVal;

	LDAPDebug( LDAP_DEBUG_TRACE, "--> vattr_add_attrval\n",0,0,0);

	/* create the attrvalue */
	theVal = (objAttrValue*) slapi_ch_malloc(sizeof(objAttrValue));
	if(theVal)
	{
		theVal->val = slapi_value_new_string(val);
		if(theVal->val)
		{
			theVal->pNext = *attrval;
			*attrval = theVal;
		}
		else
		{
			slapi_ch_free((void**)&theVal);
			LDAPDebug( LDAP_DEBUG_ANY, "vattr_add_attrval: failed to allocate memory\n",0,0,0);
			ret = -1;
		}
	}
	else
	{
		LDAPDebug( LDAP_DEBUG_ANY, "vattr_add_attrval: failed to allocate memory\n",0,0,0);
		ret = -1;
	}

	LDAPDebug( LDAP_DEBUG_TRACE, "<-- vattr_add_attrval\n",0,0,0);
	return ret;
}


objAttrValue *vattr_map_entry_build_schema(char *type_name)
{
	objAttrValue *ret = 0;
	struct objclass	*oc;

	LDAPDebug( LDAP_DEBUG_TRACE, "--> vattr_map_entry_build_schema\n",0,0,0);

	/* this is the first opportunity to register
	 * with the statechange api, our init function
	 * gets called prior to loading plugins, so it
	 * was not available then
	 */
	if(!statechange_api)
	{
		/* grab statechange api - we never release this */
		if(!slapi_apib_get_interface(StateChange_v1_0_GUID, &statechange_api))
		{
			/* register for schema changes via dn */
			statechange_register(statechange_api, "vattr", "cn=schema", NULL, NULL, (notify_callback) schema_changed_callback);
		}
	}

	if(!config_get_schemacheck())
	{
		LDAPDebug( LDAP_DEBUG_TRACE, "<-- vattr_map_entry_build_schema - schema off\n",0,0,0);
		return 0;
	}

    oc_lock_read();
	for ( oc = g_get_global_oc_nolock(); oc != NULL; oc = oc->oc_next )
	{
		char **pppAttrs[2];
		int index;
		int attrType = 0;
		int oc_added = 0;

		pppAttrs[0] = oc->oc_required;
		pppAttrs[1] = oc->oc_allowed;

		/* we need to check both required and allowed attributes */
		while(!oc_added && attrType < 2)
		{
			if(pppAttrs[attrType])
			{
				index = 0;

				while(pppAttrs[attrType][index])
				{
					if(!PL_strcasecmp(pppAttrs[attrType][index], type_name))
					{
						vattr_add_attrval(&ret, oc->oc_name);
						oc_added = 1;
						break;
					}
					index++;
				}
			}
			attrType++;
		}
	}
    oc_unlock();

	LDAPDebug( LDAP_DEBUG_TRACE, "<-- vattr_map_entry_build_schema\n",0,0,0);
	return ret;
}

static PRIntn vattr_map_entry_rebuild_schema(PLHashEntry *he, PRIntn i, void *arg)
{
	vattr_map_entry *entry = (vattr_map_entry *)(he->value);

	if(entry->objectclasses)
		vattr_delete_attrvals(&(entry->objectclasses));
	
	entry->objectclasses = vattr_map_entry_build_schema(entry->type_name);

	return HT_ENUMERATE_NEXT;
}

void schema_changed_callback(Slapi_Entry *e, char *dn, int modtype, Slapi_PBlock *pb, void *caller_data)
{
	/* Get the writer lock */
	PR_RWLock_Wlock(the_map->lock);

	/* go through the list */
	PL_HashTableEnumerateEntries(the_map->hashtable, vattr_map_entry_rebuild_schema, 0);

	/* Unlock and we're done */
	PR_RWLock_Unlock(the_map->lock);
}


int slapi_vattr_schema_check_type(Slapi_Entry *e, char *type)
{
	int ret = 0;
	vattr_map_entry *map_entry;

	if(config_get_schemacheck())
	{		
		Slapi_Attr *attr;

		if(0 == slapi_entry_attr_find(e, "objectclass", &attr))
		{
			Slapi_ValueSet *vs;

			if(0 == slapi_attr_get_valueset(attr, &vs))
			{
				objAttrValue *obj;

				if(0 == vattr_map_lookup(type, &map_entry))
				{
					PR_RWLock_Rlock(the_map->lock);

					obj = map_entry->objectclasses;

					while(obj)
					{
						if(slapi_valueset_find(attr, vs, obj->val))
						{
							/* this entry has an objectclass
							 * that allows or requires the
							 * attribute type
							 */
							ret = -1;