aclgroup.c 14 KB

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