views.c 49 KB

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