aclgroup.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. #include "acl.h"
  13. /***************************************************************************
  14. *
  15. * This module deals with the global user group cache.
  16. *
  17. * A LRU queue mechanism is used to maintain the groups the user currently in.
  18. * At this moment the QUEUE is invalidated if there is a group change. A better
  19. * way would have been to invalidate only the one which are effected.
  20. * However to accomplish that will require quite a bit of work which may not be
  21. * cost-efftive.
  22. **************************************************************************/
  23. static aclGroupCache *aclUserGroups;
  24. #define ACL_MAXCACHE_USERGROUPS 200
  25. #define ACLG_LOCK_GROUPCACHE_READ() slapi_rwlock_rdlock ( aclUserGroups->aclg_rwlock )
  26. #define ACLG_LOCK_GROUPCACHE_WRITE() slapi_rwlock_wrlock ( aclUserGroups->aclg_rwlock )
  27. #define ACLG_ULOCK_GROUPCACHE_WRITE() slapi_rwlock_unlock ( aclUserGroups->aclg_rwlock )
  28. #define ACLG_ULOCK_GROUPCACHE_READ() slapi_rwlock_unlock ( aclUserGroups->aclg_rwlock )
  29. static void __aclg__delete_userGroup ( aclUserGroup *u_group );
  30. int
  31. aclgroup_init ()
  32. {
  33. aclUserGroups = ( aclGroupCache * ) slapi_ch_calloc (1, sizeof ( aclGroupCache ) );
  34. if ( NULL == (aclUserGroups->aclg_rwlock = slapi_new_rwlock())) {
  35. slapi_log_error(SLAPI_LOG_ERR, plugin_name, "Unable to allocate RWLOCK for group cache\n");
  36. return 1;
  37. }
  38. return 0;
  39. }
  40. void
  41. aclgroup_free()
  42. {
  43. slapi_destroy_rwlock(aclUserGroups->aclg_rwlock);
  44. slapi_ch_free((void **)&aclUserGroups);
  45. }
  46. /*
  47. * aclg_init_userGroup
  48. *
  49. * Go thru the Global Group CACHE and see if we have group information for
  50. * the user. The user's group cache is invalidated when a group is modified
  51. * (in which case ALL usergroups are invalidated) or when the user's entry
  52. * is modified in which case just his is invalidated.
  53. *
  54. * We need to scan the whole cache looking for a valid entry that matches
  55. * this user. If we find invalid entries along the way.
  56. *
  57. * If we don't have anything it's fine. we will allocate a space when we
  58. * need it i.e during the group evaluation.
  59. *
  60. * Inputs:
  61. * struct acl_pblock - ACL private block
  62. * char *dn - the client's dn
  63. * int got_lock - 1: already obtained WRITE Lock
  64. * - 0: Nope; get one
  65. * Returns:
  66. * None.
  67. */
  68. void
  69. aclg_init_userGroup ( struct acl_pblock *aclpb, const char *n_dn , int got_lock )
  70. {
  71. aclUserGroup *u_group = NULL;
  72. aclUserGroup *next_ugroup = NULL;
  73. aclUserGroup *p_group, *n_group;
  74. int found = 0;
  75. /* Check for Anonymous user */
  76. if ( n_dn && *n_dn == '\0') return;
  77. if ( !got_lock ) ACLG_LOCK_GROUPCACHE_WRITE ();
  78. u_group = aclUserGroups->aclg_first;
  79. aclpb->aclpb_groupinfo = NULL;
  80. while ( u_group != NULL ) {
  81. next_ugroup = u_group->aclug_next;
  82. if ( aclUserGroups->aclg_signature != u_group->aclug_signature) {
  83. /*
  84. * This means that this usergroup is no longer valid and
  85. * this operation so delete this one if no one is using it.
  86. */
  87. if ( !u_group->aclug_refcnt ) {
  88. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  89. "aclg_init_userGroup - In traversal group deallocation\n" );
  90. __aclg__delete_userGroup (u_group);
  91. }
  92. } else {
  93. /*
  94. * Here, u_group is valid--if it matches then take it.
  95. */
  96. if ( slapi_utf8casecmp((ACLUCHP)u_group->aclug_ndn,
  97. (ACLUCHP)n_dn ) == 0 ) {
  98. u_group->aclug_refcnt++;
  99. aclpb->aclpb_groupinfo = u_group;
  100. found = 1;
  101. break;
  102. }
  103. }
  104. u_group = next_ugroup;
  105. }
  106. /* Move the new one to the top of the queue */
  107. if ( found ) {
  108. p_group = u_group->aclug_prev;
  109. n_group = u_group->aclug_next;
  110. if ( p_group ) {
  111. aclUserGroup *t_group = NULL;
  112. p_group->aclug_next = n_group;
  113. if ( n_group ) n_group->aclug_prev = p_group;
  114. t_group = aclUserGroups->aclg_first;
  115. if ( t_group ) t_group->aclug_prev = u_group;
  116. u_group->aclug_prev = NULL;
  117. u_group->aclug_next = t_group;
  118. aclUserGroups->aclg_first = u_group;
  119. if ( u_group == aclUserGroups->aclg_last )
  120. aclUserGroups->aclg_last = p_group;
  121. }
  122. slapi_log_error(SLAPI_LOG_ACL, plugin_name, "acl_init_userGroup - Found in cache for dn:%s\n", n_dn);
  123. }
  124. if (!got_lock ) ACLG_ULOCK_GROUPCACHE_WRITE ();
  125. }
  126. /*
  127. *
  128. * aclg_reset_userGroup
  129. * Reset the reference count to the user's group.
  130. *
  131. * Inputs:
  132. * struct acl_pblock -- The acl private block.
  133. * Returns:
  134. * None.
  135. *
  136. * Note: A WRITE Lock on the GroupCache is obtained during the change:
  137. */
  138. void
  139. aclg_reset_userGroup ( struct acl_pblock *aclpb )
  140. {
  141. aclUserGroup *u_group;
  142. ACLG_LOCK_GROUPCACHE_WRITE();
  143. if ( (u_group = aclpb->aclpb_groupinfo) != NULL ) {
  144. u_group->aclug_refcnt--;
  145. /* If I am the last one but I was using an invalid group cache
  146. ** in the meantime, it is time now to get rid of it so that we will
  147. ** not have duplicate cache.
  148. */
  149. if ( !u_group->aclug_refcnt &&
  150. ( aclUserGroups->aclg_signature != u_group->aclug_signature )) {
  151. __aclg__delete_userGroup ( u_group );
  152. }
  153. }
  154. ACLG_ULOCK_GROUPCACHE_WRITE();
  155. aclpb->aclpb_groupinfo = NULL;
  156. }
  157. /*
  158. * Find a user group in the global cache, returning a pointer to it,
  159. * ensuring that the refcnt has been bumped to stop
  160. * another thread freeing it underneath us.
  161. */
  162. aclUserGroup*
  163. aclg_find_userGroup(const char *n_dn)
  164. {
  165. aclUserGroup *u_group = NULL;
  166. int i;
  167. /* Check for Anonymous user */
  168. if ( n_dn && *n_dn == '\0') return (NULL) ;
  169. ACLG_LOCK_GROUPCACHE_READ ();
  170. u_group = aclUserGroups->aclg_first;
  171. for ( i=0; i < aclUserGroups->aclg_num_userGroups; i++ ) {
  172. if ( aclUserGroups->aclg_signature == u_group->aclug_signature &&
  173. slapi_utf8casecmp((ACLUCHP)u_group->aclug_ndn,
  174. (ACLUCHP)n_dn ) == 0 ) {
  175. aclg_reader_incr_ugroup_refcnt(u_group);
  176. break;
  177. }
  178. u_group = u_group->aclug_next;
  179. }
  180. ACLG_ULOCK_GROUPCACHE_READ ();
  181. return(u_group);
  182. }
  183. /*
  184. * Mark a usergroup for removal from the usergroup cache.
  185. * It will be removed by the first operation traversing the cache
  186. * that finds it.
  187. */
  188. void
  189. aclg_markUgroupForRemoval ( aclUserGroup* u_group) {
  190. ACLG_LOCK_GROUPCACHE_WRITE ();
  191. aclg_regen_ugroup_signature(u_group);
  192. u_group->aclug_refcnt--;
  193. ACLG_ULOCK_GROUPCACHE_WRITE ();
  194. }
  195. /*
  196. *
  197. * aclg_get_usersGroup
  198. *
  199. * If we already have a the group info then we are done. If we
  200. * don't, then allocate a new one and attach it.
  201. *
  202. * Inputs:
  203. * struct acl_pblock -- The acl private block.
  204. * char *n_dn - normalized client's DN
  205. *
  206. * Returns:
  207. * aclUserGroup - The Group info block.
  208. *
  209. */
  210. aclUserGroup *
  211. aclg_get_usersGroup ( struct acl_pblock *aclpb , char *n_dn)
  212. {
  213. aclUserGroup *u_group, *f_group;
  214. if ( !aclpb ) {
  215. slapi_log_error(SLAPI_LOG_ACL, plugin_name, "aclg_get_usersGroup - NULL acl pblock\n" );
  216. return NULL;
  217. }
  218. if ( aclpb->aclpb_groupinfo )
  219. return aclpb->aclpb_groupinfo;
  220. ACLG_LOCK_GROUPCACHE_WRITE();
  221. /* try it one more time. We might have one in the meantime */
  222. aclg_init_userGroup (aclpb, n_dn , 1 /* got the lock */);
  223. if ( aclpb->aclpb_groupinfo ) {
  224. ACLG_ULOCK_GROUPCACHE_WRITE();
  225. return aclpb->aclpb_groupinfo;
  226. }
  227. /*
  228. * It is possible at this point that we already have a group cache for the user
  229. * but is is invalid. We can't use it anyway. So, we march along and allocate a new one.
  230. * That's fine as the invalid one will be deallocated when done.
  231. */
  232. slapi_log_error(SLAPI_LOG_ACL, plugin_name, "aclg_get_usersGroup - ALLOCATING GROUP FOR:%s\n", n_dn );
  233. u_group = ( aclUserGroup * ) slapi_ch_calloc ( 1, sizeof ( aclUserGroup ) );
  234. u_group->aclug_refcnt = 1;
  235. if ( (u_group->aclug_refcnt_mutex = PR_NewLock()) == NULL ) {
  236. slapi_ch_free((void **)&u_group);
  237. ACLG_ULOCK_GROUPCACHE_WRITE();
  238. return(NULL);
  239. }
  240. u_group->aclug_member_groups = (char **)
  241. slapi_ch_calloc ( 1,
  242. (ACLUG_INCR_GROUPS_LIST * sizeof (char *)));
  243. u_group->aclug_member_group_size = ACLUG_INCR_GROUPS_LIST;
  244. u_group->aclug_numof_member_group = 0;
  245. u_group->aclug_notmember_groups = (char **)
  246. slapi_ch_calloc ( 1,
  247. (ACLUG_INCR_GROUPS_LIST * sizeof (char *)));
  248. u_group->aclug_notmember_group_size = ACLUG_INCR_GROUPS_LIST;
  249. u_group->aclug_numof_notmember_group = 0;
  250. u_group->aclug_ndn = slapi_ch_strdup ( n_dn ) ;
  251. u_group->aclug_signature = aclUserGroups->aclg_signature;
  252. /* Do we have already the max number. If we have then delete the last one */
  253. if ( aclUserGroups->aclg_num_userGroups >= ACL_MAXCACHE_USERGROUPS - 5 ) {
  254. aclUserGroup *d_group;
  255. /* We need to traverse thru backwards and delete the one with a refcnt = 0 */
  256. d_group = aclUserGroups->aclg_last;
  257. while ( d_group ) {
  258. if ( !d_group->aclug_refcnt ) {
  259. __aclg__delete_userGroup ( d_group );
  260. break;
  261. } else {
  262. d_group = d_group->aclug_prev;
  263. }
  264. }
  265. /* If we didn't find any, which should be never,
  266. ** we have 5 more tries to do it.
  267. */
  268. }
  269. f_group = aclUserGroups->aclg_first;
  270. u_group->aclug_next = f_group;
  271. if ( f_group ) f_group->aclug_prev = u_group;
  272. aclUserGroups->aclg_first = u_group;
  273. if ( aclUserGroups->aclg_last == NULL )
  274. aclUserGroups->aclg_last = u_group;
  275. aclUserGroups->aclg_num_userGroups++;
  276. /* Put it in the queue */
  277. ACLG_ULOCK_GROUPCACHE_WRITE();
  278. /* Now hang on to it */
  279. aclpb->aclpb_groupinfo = u_group;
  280. return u_group;
  281. }
  282. /*
  283. *
  284. * __aclg__delete_userGroup
  285. *
  286. * Delete the User's Group cache.
  287. *
  288. * Inputs:
  289. * aclUserGroup - remove this one
  290. * Returns:
  291. * None.
  292. *
  293. * Note: A WRITE Lock on the GroupCache is obtained by the caller
  294. */
  295. static void
  296. __aclg__delete_userGroup ( aclUserGroup *u_group )
  297. {
  298. aclUserGroup *next_group, *prev_group;
  299. int i;
  300. if ( !u_group ) return;
  301. prev_group = u_group->aclug_prev;
  302. next_group = u_group->aclug_next;
  303. /*
  304. * At this point we must have a 0 refcnt or else we are in a bad shape.
  305. * If we don't have one then at least remove the user's dn so that it will
  306. * be in a condemned state and later deleted.
  307. */
  308. slapi_log_error(SLAPI_LOG_ACL, plugin_name, "__aclg__delete_userGroup - DEALLOCATING GROUP FOR:%s\n", u_group->aclug_ndn );
  309. slapi_ch_free ( (void **) &u_group->aclug_ndn );
  310. PR_DestroyLock(u_group->aclug_refcnt_mutex);
  311. /* Remove the member GROUPS */
  312. for (i=0; i < u_group->aclug_numof_member_group; i++ )
  313. slapi_ch_free ( (void **) &u_group->aclug_member_groups[i] );
  314. slapi_ch_free ( (void **) &u_group->aclug_member_groups );
  315. /* Remove the NOT member GROUPS */
  316. for (i=0; i < u_group->aclug_numof_notmember_group; i++ )
  317. slapi_ch_free ( (void **) &u_group->aclug_notmember_groups[i] );
  318. slapi_ch_free ( (void **) &u_group->aclug_notmember_groups );
  319. slapi_ch_free ( (void **) &u_group );
  320. if ( prev_group == NULL && next_group == NULL ) {
  321. aclUserGroups->aclg_first = NULL;
  322. aclUserGroups->aclg_last = NULL;
  323. } else if ( prev_group == NULL ) {
  324. next_group->aclug_prev = NULL;
  325. aclUserGroups->aclg_first = next_group;
  326. } else {
  327. prev_group->aclug_next = next_group;
  328. if ( next_group )
  329. next_group->aclug_prev = prev_group;
  330. else
  331. aclUserGroups->aclg_last = prev_group;
  332. }
  333. aclUserGroups->aclg_num_userGroups--;
  334. }
  335. void
  336. aclg_regen_group_signature( )
  337. {
  338. aclUserGroups->aclg_signature = aclutil_gen_signature ( aclUserGroups->aclg_signature );
  339. }
  340. void
  341. aclg_regen_ugroup_signature( aclUserGroup *ugroup)
  342. {
  343. ugroup->aclug_signature =
  344. aclutil_gen_signature ( ugroup->aclug_signature );
  345. }
  346. void
  347. aclg_lock_groupCache ( int type /* 1 for reader and 2 for writer */)
  348. {
  349. if (type == 1 )
  350. ACLG_LOCK_GROUPCACHE_READ();
  351. else
  352. ACLG_LOCK_GROUPCACHE_WRITE();
  353. }
  354. void
  355. aclg_unlock_groupCache ( int type /* 1 for reader and 2 for writer */)
  356. {
  357. if (type == 1 )
  358. ACLG_ULOCK_GROUPCACHE_READ();
  359. else
  360. ACLG_ULOCK_GROUPCACHE_WRITE();
  361. }
  362. /*
  363. * If you have the write lock on the group cache, you can
  364. * increment the refcnt without taking the mutex.
  365. * If you just have the reader lock on the refcnt then you need to
  366. * take the mutex on the refcnt to increment it--which is what this routine is
  367. * for.
  368. *
  369. */
  370. void
  371. aclg_reader_incr_ugroup_refcnt(aclUserGroup* u_group) {
  372. PR_Lock(u_group->aclug_refcnt_mutex);
  373. u_group->aclug_refcnt++;
  374. PR_Unlock(u_group->aclug_refcnt_mutex);
  375. }
  376. /* You need the usergroups read lock to call this routine*/
  377. int
  378. aclg_numof_usergroups(void) {
  379. return(aclUserGroups->aclg_num_userGroups);
  380. }