views.c 50 KB

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