aclgroup.c 14 KB

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