views.c 48 KB

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