roles_cache.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111
  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. #include <stdio.h>
  42. #include <string.h>
  43. #include <sys/types.h>
  44. #include "portable.h"
  45. #include "slapi-plugin.h"
  46. /* This is naughty ... */
  47. #include "slapi-private.h"
  48. /* include NSPR header files */
  49. #include "slap.h"
  50. #include "prthread.h"
  51. #include "prlock.h"
  52. #include "prerror.h"
  53. #include "prcvar.h"
  54. #include "prio.h"
  55. #include "avl.h"
  56. #include "vattr_spi.h"
  57. #include "roles_cache.h"
  58. #include "views.h"
  59. #ifdef SOLARIS
  60. #include <tnf/probe.h>
  61. #else
  62. #define TNF_PROBE_0(a,b,c)
  63. #endif
  64. #define MAX_NESTED_ROLES 30
  65. static char *allUserAttributes[] = {
  66. LDAP_ALL_USER_ATTRS,
  67. NULL
  68. };
  69. /* views scoping */
  70. static void **views_api;
  71. /* Service provider handler */
  72. static vattr_sp_handle *vattr_handle = NULL;
  73. /* List of nested roles */
  74. typedef struct _role_object_nested {
  75. Slapi_DN *dn; /* value of attribute nsroledn in a nested role definition */
  76. } role_object_nested;
  77. /* Role object structure */
  78. typedef struct _role_object {
  79. Slapi_DN *dn; /* dn of a role entry */
  80. int type; /* ROLE_TYPE_MANAGED|ROLE_TYPE_FILTERED|ROLE_TYPE_NESTED */
  81. Slapi_Filter *filter; /* if ROLE_TYPE_FILTERED */
  82. Avlnode *avl_tree; /* if ROLE_TYPE_NESTED: tree of nested DNs (avl_data is a role_object_nested struct) */
  83. } role_object;
  84. /* Structure containing the roles definitions for a given suffix */
  85. typedef struct _roles_cache_def {
  86. /* Suffix DN*/
  87. Slapi_DN *suffix_dn;
  88. /* Module level thread control */
  89. PRThread *roles_tid;
  90. int keeprunning;
  91. PRRWLock *cache_lock;
  92. Slapi_Mutex *stop_lock;
  93. Slapi_Mutex *change_lock;
  94. Slapi_CondVar *something_changed;
  95. Slapi_Mutex *create_lock;
  96. Slapi_CondVar *suffix_created;
  97. int is_ready;
  98. /* Root of the avl tree containing all the roles definitions
  99. NB: avl_data field is of type role_object
  100. */
  101. Avlnode *avl_tree;
  102. /* Next roles suffix definitions */
  103. struct _roles_cache_def *next;
  104. /* Info passed from the server when an notification is sent to the plugin */
  105. char *notified_dn;
  106. Slapi_Entry *notified_entry;
  107. int notified_operation;
  108. } roles_cache_def;
  109. /* Global list containing all the roles definitions per suffix */
  110. static roles_cache_def *roles_list = NULL;
  111. static PRRWLock *global_lock = NULL;
  112. /* Structure holding the nsrole values */
  113. typedef struct _roles_cache_build_result
  114. {
  115. Slapi_ValueSet **nsrole_values; /* nsrole computed values */
  116. Slapi_Entry *requested_entry; /* entry to get nsrole from */
  117. int has_value; /* flag to determine if a new value has been added to the result */
  118. int need_value; /* flag to determine if we need the result */
  119. vattr_context *context; /* vattr context */
  120. } roles_cache_build_result;
  121. /* Structure used to check if is_entry_member_of is part of a role defined in its suffix */
  122. typedef struct _roles_cache_search_in_nested
  123. {
  124. Slapi_Entry *is_entry_member_of;
  125. int present; /* flag to know if the entry is part of a role */
  126. int hint; /* to check the depth of the nested */
  127. } roles_cache_search_in_nested;
  128. /* Structure used to handle roles searches */
  129. typedef struct _roles_cache_search_roles
  130. {
  131. roles_cache_def *suffix_def;
  132. int rc; /* to check the depth of the nested */
  133. } roles_cache_search_roles;
  134. static roles_cache_def* roles_cache_create_suffix(Slapi_DN *sdn);
  135. static int roles_cache_add_roles_from_suffix(Slapi_DN *suffix_dn, roles_cache_def *suffix_def);
  136. static void roles_cache_wait_on_change(void * arg);
  137. static void roles_cache_trigger_update_suffix(void *handle, char *be_name, int old_be_state, int new_be_state);
  138. static void roles_cache_trigger_update_role(char *dn, Slapi_Entry *role_entry, Slapi_DN *be_dn, int operation);
  139. static int roles_cache_update(roles_cache_def *suffix_to_update);
  140. static int roles_cache_create_role_under(roles_cache_def** roles_cache_suffix, Slapi_Entry *entry);
  141. static int roles_cache_create_object_from_entry(Slapi_Entry *role_entry, role_object **result, int hint);
  142. static int roles_cache_determine_class(Slapi_Entry *role_entry);
  143. static int roles_cache_node_cmp( caddr_t d1, caddr_t d2 );
  144. static int roles_cache_insert_object(Avlnode **tree, role_object *object);
  145. static int roles_cache_node_nested_cmp( caddr_t d1, caddr_t d2 );
  146. static int roles_cache_insert_object_nested(Avlnode **tree, role_object_nested *object);
  147. static int roles_cache_object_nested_from_dn(Slapi_DN *role_dn, role_object_nested **result);
  148. static int roles_cache_build_nsrole( caddr_t data, caddr_t arg );
  149. static int roles_cache_find_node( caddr_t d1, caddr_t d2 );
  150. static int roles_cache_find_roles_in_suffix(Slapi_DN *target_entry_dn, roles_cache_def **list_of_roles);
  151. static int roles_is_entry_member_of_object(caddr_t data, caddr_t arg );
  152. static int roles_is_entry_member_of_object_ext(vattr_context *c, caddr_t data, caddr_t arg );
  153. static int roles_check_managed(Slapi_Entry *entry_to_check, role_object *role, int *present);
  154. static int roles_check_filtered(vattr_context *c, Slapi_Entry *entry_to_check, role_object *role, int *present);
  155. static int roles_check_nested(caddr_t data, caddr_t arg);
  156. static int roles_is_inscope(Slapi_Entry *entry_to_check, Slapi_DN *role_dn);
  157. static void berval_set_string(struct berval *bv, const char* string);
  158. static void roles_cache_role_def_delete(roles_cache_def *role_def);
  159. static void roles_cache_role_def_free(roles_cache_def *role_def);
  160. static void roles_cache_role_object_free(role_object *this_role);
  161. static void roles_cache_role_object_nested_free(role_object_nested *this_role);
  162. static int roles_cache_dump( caddr_t data, caddr_t arg );
  163. static int roles_cache_add_entry_cb(Slapi_Entry* e, void *callback_data);
  164. static void roles_cache_result_cb( int rc, void *callback_data);
  165. static Slapi_DN* roles_cache_get_top_suffix(Slapi_DN *suffix);
  166. /* ============== FUNCTIONS ================ */
  167. /* roles_cache_init
  168. ----------------
  169. create the cache for all the existing suffixes
  170. starts up the threads which wait for changes
  171. also registers vattr callbacks
  172. return 0 if OK
  173. return -1 otherwise
  174. */
  175. int roles_cache_init()
  176. {
  177. int rc = 0;
  178. void *node = NULL;
  179. Slapi_DN *sdn = NULL;
  180. roles_cache_def *new_suffix = NULL;
  181. slapi_log_error(SLAPI_LOG_PLUGIN,
  182. ROLES_PLUGIN_SUBSYSTEM, "--> roles_cache_init\n");
  183. if ( global_lock == NULL )
  184. {
  185. global_lock = PR_NewRWLock(0,"roles_cache");
  186. }
  187. /* grab the views interface */
  188. if(slapi_apib_get_interface(Views_v1_0_GUID, &views_api))
  189. {
  190. /* lets be tolerant if views is disabled */
  191. views_api = 0;
  192. }
  193. /* For each top suffix, get the roles definitions defined below it */
  194. PR_RWLock_Wlock(global_lock);
  195. sdn = slapi_get_first_suffix(&node, 0);
  196. while (sdn)
  197. {
  198. if ( (new_suffix = roles_cache_create_suffix(sdn)) == NULL )
  199. {
  200. PR_DestroyRWLock(global_lock);
  201. global_lock = NULL;
  202. return(-1);
  203. }
  204. if ( roles_cache_add_roles_from_suffix(sdn, new_suffix) != 0 )
  205. {
  206. /* No roles in that suffix, stop the thread and remove it ? */
  207. }
  208. sdn = slapi_get_next_suffix(&node, 0);
  209. }
  210. PR_RWLock_Unlock(global_lock);
  211. /* to expose roles_check to ACL plugin */
  212. slapi_register_role_check(roles_check);
  213. /* Register a callback on backends creation|modification|deletion,
  214. so that we update the corresponding cache */
  215. slapi_register_backend_state_change(NULL, roles_cache_trigger_update_suffix);
  216. if ( slapi_vattrspi_register((vattr_sp_handle **)&vattr_handle,
  217. roles_sp_get_value,
  218. roles_sp_compare_value,
  219. roles_sp_list_types) )
  220. {
  221. slapi_log_error( SLAPI_LOG_FATAL, ROLES_PLUGIN_SUBSYSTEM,
  222. "roles_cache_init: slapi_vattrspi_register failed\n");
  223. PR_DestroyRWLock(global_lock);
  224. global_lock = NULL;
  225. return(-1);
  226. }
  227. else if ( slapi_vattrspi_regattr(vattr_handle,NSROLEATTR,"", NULL) )
  228. {
  229. slapi_log_error( SLAPI_LOG_FATAL, ROLES_PLUGIN_SUBSYSTEM,
  230. "roles_cache_init: slapi_vattrspi_regattr failed\n");
  231. slapi_ch_free((void **)&vattr_handle);
  232. PR_DestroyRWLock(global_lock);
  233. global_lock = NULL;
  234. return(-1);
  235. }
  236. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "<-- roles_cache_init\n");
  237. return rc;
  238. }
  239. /* roles_cache_create_suffix
  240. -------------------------
  241. Create a new entry in the global list
  242. return a pointer on the suffix stucture: OK
  243. return NULL: fail
  244. */
  245. static roles_cache_def *roles_cache_create_suffix(Slapi_DN *sdn)
  246. {
  247. roles_cache_def *current_suffix = NULL;
  248. roles_cache_def *new_suffix = NULL;
  249. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "--> roles_cache_create_suffix\n");
  250. /* Allocate a new suffix block */
  251. new_suffix = (roles_cache_def*)slapi_ch_calloc(1, sizeof(roles_cache_def));
  252. if ( new_suffix == NULL )
  253. {
  254. slapi_log_error(SLAPI_LOG_FATAL,
  255. ROLES_PLUGIN_SUBSYSTEM,
  256. "roles_cache_create_suffix: Unable to allocate memory, cannot create role cache\n");
  257. return(NULL);
  258. }
  259. new_suffix->cache_lock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "roles_def_lock");
  260. new_suffix->change_lock = slapi_new_mutex();
  261. new_suffix->stop_lock = slapi_new_mutex();
  262. new_suffix->create_lock = slapi_new_mutex();
  263. if ( new_suffix->stop_lock == NULL ||
  264. new_suffix->change_lock == NULL ||
  265. new_suffix->cache_lock == NULL ||
  266. new_suffix->create_lock == NULL )
  267. {
  268. slapi_log_error( SLAPI_LOG_FATAL, ROLES_PLUGIN_SUBSYSTEM,
  269. "roles_cache_create_suffix: Lock creation failed\n");
  270. roles_cache_role_def_free(new_suffix);
  271. return(NULL);
  272. }
  273. new_suffix->something_changed = slapi_new_condvar(new_suffix->change_lock);
  274. if ( new_suffix->something_changed == NULL)
  275. {
  276. slapi_log_error( SLAPI_LOG_FATAL, ROLES_PLUGIN_SUBSYSTEM,
  277. "roles_cache_create_suffix: Lock creation failed\n");
  278. roles_cache_role_def_free(new_suffix);
  279. return(NULL);
  280. }
  281. new_suffix->suffix_created = slapi_new_condvar(new_suffix->create_lock);
  282. if ( new_suffix->suffix_created == NULL)
  283. {
  284. slapi_log_error( SLAPI_LOG_FATAL, ROLES_PLUGIN_SUBSYSTEM,
  285. "roles_cache_create_suffix: Lock creation failed\n");
  286. roles_cache_role_def_free(new_suffix);
  287. return(NULL);
  288. }
  289. new_suffix->keeprunning = 1;
  290. new_suffix->suffix_dn = slapi_sdn_dup(sdn);
  291. /* those 3 items are used to give back info to the thread when
  292. it is awakened */
  293. new_suffix->notified_dn = NULL;
  294. new_suffix->notified_entry = NULL;
  295. new_suffix->notified_operation = 0;
  296. /* Create the global list */
  297. if ( roles_list == NULL )
  298. {
  299. roles_list = new_suffix;
  300. }
  301. else
  302. {
  303. current_suffix = roles_list;
  304. while ( current_suffix != NULL )
  305. {
  306. if ( current_suffix->next == NULL )
  307. {
  308. current_suffix->next = new_suffix;
  309. break;
  310. }
  311. else
  312. {
  313. current_suffix = current_suffix->next;
  314. }
  315. }
  316. }
  317. /* to prevent deadlock */
  318. new_suffix->is_ready = 0;
  319. if ( (new_suffix->roles_tid = PR_CreateThread (PR_USER_THREAD,
  320. roles_cache_wait_on_change,
  321. (void*)new_suffix,
  322. PR_PRIORITY_NORMAL,
  323. PR_GLOBAL_THREAD,
  324. PR_UNJOINABLE_THREAD,
  325. SLAPD_DEFAULT_THREAD_STACKSIZE)) == NULL )
  326. {
  327. slapi_log_error( SLAPI_LOG_FATAL, ROLES_PLUGIN_SUBSYSTEM,
  328. "roles_cache_create_suffix: PR_CreateThread failed\n");
  329. roles_cache_role_def_delete(new_suffix);
  330. return(NULL);
  331. }
  332. slapi_lock_mutex(new_suffix->create_lock);
  333. if (new_suffix->is_ready != 1)
  334. {
  335. slapi_wait_condvar(new_suffix->suffix_created, NULL);
  336. }
  337. slapi_unlock_mutex(new_suffix->create_lock);
  338. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "<-- roles_cache_create_suffix\n");
  339. return(new_suffix);
  340. }
  341. /* roles_cache_wait_on_change
  342. --------------------------
  343. Sit around waiting on a notification that something has
  344. changed, then fires off the updates
  345. */
  346. static void roles_cache_wait_on_change(void * arg)
  347. {
  348. roles_cache_def *roles_def = (roles_cache_def*)arg;
  349. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "--> roles_cache_wait_on_change\n");
  350. slapi_lock_mutex(roles_def->stop_lock);
  351. slapi_lock_mutex(roles_def->change_lock);
  352. while ( roles_def->keeprunning)
  353. {
  354. slapi_unlock_mutex(roles_def->change_lock);
  355. slapi_lock_mutex(roles_def->change_lock);
  356. /* means that the thread corresponding to that suffix is ready to receive notifications
  357. from the server */
  358. slapi_lock_mutex(roles_def->create_lock);
  359. if ( roles_def->is_ready == 0 )
  360. {
  361. slapi_notify_condvar( roles_def->suffix_created, 1 );
  362. roles_def->is_ready = 1;
  363. }
  364. slapi_unlock_mutex(roles_def->create_lock);
  365. /* XXX In case the BE containing this role_def signaled
  366. a shut down between the unlock/lock above should
  367. test roles_def->keeprunning before
  368. going to sleep.
  369. */
  370. slapi_wait_condvar(roles_def->something_changed, NULL);
  371. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "roles_cache_wait_on_change \n");
  372. if ( roles_def->keeprunning )
  373. {
  374. roles_cache_update(roles_def);
  375. }
  376. }
  377. /* shut down the cache */
  378. slapi_unlock_mutex(roles_def->change_lock);
  379. slapi_unlock_mutex(roles_def->stop_lock);
  380. roles_cache_role_def_free(roles_def);
  381. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "<-- roles_cache_wait_on_change thread exit\n");
  382. }
  383. /* roles_cache_trigger_update_suffix
  384. --------------------------------
  385. This is called when a backend changes state (created|modified|deleted)
  386. We simply signal to update the associated role cache in this case
  387. */
  388. static void roles_cache_trigger_update_suffix(void *handle, char *be_name, int old_be_state, int new_be_state)
  389. {
  390. roles_cache_def *current_role = roles_list;
  391. const Slapi_DN *be_suffix_dn = NULL;
  392. Slapi_DN *top_suffix_dn = NULL;
  393. Slapi_Backend *backend = NULL;
  394. int found = 0;
  395. PR_RWLock_Wlock(global_lock);
  396. if ( (new_be_state == SLAPI_BE_STATE_DELETE) || (new_be_state == SLAPI_BE_STATE_OFFLINE) )
  397. {
  398. /* Invalidate and rebuild the whole cache */
  399. roles_cache_def *current_role = NULL;
  400. roles_cache_def *next_role = NULL;
  401. Slapi_DN *sdn = NULL;
  402. void *node = NULL;
  403. roles_cache_def *new_suffix = NULL;
  404. /* Go through all the roles list and trigger the associated structure */
  405. current_role = roles_list;
  406. while ( current_role )
  407. {
  408. slapi_lock_mutex(current_role->change_lock);
  409. current_role->keeprunning = 0;
  410. next_role = current_role->next;
  411. slapi_notify_condvar(current_role->something_changed, 1 );
  412. slapi_unlock_mutex(current_role->change_lock);
  413. current_role = next_role;
  414. }
  415. /* rebuild a new one */
  416. roles_list = NULL;
  417. sdn = slapi_get_first_suffix(&node, 0);
  418. while (sdn)
  419. {
  420. if ( (new_suffix = roles_cache_create_suffix(sdn)) == NULL )
  421. {
  422. PR_RWLock_Unlock(global_lock);
  423. return;
  424. }
  425. roles_cache_add_roles_from_suffix(sdn, new_suffix);
  426. sdn = slapi_get_next_suffix(&node, 0);
  427. }
  428. PR_RWLock_Unlock(global_lock);
  429. return;
  430. }
  431. /* Backend back on line or new one created*/
  432. backend = slapi_be_select_by_instance_name(be_name);
  433. if ( backend != NULL )
  434. {
  435. be_suffix_dn = slapi_be_getsuffix(backend, 0);
  436. top_suffix_dn = roles_cache_get_top_suffix((Slapi_DN *)be_suffix_dn);
  437. }
  438. while ( (current_role != NULL) && !found && (top_suffix_dn != NULL) )
  439. {
  440. /* The backend already exists (back online): so invalidate "old roles definitions" */
  441. if ( slapi_sdn_compare(current_role->suffix_dn, top_suffix_dn) == 0 )
  442. {
  443. roles_cache_role_def_delete(current_role);
  444. found = 1;
  445. }
  446. else
  447. {
  448. current_role = current_role->next;
  449. }
  450. }
  451. if ( top_suffix_dn != NULL )
  452. {
  453. /* Add the new definitions in the cache */
  454. roles_cache_def *new_suffix = roles_cache_create_suffix(top_suffix_dn);
  455. if ( new_suffix != NULL )
  456. {
  457. roles_cache_add_roles_from_suffix(top_suffix_dn, new_suffix);
  458. }
  459. slapi_sdn_free(&top_suffix_dn);
  460. }
  461. PR_RWLock_Unlock(global_lock);
  462. }
  463. /* roles_cache_trigger_update_role
  464. --------------------------------
  465. Call when an entry containing a role definition has been added, modified
  466. or deleted
  467. */
  468. static void roles_cache_trigger_update_role(char *dn, Slapi_Entry *roles_entry, Slapi_DN *be_dn, int operation)
  469. {
  470. int found = 0;
  471. roles_cache_def *current_role = NULL;
  472. PR_RWLock_Wlock(global_lock);
  473. current_role = roles_list;
  474. slapi_log_error( SLAPI_LOG_PLUGIN,
  475. ROLES_PLUGIN_SUBSYSTEM, "--> roles_cache_trigger_update_role: %p \n", roles_list);
  476. /* Go through all the roles list and trigger the associated structure */
  477. /* be_dn is already the top suffix for that dn */
  478. while ( (current_role != NULL) && !found )
  479. {
  480. if ( slapi_sdn_compare(current_role->suffix_dn, be_dn) == 0 )
  481. {
  482. found = 1;
  483. }
  484. else
  485. {
  486. current_role = current_role->next;
  487. }
  488. }
  489. if ( found )
  490. {
  491. slapi_lock_mutex(current_role->change_lock);
  492. slapi_entry_free (current_role->notified_entry);
  493. current_role->notified_entry = roles_entry;
  494. slapi_ch_free ((void**)&(current_role->notified_dn));
  495. current_role->notified_dn = dn;
  496. current_role->notified_operation = operation;
  497. roles_cache_update(current_role);
  498. slapi_unlock_mutex(current_role->change_lock);
  499. }
  500. PR_RWLock_Unlock(global_lock);
  501. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "<-- roles_cache_trigger_update_role: %p \n", roles_list);
  502. }
  503. /* roles_cache_update
  504. ------------------
  505. Update the cache associated to a suffix
  506. Return 0: ok
  507. Return -1: fail
  508. */
  509. static int roles_cache_update(roles_cache_def *suffix_to_update)
  510. {
  511. int rc = 0;
  512. int operation;
  513. Slapi_Entry *entry = NULL;
  514. Slapi_DN *dn = NULL;
  515. role_object *to_delete = NULL;
  516. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "--> roles_cache_update \n");
  517. PR_RWLock_Wlock(suffix_to_update->cache_lock);
  518. operation = suffix_to_update->notified_operation;
  519. entry = suffix_to_update->notified_entry;
  520. dn = slapi_sdn_new();
  521. slapi_sdn_set_dn_byval(dn, suffix_to_update->notified_dn);
  522. if ( (entry != NULL) && (dn != NULL) )
  523. {
  524. if ( (operation == SLAPI_OPERATION_MODIFY) ||
  525. (operation == SLAPI_OPERATION_DELETE) )
  526. {
  527. /* delete it */
  528. int dummy;
  529. to_delete = (role_object *)avl_delete(&(suffix_to_update->avl_tree), dn, roles_cache_find_node, &dummy);
  530. roles_cache_role_object_free(to_delete);
  531. to_delete = NULL;
  532. if ( slapi_is_loglevel_set(SLAPI_LOG_PLUGIN) )
  533. {
  534. avl_apply(suffix_to_update->avl_tree, (IFP)roles_cache_dump, &rc, -1, AVL_INORDER);
  535. }
  536. }
  537. if ( (operation == SLAPI_OPERATION_MODIFY) ||
  538. (operation ==SLAPI_OPERATION_ADD) )
  539. {
  540. rc = roles_cache_create_role_under(&suffix_to_update,entry);
  541. }
  542. if ( entry != NULL )
  543. {
  544. slapi_entry_free(entry);
  545. }
  546. suffix_to_update->notified_entry = NULL;
  547. }
  548. PR_RWLock_Unlock(suffix_to_update->cache_lock);
  549. if ( dn != NULL )
  550. {
  551. slapi_sdn_free(&dn);
  552. }
  553. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "<-- roles_cache_update \n");
  554. return(rc);
  555. }
  556. /* roles_cache_stop
  557. ----------------
  558. XXX the stop_lock of a roles_cache_def
  559. doesn't seem to serve any useful purpose...
  560. */
  561. void roles_cache_stop()
  562. {
  563. roles_cache_def *current_role = NULL;
  564. roles_cache_def *next_role = NULL;
  565. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "--> roles_cache_stop\n");
  566. /* Go through all the roles list and trigger the associated structure */
  567. PR_RWLock_Wlock(global_lock);
  568. current_role = roles_list;
  569. while ( current_role )
  570. {
  571. slapi_lock_mutex(current_role->change_lock);
  572. current_role->keeprunning = 0;
  573. next_role = current_role->next;
  574. slapi_notify_condvar(current_role->something_changed, 1 );
  575. slapi_unlock_mutex(current_role->change_lock);
  576. current_role = next_role;
  577. }
  578. PR_RWLock_Unlock(global_lock);
  579. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "<-- roles_cache_stop\n");
  580. }
  581. /* roles_cache_is_role_entry
  582. -------------------------
  583. Check if the entry is a role
  584. return -1: error in processing
  585. return 0: entry is not a role
  586. return 1: entry is a role
  587. */
  588. static int roles_cache_is_role_entry(struct slapi_entry *entry)
  589. {
  590. Slapi_Attr *pObjclasses = NULL;
  591. Slapi_Value *val = NULL;
  592. char *pObj = NULL;
  593. int index = 0;
  594. int nsroledefinition = 0;
  595. int nsrolesimpleOrComplex = 0;
  596. int nsroletype = 0;
  597. if ( entry == NULL )
  598. {
  599. return(0);
  600. }
  601. if ( slapi_entry_attr_find(entry, "objectclass", &pObjclasses) )
  602. {
  603. slapi_log_error( SLAPI_LOG_FATAL,
  604. ROLES_PLUGIN_SUBSYSTEM,
  605. "roles_cache_is_role_entry: failed to get objectclass from %s\n",slapi_entry_get_dn_const(entry));
  606. return(-1);
  607. }
  608. /* Check out the object classes to see if this was a nsroledefinition */
  609. val = 0;
  610. index = slapi_attr_first_value( pObjclasses, &val );
  611. while(val)
  612. {
  613. const char *p;
  614. int len = 0;
  615. pObj = (char*)slapi_value_get_string(val);
  616. for ( p = pObj, len = 0;
  617. (*p != '\0') && (*p != ' ');
  618. p++, len++ )
  619. {
  620. ; /* NULL */
  621. }
  622. if ( !strncasecmp(pObj, (char*)"nsroledefinition", len) )
  623. {
  624. nsroledefinition = 1;
  625. }
  626. if ( !strncasecmp(pObj, (char*)"nssimpleroledefinition", len) ||
  627. !strncasecmp(pObj, (char*)"nscomplexroledefinition", len) )
  628. {
  629. nsrolesimpleOrComplex = 1;
  630. }
  631. if( !strncasecmp(pObj, (char*)"nsmanagedroledefinition", len) ||
  632. !strncasecmp(pObj, (char*)"nsfilteredroledefinition", len) ||
  633. !strncasecmp(pObj, (char*)"nsnestedroledefinition", len)
  634. )
  635. {
  636. nsroletype = 1;
  637. }
  638. index = slapi_attr_next_value( pObjclasses, index, &val );
  639. }
  640. if ( (nsroledefinition == 0) ||
  641. (nsrolesimpleOrComplex == 0) ||
  642. (nsroletype == 0) )
  643. {
  644. return(0);
  645. }
  646. return(1);
  647. }
  648. /* roles_cache_change_notify
  649. -------------------------
  650. determines if the change effects the cache and if so
  651. signals a rebuild
  652. -- called when modify|modrdn|add|delete operation is performed --
  653. -- called from a postoperation on an entry
  654. XXX this implies that the client may have already received his LDAP response,
  655. but that there will be a delay before he sees the effect in the roles cache.
  656. Should we be doing this processing in a BE_POST_MODIFY postop
  657. which is called _before_ the response goes to the client ?
  658. */
  659. void roles_cache_change_notify(Slapi_PBlock *pb)
  660. {
  661. char *dn = NULL;
  662. struct slapi_entry *e = NULL;
  663. struct slapi_entry *pre = NULL;
  664. struct slapi_entry *entry = NULL;
  665. Slapi_Backend *be = NULL;
  666. Slapi_Operation *pb_operation = NULL;
  667. int operation;
  668. int do_update = 0;
  669. int rc = -1;
  670. slapi_log_error(SLAPI_LOG_PLUGIN,
  671. ROLES_PLUGIN_SUBSYSTEM,
  672. "--> roles_cache_change_notify\n");
  673. /* if the current operation has failed, don't even try the post operation */
  674. slapi_pblock_get( pb, SLAPI_PLUGIN_OPRETURN, &rc );
  675. if ( rc != LDAP_SUCCESS )
  676. {
  677. return;
  678. }
  679. /* Don't update local cache when remote entries are updated */
  680. slapi_pblock_get( pb, SLAPI_BACKEND, &be );
  681. if ( ( be!=NULL ) &&
  682. ( slapi_be_is_flag_set(be,SLAPI_BE_FLAG_REMOTE_DATA)) )
  683. {
  684. return;
  685. }
  686. slapi_pblock_get(pb, SLAPI_TARGET_DN, &dn);
  687. if( dn == NULL )
  688. {
  689. return;
  690. }
  691. slapi_pblock_get (pb, SLAPI_OPERATION, &pb_operation);
  692. operation = operation_get_type(pb_operation);
  693. switch (operation)
  694. {
  695. case SLAPI_OPERATION_DELETE:
  696. slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, &e);
  697. if( e == NULL )
  698. {
  699. return;
  700. }
  701. break;
  702. case SLAPI_OPERATION_ADD:
  703. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &e);
  704. if ( e == NULL )
  705. {
  706. return;
  707. }
  708. break;
  709. case SLAPI_OPERATION_MODIFY:
  710. case SLAPI_OPERATION_MODRDN:
  711. /* those operations are treated the same way and modify is a deletion followed by an addition.
  712. the only point to take care is that dn is the olddn */
  713. operation = SLAPI_OPERATION_MODIFY;
  714. slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, &pre);
  715. if( pre == NULL )
  716. {
  717. return;
  718. }
  719. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &e);
  720. if ( e == NULL )
  721. {
  722. return;
  723. }
  724. break;
  725. default:
  726. slapi_log_error( SLAPI_LOG_FATAL,
  727. ROLES_PLUGIN_SUBSYSTEM,
  728. "roles_cache_change_notify: unknown operation %d\n",operation);
  729. return;
  730. }
  731. if ( operation != SLAPI_OPERATION_MODIFY )
  732. {
  733. if ( roles_cache_is_role_entry(e) != 1 )
  734. {
  735. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "<-- roles_cache_change_notify: not a role entry\n");
  736. return;
  737. }
  738. entry = slapi_entry_dup(e);
  739. do_update = 1;
  740. }
  741. else
  742. {
  743. int is_pre_role = roles_cache_is_role_entry(pre);
  744. int is_post_role = roles_cache_is_role_entry(e);
  745. if ( (is_pre_role==1) && (is_post_role==1) ) /* role definition has changed */
  746. {
  747. entry = slapi_entry_dup(e);
  748. do_update = 1;
  749. }
  750. else if ( is_pre_role == 1 ) /* entry is no more a role */
  751. {
  752. operation = SLAPI_OPERATION_DELETE;
  753. do_update = 1;
  754. }
  755. else if ( is_post_role == 1 ) /* entry is now a role */
  756. {
  757. operation = SLAPI_OPERATION_ADD;
  758. entry = slapi_entry_dup(e);
  759. do_update = 1;
  760. }
  761. else
  762. {
  763. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "<-- roles_cache_change_notify: not a role entry\n");
  764. return;
  765. }
  766. }
  767. if ( do_update )
  768. {
  769. #ifdef moretrace
  770. if ( e != NULL )
  771. {
  772. Slapi_Attr *attr = NULL;
  773. int rc;
  774. /* Get the list of nested roles */
  775. rc = slapi_entry_attr_find(e,ROLE_NESTED_ATTR_NAME,&attr);
  776. if ( (rc == 0) && attr)
  777. {
  778. /* Recurse to get the definition objects for them */
  779. Slapi_Value **va = attr_get_present_values(attr);
  780. int i = 0;
  781. char *string = NULL;
  782. for ( i = 0; va[i] != NULL; i++ )
  783. {
  784. string = (char*)slapi_value_get_string(va[i]);
  785. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "roles_cache_change_notify:%s\n",string);
  786. }
  787. }
  788. }
  789. #endif
  790. Slapi_DN *top_suffix = roles_cache_get_top_suffix(*(be->be_suffix));
  791. if ( top_suffix != NULL )
  792. {
  793. roles_cache_trigger_update_role( slapi_ch_strdup(dn), entry,
  794. top_suffix,
  795. operation);
  796. slapi_sdn_free(&top_suffix);
  797. }
  798. }
  799. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "<-- roles_cache_change_notify\n");
  800. }
  801. /* roles_cache_get_top_suffix
  802. -------------------------
  803. The returned Slapi_DN must be freed with slapi_sdn_free().
  804. */
  805. static Slapi_DN* roles_cache_get_top_suffix(Slapi_DN *suffix)
  806. {
  807. Slapi_DN *current_suffix = NULL;
  808. Slapi_DN parent_suffix;
  809. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "--> roles_cache_get_top_suffix\n");
  810. if ( suffix == NULL )
  811. {
  812. return(NULL);
  813. }
  814. current_suffix = slapi_sdn_new();
  815. slapi_sdn_init(&parent_suffix);
  816. /* we must get the top suffix for that DN */
  817. slapi_sdn_copy(suffix,current_suffix);
  818. while ( !slapi_sdn_isempty(current_suffix) )
  819. {
  820. if ( slapi_is_root_suffix(current_suffix) != 1 )
  821. {
  822. slapi_sdn_get_parent(current_suffix,&parent_suffix);
  823. slapi_sdn_copy(&parent_suffix, current_suffix);
  824. }
  825. else
  826. {
  827. slapi_sdn_done(&parent_suffix);
  828. return(current_suffix);
  829. }
  830. }
  831. /* we should not return that way ... */
  832. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "<-- roles_cache_get_top_suffix\n");
  833. slapi_sdn_done(&parent_suffix);
  834. slapi_sdn_free(&current_suffix);
  835. return(NULL);
  836. }
  837. /* roles_cache_add_roles_from_suffix
  838. -------------------------------
  839. Get the roles entries under the suffix
  840. return 0: OK
  841. return -1: this suffix has no role defined
  842. */
  843. static int roles_cache_add_roles_from_suffix(Slapi_DN *suffix_dn, roles_cache_def *suffix_def)
  844. {
  845. /* Search subtree-level under this entry */
  846. int rc = -1;
  847. roles_cache_search_roles info;
  848. Slapi_PBlock *int_search_pb = NULL;
  849. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "--> roles_cache_add_roles_from_suffix\n");
  850. info.suffix_def = suffix_def;
  851. info.rc = LDAP_NO_SUCH_OBJECT;
  852. /* Get the roles definitions of the given suffix_dn */
  853. int_search_pb = slapi_pblock_new ();
  854. slapi_search_internal_set_pb(int_search_pb,
  855. (char*)slapi_sdn_get_ndn(suffix_dn),
  856. LDAP_SCOPE_SUBTREE,
  857. ROLE_DEFINITION_FILTER,
  858. allUserAttributes,
  859. 0 /* attrsonly */,
  860. NULL /* controls */,
  861. NULL /* uniqueid */,
  862. roles_get_plugin_identity(),
  863. SLAPI_OP_FLAG_NEVER_CHAIN /* actions : get local roles only */ );
  864. slapi_search_internal_callback_pb(int_search_pb,
  865. &info /* callback_data */,
  866. roles_cache_result_cb,
  867. roles_cache_add_entry_cb,
  868. NULL /* referral_callback */);
  869. slapi_pblock_destroy (int_search_pb);
  870. int_search_pb = NULL;
  871. if ( info.rc == LDAP_SUCCESS )
  872. {
  873. rc = 0;
  874. }
  875. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "<-- roles_cache_add_roles_from_suffix\n");
  876. return(rc);
  877. }
  878. /* roles_cache_add_entry_cb
  879. -----------------------
  880. */
  881. static int roles_cache_add_entry_cb(Slapi_Entry* e, void *callback_data)
  882. {
  883. roles_cache_search_roles *info=(roles_cache_search_roles *)callback_data;
  884. roles_cache_def *suffix = info->suffix_def;
  885. roles_cache_create_role_under(&suffix, e);
  886. return(0);
  887. }
  888. /* roles_cache_result_cb
  889. -----------------------
  890. */
  891. static void roles_cache_result_cb( int rc, void *callback_data) {
  892. roles_cache_search_roles *info=(roles_cache_search_roles *)callback_data;
  893. info->rc = rc;
  894. }
  895. /* roles_cache_create_role_under
  896. ----------------------------
  897. Create the avl tree of roles definitions defined in the scope
  898. of the suffix
  899. Return 0: OK
  900. Return -1: fail
  901. */
  902. static int roles_cache_create_role_under(roles_cache_def** roles_cache_suffix, Slapi_Entry *entry)
  903. {
  904. int rc;
  905. role_object *new_role = NULL;
  906. slapi_log_error(SLAPI_LOG_PLUGIN,
  907. ROLES_PLUGIN_SUBSYSTEM, "--> roles_cache_create_role_under: %s - %p\n",
  908. slapi_sdn_get_dn((*roles_cache_suffix)->suffix_dn),
  909. (*roles_cache_suffix)->avl_tree);
  910. rc = roles_cache_create_object_from_entry(entry,&new_role,0);
  911. slapi_log_error(SLAPI_LOG_PLUGIN,
  912. ROLES_PLUGIN_SUBSYSTEM,
  913. "roles_cache_create_role_under: create node for entry %s - rc: %d SUFFIX: %p\n",
  914. slapi_entry_get_dn_const(entry), rc, (*roles_cache_suffix)->avl_tree);
  915. if ( (rc == 0) && new_role)
  916. {
  917. /* Add to the tree where avl_data is a role_object struct */
  918. rc = roles_cache_insert_object(&((*roles_cache_suffix)->avl_tree),new_role);
  919. slapi_log_error(SLAPI_LOG_PLUGIN,
  920. ROLES_PLUGIN_SUBSYSTEM, "roles_cache_create_role_under:%s in tree %p rc: %d\n",
  921. (char*)slapi_sdn_get_ndn(new_role->dn),
  922. (*roles_cache_suffix)->avl_tree, rc);
  923. }
  924. return(rc);
  925. }
  926. /* roles_cache_create_object_from_entry
  927. ------------------------------------
  928. Create a node role_object from the information contained in role_entry
  929. Return 0
  930. Return ENOMEM: fail
  931. Return SLAPI_ROLE_DEFINITION_ERROR: fail
  932. Return SLAPI_ROLE_ERROR_NO_FILTER_SPECIFIED: fail
  933. Return SLAPI_ROLE_ERROR_FILTER_BAD: fail
  934. */
  935. static int roles_cache_create_object_from_entry(Slapi_Entry *role_entry, role_object **result, int hint)
  936. {
  937. int rc = 0;
  938. int type = 0;
  939. role_object *this_role = NULL;
  940. slapi_log_error(SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM,
  941. "--> roles_cache_create_object_from_entry\n");
  942. *result = NULL;
  943. /* Do not allow circular dependencies */
  944. if ( hint > MAX_NESTED_ROLES )
  945. {
  946. char *ndn = NULL;
  947. ndn = slapi_entry_get_ndn( role_entry );
  948. slapi_log_error(
  949. SLAPI_LOG_FATAL,
  950. ROLES_PLUGIN_SUBSYSTEM,
  951. "Maximum roles nesting exceeded (%d), not retrieving roles from entry %s--probable circular definition\n",
  952. MAX_NESTED_ROLES,
  953. ndn);
  954. return (0);
  955. }
  956. /* Create the role cache definition */
  957. this_role = (role_object*)slapi_ch_calloc(1, sizeof(role_object));
  958. if (this_role == NULL )
  959. {
  960. return ENOMEM;
  961. }
  962. /* Check the entry is OK */
  963. /* Determine role type and assign to structure */
  964. /* We determine the role type by reading the objectclass */
  965. if ( roles_cache_is_role_entry(role_entry) == 0 )
  966. {
  967. /* Bad type */
  968. slapi_ch_free((void**)&this_role);
  969. return SLAPI_ROLE_DEFINITION_ERROR;
  970. }
  971. type = roles_cache_determine_class(role_entry);
  972. if (type != 0)
  973. {
  974. this_role->type = type;
  975. }
  976. else
  977. {
  978. /* Bad type */
  979. slapi_ch_free((void**)&this_role);
  980. return SLAPI_ROLE_DEFINITION_ERROR;
  981. }
  982. this_role->dn = slapi_sdn_new();
  983. slapi_sdn_copy(slapi_entry_get_sdn(role_entry),this_role->dn);
  984. /* Depending upon role type, pull out the remaining information we need */
  985. switch (this_role->type)
  986. {
  987. case ROLE_TYPE_MANAGED:
  988. /* Nothing further needed */
  989. break;
  990. case ROLE_TYPE_FILTERED:
  991. {
  992. Slapi_Filter *filter = NULL;
  993. char *filter_attr_value = NULL;
  994. /* Get the filter and retrieve the filter attribute */
  995. filter_attr_value = slapi_entry_attr_get_charptr(role_entry,ROLE_FILTER_ATTR_NAME);
  996. if ( filter_attr_value == NULL )
  997. {
  998. /* Means probably no attribute or no value there */
  999. slapi_ch_free((void**)&this_role);
  1000. return SLAPI_ROLE_ERROR_NO_FILTER_SPECIFIED;
  1001. }
  1002. /* Turn it into a slapi filter object */
  1003. filter = slapi_str2filter(filter_attr_value);
  1004. slapi_ch_free((void**)&filter_attr_value);
  1005. if ( filter == NULL )
  1006. {
  1007. /* An error has occured */
  1008. slapi_ch_free((void**)&this_role);
  1009. return SLAPI_ROLE_ERROR_FILTER_BAD;
  1010. }
  1011. /* Store on the object */
  1012. this_role->filter = filter;
  1013. break;
  1014. }
  1015. case ROLE_TYPE_NESTED:
  1016. {
  1017. Slapi_Attr *attr = NULL;
  1018. /* Get the list of nested roles */
  1019. rc = slapi_entry_attr_find(role_entry,ROLE_NESTED_ATTR_NAME,&attr);
  1020. if ( (rc == 0) && attr)
  1021. {
  1022. /* Recurse to get the definition objects for them */
  1023. Slapi_Value **va = attr_get_present_values(attr);
  1024. int i = 0;
  1025. char *string = NULL;
  1026. Slapi_DN nested_role_dn;
  1027. role_object_nested *nested_role_object = NULL;
  1028. for ( i = 0; va[i] != NULL; i++ )
  1029. {
  1030. string = (char*)slapi_value_get_string(va[i]);
  1031. /* Make a DN from the string */
  1032. slapi_sdn_init_dn_byref(&nested_role_dn,string);
  1033. slapi_log_error(SLAPI_LOG_PLUGIN,
  1034. ROLES_PLUGIN_SUBSYSTEM, "roles_cache_create_object_from_entry: dn %s, nested %s\n",
  1035. (char*)slapi_sdn_get_ndn(this_role->dn),string);
  1036. /* Make a role object nested from the DN */
  1037. rc = roles_cache_object_nested_from_dn(&nested_role_dn,&nested_role_object);
  1038. /* Insert it into the nested list */
  1039. if ( (rc == 0) && nested_role_object)
  1040. {
  1041. /* Add to the tree where avl_data is a role_object_nested struct */
  1042. rc = roles_cache_insert_object_nested(&(this_role->avl_tree),nested_role_object);
  1043. }
  1044. slapi_sdn_done(&nested_role_dn);
  1045. }
  1046. }
  1047. break;
  1048. }
  1049. default:
  1050. slapi_log_error(SLAPI_LOG_FATAL,
  1051. ROLES_PLUGIN_SUBSYSTEM, "wrong role type\n");
  1052. }
  1053. if ( rc == 0 )
  1054. {
  1055. *result = this_role;
  1056. }
  1057. slapi_log_error(SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM,
  1058. "<-- roles_cache_create_object_from_entry\n");
  1059. return rc;
  1060. }
  1061. /* roles_cache_determine_class:
  1062. ----------------------------
  1063. Determine the type of role depending on the objectclass
  1064. Return the type of the role
  1065. */
  1066. static int roles_cache_determine_class(Slapi_Entry *role_entry)
  1067. {
  1068. /* Examine the entry's objectclass attribute */
  1069. int found_managed = 0;
  1070. int found_filtered = 0;
  1071. int found_nested = 0;
  1072. Slapi_Attr *attr= NULL;
  1073. struct berval bv = {0};
  1074. int rc = 0;
  1075. int type = 0;
  1076. slapi_log_error(SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM,
  1077. "--> roles_cache_determine_class\n");
  1078. rc = slapi_entry_attr_find(role_entry,"objectclass",&attr);
  1079. if ( rc != 0 )
  1080. {
  1081. /* No objectclass, definitely an error */
  1082. return 0;
  1083. }
  1084. berval_set_string(&bv,ROLE_OBJECTCLASS_MANAGED);
  1085. rc = slapi_attr_value_find(attr,&bv);
  1086. if ( rc == 0 )
  1087. {
  1088. found_managed = 1;
  1089. type = ROLE_TYPE_MANAGED;
  1090. }
  1091. berval_set_string(&bv,ROLE_OBJECTCLASS_FILTERED);
  1092. rc = slapi_attr_value_find(attr,&bv);
  1093. if ( rc == 0 )
  1094. {
  1095. found_filtered = 1;
  1096. type = ROLE_TYPE_FILTERED;
  1097. }
  1098. berval_set_string(&bv,ROLE_OBJECTCLASS_NESTED);
  1099. rc = slapi_attr_value_find(attr,&bv);
  1100. if ( rc == 0 )
  1101. {
  1102. found_nested = 1;
  1103. type = ROLE_TYPE_NESTED;
  1104. }
  1105. if ( (found_managed + found_nested + found_filtered) > 1 )
  1106. {
  1107. /* Means some goofball configured a role definition which is trying to be more than one different type. error. */
  1108. return 0;
  1109. }
  1110. if ( (found_managed + found_nested + found_filtered) == 0)
  1111. {
  1112. /* Means this entry isn't any of the role types we handle. error. */
  1113. return 0;
  1114. }
  1115. slapi_log_error(SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM,
  1116. "<-- roles_cache_determine_class\n");
  1117. /* Return the appropriate type ordinal */
  1118. return type;
  1119. }
  1120. /* roles_cache_node_cmp:
  1121. ---------------------
  1122. Comparison function to add a new node in the avl tree (avl_data is of type role_object)
  1123. */
  1124. static int roles_cache_node_cmp( caddr_t d1, caddr_t d2 )
  1125. {
  1126. role_object *role_to_insert = (role_object*)d1;
  1127. role_object *current_role = (role_object*)d2;
  1128. /* role_to_insert and current_role are never NULL in that context */
  1129. slapi_log_error(SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM,
  1130. "roles_cache_node_cmp\n");
  1131. return (slapi_sdn_compare((Slapi_DN *)role_to_insert->dn, (Slapi_DN *)current_role->dn));
  1132. }
  1133. /* roles_cache_insert_object:
  1134. --------------------------
  1135. Insert a new node in the avl tree of a specific suffix
  1136. */
  1137. static int roles_cache_insert_object(Avlnode **tree, role_object *object)
  1138. {
  1139. slapi_log_error(SLAPI_LOG_PLUGIN,
  1140. ROLES_PLUGIN_SUBSYSTEM, "roles_cache_insert_object: %s in tree %p\n",
  1141. (char*)slapi_sdn_get_ndn(object->dn),
  1142. *tree);
  1143. return (avl_insert(tree, (caddr_t)object, roles_cache_node_cmp, avl_dup_error));
  1144. }
  1145. /* roles_cache_node_nested_cmp:
  1146. ----------------------------
  1147. Comparison function to add a new node in the avl tree
  1148. */
  1149. static int roles_cache_node_nested_cmp( caddr_t d1, caddr_t d2 )
  1150. {
  1151. role_object_nested *role_to_insert = (role_object_nested*)d1;
  1152. role_object_nested *current_role = (role_object_nested*)d2;
  1153. /* role_to_insert and current_role are never NULL in that context */
  1154. slapi_log_error(SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM,
  1155. "roles_cache_node_nested_cmp\n");
  1156. return slapi_sdn_compare(role_to_insert->dn, current_role->dn);
  1157. }
  1158. /* roles_cache_insert_object_nested:
  1159. ---------------------------------
  1160. Insert a new node in the avl tree of a specific suffix
  1161. */
  1162. static int roles_cache_insert_object_nested(Avlnode **tree, role_object_nested *object)
  1163. {
  1164. slapi_log_error(SLAPI_LOG_PLUGIN,
  1165. ROLES_PLUGIN_SUBSYSTEM, "roles_cache_insert_object_nested: %s in tree %p: \n",
  1166. (char*)slapi_sdn_get_ndn(object->dn), *tree);
  1167. return (avl_insert(tree, (caddr_t)object, roles_cache_node_nested_cmp, avl_dup_error));
  1168. }
  1169. /* roles_cache_object_nested_from_dn
  1170. ----------------------------------
  1171. Get the role associated to an entry DN
  1172. Return 0: OK
  1173. Return ENOMEM: fail
  1174. */
  1175. static int roles_cache_object_nested_from_dn(Slapi_DN *role_dn, role_object_nested **result)
  1176. {
  1177. role_object_nested *nested_role = NULL;
  1178. slapi_log_error(SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM,
  1179. "--> roles_cache_object_nested_from_dn\n");
  1180. *result = NULL;
  1181. /* Create the role cache definition */
  1182. nested_role = (role_object_nested*)slapi_ch_calloc(1, sizeof(role_object_nested));
  1183. if (nested_role == NULL )
  1184. {
  1185. return ENOMEM;
  1186. }
  1187. nested_role->dn = slapi_sdn_new();
  1188. slapi_sdn_copy(role_dn,nested_role->dn);
  1189. *result = nested_role;
  1190. slapi_log_error(SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM,
  1191. "<-- roles_cache_object_nested_from_dn\n");
  1192. return 0;
  1193. }
  1194. /* roles_cache_listroles
  1195. --------------------
  1196. Lists all the roles an entry posesses
  1197. return_values = 0 means that we don't need the nsrole values
  1198. return_values = 1 means that we need the nsrole values
  1199. Return 0: the entry has nsrole
  1200. Return -1: the entry has no nsrole
  1201. */
  1202. int roles_cache_listroles(Slapi_Entry *entry, int return_values, Slapi_ValueSet **valueset_out)
  1203. {
  1204. return roles_cache_listroles_ext(NULL, entry, return_values, valueset_out);
  1205. }
  1206. int roles_cache_listroles_ext(vattr_context *c, Slapi_Entry *entry, int return_values, Slapi_ValueSet **valueset_out)
  1207. {
  1208. roles_cache_def *roles_cache = NULL;
  1209. int rc = 0;
  1210. roles_cache_build_result arg;
  1211. Slapi_Backend *backend = NULL;
  1212. slapi_log_error(SLAPI_LOG_PLUGIN,
  1213. ROLES_PLUGIN_SUBSYSTEM, "--> roles_cache_listroles\n");
  1214. backend = slapi_mapping_tree_find_backend_for_sdn(slapi_entry_get_sdn(entry));
  1215. if ( (backend != NULL) && slapi_be_is_flag_set(backend,SLAPI_BE_FLAG_REMOTE_DATA) )
  1216. {
  1217. /* the entry is not local, so don't return anything */
  1218. return (-1);
  1219. }
  1220. if ( return_values )
  1221. {
  1222. *valueset_out = (Slapi_ValueSet*)slapi_ch_calloc(1,sizeof(Slapi_ValueSet));
  1223. slapi_valueset_init(*valueset_out);
  1224. }
  1225. /* First get a list of all the in-scope roles */
  1226. /* XXX really need a mutex for this read operation ? */
  1227. PR_RWLock_Rlock(global_lock);
  1228. rc = roles_cache_find_roles_in_suffix( slapi_entry_get_sdn(entry),&roles_cache);
  1229. PR_RWLock_Unlock(global_lock);
  1230. /* Traverse the tree checking if the entry has any of the roles */
  1231. if ( roles_cache != NULL )
  1232. {
  1233. if ( roles_cache->avl_tree )
  1234. {
  1235. arg.nsrole_values = valueset_out;
  1236. arg.need_value = return_values;
  1237. arg.requested_entry = entry;
  1238. arg.has_value = 0;
  1239. arg.context = c;
  1240. /* XXX really need a mutex for this read operation ? */
  1241. PR_RWLock_Rlock(roles_cache->cache_lock);
  1242. avl_apply(roles_cache->avl_tree, (IFP)roles_cache_build_nsrole, &arg, -1, AVL_INORDER);
  1243. PR_RWLock_Unlock(roles_cache->cache_lock);
  1244. if( !arg.has_value )
  1245. {
  1246. if ( return_values )
  1247. {
  1248. slapi_valueset_free(*valueset_out);
  1249. *valueset_out = NULL;
  1250. }
  1251. rc = -1;
  1252. }
  1253. /* Free the list (we already did that) */
  1254. }
  1255. else
  1256. {
  1257. if ( return_values )
  1258. {
  1259. slapi_valueset_free(*valueset_out);
  1260. *valueset_out = NULL;
  1261. }
  1262. rc = -1;
  1263. }
  1264. }
  1265. else
  1266. {
  1267. /* no roles associated */
  1268. rc = -1;
  1269. }
  1270. slapi_log_error(SLAPI_LOG_PLUGIN,
  1271. ROLES_PLUGIN_SUBSYSTEM, "<-- roles_cache_listroles\n");
  1272. return rc;
  1273. }
  1274. /* roles_cache_build_nsrole
  1275. ------------------------
  1276. Traverse the tree containing roles definitions for a suffix and for each
  1277. one of them, check wether the entry is a member of it or not
  1278. For ones which check out positive, we add their DN to the value
  1279. always return 0 to allow to trverse all the tree
  1280. */
  1281. static int roles_cache_build_nsrole( caddr_t data, caddr_t arg )
  1282. {
  1283. Slapi_Value *value = NULL;
  1284. roles_cache_build_result *result = (roles_cache_build_result*)arg;
  1285. role_object *this_role = (role_object*)data;
  1286. roles_cache_search_in_nested get_nsrole;
  1287. /* Return a value different from the stop flag to be able
  1288. to go through all the tree */
  1289. int rc = 0;
  1290. int tmprc = 0;
  1291. slapi_log_error(SLAPI_LOG_PLUGIN,
  1292. ROLES_PLUGIN_SUBSYSTEM, "--> roles_cache_build_nsrole: role %s\n",
  1293. (char*) slapi_sdn_get_ndn(this_role->dn));
  1294. value = slapi_value_new_string("");
  1295. get_nsrole.is_entry_member_of = result->requested_entry;
  1296. get_nsrole.present = 0;
  1297. get_nsrole.hint = 0;
  1298. tmprc = roles_is_entry_member_of_object_ext(result->context, (caddr_t)this_role, (caddr_t)&get_nsrole);
  1299. if (SLAPI_VIRTUALATTRS_LOOP_DETECTED == tmprc)
  1300. {
  1301. /* all we want to detect and return is loop/stack overflow */
  1302. rc = tmprc;
  1303. }
  1304. /* If so, add its DN to the attribute */
  1305. if (get_nsrole.present)
  1306. {
  1307. result->has_value = 1;
  1308. if ( result->need_value )
  1309. {
  1310. slapi_value_set_string(value,(char*) slapi_sdn_get_ndn(this_role->dn));
  1311. slapi_valueset_add_value(*(result->nsrole_values),value);
  1312. }
  1313. else
  1314. {
  1315. /* we don't need the value but we already know there is one nsrole.
  1316. stop the traversal
  1317. */
  1318. rc = -1;
  1319. }
  1320. }
  1321. slapi_value_free(&value);
  1322. slapi_log_error(SLAPI_LOG_PLUGIN,
  1323. ROLES_PLUGIN_SUBSYSTEM, "<-- roles_cache_build_nsrole\n");
  1324. return rc;
  1325. }
  1326. /* roles_check
  1327. -----------
  1328. Checks if an entry has a presented role, assuming that we've already verified
  1329. that
  1330. the role both exists and is in scope
  1331. return 0: no processing error
  1332. return -1: error
  1333. */
  1334. int roles_check(Slapi_Entry *entry_to_check, Slapi_DN *role_dn, int *present)
  1335. {
  1336. roles_cache_def *roles_cache = NULL;
  1337. role_object *this_role = NULL;
  1338. roles_cache_search_in_nested get_nsrole;
  1339. int rc = 0;
  1340. slapi_log_error(SLAPI_LOG_PLUGIN,
  1341. ROLES_PLUGIN_SUBSYSTEM, "--> roles_check\n");
  1342. *present = 0;
  1343. PR_RWLock_Rlock(global_lock);
  1344. if ( roles_cache_find_roles_in_suffix(slapi_entry_get_sdn(entry_to_check),
  1345. &roles_cache) != 0 )
  1346. {
  1347. PR_RWLock_Unlock(global_lock);
  1348. return -1;
  1349. }
  1350. PR_RWLock_Unlock(global_lock);
  1351. this_role = (role_object *)avl_find(roles_cache->avl_tree, role_dn, (IFP)roles_cache_find_node);
  1352. /* MAB: For some reason the assumption made by this function (the role exists and is in scope)
  1353. * does not seem to be true... this_role might be NULL after the avl_find call (is the avl_tree
  1354. * broken? Anyway, this is crashing the 5.1 server on 29-Aug-01, so I am applying the following patch
  1355. * to avoid the crash inside roles_is_entry_member_of_object */
  1356. /* Begin patch */
  1357. if (!this_role) {
  1358. /* Assume that the entry is not member of the role (*present=0) and leave... */
  1359. return rc;
  1360. }
  1361. /* End patch */
  1362. get_nsrole.is_entry_member_of = entry_to_check;
  1363. get_nsrole.present = 0;
  1364. get_nsrole.hint = 0;
  1365. roles_is_entry_member_of_object((caddr_t)this_role, (caddr_t)&get_nsrole);
  1366. *present = get_nsrole.present;
  1367. slapi_log_error(SLAPI_LOG_PLUGIN,
  1368. ROLES_PLUGIN_SUBSYSTEM, "<-- roles_check\n");
  1369. return rc;
  1370. }
  1371. /* roles_cache_find_node:
  1372. ---------------------
  1373. Comparison function to add a new node in the avl tree
  1374. */
  1375. static int roles_cache_find_node( caddr_t d1, caddr_t d2 )
  1376. {
  1377. Slapi_DN *data = (Slapi_DN *)d1;
  1378. role_object *role= (role_object *)d2;
  1379. /* role is not NULL in that context */
  1380. slapi_log_error(SLAPI_LOG_PLUGIN,
  1381. ROLES_PLUGIN_SUBSYSTEM, "roles_cache_find_node: %s %s\n",
  1382. slapi_sdn_get_dn(data), slapi_sdn_get_dn(role->dn));
  1383. return (slapi_sdn_compare(data, (Slapi_DN *)role->dn));
  1384. }
  1385. /* roles_cache_find_roles_in_suffix
  1386. -------------------------------
  1387. Find all the roles in scope to an entry
  1388. */
  1389. static int roles_cache_find_roles_in_suffix(Slapi_DN *target_entry_dn, roles_cache_def **list_of_roles)
  1390. {
  1391. int rc = -1;
  1392. Slapi_Backend *backend = NULL;
  1393. slapi_log_error(SLAPI_LOG_PLUGIN,
  1394. ROLES_PLUGIN_SUBSYSTEM, "--> roles_cache_find_roles_in_suffix\n");
  1395. *list_of_roles = NULL;
  1396. backend = slapi_mapping_tree_find_backend_for_sdn(target_entry_dn);
  1397. if ( (backend != NULL) && !slapi_be_is_flag_set(backend,SLAPI_BE_FLAG_REMOTE_DATA) )
  1398. {
  1399. Slapi_DN *suffix = roles_cache_get_top_suffix(*(backend->be_suffix));
  1400. roles_cache_def *current_role = roles_list;
  1401. /* Go through all the roles list and trigger the associated structure */
  1402. while ( (current_role != NULL) && (suffix != NULL) )
  1403. {
  1404. if ( slapi_sdn_compare(current_role->suffix_dn, suffix) == 0 )
  1405. {
  1406. *list_of_roles = current_role;
  1407. /* OK, we have found one */
  1408. slapi_sdn_free(&suffix);
  1409. return 0;
  1410. }
  1411. else
  1412. {
  1413. current_role = current_role->next;
  1414. }
  1415. }
  1416. if ( suffix != NULL )
  1417. {
  1418. slapi_sdn_free(&suffix);
  1419. }
  1420. /* If we got out that way, means that we didn't have find
  1421. roles definitions for that suffix */
  1422. return rc;
  1423. }
  1424. slapi_log_error(SLAPI_LOG_PLUGIN,
  1425. ROLES_PLUGIN_SUBSYSTEM, "<-- roles_cache_find_roles_in_suffix\n");
  1426. return rc;
  1427. }
  1428. /* roles_is_entry_member_of_object
  1429. --------------------------------
  1430. Check if the entry is part of a role defined in its suffix
  1431. return 0: ok
  1432. return 1: fail
  1433. -> to check the presence, see present
  1434. */
  1435. static int roles_is_entry_member_of_object(caddr_t data, caddr_t argument )
  1436. {
  1437. return roles_is_entry_member_of_object_ext(NULL, data, argument );
  1438. }
  1439. static int roles_is_entry_member_of_object_ext(vattr_context *c, caddr_t data, caddr_t argument )
  1440. {
  1441. int rc = -1;
  1442. roles_cache_search_in_nested *get_nsrole = (roles_cache_search_in_nested*)argument;
  1443. role_object *this_role = (role_object*)data;
  1444. Slapi_Entry *entry_to_check = get_nsrole->is_entry_member_of;
  1445. slapi_log_error(SLAPI_LOG_PLUGIN,
  1446. ROLES_PLUGIN_SUBSYSTEM, "--> roles_is_entry_member_of_object\n");
  1447. if (!roles_is_inscope(entry_to_check, this_role->dn))
  1448. {
  1449. slapi_log_error(SLAPI_LOG_PLUGIN,
  1450. ROLES_PLUGIN_SUBSYSTEM, "roles_is_entry_member_of_object-> entry not in scope of role\n");
  1451. return rc;
  1452. }
  1453. if ( this_role != NULL )
  1454. {
  1455. /* Determine the role type */
  1456. switch (this_role->type)
  1457. {
  1458. case ROLE_TYPE_MANAGED:
  1459. rc = roles_check_managed(entry_to_check,this_role,&get_nsrole->present);
  1460. break;
  1461. case ROLE_TYPE_FILTERED:
  1462. rc = roles_check_filtered(c, entry_to_check,this_role,&get_nsrole->present);
  1463. break;
  1464. case ROLE_TYPE_NESTED:
  1465. {
  1466. /* Go through the tree of the nested DNs */
  1467. get_nsrole->hint++;
  1468. avl_apply(this_role->avl_tree, (IFP)roles_check_nested, get_nsrole, 0, AVL_INORDER);
  1469. get_nsrole->hint--;
  1470. /* kexcoff?? */
  1471. rc = get_nsrole->present;
  1472. break;
  1473. }
  1474. default:
  1475. slapi_log_error(SLAPI_LOG_FATAL,
  1476. ROLES_PLUGIN_SUBSYSTEM, "roles_is_entry_member_of_object-> invalid role type\n");
  1477. }
  1478. }
  1479. slapi_log_error(SLAPI_LOG_PLUGIN,
  1480. ROLES_PLUGIN_SUBSYSTEM, "<-- roles_is_entry_member_of_object\n");
  1481. return rc;
  1482. }
  1483. /* roles_check_managed
  1484. -------------------------
  1485. Check a managed role: we just need to check the content of the entry's nsRoleDN attribute
  1486. against the role DN
  1487. return 0: ok
  1488. return 1: fail
  1489. -> to check the presence, see present
  1490. */
  1491. static int roles_check_managed(Slapi_Entry *entry_to_check, role_object *role, int *present)
  1492. {
  1493. int rc = 0;
  1494. Slapi_Attr *attr = NULL;
  1495. slapi_log_error(SLAPI_LOG_PLUGIN,
  1496. ROLES_PLUGIN_SUBSYSTEM, "--> roles_check_managed\n");
  1497. /* Get the attribute */
  1498. rc = slapi_entry_attr_find(entry_to_check,ROLE_MANAGED_ATTR_NAME,&attr);
  1499. if ( rc == 0)
  1500. {
  1501. struct berval bv = {0};
  1502. char *dn_string = NULL;
  1503. /* Check content against the presented DN */
  1504. /* We assume that this function handles normalization and so on */
  1505. dn_string = (char*) slapi_sdn_get_ndn(role->dn);
  1506. berval_set_string(&bv,dn_string);
  1507. rc = slapi_attr_value_find(attr,&bv);
  1508. if ( rc == 0 )
  1509. {
  1510. *present = 1;
  1511. }
  1512. }
  1513. slapi_log_error(SLAPI_LOG_PLUGIN,
  1514. ROLES_PLUGIN_SUBSYSTEM,
  1515. "<-- roles_check_managed: entry %s role %s present %d\n",
  1516. slapi_entry_get_dn_const(entry_to_check),(char*)slapi_sdn_get_ndn(role->dn),*present);
  1517. return rc;
  1518. }
  1519. /* roles_check_filtered
  1520. --------------------------
  1521. Check a filtered role: call slapi_filter_test here on the entry
  1522. and the filter from the role object
  1523. return 0: ok
  1524. return 1: fail
  1525. -> to check the presence, see present
  1526. */
  1527. static int roles_check_filtered(vattr_context *c, Slapi_Entry *entry_to_check, role_object *role, int *present)
  1528. {
  1529. int rc = 0;
  1530. slapi_log_error(SLAPI_LOG_PLUGIN,
  1531. ROLES_PLUGIN_SUBSYSTEM, "--> roles_check_filtered\n");
  1532. rc = slapi_vattr_filter_test_ext(slapi_vattr_get_pblock_from_context(c),
  1533. entry_to_check, role->filter, 0, 0);
  1534. if ( rc == 0 )
  1535. {
  1536. *present = 1;
  1537. }
  1538. slapi_log_error(SLAPI_LOG_PLUGIN,
  1539. ROLES_PLUGIN_SUBSYSTEM,
  1540. "<-- roles_check_filtered: entry %s role %s present %d\n",
  1541. slapi_entry_get_dn_const(entry_to_check),(char*)slapi_sdn_get_ndn(role->dn),*present);
  1542. return rc;
  1543. }
  1544. /* roles_check_nested
  1545. ------------------------
  1546. Check a nested role
  1547. return 0: ok
  1548. return -1: fail
  1549. -> to check the presence, see present
  1550. */
  1551. static int roles_check_nested(caddr_t data, caddr_t arg)
  1552. {
  1553. roles_cache_search_in_nested *get_nsrole = (roles_cache_search_in_nested*)arg;
  1554. int rc = -1;
  1555. role_object_nested *current_nested_role = (role_object_nested*)data;
  1556. /* do not allow circular dependencies, the cheap and easy way */
  1557. if( get_nsrole->hint > MAX_NESTED_ROLES)
  1558. {
  1559. char *ndn = NULL;
  1560. ndn = slapi_entry_get_ndn( get_nsrole->is_entry_member_of );
  1561. slapi_log_error(SLAPI_LOG_FATAL,
  1562. ROLES_PLUGIN_SUBSYSTEM,
  1563. "Maximum roles nesting exceeded (max %d current %d), not checking roles in entry %s--probable circular definition\n",
  1564. MAX_NESTED_ROLES,
  1565. get_nsrole->hint,
  1566. ndn);
  1567. /* Stop traversal value */
  1568. return 0;
  1569. }
  1570. /* Here we traverse the list of nested roles, calling the appropriate
  1571. evaluation function for those in turn */
  1572. if (current_nested_role)
  1573. {
  1574. roles_cache_def *roles_cache = NULL;
  1575. role_object *this_role = NULL;
  1576. slapi_log_error(SLAPI_LOG_PLUGIN,
  1577. ROLES_PLUGIN_SUBSYSTEM,
  1578. "-->roles_check_nested: entry %s role %s present %d\n",
  1579. slapi_entry_get_dn_const(get_nsrole->is_entry_member_of),
  1580. (char*)slapi_sdn_get_ndn(current_nested_role->dn),
  1581. get_nsrole->present);
  1582. if ( roles_cache_find_roles_in_suffix(current_nested_role->dn,
  1583. &roles_cache) != 0 )
  1584. {
  1585. return rc;
  1586. }
  1587. if ( slapi_is_loglevel_set(SLAPI_LOG_PLUGIN) )
  1588. {
  1589. avl_apply(roles_cache->avl_tree, (IFP)roles_cache_dump, &rc, -1, AVL_INORDER);
  1590. }
  1591. this_role = (role_object *)avl_find(roles_cache->avl_tree,
  1592. current_nested_role->dn,
  1593. (IFP)roles_cache_find_node);
  1594. if ( this_role == NULL )
  1595. {
  1596. /* the nested role doesn't exist */
  1597. slapi_log_error(SLAPI_LOG_FATAL,
  1598. ROLES_PLUGIN_SUBSYSTEM,
  1599. "The nested role %s doesn't exist\n",
  1600. (char*)slapi_sdn_get_ndn(current_nested_role->dn));
  1601. return rc;
  1602. }
  1603. /* get the role_object data associated to that dn */
  1604. if ( roles_is_inscope(get_nsrole->is_entry_member_of, this_role->dn) )
  1605. {
  1606. /* The list of nested roles is contained in the role definition */
  1607. roles_is_entry_member_of_object((caddr_t)this_role, (caddr_t)get_nsrole);
  1608. if ( get_nsrole->present == 1 )
  1609. {
  1610. return 0;
  1611. }
  1612. }
  1613. }
  1614. slapi_log_error(SLAPI_LOG_PLUGIN,
  1615. ROLES_PLUGIN_SUBSYSTEM, "<-- roles_check_nested\n");
  1616. return rc;
  1617. }
  1618. /* roles_is_inscope
  1619. ----------------------
  1620. Tells us if a presented role is in scope with respect to the presented entry
  1621. */
  1622. static int roles_is_inscope(Slapi_Entry *entry_to_check, Slapi_DN *role_dn)
  1623. {
  1624. int rc;
  1625. Slapi_DN role_parent;
  1626. slapi_log_error(SLAPI_LOG_PLUGIN,
  1627. ROLES_PLUGIN_SUBSYSTEM, "--> roles_is_inscope\n");
  1628. slapi_sdn_init(&role_parent);
  1629. slapi_sdn_get_parent(role_dn,&role_parent);
  1630. rc = slapi_sdn_scope_test(slapi_entry_get_sdn( entry_to_check ),
  1631. &role_parent,
  1632. LDAP_SCOPE_SUBTREE);
  1633. /* we need to check whether the entry would be returned by a view in scope */
  1634. if(!rc && views_api)
  1635. {
  1636. rc = views_entry_exists(views_api, (char*)slapi_sdn_get_ndn(&role_parent), entry_to_check);
  1637. }
  1638. slapi_sdn_done(&role_parent);
  1639. slapi_log_error(SLAPI_LOG_PLUGIN,
  1640. ROLES_PLUGIN_SUBSYSTEM, "<-- roles_is_inscope: entry %s role %s result %d\n",
  1641. slapi_entry_get_dn_const(entry_to_check),(char*)slapi_sdn_get_ndn(role_dn), rc);
  1642. return (rc);
  1643. }
  1644. static void berval_set_string(struct berval *bv, const char* string)
  1645. {
  1646. bv->bv_len= strlen(string);
  1647. bv->bv_val= (void*)string; /* We cast away the const, but we're not going to change anything
  1648. */
  1649. }
  1650. /* roles_cache_role_def_delete
  1651. ----------------------------
  1652. */
  1653. static void roles_cache_role_def_delete(roles_cache_def *role_def)
  1654. {
  1655. roles_cache_def *current = roles_list;
  1656. roles_cache_def *previous = NULL;
  1657. slapi_log_error(SLAPI_LOG_PLUGIN,
  1658. ROLES_PLUGIN_SUBSYSTEM, "--> roles_cache_role_def_delete\n");
  1659. while ( current!= NULL )
  1660. {
  1661. if ( slapi_sdn_compare(current->suffix_dn, role_def->suffix_dn) == 0 )
  1662. {
  1663. if ( previous== NULL )
  1664. {
  1665. roles_list = current->next;
  1666. }
  1667. else
  1668. {
  1669. previous->next = current->next;
  1670. }
  1671. slapi_lock_mutex(role_def->change_lock);
  1672. role_def->keeprunning = 0;
  1673. slapi_notify_condvar(role_def->something_changed, 1 );
  1674. slapi_unlock_mutex(role_def->change_lock);
  1675. break;
  1676. }
  1677. else
  1678. {
  1679. previous = current;
  1680. current = current->next;
  1681. }
  1682. }
  1683. slapi_log_error(SLAPI_LOG_PLUGIN,
  1684. ROLES_PLUGIN_SUBSYSTEM, "<-- roles_cache_role_def_delete\n");
  1685. }
  1686. /* roles_cache_role_def_free
  1687. ----------------------------
  1688. */
  1689. static void roles_cache_role_def_free(roles_cache_def *role_def)
  1690. {
  1691. slapi_log_error(SLAPI_LOG_PLUGIN,
  1692. ROLES_PLUGIN_SUBSYSTEM, "--> roles_cache_role_def_free\n");
  1693. if ( role_def == NULL )
  1694. {
  1695. return;
  1696. }
  1697. slapi_lock_mutex(role_def->stop_lock);
  1698. avl_free(role_def->avl_tree, (IFP)roles_cache_role_object_free);
  1699. slapi_sdn_free(&(role_def->suffix_dn));
  1700. PR_DestroyRWLock(role_def->cache_lock);
  1701. role_def->cache_lock = NULL;
  1702. slapi_destroy_mutex(role_def->change_lock);
  1703. role_def->change_lock = NULL;
  1704. slapi_destroy_condvar(role_def->something_changed);
  1705. slapi_destroy_mutex(role_def->create_lock);
  1706. role_def->create_lock = NULL;
  1707. slapi_destroy_condvar(role_def->suffix_created);
  1708. slapi_ch_free((void**)&role_def->notified_dn);
  1709. if ( role_def->notified_entry != NULL )
  1710. {
  1711. slapi_entry_free(role_def->notified_entry);
  1712. }
  1713. slapi_unlock_mutex(role_def->stop_lock);
  1714. slapi_destroy_mutex(role_def->stop_lock);
  1715. role_def->stop_lock = NULL;
  1716. slapi_ch_free((void**)&role_def);
  1717. slapi_log_error(SLAPI_LOG_PLUGIN,
  1718. ROLES_PLUGIN_SUBSYSTEM, "<-- roles_cache_role_def_free\n");
  1719. }
  1720. /* roles_cache_role_object_free
  1721. ----------------------------
  1722. */
  1723. static void roles_cache_role_object_free(role_object *this_role)
  1724. {
  1725. slapi_log_error(SLAPI_LOG_PLUGIN,
  1726. ROLES_PLUGIN_SUBSYSTEM, "--> roles_cache_role_object_free\n");
  1727. if ( this_role == NULL )
  1728. {
  1729. return;
  1730. }
  1731. switch (this_role->type)
  1732. {
  1733. case ROLE_TYPE_MANAGED:
  1734. /* Nothing further needed */
  1735. break;
  1736. case ROLE_TYPE_FILTERED:
  1737. /* Free the filter */
  1738. if (this_role->filter)
  1739. {
  1740. slapi_filter_free(this_role->filter,1);
  1741. this_role->filter = NULL;
  1742. }
  1743. break;
  1744. case ROLE_TYPE_NESTED:
  1745. /* Free the list of nested roles */
  1746. {
  1747. avl_free(this_role->avl_tree, roles_cache_role_object_nested_free);
  1748. }
  1749. break;
  1750. }
  1751. slapi_sdn_free(&this_role->dn);
  1752. /* Free the object */
  1753. slapi_ch_free((void**)&this_role);
  1754. slapi_log_error(SLAPI_LOG_PLUGIN,
  1755. ROLES_PLUGIN_SUBSYSTEM, "<-- roles_cache_role_object_free\n");
  1756. }
  1757. /* roles_cache_role_object_nested_free
  1758. ------------------------------------
  1759. */
  1760. static void roles_cache_role_object_nested_free(role_object_nested *this_role)
  1761. {
  1762. slapi_log_error(SLAPI_LOG_PLUGIN,
  1763. ROLES_PLUGIN_SUBSYSTEM, "--> roles_cache_role_object_nested_free\n");
  1764. if ( this_role == NULL )
  1765. {
  1766. return;
  1767. }
  1768. slapi_sdn_free(&this_role->dn);
  1769. /* Free the object */
  1770. slapi_ch_free((void**)&this_role);
  1771. slapi_log_error(SLAPI_LOG_PLUGIN,
  1772. ROLES_PLUGIN_SUBSYSTEM, "<-- roles_cache_role_object_nested_free\n");
  1773. }
  1774. static int roles_cache_dump( caddr_t data, caddr_t arg )
  1775. {
  1776. role_object *this_role = (role_object*)data;
  1777. slapi_log_error(SLAPI_LOG_PLUGIN,
  1778. ROLES_PLUGIN_SUBSYSTEM, "roles_cache_dump: %p - %s - %p\n",
  1779. this_role, (char*)slapi_sdn_get_ndn(this_role->dn), this_role->avl_tree);
  1780. return 0;
  1781. }