1
0

acllist.c 26 KB

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