views.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* plugin which implements directory server views */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "portable.h"
  16. #include "slapi-plugin.h"
  17. #include "statechange.h"
  18. #include "views.h"
  19. #include <sys/stat.h>
  20. #include "slapi-plugin-compat4.h"
  21. #include "slapi-private.h"
  22. #define VIEW_OBJECTCLASS "nsView"
  23. #define VIEW_FILTER_ATTR "nsViewFilter"
  24. #define STATECHANGE_VIEWS_ID "Views"
  25. #define STATECHANGE_VIEWS_CONFG_FILTER "objectclass=" VIEW_OBJECTCLASS
  26. #define VIEWS_PLUGIN_SUBSYSTEM "views-plugin" /* used for logging */
  27. /* cache data structs */
  28. struct _viewLinkedList
  29. {
  30. void *pNext;
  31. void *pPrev;
  32. };
  33. typedef struct _viewLinkedList viewLinkedList;
  34. #if defined(DEBUG)
  35. #define _VIEW_DEBUG_FILTERS /* Turning on hurts performance */
  36. #endif
  37. struct _viewEntry
  38. {
  39. viewLinkedList list;
  40. char *pDn;
  41. char *viewfilter; /* the raw view */
  42. Slapi_Filter *includeAncestorFiltersFilter; /* the filter with all ancestor filters */
  43. Slapi_Filter *excludeAllButDescendentViewsFilter; /* for building the view of views */
  44. Slapi_Filter *excludeChildFiltersFilter; /* NOT all children views, for one level searches */
  45. Slapi_Filter *excludeGrandChildViewsFilter; /* view filter for one level searches */
  46. Slapi_Filter *includeChildViewsFilter; /* view filter for subtree searches */
  47. #ifdef _VIEW_DEBUG_FILTERS
  48. /* monitor the cached filters with these */
  49. char includeAncestorFiltersFilter_str[1024]; /* the filter with all ancestor filters */
  50. char excludeAllButDescendentViewsFilter_str[1024]; /* for building the view of views */
  51. char excludeChildFiltersFilter_str[1024]; /* NOT all children views, for one level searches */
  52. char excludeGrandChildViewsFilter_str[1024]; /* view filter for one level searches */
  53. char includeChildViewsFilter_str[1024]; /* view filter for subtree searches */
  54. #endif
  55. char *pSearch_base; /* the parent of the top most view */
  56. void *pParent;
  57. void **pChildren;
  58. int child_count;
  59. unsigned long entryid; /* unique identifier for this entry */
  60. unsigned long parentid; /* unique identifier for the parent entry */
  61. };
  62. typedef struct _viewEntry viewEntry;
  63. struct _globalViewCache
  64. {
  65. viewEntry *pCacheViews;
  66. viewEntry **ppViewIndex;
  67. int cache_built;
  68. int view_count;
  69. PRThread *currentUpdaterThread;
  70. };
  71. typedef struct _globalViewCache golbalViewCache;
  72. static golbalViewCache theCache;
  73. static PRUint64 g_plugin_started = 0;
  74. /*
  75. * We can not fully use the built in plugin counter in the views plugin,
  76. * so we have to use our own.
  77. */
  78. static Slapi_Counter *op_counter = NULL;
  79. /* other function prototypes */
  80. int views_init( Slapi_PBlock *pb );
  81. static int views_start( Slapi_PBlock *pb );
  82. static int views_close( Slapi_PBlock *pb );
  83. static int views_cache_create();
  84. static void views_update_views_cache( Slapi_Entry *e, char *dn, int modtype, Slapi_PBlock *pb, void *caller_data );
  85. static int views_cache_build_view_list(viewEntry **pViews);
  86. static int views_cache_index();
  87. static int views_dn_views_cb (Slapi_Entry* e, void *callback_data);
  88. static int views_cache_add_dn_views(char *dn, viewEntry **pViews);
  89. static void views_cache_add_ll_entry(void** attrval, void *theVal);
  90. static void views_cache_discover_parent(viewEntry *pView);
  91. static void views_cache_discover_parent_for_children(viewEntry *pView);
  92. static void views_cache_discover_children(viewEntry *pView);
  93. static void views_cache_discover_view_scope(viewEntry *pView);
  94. static void views_cache_create_applied_filter(viewEntry *pView);
  95. static void views_cache_create_exclusion_filter(viewEntry *pView);
  96. static void views_cache_create_inclusion_filter(viewEntry *pView);
  97. Slapi_Filter *views_cache_create_descendent_filter(viewEntry *ancestor, PRBool useID);
  98. static int view_search_rewrite_callback(Slapi_PBlock *pb);
  99. static void views_cache_backend_state_change(void *handle, char *be_name, int old_be_state, int new_be_state);
  100. static void views_cache_act_on_change_thread(void *arg);
  101. static viewEntry *views_cache_find_view(char *view);
  102. static void views_cache_free();
  103. /* our api broker published api */
  104. static void *api[3];
  105. static int _internal_api_views_entry_exists(char *view_dn, Slapi_Entry *e);
  106. static int _internal_api_views_entry_dn_exists(char *view_dn, char *e_dn);
  107. static int _internal_api_views_entry_exists_general(char *view_dn, Slapi_Entry *e, char *e_dn);
  108. static Slapi_PluginDesc pdesc = { "views", VENDOR, DS_PACKAGE_VERSION,
  109. "virtual directory information tree views plugin" };
  110. static void * view_plugin_identity = NULL;
  111. static Slapi_RWLock *g_views_cache_lock;
  112. /*
  113. ** Plugin identity mgmt
  114. */
  115. void view_set_plugin_identity(void * identity)
  116. {
  117. view_plugin_identity=identity;
  118. }
  119. void * view_get_plugin_identity()
  120. {
  121. return view_plugin_identity;
  122. }
  123. /*
  124. views_init
  125. --------
  126. adds our callbacks to the list
  127. */
  128. int views_init( Slapi_PBlock *pb )
  129. {
  130. int ret = SLAPI_PLUGIN_SUCCESS;
  131. void * plugin_identity=NULL;
  132. slapi_log_error( SLAPI_LOG_TRACE, VIEWS_PLUGIN_SUBSYSTEM, "--> views_init\n");
  133. /*
  134. ** Store the plugin identity for later use.
  135. ** Used for internal operations
  136. */
  137. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  138. view_set_plugin_identity(plugin_identity);
  139. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  140. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  141. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  142. (void *) views_start ) != 0 ||
  143. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  144. (void *) views_close ) != 0 ||
  145. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  146. (void *)&pdesc ) != 0 )
  147. {
  148. slapi_log_error( SLAPI_LOG_FATAL, VIEWS_PLUGIN_SUBSYSTEM,
  149. "views_init: failed to register plugin\n" );
  150. ret = SLAPI_PLUGIN_FAILURE;
  151. }
  152. slapi_log_error( SLAPI_LOG_TRACE, VIEWS_PLUGIN_SUBSYSTEM, "<-- views_init\n");
  153. return ret;
  154. }
  155. void views_read_lock()
  156. {
  157. slapi_rwlock_rdlock(g_views_cache_lock);
  158. }
  159. void views_write_lock()
  160. {
  161. slapi_rwlock_wrlock(g_views_cache_lock);
  162. }
  163. void views_unlock()
  164. {
  165. slapi_rwlock_unlock(g_views_cache_lock);
  166. }
  167. /*
  168. views_start
  169. ---------
  170. This function publishes the interface for this plugin
  171. */
  172. static int views_start( Slapi_PBlock *pb )
  173. {
  174. int ret = SLAPI_PLUGIN_SUCCESS;
  175. void **statechange_api;
  176. slapi_log_error( SLAPI_LOG_TRACE, VIEWS_PLUGIN_SUBSYSTEM, "--> views_start\n");
  177. theCache.cache_built = 0;
  178. g_views_cache_lock = slapi_new_rwlock();
  179. g_plugin_started = 1;
  180. /* first register our backend state change func (we'll use func pointer as handle) */
  181. slapi_register_backend_state_change((void *)views_cache_backend_state_change, views_cache_backend_state_change);
  182. /* create the view cache */
  183. views_cache_create();
  184. /* register callbacks for filter and search rewriting */
  185. slapi_compute_add_search_rewriter(view_search_rewrite_callback);
  186. /* register for state changes to view configuration */
  187. if(!slapi_apib_get_interface(StateChange_v1_0_GUID, &statechange_api))
  188. {
  189. statechange_register(statechange_api,
  190. STATECHANGE_VIEWS_ID,
  191. NULL,
  192. STATECHANGE_VIEWS_CONFG_FILTER,
  193. NULL,
  194. views_update_views_cache);
  195. }
  196. /* register our api so that other subsystems can be views aware */
  197. api[0] = NULL; /* reserved for api broker use */
  198. api[1] = (void *)_internal_api_views_entry_exists;
  199. api[2] = (void *)_internal_api_views_entry_dn_exists;
  200. if( slapi_apib_register(Views_v1_0_GUID, api) )
  201. {
  202. slapi_log_error( SLAPI_LOG_FATAL, VIEWS_PLUGIN_SUBSYSTEM, "views: failed to publish views interface\n");
  203. if(statechange_api){
  204. statechange_unregister(statechange_api,
  205. NULL,
  206. STATECHANGE_VIEWS_CONFG_FILTER,
  207. views_update_views_cache);
  208. }
  209. views_cache_free();
  210. slapi_destroy_rwlock(g_views_cache_lock);
  211. g_views_cache_lock = NULL;
  212. g_plugin_started = 0;
  213. ret = SLAPI_PLUGIN_FAILURE;
  214. } else {
  215. op_counter = slapi_counter_new();
  216. }
  217. slapi_log_error( SLAPI_LOG_TRACE, VIEWS_PLUGIN_SUBSYSTEM, "<-- views_start\n");
  218. return ret;
  219. }
  220. /* _internal_api_views_entry_exists()
  221. * ----------------------------------
  222. * externally published api to allow other subsystems to
  223. * be views aware. Given a view and an entry, this function
  224. * returns PR_TRUE if the entry would be returned by a subtree
  225. * search on the view, PR_FALSE otherwise.
  226. */
  227. static int _internal_api_views_entry_exists(char *view_dn, Slapi_Entry *e)
  228. {
  229. return _internal_api_views_entry_exists_general(view_dn, e, NULL);
  230. }
  231. static int _internal_api_views_entry_dn_exists(char *view_dn, char *e_dn)
  232. {
  233. return _internal_api_views_entry_exists_general(view_dn, NULL, e_dn);
  234. }
  235. static int _internal_api_views_entry_exists_general(char *view_dn, Slapi_Entry *e, char *e_dn)
  236. {
  237. int ret = SLAPI_PLUGIN_SUCCESS;
  238. viewEntry *view;
  239. char *dn;
  240. slapi_counter_increment(op_counter);
  241. if(g_plugin_started == 0){
  242. slapi_counter_decrement(op_counter);
  243. return ret;
  244. }
  245. /* there are two levels of scope for a view,
  246. * from the parent of the view without a view filter
  247. * and the parent of the top most view including a
  248. * view filter - either match will do
  249. */
  250. /* Read lock the cache */
  251. views_read_lock();
  252. /* find the view */
  253. view = views_cache_find_view(view_dn);
  254. if(0==view)
  255. {
  256. /* this is not the entry you are looking for */
  257. goto bail;
  258. }
  259. /* normal scope - is the view an ancestor of the entry */
  260. if(e_dn)
  261. dn = e_dn;
  262. else
  263. dn = slapi_entry_get_ndn(e);
  264. if(slapi_dn_issuffix(dn, view_dn))
  265. {
  266. /* this entry is physically contained in the view hiearchy */
  267. ret = SLAPI_PLUGIN_FAILURE;
  268. goto bail;
  269. }
  270. /* view scope - view hiearchy scope plus view filter */
  271. if(slapi_dn_issuffix(dn, view->pSearch_base))
  272. {
  273. if(0==e)
  274. {
  275. Slapi_DN *sdn = slapi_sdn_new_dn_byref(dn);
  276. slapi_search_internal_get_entry( sdn, NULL, &e , view_get_plugin_identity());
  277. slapi_sdn_free(&sdn);
  278. }
  279. /* so far so good, apply filter */
  280. if(0==slapi_filter_test_simple(e,view->includeAncestorFiltersFilter))
  281. {
  282. /* this entry would appear in the view */
  283. ret = SLAPI_PLUGIN_FAILURE;
  284. }
  285. }
  286. bail:
  287. views_unlock();
  288. slapi_counter_decrement(op_counter);
  289. return ret;
  290. }
  291. /*
  292. views_close
  293. ---------
  294. unregisters the interface for this plugin
  295. */
  296. static int views_close( Slapi_PBlock *pb )
  297. {
  298. void **statechange_api;
  299. slapi_log_error( SLAPI_LOG_TRACE, VIEWS_PLUGIN_SUBSYSTEM, "--> views_close\n");
  300. g_plugin_started = 0;
  301. while(slapi_counter_get_value(op_counter) > 0){
  302. PR_Sleep(PR_MillisecondsToInterval(100));
  303. }
  304. slapi_counter_destroy(&op_counter);
  305. /* unregister backend state change notification */
  306. slapi_unregister_backend_state_change((void *)views_cache_backend_state_change);
  307. /* unregister the api-broker callback */
  308. if(!slapi_apib_get_interface(StateChange_v1_0_GUID, &statechange_api))
  309. {
  310. statechange_unregister(statechange_api,
  311. NULL,
  312. STATECHANGE_VIEWS_CONFG_FILTER,
  313. views_update_views_cache);
  314. }
  315. views_cache_free();
  316. slapi_destroy_rwlock(g_views_cache_lock);
  317. g_views_cache_lock = NULL;
  318. slapi_log_error( SLAPI_LOG_TRACE, VIEWS_PLUGIN_SUBSYSTEM, "<-- views_close\n");
  319. return SLAPI_PLUGIN_SUCCESS;
  320. }
  321. static void views_cache_free()
  322. {
  323. viewEntry *head = theCache.pCacheViews;
  324. viewEntry *current;
  325. slapi_log_error( SLAPI_LOG_TRACE, VIEWS_PLUGIN_SUBSYSTEM, "--> views_cache_free\n");
  326. /* free the cache */
  327. current = head;
  328. while(current != NULL)
  329. {
  330. viewEntry *theView = current;
  331. current = current->list.pNext;
  332. /* free the view */
  333. slapi_ch_free((void**)&theView->pDn);
  334. slapi_ch_free((void**)&theView->viewfilter);
  335. slapi_filter_free(theView->includeAncestorFiltersFilter,1);
  336. slapi_filter_free(theView->excludeAllButDescendentViewsFilter,1);
  337. slapi_filter_free(theView->excludeChildFiltersFilter,1);
  338. slapi_filter_free(theView->excludeGrandChildViewsFilter,1);
  339. slapi_filter_free(theView->includeChildViewsFilter,1);
  340. slapi_ch_free((void**)&theView->pSearch_base);
  341. slapi_ch_free((void**)&theView->pChildren);
  342. slapi_ch_free((void**)&theView);
  343. }
  344. theCache.pCacheViews = NULL;
  345. slapi_ch_free((void**)&theCache.ppViewIndex);
  346. theCache.view_count = 0;
  347. slapi_log_error( SLAPI_LOG_TRACE, VIEWS_PLUGIN_SUBSYSTEM, "<-- views_cache_free\n");
  348. }
  349. /*
  350. views_cache_create
  351. ---------------------
  352. Walks the views in the DIT and populates the cache.
  353. */
  354. static int views_cache_create()
  355. {
  356. int ret = SLAPI_PLUGIN_FAILURE;
  357. slapi_counter_increment(op_counter);
  358. if (0 == g_plugin_started) {
  359. slapi_counter_decrement(op_counter);
  360. return SLAPI_PLUGIN_SUCCESS;
  361. }
  362. slapi_log_error( SLAPI_LOG_TRACE, VIEWS_PLUGIN_SUBSYSTEM, "--> views_cache_create\n");
  363. /* lock cache */
  364. views_write_lock();
  365. theCache.currentUpdaterThread = PR_GetCurrentThread(); /* to avoid deadlock */
  366. if(theCache.pCacheViews)
  367. {
  368. /* need to get rid of the existing views */
  369. views_cache_free();
  370. }
  371. /* grab the view entries */
  372. ret = views_cache_build_view_list(&(theCache.pCacheViews));
  373. if(!ret && theCache.pCacheViews)
  374. {
  375. viewEntry *head = theCache.pCacheViews;
  376. viewEntry *current;
  377. /* OK, we have a basic cache, now we need to
  378. * fix up parent and children pointers
  379. */
  380. for(current = head; current != NULL; current = current->list.pNext)
  381. {
  382. views_cache_discover_parent(current);
  383. views_cache_discover_children(current);
  384. }
  385. /* scope of views and cache search filters... */
  386. for(current = head; current != NULL; current = current->list.pNext)
  387. {
  388. views_cache_discover_view_scope(current);
  389. views_cache_create_applied_filter(current);
  390. views_cache_create_exclusion_filter(current);
  391. views_cache_create_inclusion_filter(current);
  392. }
  393. /* create the view index */
  394. ret = views_cache_index();
  395. if(ret != 0)
  396. {
  397. /* currently we cannot go on without the indexes */
  398. slapi_log_error(SLAPI_LOG_FATAL, VIEWS_PLUGIN_SUBSYSTEM, "views_cache_create: failed to index cache\n");
  399. }
  400. else
  401. theCache.cache_built = 1;
  402. }
  403. else
  404. {
  405. /* its ok to not have views to cache */
  406. theCache.cache_built = 0;
  407. ret = SLAPI_PLUGIN_SUCCESS;
  408. }
  409. theCache.currentUpdaterThread = 0;
  410. /* unlock cache */
  411. views_unlock();
  412. slapi_counter_decrement(op_counter);
  413. slapi_log_error( SLAPI_LOG_TRACE, VIEWS_PLUGIN_SUBSYSTEM, "<-- views_cache_create\n");
  414. return ret;
  415. }
  416. /*
  417. * views_cache_view_compare
  418. * -----------------------
  419. * compares the dns of two views - used for sorting the index
  420. */
  421. int views_cache_view_compare(const void *e1, const void *e2)
  422. {
  423. int ret;
  424. Slapi_DN *dn1 = slapi_sdn_new_dn_byval((*(viewEntry**)e1)->pDn);
  425. Slapi_DN *dn2 = slapi_sdn_new_dn_byval((*(viewEntry**)e2)->pDn);
  426. ret = slapi_sdn_compare(dn1, dn2);
  427. slapi_sdn_free(&dn1);
  428. slapi_sdn_free(&dn2);
  429. return ret;
  430. }
  431. /*
  432. * views_cache_dn_compare
  433. * -----------------------
  434. * compares a dn with the dn of a view - used for searching the index
  435. */
  436. int views_cache_dn_compare(const void *e1, const void *e2)
  437. {
  438. int ret;
  439. Slapi_DN *dn1 = slapi_sdn_new_dn_byval((char*)e1);
  440. Slapi_DN *dn2 = slapi_sdn_new_dn_byval(((viewEntry*)e2)->pDn);
  441. ret = slapi_sdn_compare(dn1, dn2);
  442. slapi_sdn_free(&dn1);
  443. slapi_sdn_free(&dn2);
  444. return ret;
  445. }
  446. /*
  447. * views_cache_index
  448. * ----------------
  449. * indexes the cache for fast look up of views
  450. */
  451. static int views_cache_index()
  452. {
  453. int ret = SLAPI_PLUGIN_FAILURE;
  454. int i;
  455. viewEntry *theView = theCache.pCacheViews;
  456. viewEntry *current = 0;
  457. if(theCache.ppViewIndex)
  458. slapi_ch_free((void**)&theCache.ppViewIndex);
  459. theCache.view_count = 0;
  460. /* lets count the views */
  461. for(current = theCache.pCacheViews; current != NULL; current = current->list.pNext)
  462. theCache.view_count++;
  463. theCache.ppViewIndex = (viewEntry**)slapi_ch_calloc(theCache.view_count, sizeof(viewEntry*));
  464. if(theCache.ppViewIndex)
  465. {
  466. /* copy over the views */
  467. for(i=0; i<theCache.view_count; i++)
  468. {
  469. theCache.ppViewIndex[i] = theView;
  470. theView = theView->list.pNext;
  471. }
  472. /* sort the views */
  473. qsort(theCache.ppViewIndex, theCache.view_count, sizeof(viewEntry*), views_cache_view_compare);
  474. ret = SLAPI_PLUGIN_SUCCESS;
  475. }
  476. return ret;
  477. }
  478. /*
  479. views_cache_view_index_bsearch - RECURSIVE
  480. ----------------------------------------
  481. performs a binary search on the cache view index
  482. return SLAPI_PLUGIN_FAILURE if key is not found
  483. */
  484. viewEntry *views_cache_view_index_bsearch( const char *key, int lower, int upper )
  485. {
  486. viewEntry *ret = 0;
  487. int index = 0;
  488. int compare_ret = 0;
  489. if(upper >= lower)
  490. {
  491. if(upper != 0)
  492. index = ((upper-lower)/2) + lower;
  493. else
  494. index = 0;
  495. compare_ret = views_cache_dn_compare(key, theCache.ppViewIndex[index]);
  496. if(!compare_ret)
  497. {
  498. ret = (theCache.ppViewIndex)[index];
  499. }
  500. else
  501. {
  502. /* seek elsewhere */
  503. if(compare_ret < 0)
  504. {
  505. /* take the low road */
  506. ret = views_cache_view_index_bsearch(key, lower, index-1);
  507. }
  508. else
  509. {
  510. /* go high */
  511. ret = views_cache_view_index_bsearch(key, index+1, upper);
  512. }
  513. }
  514. }
  515. return ret;
  516. }
  517. /*
  518. views_cache_find_view
  519. -------------------
  520. searches for a view, and if found returns it, null otherwise
  521. */
  522. static viewEntry *views_cache_find_view(char *view)
  523. {
  524. viewEntry *ret = SLAPI_PLUGIN_SUCCESS; /* assume failure */
  525. if(theCache.view_count != 1)
  526. ret = views_cache_view_index_bsearch(view, 0, theCache.view_count-1);
  527. else
  528. {
  529. /* only one view (that will fool our bsearch) lets check it here */
  530. if(!slapi_utf8casecmp((unsigned char*)view, (unsigned char*)theCache.ppViewIndex[0]->pDn))
  531. {
  532. ret = theCache.ppViewIndex[0];
  533. }
  534. }
  535. return ret;
  536. }
  537. /*
  538. views_cache_discover_parent
  539. ------------------------------
  540. finds the parent of this view and caches it in view
  541. */
  542. static void views_cache_discover_parent(viewEntry *pView)
  543. {
  544. viewEntry *head = theCache.pCacheViews;
  545. viewEntry *current;
  546. int found = 0;
  547. for(current = head; current != NULL && !found; current = current->list.pNext)
  548. {
  549. if(slapi_dn_isparent( current->pDn, pView->pDn ))
  550. {
  551. found = 1;
  552. pView->pParent = current;
  553. }
  554. }
  555. if(!found)
  556. {
  557. /* this is a top view */
  558. pView->pParent = NULL;
  559. }
  560. }
  561. /*
  562. views_cache_discover_parent_for_children
  563. ------------------------------
  564. The current node is being deleted - for each child, need
  565. to find a new parent
  566. */
  567. static void views_cache_discover_parent_for_children(viewEntry *pView)
  568. {
  569. int ii = 0;
  570. for (ii = 0; pView->pChildren && (ii < pView->child_count); ++ii)
  571. {
  572. viewEntry *current = (viewEntry *)pView->pChildren[ii];
  573. views_cache_discover_parent(current);
  574. }
  575. }
  576. /*
  577. views_cache_discover_children
  578. ------------------------------
  579. finds the children of this view and caches them in view
  580. */
  581. static void views_cache_discover_children(viewEntry *pView)
  582. {
  583. viewEntry *head = theCache.pCacheViews;
  584. viewEntry *current;
  585. int child_count = 0;
  586. int add_count = 0;
  587. if(pView->pChildren)
  588. {
  589. slapi_ch_free((void**)&pView->pChildren);
  590. pView->pChildren = NULL;
  591. }
  592. /* first lets count the children */
  593. for(current = head; current != NULL; current = current->list.pNext)
  594. {
  595. if(slapi_dn_isparent(pView->pDn, current->pDn))
  596. child_count++;
  597. }
  598. /* make the space for them */
  599. pView->child_count = child_count;
  600. if (child_count > 0)
  601. {
  602. pView->pChildren = (void **)slapi_ch_calloc(child_count, sizeof(viewEntry*));
  603. /* add them */
  604. for(current = head; current != NULL; current = current->list.pNext)
  605. {
  606. if(slapi_dn_isparent(pView->pDn, current->pDn))
  607. {
  608. ((viewEntry**)pView->pChildren)[add_count] = current;
  609. add_count++;
  610. }
  611. }
  612. }
  613. }
  614. /*
  615. views_cache_discover_view_scope
  616. ------------------------------
  617. finds the parent of the top most view and sets the scope of the view search
  618. */
  619. static void views_cache_discover_view_scope(viewEntry *pView)
  620. {
  621. viewEntry *current = pView;
  622. if(pView->pSearch_base)
  623. slapi_ch_free((void**)&pView->pSearch_base);
  624. while(current != NULL)
  625. {
  626. if(current->pParent == NULL)
  627. {
  628. /* found top */
  629. pView->pSearch_base = slapi_dn_parent(current->pDn);
  630. }
  631. current = current->pParent;
  632. }
  633. }
  634. /*
  635. views_cache_create_applied_filter
  636. --------------------------------
  637. builds the filters for:
  638. char *includeAncestorFiltersFilter; the view with all ancestor views
  639. */
  640. static void views_cache_create_applied_filter(viewEntry *pView)
  641. {
  642. viewEntry *current = pView;
  643. Slapi_Filter *pCurrentFilter = 0;
  644. Slapi_Filter *pBuiltFilter = 0;
  645. Slapi_Filter *pViewEntryExcludeFilter = 0;
  646. char *excludeFilter;
  647. if(pView->includeAncestorFiltersFilter)
  648. {
  649. /* release the current filter */
  650. slapi_filter_free(pView->includeAncestorFiltersFilter, 1);
  651. pView->includeAncestorFiltersFilter = 0;
  652. }
  653. /* create applied view filter (this view filter plus ancestors) */
  654. while(current != NULL)
  655. {
  656. /* add this view filter to the built filter using AND */
  657. char *buf;
  658. if(!current->viewfilter)
  659. {
  660. current = current->pParent;
  661. continue; /* skip this view */
  662. }
  663. buf = slapi_ch_strdup(current->viewfilter);
  664. pCurrentFilter = slapi_str2filter( buf );
  665. if (!pCurrentFilter) {
  666. slapi_log_error(SLAPI_LOG_FATAL, VIEWS_PLUGIN_SUBSYSTEM,
  667. "Error: the view filter [%s] in entry [%s] is not valid\n",
  668. buf, current->pDn);
  669. }
  670. if(pBuiltFilter && pCurrentFilter)
  671. pBuiltFilter = slapi_filter_join_ex( LDAP_FILTER_AND, pBuiltFilter, pCurrentFilter, 0 );
  672. else
  673. pBuiltFilter = pCurrentFilter;
  674. slapi_ch_free((void **)&buf);
  675. current = current->pParent;
  676. }
  677. /* filter for removing view entries from search */
  678. /* richm - slapi_str2filter _writes_ to it's argument, so we have to pass in
  679. some writeable memory, or core dump, do not pass go */
  680. excludeFilter = slapi_ch_strdup("(!(objectclass=" VIEW_OBJECTCLASS "))");
  681. pViewEntryExcludeFilter = slapi_str2filter( excludeFilter );
  682. slapi_ch_free_string(&excludeFilter);
  683. if(pBuiltFilter)
  684. pView->includeAncestorFiltersFilter = slapi_filter_join_ex( LDAP_FILTER_AND, pBuiltFilter, pViewEntryExcludeFilter, 0 );
  685. else
  686. pView->includeAncestorFiltersFilter = pViewEntryExcludeFilter;
  687. #ifdef _VIEW_DEBUG_FILTERS
  688. slapi_filter_to_string(pView->includeAncestorFiltersFilter, pView->includeAncestorFiltersFilter_str, sizeof(pView->includeAncestorFiltersFilter_str));
  689. #endif
  690. }
  691. /* views_cache_create_exclusion_filter
  692. * ----------------------------------
  693. * makes a filter which is used for one level searches
  694. * so that views show up correctly if the client filter
  695. * allows: excludeGrandChildViewsFilter
  696. *
  697. * Also makes the filter which excludes entries which
  698. * belong in descendent views: excludeChildFiltersFilter
  699. */
  700. static void views_cache_create_exclusion_filter(viewEntry *pView)
  701. {
  702. /*
  703. viewEntry *current = pView;
  704. Slapi_Filter *pOrSubFilter = 0;
  705. int child_count = 0;
  706. */
  707. Slapi_Filter *excludeChildFiltersFilter = 0;
  708. char *buf = 0;
  709. /* create exclusion filter for one level searches
  710. * this requires the rdns of the grandchildren of
  711. * this view to be in a filter
  712. */
  713. if(pView->excludeGrandChildViewsFilter)
  714. {
  715. /* release the current filter */
  716. slapi_filter_free(pView->excludeGrandChildViewsFilter, 1);
  717. pView->excludeGrandChildViewsFilter = 0;
  718. }
  719. if(pView->excludeChildFiltersFilter)
  720. {
  721. /* release the current filter */
  722. slapi_filter_free(pView->excludeChildFiltersFilter, 1);
  723. pView->excludeChildFiltersFilter = 0;
  724. }
  725. /* if(pView->child_count == 0)
  726. {
  727. */ /* this view has no children */
  728. /* pView->excludeGrandChildViewsFilter = 0;
  729. pView->excludeChildFiltersFilter = 0;
  730. return;
  731. }
  732. while(child_count < pView->child_count)
  733. {
  734. current = pView->pChildren[child_count];
  735. if(current->child_count == 0)
  736. {
  737. */ /* no grandchildren here, skip */
  738. /* child_count++;
  739. continue;
  740. }
  741. */
  742. /* for each child we need to add its descendants */
  743. /* if(pOrSubFilter)
  744. {
  745. Slapi_Filter *pDescendents = views_cache_create_descendent_filter(current, TRUE);
  746. if(pDescendents)
  747. pOrSubFilter = slapi_filter_join_ex( LDAP_FILTER_OR, pOrSubFilter, pDescendents, 0 );
  748. }
  749. else
  750. pOrSubFilter = views_cache_create_descendent_filter(current, TRUE);
  751. child_count++;
  752. }
  753. */
  754. buf=PR_smprintf("(parentid=%lu)", pView->entryid);
  755. pView->excludeGrandChildViewsFilter = slapi_str2filter( buf );
  756. PR_smprintf_free(buf);
  757. /* if(pOrSubFilter)
  758. pView->excludeGrandChildViewsFilter = slapi_filter_join_ex( LDAP_FILTER_NOT, pOrSubFilter, NULL, 0 );*/
  759. excludeChildFiltersFilter = views_cache_create_descendent_filter(pView, PR_FALSE);
  760. if(excludeChildFiltersFilter)
  761. pView->excludeChildFiltersFilter = slapi_filter_join_ex( LDAP_FILTER_NOT, excludeChildFiltersFilter, NULL, 0 );
  762. #ifdef _VIEW_DEBUG_FILTERS
  763. slapi_filter_to_string(pView->excludeGrandChildViewsFilter, pView->excludeGrandChildViewsFilter_str, sizeof(pView->excludeGrandChildViewsFilter_str));
  764. slapi_filter_to_string(pView->excludeChildFiltersFilter, pView->excludeChildFiltersFilter_str, sizeof(pView->excludeChildFiltersFilter_str));
  765. #endif
  766. }
  767. Slapi_Filter *views_cache_create_descendent_filter(viewEntry *ancestor, PRBool useEntryID)
  768. {
  769. int child_count = 0;
  770. Slapi_Filter *pOrSubFilter = 0;
  771. while(child_count < ancestor->child_count)
  772. {
  773. Slapi_Filter *pDescendentSubFilter = 0;
  774. /*
  775. Slapi_RDN *rdn = 0;
  776. char *str_rdn = 0;
  777. int len = 0;
  778. */
  779. Slapi_Filter *pCurrentFilter = 0;
  780. viewEntry *currentChild = ancestor->pChildren[child_count];
  781. char *buf = 0;
  782. /* for each child we need to add its descendants
  783. * we do this now before processing this view
  784. * to try to help the filter code out by having
  785. * the most significant filters first
  786. */
  787. pDescendentSubFilter = views_cache_create_descendent_filter(currentChild, useEntryID);
  788. if(pDescendentSubFilter)
  789. {
  790. if(pOrSubFilter)
  791. pOrSubFilter = slapi_filter_join_ex( LDAP_FILTER_OR, pOrSubFilter, pDescendentSubFilter, 0 );
  792. else
  793. pOrSubFilter = pDescendentSubFilter;
  794. }
  795. if(useEntryID)
  796. {
  797. /* we need the RDN of this child */
  798. /* rdn = slapi_rdn_new_dn(currentChild->pDn);
  799. str_rdn = (char *)slapi_rdn_get_rdn(rdn);
  800. len = strlen(str_rdn);
  801. buf=PR_smprintf("(%s)", str_rdn);*/
  802. /* uniquely identify this child */
  803. buf=PR_smprintf("(parentid=%lu)", currentChild->entryid);
  804. }
  805. else
  806. {
  807. /* this is a filter based filter */
  808. if(currentChild->viewfilter)
  809. {
  810. buf=PR_smprintf("%s",currentChild->viewfilter);
  811. }
  812. }
  813. if(buf)
  814. {
  815. pCurrentFilter = slapi_str2filter( buf );
  816. if (!pCurrentFilter) {
  817. slapi_log_error(SLAPI_LOG_FATAL, VIEWS_PLUGIN_SUBSYSTEM,
  818. "Error: the view filter [%s] in entry [%s] is not valid\n",
  819. buf, currentChild->pDn);
  820. }
  821. if(pOrSubFilter && pCurrentFilter)
  822. pOrSubFilter = slapi_filter_join_ex( LDAP_FILTER_OR, pOrSubFilter, pCurrentFilter, 0 );
  823. else
  824. pOrSubFilter = pCurrentFilter;
  825. PR_smprintf_free(buf);
  826. }
  827. child_count++;
  828. }
  829. return pOrSubFilter;
  830. }
  831. /* views_cache_create_inclusion_filter
  832. * ----------------------------------
  833. * makes a filter which is used for subtree searches
  834. * so that views show up correctly if the client filter
  835. * allows
  836. */
  837. static void views_cache_create_inclusion_filter(viewEntry *pView)
  838. {
  839. #if 0
  840. viewEntry *head = theCache.pCacheViews;
  841. #endif
  842. /* viewEntry *current; */
  843. /* Slapi_Filter *view_filter; */
  844. char *view_filter_str;
  845. if(pView->includeChildViewsFilter)
  846. {
  847. /* release the current filter */
  848. slapi_filter_free(pView->includeChildViewsFilter, 1);
  849. pView->includeChildViewsFilter = 0;
  850. }
  851. #if 0
  852. for(current = head; current != NULL; current = current->list.pNext)
  853. {
  854. Slapi_DN *viewDN;
  855. Slapi_RDN *viewRDN;
  856. char *viewRDNstr;
  857. char *buf = 0;
  858. Slapi_Filter *viewSubFilter;
  859. /* if this is this a descendent, ignore it */
  860. if(slapi_dn_issuffix(current->pDn,pView->pDn) && !(current == pView))
  861. continue;
  862. viewDN = slapi_sdn_new_dn_byref(current->pDn);
  863. viewRDN = slapi_rdn_new();
  864. slapi_sdn_get_rdn(viewDN,viewRDN);
  865. viewRDNstr = (char *)slapi_rdn_get_rdn(viewRDN);
  866. buf = slapi_ch_calloc(1, strlen(viewRDNstr) + 11 ); /* 3 for filter */
  867. sprintf(buf, "(%s)", viewRDNstr );
  868. viewSubFilter = slapi_str2filter( buf );
  869. if (!viewSubFilter) {
  870. slapi_log_error(SLAPI_LOG_FATAL, VIEWS_PLUGIN_SUBSYSTEM,
  871. "Error: the view filter [%s] in entry [%s] is not valid\n",
  872. buf, current->pDn);
  873. }
  874. if(pView->includeChildViewsFilter && viewSubFilter)
  875. pView->includeChildViewsFilter = slapi_filter_join_ex( LDAP_FILTER_OR, pView->includeChildViewsFilter, viewSubFilter, 0 );
  876. else
  877. pView->includeChildViewsFilter = viewSubFilter;
  878. slapi_ch_free((void **)&buf);
  879. slapi_sdn_free(&viewDN);
  880. slapi_rdn_free(&viewRDN);
  881. child_count++;
  882. }
  883. #endif
  884. /* exclude all other view entries but decendents */
  885. /* pView->includeChildViewsFilter = slapi_filter_join_ex( LDAP_FILTER_NOT, pView->includeChildViewsFilter, NULL, 0 );
  886. */
  887. /* it seems reasonable to include entries which
  888. * may not fit the view decription but which
  889. * are actually *contained* in the view
  890. * therefore we use parentids for the view
  891. * filter
  892. */
  893. /* add decendents */
  894. pView->includeChildViewsFilter = views_cache_create_descendent_filter(pView, PR_TRUE);
  895. /* add this view */
  896. view_filter_str = PR_smprintf("(|(parentid=%lu)(entryid=%lu))", pView->entryid, pView->entryid);
  897. if(pView->includeChildViewsFilter)
  898. {
  899. pView->includeChildViewsFilter = slapi_filter_join_ex( LDAP_FILTER_OR, slapi_str2filter( view_filter_str ), pView->includeChildViewsFilter, PR_FALSE);
  900. }
  901. else
  902. {
  903. pView->includeChildViewsFilter = slapi_str2filter( view_filter_str );
  904. }
  905. PR_smprintf_free(view_filter_str);
  906. view_filter_str = NULL;
  907. /* and make sure the this applies only to views */
  908. /* if(pView->includeChildViewsFilter)
  909. {*/
  910. /* Not necessary since we now use entryid in the filter,
  911. so all will be views anyway, and the less sub-filters
  912. the better
  913. view_filter_str = strdup("(objectclass=" VIEW_OBJECTCLASS ")");
  914. view_filter = slapi_str2filter( view_filter_str );
  915. */
  916. /* child views first because entryid indexed
  917. * and makes evaluation faster when a bunch
  918. * of indexed filter evaluations with only one
  919. * target are evaluated first rather than an
  920. * indexed filter which will provide many entries
  921. * that may trigger an index evaluation short
  922. * circuit. i.e. if one of the child filters is
  923. * true then we have one entry, if not, then we
  924. * have used indexes completely to determine that
  925. * no entry matches and (objectclass=nsview) is never
  926. * evaluated.
  927. * I should imagine this will hold for all but the
  928. * very deepest, widest view trees when subtree
  929. * searches are performed from the top
  930. */
  931. /* pView->includeChildViewsFilter = slapi_filter_join_ex( LDAP_FILTER_AND, pView->includeChildViewsFilter, view_filter, 0 );
  932. }
  933. else
  934. {
  935. view_filter_str = strdup("(objectclass=nsviewincludenone)"); *//* hackery to get the right result */
  936. /* pView->includeChildViewsFilter = slapi_str2filter( view_filter_str );
  937. }
  938. */
  939. #ifdef _VIEW_DEBUG_FILTERS
  940. slapi_filter_to_string(pView->includeChildViewsFilter, pView->includeChildViewsFilter_str, sizeof(pView->includeChildViewsFilter_str));
  941. #endif
  942. }
  943. /*
  944. views_cache_build_view_list
  945. -------------------------------
  946. builds the list of views by searching for them throughout the DIT
  947. */
  948. static int views_cache_build_view_list(viewEntry **pViews)
  949. {
  950. int ret = SLAPI_PLUGIN_SUCCESS;
  951. Slapi_PBlock *pSuffixSearch = 0;
  952. Slapi_Entry **pSuffixList = 0;
  953. Slapi_Attr *suffixAttr;
  954. struct berval **suffixVals;
  955. char *attrType = 0;
  956. char *attrs[2];
  957. int suffixIndex = 0;
  958. int valIndex = 0;
  959. slapi_log_error(SLAPI_LOG_TRACE, VIEWS_PLUGIN_SUBSYSTEM, "--> views_cache_build_view_list\n");
  960. /*
  961. the views may be anywhere in the DIT,
  962. so our first task is to find them.
  963. */
  964. attrs[0] = "namingcontexts";
  965. attrs[1] = 0;
  966. slapi_log_error(SLAPI_LOG_PLUGIN, VIEWS_PLUGIN_SUBSYSTEM, "views: Building view cache.\n");
  967. pSuffixSearch = slapi_search_internal("",LDAP_SCOPE_BASE,"(objectclass=*)",NULL,attrs,0);
  968. if(pSuffixSearch)
  969. slapi_pblock_get( pSuffixSearch, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  970. if(pSuffixSearch && ret == LDAP_SUCCESS)
  971. {
  972. /* iterate through the suffixes and search for views */
  973. slapi_pblock_get( pSuffixSearch, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &pSuffixList);
  974. if(pSuffixList)
  975. {
  976. while(pSuffixList[suffixIndex])
  977. {
  978. if(!slapi_entry_first_attr(pSuffixList[suffixIndex], &suffixAttr))
  979. {
  980. do
  981. {
  982. attrType = 0;
  983. slapi_attr_get_type(suffixAttr, &attrType);
  984. if(attrType && !slapi_utf8casecmp((unsigned char*)attrType, (unsigned char*)"namingcontexts"))
  985. {
  986. if(!slapi_attr_get_bervals_copy(suffixAttr, &suffixVals))
  987. {
  988. valIndex = 0;
  989. if(suffixVals)
  990. {
  991. while(suffixVals[valIndex])
  992. {
  993. /* here's a suffix, lets search it... */
  994. if(suffixVals[valIndex]->bv_val)
  995. views_cache_add_dn_views(suffixVals[valIndex]->bv_val ,pViews);
  996. valIndex++;
  997. }
  998. ber_bvecfree( suffixVals );
  999. suffixVals = NULL;
  1000. }
  1001. }
  1002. }
  1003. } while(!slapi_entry_next_attr(pSuffixList[suffixIndex], suffixAttr, &suffixAttr));
  1004. }
  1005. suffixIndex++;
  1006. }
  1007. }
  1008. }
  1009. else
  1010. {
  1011. slapi_log_error(SLAPI_LOG_PLUGIN, VIEWS_PLUGIN_SUBSYSTEM, "views_cache_build_view_list: failed to find suffixes\n");
  1012. ret = SLAPI_PLUGIN_FAILURE;
  1013. }
  1014. /* clean up */
  1015. if(pSuffixSearch)
  1016. {
  1017. slapi_free_search_results_internal(pSuffixSearch);
  1018. slapi_pblock_destroy(pSuffixSearch);
  1019. }
  1020. slapi_log_error(SLAPI_LOG_TRACE, VIEWS_PLUGIN_SUBSYSTEM, "<-- views_cache_build_view_list\n");
  1021. return ret;
  1022. }
  1023. /* struct to support search callback API */
  1024. struct dn_views_info {
  1025. viewEntry **pViews;
  1026. int ret;
  1027. };
  1028. /* does same funcationality as views_add_dn_views except it is invoked via a callback */
  1029. static int views_dn_views_cb (Slapi_Entry* e, void *callback_data)
  1030. {
  1031. struct dn_views_info *info;
  1032. char *pDn = 0;
  1033. struct berval **dnVals;
  1034. Slapi_Attr *dnAttr;
  1035. char *attrType = 0;
  1036. viewEntry *pView;
  1037. info=(struct dn_views_info *)callback_data;
  1038. info->ret = 0;
  1039. pDn = slapi_entry_get_ndn(e);
  1040. /* create the view */
  1041. pView = (viewEntry *)slapi_ch_calloc(1, sizeof(viewEntry));
  1042. pView->pDn = slapi_ch_strdup(pDn);
  1043. if(!slapi_entry_first_attr(e, &dnAttr))
  1044. {
  1045. do
  1046. {
  1047. attrType = 0;
  1048. /* get the filter */
  1049. slapi_attr_get_type(dnAttr, &attrType);
  1050. if(attrType && !strcasecmp(attrType,VIEW_FILTER_ATTR))
  1051. {
  1052. if(!slapi_attr_get_bervals_copy(dnAttr, &dnVals))
  1053. {
  1054. /* add filter */
  1055. pView->viewfilter = slapi_ch_strdup(dnVals[0]->bv_val);
  1056. }
  1057. ber_bvecfree( dnVals );
  1058. dnVals = NULL;
  1059. }
  1060. if(attrType && !strcasecmp(attrType,"entryid"))
  1061. {
  1062. Slapi_Value *val = 0;
  1063. slapi_attr_first_value(dnAttr, &val);
  1064. pView->entryid = slapi_value_get_ulong(val);
  1065. }
  1066. if(attrType && !strcasecmp(attrType,"parentid"))
  1067. {
  1068. Slapi_Value *val = 0;
  1069. slapi_attr_first_value(dnAttr, &val);
  1070. pView->parentid = slapi_value_get_ulong(val);
  1071. }
  1072. } while(!slapi_entry_next_attr(e, dnAttr, &dnAttr));
  1073. }
  1074. /* add view to the cache */
  1075. views_cache_add_ll_entry((void**)info->pViews, (void *)pView);
  1076. return info->ret;
  1077. }
  1078. /*
  1079. views_cache_add_dn_views
  1080. -------------------------
  1081. takes a dn as argument and searches the dn for views,
  1082. adding any found to the view cache. Change to use search callback API
  1083. */
  1084. #define DN_VIEW_FILTER "(objectclass=" VIEW_OBJECTCLASS ")"
  1085. static int views_cache_add_dn_views(char *dn, viewEntry **pViews)
  1086. {
  1087. Slapi_PBlock *pDnSearch = 0;
  1088. struct dn_views_info info = {NULL, -1};
  1089. pDnSearch = slapi_pblock_new();
  1090. if (pDnSearch) {
  1091. info.ret=-1;
  1092. info.pViews=pViews;
  1093. slapi_search_internal_set_pb(pDnSearch, dn, LDAP_SCOPE_SUBTREE,
  1094. DN_VIEW_FILTER,NULL,0,
  1095. NULL,NULL,view_get_plugin_identity(),0);
  1096. slapi_search_internal_callback_pb(pDnSearch,
  1097. &info /* callback_data */,
  1098. NULL/* result_callback */,
  1099. views_dn_views_cb,
  1100. NULL /* referral_callback */);
  1101. slapi_pblock_destroy (pDnSearch);
  1102. }
  1103. return info.ret;
  1104. }
  1105. /*
  1106. views_cache_add_ll_entry
  1107. ---------------------------------------------------
  1108. the element is added to the head of the linked list
  1109. *NOTE* this function assumes and *requires* that the structures
  1110. passed to it in "attrval" and "theVal" have a viewLinkedList
  1111. member, and it is the *first* member of the structure. This
  1112. is safe because this is a module level function, and all functions
  1113. which call this one are part of the same sub-system.
  1114. */
  1115. static void views_cache_add_ll_entry(void** attrval, void *theVal)
  1116. {
  1117. slapi_log_error(SLAPI_LOG_TRACE, VIEWS_PLUGIN_SUBSYSTEM, "--> views_cache_add_ll_entry\n");
  1118. if(*attrval)
  1119. {
  1120. /* push this to the start of the list (because its quick) */
  1121. ((viewLinkedList*)theVal)->pNext = *attrval;
  1122. ((viewLinkedList*)(*attrval))->pPrev = theVal;
  1123. *attrval = theVal;
  1124. }
  1125. else
  1126. {
  1127. /* new or end of list */
  1128. ((viewLinkedList*)theVal)->pNext = NULL;
  1129. ((viewLinkedList*)theVal)->pPrev = NULL;
  1130. *attrval = theVal;
  1131. }
  1132. slapi_log_error(SLAPI_LOG_TRACE, VIEWS_PLUGIN_SUBSYSTEM, "<-- views_cache_add_ll_entry\n");
  1133. }
  1134. /*
  1135. views_update_views_cache
  1136. -----------------------
  1137. update internal view cache after state change
  1138. */
  1139. static void views_update_views_cache( Slapi_Entry *e, char *dn, int modtype, Slapi_PBlock *pb, void *caller_data )
  1140. {
  1141. char *pDn;
  1142. viewEntry *theView;
  1143. viewEntry *current;
  1144. Slapi_Attr *attr;
  1145. struct berval val;
  1146. int build_cache = 0;
  1147. slapi_counter_increment(op_counter);
  1148. if (0 == g_plugin_started) {
  1149. slapi_counter_decrement(op_counter);
  1150. return;
  1151. }
  1152. slapi_log_error( SLAPI_LOG_TRACE, VIEWS_PLUGIN_SUBSYSTEM, "--> views_update_views_cache\n");
  1153. views_write_lock();
  1154. if(!theCache.cache_built)
  1155. {
  1156. /* zarro views = no cache,
  1157. * this is probably an add op
  1158. * lets build the cache
  1159. */
  1160. build_cache = 1;
  1161. goto unlock_cache;
  1162. }
  1163. pDn = slapi_entry_get_ndn(e);
  1164. theView = views_cache_find_view(pDn);
  1165. switch(modtype)
  1166. {
  1167. case LDAP_CHANGETYPE_MODIFY:
  1168. /* if still a view and exists
  1169. * update string filter
  1170. * update the filters of all views
  1171. * if just became a view fall through to add op
  1172. * if stopped being a view fall through to delete op
  1173. */
  1174. /* determine what happenned - does the view exist currently? */
  1175. if(theView)
  1176. {
  1177. /* does it have the view objectclass? */
  1178. if(!slapi_entry_attr_find( e, "objectclass", &attr ))
  1179. {
  1180. val.bv_len = 8;
  1181. val.bv_val = VIEW_OBJECTCLASS;
  1182. if(!slapi_attr_value_find( attr, &val))
  1183. {
  1184. /* it is a view */
  1185. attr = 0;
  1186. /* has the filter changed? */
  1187. slapi_entry_attr_find( e, VIEW_FILTER_ATTR, &attr );
  1188. if(attr)
  1189. {
  1190. if(theView->viewfilter) /* NULL means a filter added */
  1191. {
  1192. /* we could translate the string filter into
  1193. * a real filter and compare against
  1194. * the view - that would tell us if the filter
  1195. * was substantively changed.
  1196. *
  1197. * But we're not gonna do that :)
  1198. */
  1199. val.bv_len = strlen(theView->viewfilter)+1;
  1200. val.bv_val = theView->viewfilter;
  1201. if(!slapi_attr_value_find( attr, &val))
  1202. {
  1203. /* filter unchanged */
  1204. break;
  1205. }
  1206. }
  1207. }
  1208. else
  1209. {
  1210. /* if no filter in view, then no change */
  1211. if(theView->viewfilter == 0)
  1212. break;
  1213. }
  1214. /* this was indeed a significant mod, add the new filter */
  1215. if(theView->viewfilter)
  1216. slapi_ch_free((void**)&theView->viewfilter);
  1217. if(attr)
  1218. {
  1219. Slapi_Value *v;
  1220. slapi_attr_first_value( attr, &v );
  1221. theView->viewfilter = slapi_ch_strdup(slapi_value_get_string(v));
  1222. }
  1223. /* update all filters */
  1224. for(current = theCache.pCacheViews; current != NULL; current = current->list.pNext)
  1225. {
  1226. views_cache_create_applied_filter(current);
  1227. views_cache_create_exclusion_filter(current);
  1228. views_cache_create_inclusion_filter(current);
  1229. }
  1230. }
  1231. else
  1232. {
  1233. /* this is a delete operation */
  1234. modtype = LDAP_CHANGETYPE_DELETE;
  1235. }
  1236. }
  1237. else
  1238. /* thats bad */
  1239. break;
  1240. }
  1241. else
  1242. {
  1243. /* this is an add operation */
  1244. modtype = LDAP_CHANGETYPE_ADD;
  1245. }
  1246. case LDAP_CHANGETYPE_DELETE:
  1247. /* remove view entry from list
  1248. * update children of parent
  1249. * update all child filters
  1250. * re-index
  1251. */
  1252. if(modtype == LDAP_CHANGETYPE_DELETE)
  1253. {
  1254. if(theCache.view_count-1)
  1255. {
  1256. /* detach view */
  1257. if(theView->list.pPrev)
  1258. ((viewEntry*)(theView->list.pPrev))->list.pNext = theView->list.pNext;
  1259. if(theView->list.pNext)
  1260. {
  1261. ((viewEntry*)(theView->list.pNext))->list.pPrev = theView->list.pPrev;
  1262. if(theView->list.pPrev == NULL) /* if this is the head */
  1263. theCache.pCacheViews = (viewEntry*)(theView->list.pNext);
  1264. }
  1265. /* the parent of this node needs to know about its children */
  1266. if(theView->pParent)
  1267. views_cache_discover_children((viewEntry*)theView->pParent);
  1268. /* each child of the deleted node will need to discover a new parent */
  1269. views_cache_discover_parent_for_children((viewEntry*)theView);
  1270. /* update filters */
  1271. for(current = theCache.pCacheViews; current != NULL; current = current->list.pNext)
  1272. {
  1273. views_cache_create_applied_filter(current);
  1274. views_cache_create_exclusion_filter(current);
  1275. views_cache_create_inclusion_filter(current);
  1276. }
  1277. /* reindex */
  1278. views_cache_index();
  1279. }
  1280. else
  1281. {
  1282. theCache.pCacheViews = NULL;
  1283. theCache.view_count = 0;
  1284. theCache.cache_built = 0;
  1285. }
  1286. /* free the view */
  1287. slapi_ch_free((void**)&theView->pDn);
  1288. slapi_ch_free((void**)&theView->viewfilter);
  1289. slapi_filter_free(theView->includeAncestorFiltersFilter,1);
  1290. slapi_filter_free(theView->excludeAllButDescendentViewsFilter,1);
  1291. slapi_filter_free(theView->excludeChildFiltersFilter,1);
  1292. slapi_filter_free(theView->excludeGrandChildViewsFilter,1);
  1293. slapi_filter_free(theView->includeChildViewsFilter,1);
  1294. slapi_ch_free((void**)&theView->pSearch_base);
  1295. slapi_ch_free((void**)&theView->pChildren);
  1296. slapi_ch_free((void**)&theView);
  1297. break;
  1298. }
  1299. case LDAP_CHANGETYPE_ADD:
  1300. /* create view entry
  1301. * add it to list
  1302. * update children of parent
  1303. * update all child filters
  1304. * re-index
  1305. */
  1306. if(modtype == LDAP_CHANGETYPE_ADD)
  1307. {
  1308. theView = (viewEntry *)slapi_ch_calloc(1, sizeof(viewEntry));
  1309. theView->pDn = slapi_ch_strdup(pDn);
  1310. /* get the view filter, the entryid, and the parentid */
  1311. slapi_entry_attr_find( e, VIEW_FILTER_ATTR, &attr );
  1312. if(attr)
  1313. {
  1314. Slapi_Value *v;
  1315. slapi_attr_first_value( attr, &v );
  1316. theView->viewfilter = slapi_ch_strdup(slapi_value_get_string(v));
  1317. }
  1318. else
  1319. theView->viewfilter = NULL;
  1320. slapi_entry_attr_find( e, "entryid", &attr );
  1321. if(attr)
  1322. {
  1323. Slapi_Value *v;
  1324. slapi_attr_first_value( attr, &v );
  1325. theView->entryid = slapi_value_get_ulong(v);
  1326. }
  1327. else
  1328. theView->entryid = 0;
  1329. slapi_entry_attr_find( e, "parentid", &attr );
  1330. if(attr)
  1331. {
  1332. Slapi_Value *v;
  1333. slapi_attr_first_value( attr, &v );
  1334. theView->parentid = slapi_value_get_ulong(v);
  1335. }
  1336. else
  1337. theView->parentid = 0;
  1338. /* add view to the cache */
  1339. views_cache_add_ll_entry((void**)&theCache.pCacheViews, (void *)theView);
  1340. views_cache_discover_parent(theView);
  1341. if(theView->pParent)
  1342. views_cache_discover_children((viewEntry*)theView->pParent);
  1343. /* update filters */
  1344. for(current = theCache.pCacheViews; current != NULL; current = current->list.pNext)
  1345. {
  1346. views_cache_discover_view_scope(current); /* if ns-view oc added, new view may be top */
  1347. views_cache_create_applied_filter(current);
  1348. views_cache_create_exclusion_filter(current);
  1349. views_cache_create_inclusion_filter(current);
  1350. }
  1351. /* reindex */
  1352. views_cache_index();
  1353. break;
  1354. }
  1355. case LDAP_CHANGETYPE_MODDN:
  1356. /* get old dn to find the view
  1357. * change dn
  1358. * update parents and children
  1359. * update all filters
  1360. * reindex
  1361. */
  1362. {
  1363. char *old_dn;
  1364. Slapi_Entry *old_entry;
  1365. slapi_pblock_get( pb, SLAPI_ENTRY_PRE_OP, &old_entry );
  1366. old_dn = slapi_entry_get_ndn(old_entry);
  1367. theView = views_cache_find_view(old_dn);
  1368. if(theView)
  1369. {
  1370. slapi_ch_free((void**)&theView->pDn);
  1371. theView->pDn = slapi_ch_strdup(pDn);
  1372. for(current = theCache.pCacheViews; current != NULL; current = current->list.pNext)
  1373. {
  1374. views_cache_discover_parent(current);
  1375. views_cache_discover_children(current);
  1376. }
  1377. for(current = theCache.pCacheViews; current != NULL; current = current->list.pNext)
  1378. {
  1379. views_cache_discover_view_scope(current);
  1380. views_cache_create_applied_filter(current);
  1381. views_cache_create_exclusion_filter(current);
  1382. views_cache_create_inclusion_filter(current);
  1383. }
  1384. }
  1385. /* reindex */
  1386. views_cache_index();
  1387. break;
  1388. }
  1389. default:
  1390. /* we don't care about this op */
  1391. break;
  1392. }
  1393. unlock_cache:
  1394. views_unlock();
  1395. if(build_cache)
  1396. {
  1397. views_cache_create();
  1398. }
  1399. slapi_counter_decrement(op_counter);
  1400. slapi_log_error( SLAPI_LOG_TRACE, VIEWS_PLUGIN_SUBSYSTEM, "<-- views_update_views_cache\n");
  1401. }
  1402. /*
  1403. * view_search_rewrite_callback
  1404. * ----------------------------
  1405. * this is the business end of the plugin
  1406. * this function is called from slapd
  1407. * rewrites the search to conform to the view
  1408. * Meaning of the return code :
  1409. * -1 : keep looking
  1410. * 0 : rewrote OK
  1411. * 1 : refuse to do this search
  1412. * 2 : operations error
  1413. */
  1414. static int view_search_rewrite_callback(Slapi_PBlock *pb)
  1415. {
  1416. int ret = -1;
  1417. char *base = 0;
  1418. Slapi_Filter *clientFilter = 0;
  1419. Slapi_Filter *includeAncestorFiltersFilter = 0; /* the view with all ancestor views */
  1420. Slapi_Filter *excludeChildFiltersFilter = 0; /* NOT all children views, for one level searches */
  1421. Slapi_Filter *excludeGrandChildViewsFilter = 0; /* view filter for one level searches */
  1422. Slapi_Filter *includeChildViewsFilter = 0; /* view filter for subtree searches */
  1423. Slapi_Filter *seeViewsFilter = 0; /* view filter to see views */
  1424. Slapi_Filter *outFilter = 0;
  1425. int scope = 0;
  1426. int set_scope = LDAP_SCOPE_SUBTREE;
  1427. viewEntry *theView = 0;
  1428. Slapi_DN *basesdn = NULL;
  1429. #ifdef _VIEW_DEBUG_FILTERS
  1430. char outFilter_str[1024];
  1431. char clientFilter_str[1024];
  1432. char includeAncestorFiltersFilter_str[1024];
  1433. char excludeChildFiltersFilter_str[1024];
  1434. char excludeGrandChildViewsFilter_str[1024];
  1435. char includeChildViewsFilter_str[1024];
  1436. #endif
  1437. /* if no cache, no views */
  1438. if(!theCache.cache_built)
  1439. goto end;
  1440. /* avoid locking if this thread is the updater */
  1441. if(theCache.currentUpdaterThread)
  1442. {
  1443. PRThread *thisThread = PR_GetCurrentThread();
  1444. if(thisThread == theCache.currentUpdaterThread)
  1445. goto end;
  1446. }
  1447. /* first, find out if this is a base search (we do nothing) */
  1448. slapi_pblock_get(pb, SLAPI_SEARCH_SCOPE, &scope);
  1449. if(scope == LDAP_SCOPE_BASE)
  1450. goto end;
  1451. /* if base of the search is a view */
  1452. slapi_pblock_get(pb, SLAPI_SEARCH_TARGET_SDN, &basesdn);
  1453. base = (char *)slapi_sdn_get_dn(basesdn);
  1454. /* Read lock the cache */
  1455. views_read_lock();
  1456. theView = views_cache_find_view(base);
  1457. /* if the view is disabled (we service subtree searches in this case) */
  1458. if(!theView || (!theView->viewfilter && scope == LDAP_SCOPE_ONELEVEL))
  1459. {
  1460. /* unlock the cache */
  1461. views_unlock();
  1462. goto end;
  1463. }
  1464. /* this is a view search, and we are smokin' */
  1465. /* grab the view filters we are going to need now so we can release the cache lock */
  1466. if(scope == LDAP_SCOPE_ONELEVEL)
  1467. {
  1468. excludeChildFiltersFilter = slapi_filter_dup(theView->excludeChildFiltersFilter);
  1469. excludeGrandChildViewsFilter = slapi_filter_dup(theView->excludeGrandChildViewsFilter);
  1470. #ifdef _VIEW_DEBUG_FILTERS
  1471. slapi_filter_to_string(excludeChildFiltersFilter, excludeChildFiltersFilter_str, sizeof(excludeChildFiltersFilter_str));
  1472. slapi_filter_to_string(excludeGrandChildViewsFilter, excludeGrandChildViewsFilter_str, sizeof(excludeGrandChildViewsFilter_str));
  1473. #endif
  1474. }
  1475. else
  1476. {
  1477. includeChildViewsFilter = slapi_filter_dup(theView->includeChildViewsFilter);
  1478. }
  1479. #ifdef _VIEW_DEBUG_FILTERS
  1480. slapi_filter_to_string(includeChildViewsFilter, includeChildViewsFilter_str, sizeof(includeChildViewsFilter_str));
  1481. #endif
  1482. /* always used */
  1483. includeAncestorFiltersFilter = slapi_filter_dup(theView->includeAncestorFiltersFilter);
  1484. #ifdef _VIEW_DEBUG_FILTERS
  1485. slapi_filter_to_string(includeAncestorFiltersFilter, includeAncestorFiltersFilter_str, sizeof(includeAncestorFiltersFilter_str));
  1486. #endif
  1487. /* unlock the cache */
  1488. views_unlock();
  1489. /* rewrite search scope and base*/
  1490. slapi_pblock_set(pb, SLAPI_SEARCH_SCOPE, &set_scope);
  1491. slapi_pblock_get(pb, SLAPI_SEARCH_TARGET_SDN, &basesdn);
  1492. slapi_sdn_free(&basesdn);
  1493. basesdn = slapi_sdn_new_dn_byval(theView->pSearch_base);
  1494. slapi_pblock_set(pb, SLAPI_SEARCH_TARGET_SDN, basesdn);
  1495. /* concatenate the filters */
  1496. /* grab the client filter - we need 2 copies */
  1497. slapi_pblock_get(pb, SLAPI_SEARCH_FILTER, &clientFilter);
  1498. #ifdef _VIEW_DEBUG_FILTERS
  1499. slapi_filter_to_string(clientFilter, clientFilter_str, sizeof(clientFilter_str));
  1500. #endif
  1501. /* There are two major clauses in a views filter, one looks
  1502. for entries that match the view filters themselves plus
  1503. the presented client filter, and the other looks for entries
  1504. that exist in the view hierarchy that also match the client
  1505. presented filter
  1506. */
  1507. /* client supplied filter AND views inclusion filter
  1508. - make sure we can see entries in the view tree */
  1509. if(scope == LDAP_SCOPE_ONELEVEL)
  1510. {
  1511. /* this filter is to lock our view to the onelevel search */
  1512. if(excludeGrandChildViewsFilter)
  1513. {
  1514. seeViewsFilter = excludeGrandChildViewsFilter;
  1515. }
  1516. }
  1517. else
  1518. {
  1519. /* this filter is to lock our view to the subtree search */
  1520. if(includeChildViewsFilter)
  1521. {
  1522. seeViewsFilter = includeChildViewsFilter;
  1523. }
  1524. }
  1525. /* but only view tree entries that match the client filter */
  1526. if(seeViewsFilter)
  1527. {
  1528. seeViewsFilter = slapi_filter_join_ex( LDAP_FILTER_AND, slapi_filter_dup(clientFilter), seeViewsFilter, 0 );
  1529. }
  1530. /* create target filter */
  1531. if(includeAncestorFiltersFilter)
  1532. outFilter = slapi_filter_join_ex( LDAP_FILTER_AND, includeAncestorFiltersFilter, clientFilter, 0 );
  1533. else
  1534. outFilter = clientFilter;
  1535. if(scope == LDAP_SCOPE_ONELEVEL)
  1536. {
  1537. if(excludeChildFiltersFilter)
  1538. outFilter = slapi_filter_join_ex( LDAP_FILTER_AND, outFilter, excludeChildFiltersFilter, 0 );
  1539. }
  1540. if(seeViewsFilter)
  1541. outFilter = slapi_filter_join_ex( LDAP_FILTER_OR, outFilter, seeViewsFilter, 0 );
  1542. #ifdef _VIEW_DEBUG_FILTERS
  1543. slapi_filter_to_string(outFilter, outFilter_str, sizeof(outFilter_str));
  1544. #endif
  1545. /* make it happen */
  1546. slapi_pblock_set(pb, SLAPI_SEARCH_FILTER, outFilter);
  1547. ret = -2;
  1548. end:
  1549. return ret;
  1550. }
  1551. /*
  1552. * views_cache_backend_state_change()
  1553. * --------------------------------
  1554. * This is called when a backend changes state
  1555. * We simply signal to rebuild the cache in this case
  1556. *
  1557. */
  1558. static void views_cache_backend_state_change(void *handle, char *be_name,
  1559. int old_be_state, int new_be_state)
  1560. {
  1561. /* we will create a thread to do this since
  1562. * calling views_cache_create() directly will
  1563. * hold up the op
  1564. */
  1565. if ((PR_CreateThread (PR_USER_THREAD,
  1566. views_cache_act_on_change_thread,
  1567. NULL,
  1568. PR_PRIORITY_NORMAL,
  1569. PR_GLOBAL_THREAD,
  1570. PR_UNJOINABLE_THREAD,
  1571. SLAPD_DEFAULT_THREAD_STACKSIZE)) == NULL )
  1572. {
  1573. slapi_log_error( SLAPI_LOG_FATAL, VIEWS_PLUGIN_SUBSYSTEM,
  1574. "views_cache_backend_state_change: PR_CreateThread failed\n" );
  1575. }
  1576. }
  1577. static void views_cache_act_on_change_thread(void *arg)
  1578. {
  1579. views_cache_create();
  1580. }