acllist.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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, const char *base)
  481. {
  482. Acl_PBlock *aclpb;
  483. AciContainer *root;
  484. char *basedn = NULL;
  485. int index;
  486. if ( acl_skip_access_check ( pb, NULL ) ) {
  487. return;
  488. }
  489. /*acllist_print_tree ( acllistRoot, &depth, "top", "top") ; */
  490. /* my_print ( acllistRoot );*/
  491. /* If we have an anonymous profile and I am an anom dude - let's skip it */
  492. if ( aclanom_is_client_anonymous ( pb )) {
  493. return;
  494. }
  495. aclpb = acl_get_aclpb (pb, ACLPB_BINDDN_PBLOCK );
  496. if ( !aclpb ) {
  497. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name, "Missing aclpb 4 \n" );
  498. return;
  499. }
  500. aclpb->aclpb_handles_index[0] = -1;
  501. /* If base is NULL - it means we are going to go thru all the ACLs
  502. * This is needed when we do anonymous profile generation.
  503. */
  504. if ( NULL == base ) {
  505. return;
  506. }
  507. aclpb->aclpb_state |= ACLPB_SEARCH_BASED_ON_LIST ;
  508. acllist_acicache_READ_LOCK();
  509. basedn = slapi_ch_strdup (base);
  510. index = 0;
  511. aclpb->aclpb_search_base = slapi_ch_strdup ( base );
  512. while (basedn) {
  513. char *tmp = NULL;
  514. slapi_sdn_set_normdn_byref(aclpb->aclpb_aclContainer->acic_sdn, basedn);
  515. root = (AciContainer *) avl_find(acllistRoot,
  516. (caddr_t) aclpb->aclpb_aclContainer,
  517. (IFP) __acllist_aciContainer_node_cmp);
  518. if ( index >= aclpb_max_selected_acls -2 ) {
  519. aclpb->aclpb_handles_index[0] = -1;
  520. slapi_ch_free ( (void **) &basedn);
  521. break;
  522. } else if ( NULL != root ) {
  523. aclpb->aclpb_base_handles_index[index++] = root->acic_index;
  524. aclpb->aclpb_base_handles_index[index] = -1;
  525. } else if ( NULL == root ) {
  526. /* slapi_dn_parent returns the "parent" dn syntactically.
  527. * Most likely, basedn is above suffix (e.g., dn=com).
  528. * Thus, no need to make it FATAL. */
  529. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  530. "Failed to find root for base: %s \n", basedn );
  531. }
  532. tmp = slapi_dn_parent ( basedn );
  533. slapi_ch_free ( (void **) &basedn);
  534. basedn = tmp;
  535. }
  536. acllist_done_aciContainer ( aclpb->aclpb_aclContainer);
  537. if ( aclpb->aclpb_base_handles_index[0] == -1 )
  538. aclpb->aclpb_state &= ~ACLPB_SEARCH_BASED_ON_LIST ;
  539. acllist_acicache_READ_UNLOCK();
  540. }
  541. /*
  542. * Initialize aclpb_handles_index[] (sentinel -1) to
  543. * contain a list of all aci items at and above edn in the DIT tree.
  544. * This list will be subsequestly scanned to find applicable aci's for
  545. * the given operation.
  546. */
  547. /* edn is normalized & case-ignored */
  548. void
  549. acllist_aciscan_update_scan ( Acl_PBlock *aclpb, char *edn )
  550. {
  551. int index = 0;
  552. char *basedn = NULL;
  553. AciContainer *root;
  554. int is_not_search_base = 1;
  555. if ( !aclpb ) {
  556. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  557. "acllist_aciscan_update_scan: NULL acl pblock\n");
  558. return;
  559. }
  560. /* First copy the containers indx from the base to the one which is
  561. * going to be used.
  562. * The base handles get done in acllist_init_scan().
  563. * This stuff is only used if it's a search operation.
  564. */
  565. if ( aclpb->aclpb_search_base ) {
  566. if ( strcasecmp ( edn, aclpb->aclpb_search_base) == 0) {
  567. is_not_search_base = 0;
  568. }
  569. for (index = 0; (aclpb->aclpb_base_handles_index[index] != -1) &&
  570. (index < aclpb_max_selected_acls - 2); index++) ;
  571. memcpy(aclpb->aclpb_handles_index, aclpb->aclpb_base_handles_index,
  572. sizeof(*aclpb->aclpb_handles_index) * index);
  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. }
  617. aci_t *
  618. acllist_get_first_aci (Acl_PBlock *aclpb, PRUint32 *cookie )
  619. {
  620. int val;
  621. *cookie = val = 0;
  622. if ( aclpb && aclpb->aclpb_handles_index[0] != -1 ) {
  623. val = aclpb->aclpb_handles_index[*cookie];
  624. }
  625. if ( NULL == aciContainerArray[val]) {
  626. return ( acllist_get_next_aci ( aclpb, NULL, cookie ) );
  627. }
  628. return (aciContainerArray[val]->acic_list );
  629. }
  630. /*
  631. * acllist_get_next_aci
  632. * Return the next aci in the list
  633. *
  634. * Inputs
  635. * Acl_PBlock *aclpb -- acl Main block; if aclpb= NULL,
  636. * -- then we scan thru the whole list.
  637. * -- which is used by anom profile code.
  638. * aci_t *curaci -- the current aci
  639. * PRUint32 *cookie -- cookie -- to maintain a state (what's next)
  640. *
  641. */
  642. aci_t *
  643. acllist_get_next_aci ( Acl_PBlock *aclpb, aci_t *curaci, PRUint32 *cookie )
  644. {
  645. PRUint32 val;
  646. int scan_entire_list;
  647. /*
  648. Here, if we're passed a curaci and there's another aci in the same node,
  649. return that one.
  650. */
  651. if ( curaci && curaci->aci_next )
  652. return ( curaci->aci_next );
  653. /*
  654. Determine if we need to scan the entire list of acis.
  655. We do if the aclpb==NULL or if the first handle index is -1.
  656. That means that we want to go through
  657. the entire aciContainerArray up to the currContainerIndex to get
  658. acis; the -1 in the first position is a special keyword which tells
  659. us that the acis have changed, so we need to go through all of them.
  660. */
  661. scan_entire_list = (aclpb == NULL || aclpb->aclpb_handles_index[0] == -1);
  662. start:
  663. (*cookie)++;
  664. val = *cookie;
  665. /* if we are not scanning the entire aciContainerArray list, we only want to
  666. look at the indexes specified in the handles index */
  667. if ( !scan_entire_list )
  668. val = aclpb->aclpb_handles_index[*cookie];
  669. /* the hard max end */
  670. if ( val >= maxContainerIndex)
  671. return NULL;
  672. /* reached the end of the array */
  673. if ((!scan_entire_list && (*cookie >= (aclpb_max_selected_acls-1))) ||
  674. (*cookie >= currContainerIndex)) {
  675. return NULL;
  676. }
  677. /* if we're only using the handles list for our aciContainerArray
  678. indexes, the -1 value marks the end of that list */
  679. if ( !scan_entire_list && (aclpb->aclpb_handles_index[*cookie] == -1) ) {
  680. return NULL;
  681. }
  682. /* if we're scanning the entire list, and we hit a null value in the
  683. middle of the list, just try the next one; this can happen if
  684. an aci was deleted - it can leave "holes" in the array */
  685. if ( scan_entire_list && ( NULL == aciContainerArray[val])) {
  686. goto start;
  687. }
  688. if ( aciContainerArray[val] )
  689. return (aciContainerArray[val]->acic_list );
  690. else
  691. return NULL;
  692. }
  693. void
  694. acllist_acicache_READ_UNLOCK( )
  695. {
  696. ACILIST_UNLOCK_READ ();
  697. }
  698. void
  699. acllist_acicache_READ_LOCK()
  700. {
  701. /* get a reader lock */
  702. ACILIST_LOCK_READ ();
  703. }
  704. void
  705. acllist_acicache_WRITE_UNLOCK( )
  706. {
  707. ACILIST_UNLOCK_WRITE ();
  708. }
  709. void
  710. acllist_acicache_WRITE_LOCK( )
  711. {
  712. ACILIST_LOCK_WRITE ();
  713. }
  714. /* This routine must be called with the acicache write lock taken */
  715. /* newdn is normalized (no need to be case-ignored) */
  716. int
  717. acllist_moddn_aci_needsLock ( Slapi_DN *oldsdn, char *newdn )
  718. {
  719. AciContainer *aciListHead;
  720. AciContainer *head;
  721. aci_t *acip;
  722. const char *oldndn;
  723. /* first get the container */
  724. aciListHead = acllist_get_aciContainer_new ( );
  725. slapi_sdn_free(&aciListHead->acic_sdn);
  726. aciListHead->acic_sdn = oldsdn;
  727. if ( NULL == (head = (AciContainer *) avl_find( acllistRoot, aciListHead,
  728. (IFP) __acllist_aciContainer_node_cmp ) ) ) {
  729. slapi_log_error ( SLAPI_PLUGIN_ACL, plugin_name,
  730. "Can't find the acl in the tree for moddn operation:olddn%s\n",
  731. slapi_sdn_get_ndn ( oldsdn ));
  732. aciListHead->acic_sdn = NULL;
  733. __acllist_free_aciContainer ( &aciListHead );
  734. return 1;
  735. }
  736. /* Now set the new DN */
  737. slapi_sdn_set_normdn_byval(head->acic_sdn, newdn);
  738. /* If necessary, reset the target DNs, as well. */
  739. oldndn = slapi_sdn_get_ndn(oldsdn);
  740. for (acip = head->acic_list; acip; acip = acip->aci_next) {
  741. const char *ndn = slapi_sdn_get_ndn(acip->aci_sdn);
  742. char *p = PL_strstr(ndn, oldndn);
  743. if (p) {
  744. if (p == ndn) {
  745. /* target dn is identical, replace it with new DN*/
  746. slapi_sdn_set_normdn_byval(acip->aci_sdn, newdn);
  747. } else {
  748. /* target dn is a descendent of olddn, merge it with new DN*/
  749. char *mynewdn;
  750. *p = '\0';
  751. mynewdn = slapi_ch_smprintf("%s%s", ndn, newdn);
  752. slapi_sdn_set_normdn_passin(acip->aci_sdn, mynewdn);
  753. }
  754. }
  755. }
  756. aciListHead->acic_sdn = NULL;
  757. __acllist_free_aciContainer ( &aciListHead );
  758. return 0;
  759. }
  760. void
  761. acllist_print_tree ( Avlnode *root, int *depth, char *start, char *side)
  762. {
  763. AciContainer *aciHeadList;
  764. if ( NULL == root ) {
  765. return;
  766. }
  767. aciHeadList = (AciContainer *) root->avl_data;
  768. slapi_log_error ( SLAPI_LOG_ACL, "plugin_name",
  769. "Container[ Depth=%d%s-%s]: %s\n", *depth, start, side,
  770. slapi_sdn_get_ndn ( aciHeadList->acic_sdn ) );
  771. (*depth)++;
  772. acllist_print_tree ( root->avl_left, depth, side, "L" );
  773. acllist_print_tree ( root->avl_right, depth, side, "R" );
  774. (*depth)--;
  775. }
  776. static
  777. void
  778. ravl_print( Avlnode *root, int depth )
  779. {
  780. int i;
  781. AciContainer *aciHeadList;
  782. if ( root == 0 )
  783. return;
  784. ravl_print( root->avl_right, depth+1 );
  785. for ( i = 0; i < depth; i++ )
  786. printf( " " );
  787. aciHeadList = (AciContainer *) root->avl_data;
  788. printf( "%s\n", slapi_sdn_get_ndn ( aciHeadList->acic_sdn ) );
  789. ravl_print( root->avl_left, depth+1 );
  790. }
  791. void
  792. my_print( Avlnode *root )
  793. {
  794. printf( "********\n" );
  795. if ( root == 0 )
  796. printf( "\tNULL\n" );
  797. else
  798. ( void ) ravl_print( root, 0 );
  799. printf( "********\n" );
  800. }