acllist.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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. /************************************************************************
  39. *
  40. * ACLLIST
  41. *
  42. * All the ACLs are read when the server is started. The ACLs are
  43. * parsed and kept in an AVL tree. All the ACL List management are
  44. * in this file.
  45. *
  46. * The locking on the aci cache is implemented using the acllist_acicache*()
  47. * routines--a read/write lock.
  48. *
  49. * The granularity of the view of the cache is the entry level--ie.
  50. * when an entry is being modified (mod,add,delete,modrdn) and the mod
  51. * involves the aci attribute, then other operations will see the acl cache
  52. * before the whole change or after the whole change, but not during the change.
  53. * cf. acl.c:acl_modified()
  54. *
  55. * The only tricky issue is that there is also locking
  56. * implemented for the anonymous profile and sometimes we need to take both
  57. * locks cf. aclanom_anom_genProfile(). The rule is
  58. * always take the acicache lock first, followed by the anon lock--following
  59. * this rule will ensure no dead lock scenarios can arise.
  60. *
  61. * Some routines are called in different places with different lock
  62. * contexts--for these routines acl_lock_flag_t is used to
  63. * pass the context.
  64. *
  65. */
  66. #include "acl.h"
  67. static PRRWLock *aci_rwlock = NULL;
  68. #define ACILIST_LOCK_READ() PR_RWLock_Rlock (aci_rwlock )
  69. #define ACILIST_UNLOCK_READ() PR_RWLock_Unlock (aci_rwlock )
  70. #define ACILIST_LOCK_WRITE() PR_RWLock_Wlock (aci_rwlock )
  71. #define ACILIST_UNLOCK_WRITE() PR_RWLock_Unlock (aci_rwlock )
  72. /* Root of the TREE */
  73. static Avlnode *acllistRoot = NULL;
  74. #define CONTAINER_INCR 2000
  75. /* The container array */
  76. static AciContainer **aciContainerArray;
  77. static PRUint32 currContainerIndex =0;
  78. static PRUint32 maxContainerIndex = 0;
  79. static int curAciIndex = 1;
  80. /* PROTOTYPES */
  81. static int __acllist_add_aci ( aci_t *aci );
  82. static int __acllist_aciContainer_node_cmp ( caddr_t d1, caddr_t d2 );
  83. static int __acllist_aciContainer_node_dup ( caddr_t d1, caddr_t d2 );
  84. static void __acllist_free_aciContainer ( AciContainer **container);
  85. static void free_targetattrfilters( Targetattrfilter ***input_attrFilterArray);
  86. void my_print( Avlnode *root );
  87. int
  88. acllist_init ()
  89. {
  90. if (( aci_rwlock = PR_NewRWLock( PR_RWLOCK_RANK_NONE,"ACLLIST LOCK") ) == NULL ) {
  91. slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
  92. "acllist_init:failed in getting the rwlock\n" );
  93. return 1;
  94. }
  95. aciContainerArray = (AciContainer **) slapi_ch_calloc ( 1,
  96. CONTAINER_INCR * sizeof ( AciContainer * ) );
  97. maxContainerIndex = CONTAINER_INCR;
  98. currContainerIndex = 0;
  99. return 0;
  100. }
  101. /*
  102. * This is the callback for backend state changes.
  103. * It needs to add/remove acis as backends come up and go down.
  104. *
  105. * The strategy is simple:
  106. * When a backend moves to the SLAPI_BE_STATE_ON then we go get all the acis
  107. * add them to the cache.
  108. * When a backend moves out of the SLAPI_BE_STATE_ON then we remove them all.
  109. *
  110. */
  111. void acl_be_state_change_fnc ( void *handle, char *be_name, int old_state,
  112. int new_state) {
  113. Slapi_Backend *be=NULL;
  114. const Slapi_DN *sdn;
  115. if ( old_state == SLAPI_BE_STATE_ON && new_state != SLAPI_BE_STATE_ON) {
  116. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  117. "Backend %s is no longer STARTED--deactivating it's acis\n",
  118. be_name);
  119. if ( (be = slapi_be_select_by_instance_name( be_name )) == NULL) {
  120. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  121. "Failed to retreive backend--NOT activating it's acis\n");
  122. return;
  123. }
  124. /*
  125. * Just get the first suffix--if there are multiple XXX ?
  126. */
  127. if ( (sdn = slapi_be_getsuffix( be, 0)) == NULL ) {
  128. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  129. "Failed to retreive backend--NOT activating it's acis\n");
  130. return;
  131. }
  132. aclinit_search_and_update_aci ( 1, /* thisbeonly */
  133. sdn, /* base */
  134. be_name,/* be name */
  135. LDAP_SCOPE_SUBTREE,
  136. ACL_REMOVE_ACIS,
  137. DO_TAKE_ACLCACHE_WRITELOCK);
  138. } else if ( old_state != SLAPI_BE_STATE_ON && new_state == SLAPI_BE_STATE_ON) {
  139. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  140. "Backend %s is now STARTED--activating it's acis\n", be_name);
  141. if ( (be = slapi_be_select_by_instance_name( be_name )) == NULL) {
  142. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  143. "Failed to retreive backend--NOT activating it's acis\n");
  144. return;
  145. }
  146. /*
  147. * In fact there can onlt be one sufffix here.
  148. */
  149. if ( (sdn = slapi_be_getsuffix( be, 0)) == NULL ) {
  150. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  151. "Failed to retreive backend--NOT activating it's acis\n");
  152. return;
  153. }
  154. aclinit_search_and_update_aci ( 1, /* thisbeonly */
  155. sdn,
  156. be_name, /* be name */
  157. LDAP_SCOPE_SUBTREE,
  158. ACL_ADD_ACIS,
  159. DO_TAKE_ACLCACHE_WRITELOCK);
  160. }
  161. }
  162. /* This routine must be called with the acicache write lock taken */
  163. int
  164. acllist_insert_aci_needsLock( const Slapi_DN *e_sdn, const struct berval* aci_attr)
  165. {
  166. aci_t *aci;
  167. char *acl_str;
  168. int rv =0;
  169. if (aci_attr->bv_len <= 0)
  170. return 0;
  171. aci = acllist_get_aci_new ();
  172. slapi_sdn_set_ndn_byval ( aci->aci_sdn, slapi_sdn_get_ndn ( e_sdn ) );
  173. acl_str = slapi_ch_strdup(aci_attr->bv_val);
  174. /* Parse the ACL TEXT */
  175. if ( 0 != (rv = acl_parse ( acl_str, aci )) ) {
  176. slapi_log_error (SLAPI_LOG_FATAL, plugin_name,
  177. "ACL PARSE ERR(rv=%d): %s\n", rv, acl_str );
  178. slapi_ch_free ( (void **) &acl_str );
  179. acllist_free_aci ( aci );
  180. return 1;
  181. }
  182. /* Now add it to the list */
  183. if ( 0 != (rv =__acllist_add_aci ( aci ))) {
  184. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  185. "ACL ADD ACI ERR(rv=%d): %s\n", rv, acl_str );
  186. slapi_ch_free ( (void **) &acl_str );
  187. acllist_free_aci ( aci );
  188. return 1;
  189. }
  190. slapi_ch_free ( (void **) &acl_str );
  191. acl_regen_aclsignature ();
  192. if ( aci->aci_elevel == ACI_ELEVEL_USERDN_ANYONE)
  193. aclanom_invalidateProfile ();
  194. return 0;
  195. }
  196. /* This routine must be called with the acicache write lock taken */
  197. static int
  198. __acllist_add_aci ( aci_t *aci )
  199. {
  200. int rv = 0; /* OK */
  201. AciContainer *aciListHead;
  202. AciContainer *head;
  203. PRUint32 i;
  204. aciListHead = acllist_get_aciContainer_new ( );
  205. slapi_sdn_set_ndn_byval ( aciListHead->acic_sdn, slapi_sdn_get_ndn ( aci->aci_sdn ) );
  206. /* insert the aci */
  207. switch (avl_insert ( &acllistRoot, aciListHead, __acllist_aciContainer_node_cmp,
  208. __acllist_aciContainer_node_dup ) ) {
  209. case 1: /* duplicate ACL on the same entry */
  210. /* Find the node that contains the acl. */
  211. if ( NULL == (head = (AciContainer *) avl_find( acllistRoot, aciListHead,
  212. (IFP) __acllist_aciContainer_node_cmp ) ) ) {
  213. slapi_log_error ( SLAPI_PLUGIN_ACL, plugin_name,
  214. "Can't insert the acl in the tree\n");
  215. rv = 1;
  216. } else {
  217. aci_t *t_aci;;
  218. /* Attach the list */
  219. t_aci = head->acic_list;;
  220. while ( t_aci && t_aci->aci_next )
  221. t_aci = t_aci->aci_next;
  222. /* Now add the new one to the end of the list */
  223. t_aci->aci_next = aci;
  224. }
  225. slapi_log_error ( SLAPI_LOG_ACL, plugin_name, "Added the ACL:%s to existing container:[%d]%s\n",
  226. aci->aclName, head->acic_index, slapi_sdn_get_ndn( head->acic_sdn ));
  227. /* now free the tmp container */
  228. aciListHead->acic_list = NULL;
  229. __acllist_free_aciContainer ( &aciListHead );
  230. break;
  231. default:
  232. /* The container is inserted. Now hook up the aci and setup the
  233. * container index. Donot free the "aciListHead" here.
  234. */
  235. aciListHead->acic_list = aci;
  236. /*
  237. * First, see if we have an open slot or not - -if we have reuse it
  238. */
  239. i = 0;
  240. while ( (i < currContainerIndex) && aciContainerArray[i] )
  241. i++;
  242. if ( currContainerIndex >= (maxContainerIndex - 2)) {
  243. maxContainerIndex += CONTAINER_INCR;
  244. aciContainerArray = (AciContainer **) slapi_ch_realloc ( (char *) aciContainerArray,
  245. maxContainerIndex * sizeof ( AciContainer * ) );
  246. }
  247. aciListHead->acic_index = i;
  248. /* If i < currContainerIndex, we are just re-using an old slot. */
  249. /* We don't need to increase currContainerIndex if we just re-use an old one. */
  250. if (i == currContainerIndex)
  251. currContainerIndex++;
  252. aciContainerArray[ aciListHead->acic_index ] = aciListHead;
  253. slapi_log_error ( SLAPI_LOG_ACL, plugin_name, "Added %s to container:%d\n",
  254. slapi_sdn_get_ndn( aciListHead->acic_sdn ), aciListHead->acic_index );
  255. break;
  256. }
  257. return rv;
  258. }
  259. static int
  260. __acllist_aciContainer_node_cmp ( caddr_t d1, caddr_t d2 )
  261. {
  262. int rc =0;
  263. AciContainer *c1 = (AciContainer *) d1;
  264. AciContainer *c2 = (AciContainer *) d2;
  265. rc = slapi_sdn_compare ( c1->acic_sdn, c2->acic_sdn );
  266. return rc;
  267. }
  268. static int
  269. __acllist_aciContainer_node_dup ( caddr_t d1, caddr_t d2 )
  270. {
  271. /* we allow duplicates -- they are not exactly duplicates
  272. ** but multiple aci value on the same node
  273. */
  274. return 1;
  275. }
  276. /*
  277. * Remove the ACL
  278. *
  279. * This routine must be called with the aclcache write lock taken.
  280. * It takes in addition the one for the anom profile taken in
  281. * aclanom_invalidateProfile().
  282. * They _must_ be taken in this order or there
  283. * is a deadlock scenario with aclanom_gen_anomProfile() which
  284. * also takes them is this order.
  285. */
  286. int
  287. acllist_remove_aci_needsLock( const Slapi_DN *sdn, const struct berval *attr )
  288. {
  289. aci_t *head, *next;
  290. int rv = 0;
  291. AciContainer *aciListHead, *root;
  292. AciContainer *dContainer;
  293. int removed_anom_acl = 0;
  294. /* we used to delete the ACL by value but we don't do that anymore.
  295. * rather we delete all the acls in that entry and then repopulate it if
  296. * there are any more acls.
  297. */
  298. aciListHead = acllist_get_aciContainer_new ( );
  299. slapi_sdn_set_ndn_byval ( aciListHead->acic_sdn, slapi_sdn_get_ndn ( sdn ) );
  300. /* now find it */
  301. if ( NULL == (root = (AciContainer *) avl_find( acllistRoot, aciListHead,
  302. (IFP) __acllist_aciContainer_node_cmp ))) {
  303. /* In that case we don't have any acl for this entry. cool !!! */
  304. __acllist_free_aciContainer ( &aciListHead );
  305. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  306. "No acis to remove in this entry\n" );
  307. return 0;
  308. }
  309. head = root->acic_list;
  310. if ( head)
  311. next = head->aci_next;
  312. while ( head ) {
  313. if ( head->aci_elevel == ACI_ELEVEL_USERDN_ANYONE)
  314. removed_anom_acl = 1;
  315. /* Free the acl */
  316. acllist_free_aci ( head );
  317. head = next;
  318. next = NULL;
  319. if ( head && head->aci_next )
  320. next = head->aci_next;
  321. }
  322. root->acic_list = NULL;
  323. /* remove the container from the slot */
  324. aciContainerArray[root->acic_index] = NULL;
  325. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  326. "Removing container[%d]=%s\n", root->acic_index,
  327. slapi_sdn_get_ndn ( root->acic_sdn) );
  328. dContainer = (AciContainer *) avl_delete ( &acllistRoot, aciListHead,
  329. __acllist_aciContainer_node_cmp );
  330. __acllist_free_aciContainer ( &dContainer );
  331. acl_regen_aclsignature ();
  332. if ( removed_anom_acl )
  333. aclanom_invalidateProfile ();
  334. /*
  335. * Now read back the entry and repopulate ACLs for that entry, but
  336. * only if a specific aci was deleted, otherwise, we do a
  337. * "When Harry met Sally" and nail 'em all.
  338. */
  339. if ( attr != NULL) {
  340. if (0 != (rv = aclinit_search_and_update_aci ( 0, /* thisbeonly */
  341. sdn, /* base */
  342. NULL, /* be name */
  343. LDAP_SCOPE_BASE,
  344. ACL_ADD_ACIS,
  345. DONT_TAKE_ACLCACHE_WRITELOCK))) {
  346. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name,
  347. " Can't add the rest of the acls for entry:%s after delete\n",
  348. slapi_sdn_get_dn ( sdn ) );
  349. }
  350. }
  351. /* Now free the tmp container we used */
  352. __acllist_free_aciContainer ( &aciListHead );
  353. /*
  354. * regenerate the anonymous profile if we have deleted
  355. * anyone acls.
  356. * We don't need the aclcache readlock because the context of
  357. * this routine is we have the write lock already.
  358. */
  359. if ( removed_anom_acl )
  360. aclanom_gen_anomProfile(DONT_TAKE_ACLCACHE_READLOCK);
  361. return rv;
  362. }
  363. AciContainer *
  364. acllist_get_aciContainer_new ( )
  365. {
  366. AciContainer *head;
  367. head = (AciContainer * ) slapi_ch_calloc ( 1, sizeof ( AciContainer ) );
  368. head->acic_sdn = slapi_sdn_new ( );
  369. head->acic_index = -1;
  370. return head;
  371. }
  372. static void
  373. __acllist_free_aciContainer ( AciContainer **container)
  374. {
  375. PR_ASSERT ( container != NULL );
  376. if ( (*container)->acic_index >= 0 )
  377. aciContainerArray[ (*container)->acic_index] = NULL;
  378. if ( (*container)->acic_sdn )
  379. slapi_sdn_free ( &(*container)->acic_sdn );
  380. slapi_ch_free ( (void **) container );
  381. }
  382. void
  383. acllist_done_aciContainer ( AciContainer *head )
  384. {
  385. PR_ASSERT ( head != NULL );
  386. slapi_sdn_done ( head->acic_sdn );
  387. head->acic_index = -1;
  388. /* The caller is responsible for taking care of list */
  389. head->acic_list = NULL;
  390. }
  391. aci_t *
  392. acllist_get_aci_new ()
  393. {
  394. aci_t *aci_item;
  395. aci_item = (aci_t *) slapi_ch_calloc (1, sizeof (aci_t));
  396. aci_item->aci_sdn = slapi_sdn_new ();
  397. aci_item->aci_index = curAciIndex++;
  398. aci_item->aci_elevel = ACI_DEFAULT_ELEVEL; /* by default it's a complex */
  399. aci_item->targetAttr = (Targetattr **) slapi_ch_calloc (
  400. ACL_INIT_ATTR_ARRAY,
  401. sizeof (Targetattr *));
  402. return aci_item;
  403. }
  404. void
  405. acllist_free_aci(aci_t *item)
  406. {
  407. Targetattr **attrArray;
  408. /* The caller is responsible for taking
  409. ** care of list issue
  410. */
  411. if (item == NULL) return;
  412. slapi_sdn_free ( &item->aci_sdn );
  413. slapi_filter_free (item->target, 1);
  414. /* slapi_filter_free(item->targetAttr, 1); */
  415. attrArray = item->targetAttr;
  416. if (attrArray) {
  417. int i = 0;
  418. Targetattr *attr;
  419. while (attrArray[i] != NULL) {
  420. attr = attrArray[i];
  421. if (attr->attr_type & ACL_ATTR_FILTER) {
  422. slapi_filter_free(attr->u.attr_filter, 1);
  423. } else {
  424. slapi_ch_free ( (void **) &attr->u.attr_str );
  425. }
  426. slapi_ch_free ( (void **) &attr );
  427. i++;
  428. }
  429. /* Now free the array */
  430. slapi_ch_free ( (void **) &attrArray );
  431. }
  432. /* Now free any targetattrfilters in this aci item */
  433. if ( item->targetAttrAddFilters ) {
  434. free_targetattrfilters(&item->targetAttrAddFilters);
  435. }
  436. if ( item->targetAttrDelFilters ) {
  437. free_targetattrfilters(&item->targetAttrDelFilters);
  438. }
  439. if (item->targetFilterStr) slapi_ch_free ( (void **) &item->targetFilterStr );
  440. slapi_filter_free(item->targetFilter, 1);
  441. /* free the handle */
  442. if (item->aci_handle) ACL_ListDestroy(NULL, item->aci_handle);
  443. /* Free the name */
  444. if (item->aclName) slapi_ch_free((void **) &item->aclName);
  445. /* Free any macro info*/
  446. if (item->aci_macro) {
  447. slapi_ch_free((void **) &item->aci_macro->match_this);
  448. slapi_ch_free((void **) &item->aci_macro);
  449. }
  450. /* free at last -- free at last */
  451. slapi_ch_free ( (void **) &item );
  452. }
  453. static void free_targetattrfilters( Targetattrfilter ***attrFilterArray) {
  454. if (*attrFilterArray) {
  455. int i = 0;
  456. Targetattrfilter *attrfilter;
  457. while ((*attrFilterArray)[i] != NULL) {
  458. attrfilter = (*attrFilterArray)[i];
  459. if ( attrfilter->attr_str != NULL) {
  460. slapi_ch_free ( (void **) &attrfilter->attr_str );
  461. }
  462. if (attrfilter->filter != NULL) {
  463. slapi_filter_free(attrfilter->filter, 1);
  464. }
  465. if( attrfilter->filterStr != NULL) {
  466. slapi_ch_free ( (void **) &attrfilter->filterStr );
  467. }
  468. slapi_ch_free ( (void **) &attrfilter );
  469. i++;
  470. }
  471. /* Now free the array */
  472. slapi_ch_free ( (void **) attrFilterArray );
  473. }
  474. }
  475. /* SEARCH */
  476. void
  477. acllist_init_scan (Slapi_PBlock *pb, int scope, char *base)
  478. {
  479. Acl_PBlock *aclpb;
  480. int i;
  481. AciContainer *root;
  482. char *basedn = NULL;
  483. int index;
  484. if ( acl_skip_access_check ( pb, NULL ) ) {
  485. return;
  486. }
  487. /*acllist_print_tree ( acllistRoot, &depth, "top", "top") ; */
  488. /* my_print ( acllistRoot );*/
  489. /* If we have an anonymous profile and I am an anom dude - let's skip it */
  490. if ( aclanom_is_client_anonymous ( pb )) {
  491. return;
  492. }
  493. aclpb = acl_get_aclpb (pb, ACLPB_BINDDN_PBLOCK );
  494. if ( !aclpb ) {
  495. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name, "Missing aclpb 4 \n" );
  496. return;
  497. }
  498. aclpb->aclpb_handles_index[0] = -1;
  499. /* If base is NULL - it means we are going to go thru all the ACLs
  500. * This is needed when we do anonymous profile generation.
  501. */
  502. if ( NULL == base ) {
  503. return;
  504. }
  505. aclpb->aclpb_state |= ACLPB_SEARCH_BASED_ON_LIST ;
  506. acllist_acicache_READ_LOCK();
  507. basedn = slapi_ch_strdup (base);
  508. index = 0;
  509. aclpb->aclpb_search_base = slapi_ch_strdup ( base );
  510. while (basedn ) {
  511. char *tmp = NULL;
  512. slapi_sdn_set_ndn_byref ( aclpb->aclpb_aclContainer->acic_sdn, basedn );
  513. root = (AciContainer *) avl_find( acllistRoot,
  514. (caddr_t) aclpb->aclpb_aclContainer,
  515. (IFP) __acllist_aciContainer_node_cmp);
  516. if ( index >= ACLPB_MAX_SELECTED_ACLS -2 ) {
  517. aclpb->aclpb_handles_index[0] = -1;
  518. slapi_ch_free ( (void **) &basedn);
  519. break;
  520. } else if ( NULL != root ) {
  521. aclpb->aclpb_base_handles_index[index++] = root->acic_index;
  522. aclpb->aclpb_base_handles_index[index] = -1;
  523. }
  524. tmp = slapi_dn_parent ( basedn );
  525. slapi_ch_free ( (void **) &basedn);
  526. basedn = tmp;
  527. }
  528. acllist_done_aciContainer ( aclpb->aclpb_aclContainer);
  529. if ( aclpb->aclpb_base_handles_index[0] == -1 )
  530. aclpb->aclpb_state &= ~ACLPB_SEARCH_BASED_ON_LIST ;
  531. acllist_acicache_READ_UNLOCK();
  532. i = 0;
  533. while ( i < ACLPB_MAX_SELECTED_ACLS && aclpb->aclpb_base_handles_index[i] != -1 ) {
  534. i++;
  535. }
  536. }
  537. /*
  538. * Initialize aclpb_handles_index[] (sentinel -1) to
  539. * contain a list of all aci items at and above edn in the DIT tree.
  540. * This list will be subsequestly scanned to find applicable aci's for
  541. * the given operation.
  542. */
  543. void
  544. acllist_aciscan_update_scan ( Acl_PBlock *aclpb, char *edn )
  545. {
  546. int i, index = 0;
  547. char *basedn = NULL;
  548. AciContainer *root;
  549. int is_not_search_base = 1;
  550. /* First copy the containers indx from the base to the one which is
  551. * going to be used.
  552. * The base handles get done in acllist_init_scan().
  553. * This stuff is only used if it's a search operation.
  554. */
  555. if ( aclpb && aclpb->aclpb_search_base ) {
  556. while ( aclpb->aclpb_base_handles_index[index] != -1 &&
  557. index < ACLPB_MAX_SELECTED_ACLS -2 ) {
  558. aclpb->aclpb_handles_index[index] =
  559. aclpb->aclpb_base_handles_index[index];
  560. index++;
  561. }
  562. if ( strcasecmp ( edn, aclpb->aclpb_search_base) == 0) {
  563. is_not_search_base = 0;
  564. }
  565. }
  566. aclpb->aclpb_handles_index[index] = -1;
  567. /*
  568. * Here, make a list of all the aci's that will apply
  569. * to edn ie. all aci's at and above edn in the DIT tree.
  570. *
  571. * Do this by walking up edn, looking at corresponding
  572. * points in the acllistRoot aci tree.
  573. *
  574. * If is_not_search_base is true, then we need to iterate on edn, otherwise
  575. * we've already got all the base handles above.
  576. *
  577. */
  578. if (is_not_search_base) {
  579. basedn = slapi_ch_strdup ( edn );
  580. while (basedn ) {
  581. char *tmp = NULL;
  582. slapi_sdn_set_ndn_byref ( aclpb->aclpb_aclContainer->acic_sdn, basedn );
  583. root = (AciContainer *) avl_find( acllistRoot,
  584. (caddr_t) aclpb->aclpb_aclContainer,
  585. (IFP) __acllist_aciContainer_node_cmp);
  586. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  587. "Searching AVL tree for update:%s: container:%d\n", basedn ,
  588. root ? root->acic_index: -1);
  589. if ( index >= ACLPB_MAX_SELECTED_ACLS -2 ) {
  590. aclpb->aclpb_handles_index[0] = -1;
  591. slapi_ch_free ( (void **) &basedn);
  592. break;
  593. } else if ( NULL != root ) {
  594. aclpb->aclpb_handles_index[index++] = root->acic_index;
  595. aclpb->aclpb_handles_index[index] = -1;
  596. }
  597. tmp = slapi_dn_parent ( basedn );
  598. slapi_ch_free ( (void **) &basedn);
  599. basedn = tmp;
  600. if ( aclpb->aclpb_search_base && tmp &&
  601. ( 0 == strcasecmp ( tmp, aclpb->aclpb_search_base))) {
  602. slapi_ch_free ( (void **) &basedn);
  603. tmp = NULL;
  604. }
  605. } /* while */
  606. }
  607. acllist_done_aciContainer ( aclpb->aclpb_aclContainer );
  608. i = 0;
  609. while ( i < ACLPB_MAX_SELECTED_ACLS && aclpb->aclpb_handles_index[i] != -1 ) {
  610. i++;
  611. }
  612. }
  613. aci_t *
  614. acllist_get_first_aci (Acl_PBlock *aclpb, PRUint32 *cookie )
  615. {
  616. int val;
  617. *cookie = val = 0;
  618. if ( aclpb && aclpb->aclpb_handles_index[0] != -1 ) {
  619. val = aclpb->aclpb_handles_index[*cookie];
  620. }
  621. if ( NULL == aciContainerArray[val]) {
  622. return ( acllist_get_next_aci ( aclpb, NULL, cookie ) );
  623. }
  624. return (aciContainerArray[val]->acic_list );
  625. }
  626. /*
  627. * acllist_get_next_aci
  628. * Return the next aci in the list
  629. *
  630. * Inputs
  631. * Acl_PBlock *aclpb -- acl Main block; if aclpb= NULL,
  632. * -- then we scan thru the whole list.
  633. * -- which is used by anom profile code.
  634. * aci_t *curaci -- the current aci
  635. * PRUint32 *cookie -- cookie -- to maintain a state (what's next)
  636. *
  637. */
  638. aci_t *
  639. acllist_get_next_aci ( Acl_PBlock *aclpb, aci_t *curaci, PRUint32 *cookie )
  640. {
  641. PRUint32 val;
  642. int scan_entire_list;
  643. /*
  644. Here, if we're passed a curaci and there's another aci in the same node,
  645. return that one.
  646. */
  647. if ( curaci && curaci->aci_next )
  648. return ( curaci->aci_next );
  649. /*
  650. Determine if we need to scan the entire list of acis.
  651. We do if the aclpb==NULL or if the first handle index is -1.
  652. That means that we want to go through
  653. the entire aciContainerArray up to the currContainerIndex to get
  654. acis; the -1 in the first position is a special keyword which tells
  655. us that the acis have changed, so we need to go through all of them.
  656. */
  657. scan_entire_list = (aclpb == NULL || aclpb->aclpb_handles_index[0] == -1);
  658. start:
  659. (*cookie)++;
  660. val = *cookie;
  661. /* if we are not scanning the entire aciContainerArray list, we only want to
  662. look at the indexes specified in the handles index */
  663. if ( !scan_entire_list )
  664. val = aclpb->aclpb_handles_index[*cookie];
  665. /* the hard max end */
  666. if ( val >= maxContainerIndex)
  667. return NULL;
  668. /* reached the end of the array */
  669. if ((!scan_entire_list && (*cookie >= (ACLPB_MAX_SELECTED_ACLS-1))) ||
  670. (*cookie >= currContainerIndex)) {
  671. return NULL;
  672. }
  673. /* if we're only using the handles list for our aciContainerArray
  674. indexes, the -1 value marks the end of that list */
  675. if ( !scan_entire_list && (aclpb->aclpb_handles_index[*cookie] == -1) ) {
  676. return NULL;
  677. }
  678. /* if we're scanning the entire list, and we hit a null value in the
  679. middle of the list, just try the next one; this can happen if
  680. an aci was deleted - it can leave "holes" in the array */
  681. if ( scan_entire_list && ( NULL == aciContainerArray[val])) {
  682. goto start;
  683. }
  684. if ( aciContainerArray[val] )
  685. return (aciContainerArray[val]->acic_list );
  686. else
  687. return NULL;
  688. }
  689. void
  690. acllist_acicache_READ_UNLOCK( )
  691. {
  692. ACILIST_UNLOCK_READ ();
  693. }
  694. void
  695. acllist_acicache_READ_LOCK()
  696. {
  697. /* get a reader lock */
  698. ACILIST_LOCK_READ ();
  699. }
  700. void
  701. acllist_acicache_WRITE_UNLOCK( )
  702. {
  703. ACILIST_UNLOCK_WRITE ();
  704. }
  705. void
  706. acllist_acicache_WRITE_LOCK( )
  707. {
  708. ACILIST_LOCK_WRITE ();
  709. }
  710. /* This routine must be called with the acicache write lock taken */
  711. int
  712. acllist_moddn_aci_needsLock ( Slapi_DN *oldsdn, char *newdn )
  713. {
  714. AciContainer *aciListHead;
  715. AciContainer *head;
  716. /* first get the container */
  717. aciListHead = acllist_get_aciContainer_new ( );
  718. slapi_sdn_free(&aciListHead->acic_sdn);
  719. aciListHead->acic_sdn = oldsdn;
  720. if ( NULL == (head = (AciContainer *) avl_find( acllistRoot, aciListHead,
  721. (IFP) __acllist_aciContainer_node_cmp ) ) ) {
  722. slapi_log_error ( SLAPI_PLUGIN_ACL, plugin_name,
  723. "Can't find the acl in the tree for moddn operation:olddn%s\n",
  724. slapi_sdn_get_ndn ( oldsdn ));
  725. aciListHead->acic_sdn = NULL;
  726. __acllist_free_aciContainer ( &aciListHead );
  727. return 1;
  728. }
  729. /* Now set the new DN */
  730. slapi_sdn_done ( head->acic_sdn );
  731. slapi_sdn_set_ndn_byval ( head->acic_sdn, newdn );
  732. aciListHead->acic_sdn = NULL;
  733. __acllist_free_aciContainer ( &aciListHead );
  734. return 0;
  735. }
  736. void
  737. acllist_print_tree ( Avlnode *root, int *depth, char *start, char *side)
  738. {
  739. AciContainer *aciHeadList;
  740. if ( NULL == root ) {
  741. return;
  742. }
  743. aciHeadList = (AciContainer *) root->avl_data;
  744. slapi_log_error ( SLAPI_LOG_ACL, "plugin_name",
  745. "Container[ Depth=%d%s-%s]: %s\n", *depth, start, side,
  746. slapi_sdn_get_ndn ( aciHeadList->acic_sdn ) );
  747. (*depth)++;
  748. acllist_print_tree ( root->avl_left, depth, side, "L" );
  749. acllist_print_tree ( root->avl_right, depth, side, "R" );
  750. (*depth)--;
  751. }
  752. static
  753. void
  754. ravl_print( Avlnode *root, int depth )
  755. {
  756. int i;
  757. AciContainer *aciHeadList;
  758. if ( root == 0 )
  759. return;
  760. ravl_print( root->avl_right, depth+1 );
  761. for ( i = 0; i < depth; i++ )
  762. printf( " " );
  763. aciHeadList = (AciContainer *) root->avl_data;
  764. printf( "%s\n", slapi_sdn_get_ndn ( aciHeadList->acic_sdn ) );
  765. ravl_print( root->avl_left, depth+1 );
  766. }
  767. void
  768. my_print( Avlnode *root )
  769. {
  770. printf( "********\n" );
  771. if ( root == 0 )
  772. printf( "\tNULL\n" );
  773. else
  774. ( void ) ravl_print( root, 0 );
  775. printf( "********\n" );
  776. }