acllist.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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. void my_print( Avlnode *root );
  89. int
  90. acllist_init ()
  91. {
  92. if (( aci_rwlock = slapi_new_rwlock() ) == NULL ) {
  93. slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
  94. "acllist_init:failed in getting the rwlock\n" );
  95. return 1;
  96. }
  97. aciContainerArray = (AciContainer **) slapi_ch_calloc ( 1,
  98. CONTAINER_INCR * sizeof ( AciContainer * ) );
  99. maxContainerIndex = CONTAINER_INCR;
  100. currContainerIndex = 0;
  101. return 0;
  102. }
  103. /*
  104. * This is the callback for backend state changes.
  105. * It needs to add/remove acis as backends come up and go down.
  106. *
  107. * The strategy is simple:
  108. * When a backend moves to the SLAPI_BE_STATE_ON then we go get all the acis
  109. * add them to the cache.
  110. * When a backend moves out of the SLAPI_BE_STATE_ON then we remove them all.
  111. *
  112. */
  113. void acl_be_state_change_fnc ( void *handle, char *be_name, int old_state,
  114. int new_state) {
  115. Slapi_Backend *be=NULL;
  116. const Slapi_DN *sdn;
  117. if ( old_state == SLAPI_BE_STATE_ON && new_state != SLAPI_BE_STATE_ON) {
  118. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  119. "Backend %s is no longer STARTED--deactivating it's acis\n",
  120. be_name);
  121. if ( (be = slapi_be_select_by_instance_name( be_name )) == NULL) {
  122. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  123. "Failed to retreive backend--NOT activating it's acis\n");
  124. return;
  125. }
  126. /*
  127. * Just get the first suffix--if there are multiple XXX ?
  128. */
  129. if ( (sdn = slapi_be_getsuffix( be, 0)) == NULL ) {
  130. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  131. "Failed to retreive backend--NOT activating it's acis\n");
  132. return;
  133. }
  134. aclinit_search_and_update_aci ( 1, /* thisbeonly */
  135. sdn, /* base */
  136. be_name,/* be name */
  137. LDAP_SCOPE_SUBTREE,
  138. ACL_REMOVE_ACIS,
  139. DO_TAKE_ACLCACHE_WRITELOCK);
  140. } else if ( old_state != SLAPI_BE_STATE_ON && new_state == SLAPI_BE_STATE_ON) {
  141. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  142. "Backend %s is now STARTED--activating it's acis\n", be_name);
  143. if ( (be = slapi_be_select_by_instance_name( be_name )) == NULL) {
  144. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  145. "Failed to retreive backend--NOT activating it's acis\n");
  146. return;
  147. }
  148. /*
  149. * In fact there can onlt be one sufffix here.
  150. */
  151. if ( (sdn = slapi_be_getsuffix( be, 0)) == NULL ) {
  152. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  153. "Failed to retreive backend--NOT activating it's acis\n");
  154. return;
  155. }
  156. aclinit_search_and_update_aci ( 1, /* thisbeonly */
  157. sdn,
  158. be_name, /* be name */
  159. LDAP_SCOPE_SUBTREE,
  160. ACL_ADD_ACIS,
  161. DO_TAKE_ACLCACHE_WRITELOCK);
  162. }
  163. }
  164. /* This routine must be called with the acicache write lock taken */
  165. int
  166. acllist_insert_aci_needsLock( const Slapi_DN *e_sdn, const struct berval* aci_attr)
  167. {
  168. return(acllist_insert_aci_needsLock_ext(NULL, e_sdn, aci_attr));
  169. }
  170. int
  171. acllist_insert_aci_needsLock_ext( Slapi_PBlock *pb, const Slapi_DN *e_sdn, const struct berval* aci_attr)
  172. {
  173. aci_t *aci;
  174. char *acl_str;
  175. int rv =0;
  176. if (aci_attr->bv_len <= 0)
  177. return 0;
  178. aci = acllist_get_aci_new ();
  179. slapi_sdn_set_ndn_byval ( aci->aci_sdn, slapi_sdn_get_ndn ( e_sdn ) );
  180. acl_str = slapi_ch_strdup(aci_attr->bv_val);
  181. /* Parse the ACL TEXT */
  182. if ( 0 != (rv = acl_parse ( pb, acl_str, aci, NULL )) ) {
  183. slapi_log_error (SLAPI_LOG_FATAL, plugin_name,
  184. "ACL PARSE ERR(rv=%d): %s\n", rv, acl_str );
  185. slapi_ch_free ( (void **) &acl_str );
  186. acllist_free_aci ( aci );
  187. return 1;
  188. }
  189. /* Now add it to the list */
  190. if ( 0 != (rv =__acllist_add_aci ( aci ))) {
  191. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  192. "ACL ADD ACI ERR(rv=%d): %s\n", rv, acl_str );
  193. slapi_ch_free ( (void **) &acl_str );
  194. acllist_free_aci ( aci );
  195. return 1;
  196. }
  197. slapi_ch_free ( (void **) &acl_str );
  198. acl_regen_aclsignature ();
  199. if ( aci->aci_elevel == ACI_ELEVEL_USERDN_ANYONE)
  200. aclanom_invalidateProfile ();
  201. return 0;
  202. }
  203. /* This routine must be called with the acicache write lock taken */
  204. static int
  205. __acllist_add_aci ( aci_t *aci )
  206. {
  207. int rv = 0; /* OK */
  208. AciContainer *aciListHead;
  209. AciContainer *head;
  210. PRUint32 i;
  211. aciListHead = acllist_get_aciContainer_new ( );
  212. slapi_sdn_set_ndn_byval ( aciListHead->acic_sdn, slapi_sdn_get_ndn ( aci->aci_sdn ) );
  213. /* insert the aci */
  214. switch (avl_insert ( &acllistRoot, aciListHead, __acllist_aciContainer_node_cmp,
  215. __acllist_aciContainer_node_dup ) ) {
  216. case 1: /* duplicate ACL on the same entry */
  217. /* Find the node that contains the acl. */
  218. if ( NULL == (head = (AciContainer *) avl_find( acllistRoot, aciListHead,
  219. (IFP) __acllist_aciContainer_node_cmp ) ) ) {
  220. slapi_log_error ( SLAPI_PLUGIN_ACL, plugin_name,
  221. "Can't insert the acl in the tree\n");
  222. rv = 1;
  223. } else {
  224. aci_t *t_aci;;
  225. /* Attach the list */
  226. t_aci = head->acic_list;;
  227. while ( t_aci && t_aci->aci_next )
  228. t_aci = t_aci->aci_next;
  229. /* Now add the new one to the end of the list */
  230. t_aci->aci_next = aci;
  231. slapi_log_error ( SLAPI_LOG_ACL, plugin_name, "Added the ACL:%s to existing container:[%d]%s\n",
  232. aci->aclName, head->acic_index, slapi_sdn_get_ndn( head->acic_sdn ));
  233. }
  234. /* now free the tmp container */
  235. aciListHead->acic_list = NULL;
  236. __acllist_free_aciContainer ( &aciListHead );
  237. break;
  238. default:
  239. /* The container is inserted. Now hook up the aci and setup the
  240. * container index. Donot free the "aciListHead" here.
  241. */
  242. aciListHead->acic_list = aci;
  243. /*
  244. * First, see if we have an open slot or not - -if we have reuse it
  245. */
  246. i = 0;
  247. while ( (i < currContainerIndex) && aciContainerArray[i] )
  248. i++;
  249. if ( currContainerIndex >= (maxContainerIndex - 2)) {
  250. maxContainerIndex += CONTAINER_INCR;
  251. aciContainerArray = (AciContainer **) slapi_ch_realloc ( (char *) aciContainerArray,
  252. maxContainerIndex * sizeof ( AciContainer * ) );
  253. }
  254. aciListHead->acic_index = i;
  255. /* If i < currContainerIndex, we are just re-using an old slot. */
  256. /* We don't need to increase currContainerIndex if we just re-use an old one. */
  257. if (i == currContainerIndex)
  258. currContainerIndex++;
  259. aciContainerArray[ aciListHead->acic_index ] = aciListHead;
  260. slapi_log_error ( SLAPI_LOG_ACL, plugin_name, "Added %s to container:%d\n",
  261. slapi_sdn_get_ndn( aciListHead->acic_sdn ), aciListHead->acic_index );
  262. break;
  263. }
  264. return rv;
  265. }
  266. static int
  267. __acllist_aciContainer_node_cmp ( caddr_t d1, caddr_t d2 )
  268. {
  269. int rc =0;
  270. AciContainer *c1 = (AciContainer *) d1;
  271. AciContainer *c2 = (AciContainer *) d2;
  272. rc = slapi_sdn_compare ( c1->acic_sdn, c2->acic_sdn );
  273. return rc;
  274. }
  275. static int
  276. __acllist_aciContainer_node_dup ( caddr_t d1, caddr_t d2 )
  277. {
  278. /* we allow duplicates -- they are not exactly duplicates
  279. ** but multiple aci value on the same node
  280. */
  281. return 1;
  282. }
  283. /*
  284. * Remove the ACL
  285. *
  286. * This routine must be called with the aclcache write lock taken.
  287. * It takes in addition the one for the anom profile taken in
  288. * aclanom_invalidateProfile().
  289. * They _must_ be taken in this order or there
  290. * is a deadlock scenario with aclanom_gen_anomProfile() which
  291. * also takes them is this order.
  292. */
  293. int
  294. acllist_remove_aci_needsLock( const Slapi_DN *sdn, const struct berval *attr )
  295. {
  296. aci_t *head, *next;
  297. int rv = 0;
  298. AciContainer *aciListHead, *root;
  299. AciContainer *dContainer;
  300. int removed_anom_acl = 0;
  301. /* we used to delete the ACL by value but we don't do that anymore.
  302. * rather we delete all the acls in that entry and then repopulate it if
  303. * there are any more acls.
  304. */
  305. aciListHead = acllist_get_aciContainer_new ( );
  306. slapi_sdn_set_ndn_byval ( aciListHead->acic_sdn, slapi_sdn_get_ndn ( sdn ) );
  307. /* now find it */
  308. if ( NULL == (root = (AciContainer *) avl_find( acllistRoot, aciListHead,
  309. (IFP) __acllist_aciContainer_node_cmp ))) {
  310. /* In that case we don't have any acl for this entry. cool !!! */
  311. __acllist_free_aciContainer ( &aciListHead );
  312. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  313. "No acis to remove in this entry\n" );
  314. return 0;
  315. }
  316. head = root->acic_list;
  317. if ( head)
  318. next = head->aci_next;
  319. while ( head ) {
  320. if ( head->aci_elevel == ACI_ELEVEL_USERDN_ANYONE)
  321. removed_anom_acl = 1;
  322. /* Free the acl */
  323. acllist_free_aci ( head );
  324. head = next;
  325. next = NULL;
  326. if ( head && head->aci_next )
  327. next = head->aci_next;
  328. }
  329. root->acic_list = NULL;
  330. /* remove the container from the slot */
  331. aciContainerArray[root->acic_index] = NULL;
  332. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  333. "Removing container[%d]=%s\n", root->acic_index,
  334. slapi_sdn_get_ndn ( root->acic_sdn) );
  335. dContainer = (AciContainer *) avl_delete ( &acllistRoot, aciListHead,
  336. __acllist_aciContainer_node_cmp );
  337. __acllist_free_aciContainer ( &dContainer );
  338. acl_regen_aclsignature ();
  339. if ( removed_anom_acl )
  340. aclanom_invalidateProfile ();
  341. /*
  342. * Now read back the entry and repopulate ACLs for that entry, but
  343. * only if a specific aci was deleted, otherwise, we do a
  344. * "When Harry met Sally" and nail 'em all.
  345. */
  346. if ( attr != NULL) {
  347. if (0 != (rv = aclinit_search_and_update_aci ( 0, /* thisbeonly */
  348. sdn, /* base */
  349. NULL, /* be name */
  350. LDAP_SCOPE_BASE,
  351. ACL_ADD_ACIS,
  352. DONT_TAKE_ACLCACHE_WRITELOCK))) {
  353. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name,
  354. " Can't add the rest of the acls for entry:%s after delete\n",
  355. slapi_sdn_get_dn ( sdn ) );
  356. }
  357. }
  358. /* Now free the tmp container we used */
  359. __acllist_free_aciContainer ( &aciListHead );
  360. /*
  361. * regenerate the anonymous profile if we have deleted
  362. * anyone acls.
  363. * We don't need the aclcache readlock because the context of
  364. * this routine is we have the write lock already.
  365. */
  366. if ( removed_anom_acl )
  367. aclanom_gen_anomProfile(DONT_TAKE_ACLCACHE_READLOCK);
  368. return rv;
  369. }
  370. AciContainer *
  371. acllist_get_aciContainer_new ( )
  372. {
  373. AciContainer *head;
  374. head = (AciContainer * ) slapi_ch_calloc ( 1, sizeof ( AciContainer ) );
  375. head->acic_sdn = slapi_sdn_new ( );
  376. head->acic_index = -1;
  377. return head;
  378. }
  379. static void
  380. __acllist_free_aciContainer ( AciContainer **container)
  381. {
  382. PR_ASSERT ( container != NULL );
  383. if ( (*container)->acic_index >= 0 )
  384. aciContainerArray[ (*container)->acic_index] = NULL;
  385. if ( (*container)->acic_sdn )
  386. slapi_sdn_free ( &(*container)->acic_sdn );
  387. slapi_ch_free ( (void **) container );
  388. }
  389. void
  390. acllist_done_aciContainer ( AciContainer *head )
  391. {
  392. PR_ASSERT ( head != NULL );
  393. slapi_sdn_done ( head->acic_sdn );
  394. head->acic_index = -1;
  395. /* The caller is responsible for taking care of list */
  396. head->acic_list = NULL;
  397. }
  398. aci_t *
  399. acllist_get_aci_new ()
  400. {
  401. aci_t *aci_item;
  402. aci_item = (aci_t *) slapi_ch_calloc (1, sizeof (aci_t));
  403. aci_item->aci_sdn = slapi_sdn_new ();
  404. aci_item->aci_index = curAciIndex++;
  405. aci_item->aci_elevel = ACI_DEFAULT_ELEVEL; /* by default it's a complex */
  406. aci_item->targetAttr = (Targetattr **) slapi_ch_calloc (
  407. ACL_INIT_ATTR_ARRAY,
  408. sizeof (Targetattr *));
  409. return aci_item;
  410. }
  411. void
  412. acllist_free_aci(aci_t *item)
  413. {
  414. Targetattr **attrArray;
  415. /* The caller is responsible for taking
  416. ** care of list issue
  417. */
  418. if (item == NULL) return;
  419. slapi_sdn_free ( &item->aci_sdn );
  420. slapi_filter_free (item->target, 1);
  421. /* slapi_filter_free(item->targetAttr, 1); */
  422. attrArray = item->targetAttr;
  423. if (attrArray) {
  424. int i = 0;
  425. Targetattr *attr;
  426. while (attrArray[i] != NULL) {
  427. attr = attrArray[i];
  428. if (attr->attr_type & ACL_ATTR_FILTER) {
  429. slapi_filter_free(attr->u.attr_filter, 1);
  430. } else {
  431. slapi_ch_free ( (void **) &attr->u.attr_str );
  432. }
  433. slapi_ch_free ( (void **) &attr );
  434. i++;
  435. }
  436. /* Now free the array */
  437. slapi_ch_free ( (void **) &attrArray );
  438. }
  439. /* Now free any targetattrfilters in this aci item */
  440. if ( item->targetAttrAddFilters ) {
  441. free_targetattrfilters(&item->targetAttrAddFilters);
  442. }
  443. if ( item->targetAttrDelFilters ) {
  444. free_targetattrfilters(&item->targetAttrDelFilters);
  445. }
  446. if (item->targetFilterStr) slapi_ch_free ( (void **) &item->targetFilterStr );
  447. slapi_filter_free(item->targetFilter, 1);
  448. /* free the handle */
  449. if (item->aci_handle) ACL_ListDestroy(NULL, item->aci_handle);
  450. /* Free the name */
  451. if (item->aclName) slapi_ch_free((void **) &item->aclName);
  452. /* Free any macro info*/
  453. if (item->aci_macro) {
  454. slapi_ch_free((void **) &item->aci_macro->match_this);
  455. slapi_ch_free((void **) &item->aci_macro);
  456. }
  457. /* free at last -- free at last */
  458. slapi_ch_free ( (void **) &item );
  459. }
  460. void
  461. free_targetattrfilters( Targetattrfilter ***attrFilterArray)
  462. {
  463. if (*attrFilterArray) {
  464. int i = 0;
  465. Targetattrfilter *attrfilter;
  466. while ((*attrFilterArray)[i] != NULL) {
  467. attrfilter = (*attrFilterArray)[i];
  468. if ( attrfilter->attr_str != NULL) {
  469. slapi_ch_free ( (void **) &attrfilter->attr_str );
  470. }
  471. if (attrfilter->filter != NULL) {
  472. slapi_filter_free(attrfilter->filter, 1);
  473. }
  474. if( attrfilter->filterStr != NULL) {
  475. slapi_ch_free ( (void **) &attrfilter->filterStr );
  476. }
  477. slapi_ch_free ( (void **) &attrfilter );
  478. i++;
  479. }
  480. /* Now free the array */
  481. slapi_ch_free ( (void **) attrFilterArray );
  482. }
  483. }
  484. /* SEARCH */
  485. void
  486. acllist_init_scan (Slapi_PBlock *pb, int scope, const char *base)
  487. {
  488. Acl_PBlock *aclpb;
  489. AciContainer *root;
  490. char *basedn = NULL;
  491. int index;
  492. if ( acl_skip_access_check ( pb, NULL ) ) {
  493. return;
  494. }
  495. /*acllist_print_tree ( acllistRoot, &depth, "top", "top") ; */
  496. /* my_print ( acllistRoot );*/
  497. /* If we have an anonymous profile and I am an anom dude - let's skip it */
  498. if ( aclanom_is_client_anonymous ( pb )) {
  499. return;
  500. }
  501. aclpb = acl_get_aclpb (pb, ACLPB_BINDDN_PBLOCK );
  502. if ( !aclpb ) {
  503. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name, "Missing aclpb 4 \n" );
  504. return;
  505. }
  506. aclpb->aclpb_handles_index[0] = -1;
  507. /* If base is NULL - it means we are going to go thru all the ACLs
  508. * This is needed when we do anonymous profile generation.
  509. */
  510. if ( NULL == base ) {
  511. return;
  512. }
  513. aclpb->aclpb_state |= ACLPB_SEARCH_BASED_ON_LIST ;
  514. acllist_acicache_READ_LOCK();
  515. basedn = slapi_ch_strdup (base);
  516. index = 0;
  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 ( (void **) &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 ( (void **) &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. }