aclgroup.c 14 KB

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