acllist.c 26 KB

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