acl_ext.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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. * END COPYRIGHT BLOCK **/
  6. #include "acl.h"
  7. static void acl__done_aclpb ( struct acl_pblock *aclpb );
  8. static void acl__dump_stats ( struct acl_pblock *aclpb , const char *block_type);
  9. static Acl_PBlock * acl__get_aclpb_from_pool ( );
  10. static int acl__put_aclpb_back_to_pool ( Acl_PBlock *aclpb );
  11. static Acl_PBlock * acl__malloc_aclpb ( );
  12. static char * acl__get_aclpb_type ( Acl_PBlock *aclpb );
  13. static PRLock *aclext_get_lock ();
  14. struct acl_pbqueue {
  15. Acl_PBlock *aclq_free;
  16. Acl_PBlock *aclq_busy;
  17. short aclq_nfree;
  18. short aclq_nbusy;
  19. PRLock *aclq_lock;
  20. };
  21. typedef struct acl_pbqueue Acl_PBqueue;
  22. static Acl_PBqueue *aclQueue;
  23. /* structure with information for each extension */
  24. typedef struct acl_ext
  25. {
  26. char *object_name; /* name of the object extended */
  27. int object_type; /* handle to the extended object */
  28. int handle; /* extension handle */
  29. } acl_ext;
  30. static acl_ext acl_ext_list [ACL_EXT_ALL];
  31. /*
  32. * EXTENSION INITIALIZATION, CONSTRUCTION, & DESTRUCTION
  33. *
  34. */
  35. int
  36. acl_init_ext ()
  37. {
  38. int rc;
  39. acl_ext_list[ACL_EXT_OPERATION].object_name = SLAPI_EXT_OPERATION;
  40. rc = slapi_register_object_extension(plugin_name, SLAPI_EXT_OPERATION,
  41. acl_operation_ext_constructor,
  42. acl_operation_ext_destructor,
  43. &acl_ext_list[ACL_EXT_OPERATION].object_type,
  44. &acl_ext_list[ACL_EXT_OPERATION].handle);
  45. if ( rc != 0 ) return rc;
  46. acl_ext_list[ACL_EXT_CONNECTION].object_name = SLAPI_EXT_CONNECTION;
  47. rc = slapi_register_object_extension(plugin_name, SLAPI_EXT_CONNECTION,
  48. acl_conn_ext_constructor,
  49. acl_conn_ext_destructor,
  50. &acl_ext_list[ACL_EXT_CONNECTION].object_type,
  51. &acl_ext_list[ACL_EXT_CONNECTION].handle);
  52. return rc;
  53. }
  54. /* Interface to get the extensions */
  55. void *
  56. acl_get_ext (ext_type type, void *object)
  57. {
  58. struct acl_ext ext;
  59. void *data;
  60. if ( type >= ACL_EXT_ALL ) {
  61. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  62. "Invalid extension type:%d\n", type );
  63. return NULL;
  64. }
  65. /* find the requested extension */
  66. ext = acl_ext_list [type];
  67. data = slapi_get_object_extension(ext.object_type, object, ext.handle);
  68. return data;
  69. }
  70. void
  71. acl_set_ext (ext_type type, void *object, void *data)
  72. {
  73. if ( type >= 0 && type < ACL_EXT_ALL )
  74. {
  75. struct acl_ext ext = acl_ext_list [type];
  76. slapi_set_object_extension ( ext.object_type, object, ext.handle, data );
  77. }
  78. }
  79. /****************************************************************************
  80. * Global lock array so that private extension between connection and operation
  81. * co-exist
  82. *
  83. ******************************************************************************/
  84. struct ext_lockArray {
  85. PRLock **lockArray;
  86. int numlocks;
  87. };
  88. static struct ext_lockArray extLockArray;
  89. /* PKBxxx: make this a configurable. Start with 2 * maxThreads */
  90. #define ACLEXT_MAX_LOCKS 40
  91. int
  92. aclext_alloc_lockarray ( )
  93. {
  94. int i;
  95. PRLock *lock;
  96. extLockArray.lockArray =
  97. (PRLock **) slapi_ch_calloc ( ACLEXT_MAX_LOCKS, sizeof ( PRLock *) );
  98. for ( i =0; i < ACLEXT_MAX_LOCKS; i++) {
  99. if (NULL == (lock = PR_NewLock()) ) {
  100. slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
  101. "Unable to allocate locks used for private extension\n");
  102. return 1;
  103. }
  104. extLockArray.lockArray[i] = lock;
  105. }
  106. extLockArray.numlocks = ACLEXT_MAX_LOCKS;
  107. return 0;
  108. }
  109. static PRUint32 slot_id =0;
  110. static PRLock *
  111. aclext_get_lock ()
  112. {
  113. PRUint16 slot = slot_id % ACLEXT_MAX_LOCKS;
  114. slot_id++;
  115. return ( extLockArray.lockArray[slot] );
  116. }
  117. /****************************************************************************/
  118. /* CONNECTION EXTENSION SPECIFIC */
  119. /****************************************************************************/
  120. void *
  121. acl_conn_ext_constructor ( void *object, void *parent )
  122. {
  123. struct acl_cblock *ext = NULL;
  124. ext = (struct acl_cblock * ) slapi_ch_calloc (1, sizeof (struct acl_cblock ) );
  125. if (( ext->aclcb_lock = aclext_get_lock () ) == NULL ) {
  126. slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
  127. "Unable to get Read/Write lock for CONNECTION extension\n");
  128. slapi_ch_free ( (void **) &ext );
  129. return NULL;
  130. }
  131. ext->aclcb_sdn = slapi_sdn_new ();
  132. /* store the signatures */
  133. ext->aclcb_aclsignature = acl_get_aclsignature();
  134. ext->aclcb_state = -1;
  135. return ext;
  136. }
  137. void
  138. acl_conn_ext_destructor ( void *ext, void *object, void *parent )
  139. {
  140. struct acl_cblock *aclcb = ext;
  141. PRLock *shared_lock;
  142. if ( NULL == aclcb ) return;
  143. PR_Lock ( aclcb->aclcb_lock );
  144. shared_lock = aclcb->aclcb_lock;
  145. acl_clean_aclEval_context ( &aclcb->aclcb_eval_context, 0 /* clean*/ );
  146. slapi_sdn_free ( &aclcb->aclcb_sdn );
  147. aclcb->aclcb_lock = NULL;
  148. slapi_ch_free ( (void **) &aclcb );
  149. PR_Unlock ( shared_lock );
  150. }
  151. /****************************************************************************/
  152. /* OPERATION EXTENSION SPECIFIC */
  153. /****************************************************************************/
  154. void *
  155. acl_operation_ext_constructor ( void *object, void *parent )
  156. {
  157. Acl_PBlock *aclpb = NULL;
  158. TNF_PROBE_0_DEBUG(acl_operation_ext_constructor_start ,"ACL","");
  159. /* This means internal operations */
  160. if ( NULL == parent) {
  161. TNF_PROBE_1_DEBUG(acl_operation_ext_constructor_end ,"ACL","",
  162. tnf_string,internal_op,"");
  163. return NULL;
  164. }
  165. aclpb = acl__get_aclpb_from_pool();
  166. if ( NULL == aclpb ) {
  167. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name,
  168. "Operation extension allocation Failed\n");
  169. }
  170. TNF_PROBE_0_DEBUG(acl_operation_ext_constructor_end ,"ACL","");
  171. return aclpb;
  172. }
  173. void
  174. acl_operation_ext_destructor ( void *ext, void *object, void *parent )
  175. {
  176. struct acl_cblock *aclcb = NULL;
  177. struct acl_pblock *aclpb = NULL;
  178. TNF_PROBE_0_DEBUG(acl_operation_ext_destructor_start ,"ACL","");
  179. if ( (NULL == parent ) || (NULL == ext)) {
  180. TNF_PROBE_1_DEBUG(acl_operation_ext_destructor_end ,"ACL","",
  181. tnf_string,internal_op,"");
  182. return;
  183. }
  184. aclpb = (Acl_PBlock *) ext;
  185. if ( (NULL == aclpb) ||
  186. (NULL == aclpb->aclpb_pblock) ||
  187. (!(aclpb->aclpb_state & ACLPB_INITIALIZED)))
  188. goto clean_aclpb;
  189. /* get the connection extension */
  190. aclcb = (struct acl_cblock *) acl_get_ext ( ACL_EXT_CONNECTION, parent );
  191. /* We are about to get out of this connection. Move all the
  192. ** cached information to the acl private block which hangs
  193. ** from the connection struct.
  194. */
  195. if ( aclcb && aclcb->aclcb_lock &&
  196. ( (aclpb->aclpb_state & ACLPB_UPD_ACLCB_CACHE ) ||
  197. (aclpb->aclpb_state & ACLPB_INCR_ACLCB_CACHE ) ) ) {
  198. aclEvalContext *c_evalContext;
  199. int attr_only = 0;
  200. PRLock *shared_lock = aclcb->aclcb_lock;
  201. if (aclcb->aclcb_lock ) PR_Lock ( shared_lock );
  202. else {
  203. goto clean_aclpb;
  204. }
  205. if ( !aclcb->aclcb_lock ) {
  206. slapi_log_error (SLAPI_LOG_FATAL, plugin_name, "aclcb lock released! aclcb cache can't be refreshed\n");
  207. PR_Unlock ( shared_lock );
  208. goto clean_aclpb;
  209. }
  210. /* We need to refresh the aclcb cache */
  211. if ( aclpb->aclpb_state & ACLPB_UPD_ACLCB_CACHE )
  212. acl_clean_aclEval_context ( &aclcb->aclcb_eval_context, 0 /* clean*/ );
  213. if ( aclpb->aclpb_prev_entryEval_context.acle_numof_attrs ) {
  214. c_evalContext = &aclpb->aclpb_prev_entryEval_context;
  215. } else {
  216. c_evalContext = &aclpb->aclpb_curr_entryEval_context;
  217. }
  218. if (( aclpb->aclpb_state & ACLPB_INCR_ACLCB_CACHE ) &&
  219. ! ( aclpb->aclpb_state & ACLPB_UPD_ACLCB_CACHE ))
  220. attr_only = 1;
  221. acl_copyEval_context ( NULL, c_evalContext, &aclcb->aclcb_eval_context, attr_only );
  222. aclcb->aclcb_aclsignature = aclpb->aclpb_signature;
  223. if ( aclcb->aclcb_sdn && aclpb->aclpb_authorization_sdn &&
  224. (0 != slapi_sdn_compare ( aclcb->aclcb_sdn,
  225. aclpb->aclpb_authorization_sdn ) ) ) {
  226. slapi_sdn_set_ndn_byval( aclcb->aclcb_sdn,
  227. slapi_sdn_get_ndn ( aclpb->aclpb_authorization_sdn ) );
  228. }
  229. aclcb->aclcb_state = 0;
  230. aclcb->aclcb_state |= ACLCB_HAS_CACHED_EVALCONTEXT;
  231. PR_Unlock ( shared_lock );
  232. }
  233. clean_aclpb:
  234. if ( aclpb ) {
  235. if ( aclpb->aclpb_proxy ) {
  236. TNF_PROBE_0_DEBUG(acl_proxy_aclpbdoneback_start ,"ACL","");
  237. acl__done_aclpb( aclpb->aclpb_proxy );
  238. /* Put back to the Pool */
  239. acl__put_aclpb_back_to_pool ( aclpb->aclpb_proxy );
  240. aclpb->aclpb_proxy = NULL;
  241. TNF_PROBE_0_DEBUG(acl_proxy_aclpbdoneback_end ,"ACL","");
  242. }
  243. TNF_PROBE_0_DEBUG(acl_aclpbdoneback_start ,"ACL","");
  244. acl__done_aclpb( aclpb);
  245. acl__put_aclpb_back_to_pool ( aclpb );
  246. TNF_PROBE_0_DEBUG(acl_aclpbdoneback_end ,"ACL","");
  247. }
  248. TNF_PROBE_0_DEBUG(acl_operation_ext_destructor_end ,"ACL","");
  249. }
  250. /****************************************************************************/
  251. /* FUNCTIONS TO MANAGE THE ACLPB POOL */
  252. /****************************************************************************/
  253. /*
  254. * Get the right acl pblock
  255. */
  256. struct acl_pblock *
  257. acl_get_aclpb ( Slapi_PBlock *pb, int type )
  258. {
  259. Acl_PBlock *aclpb = NULL;
  260. void *op = NULL;
  261. slapi_pblock_get ( pb, SLAPI_OPERATION, &op );
  262. aclpb = (Acl_PBlock *) acl_get_ext ( ACL_EXT_OPERATION, op );
  263. if (NULL == aclpb ) return NULL;
  264. if ( type == ACLPB_BINDDN_PBLOCK )
  265. return aclpb;
  266. else if ( type == ACLPB_PROXYDN_PBLOCK )
  267. return aclpb->aclpb_proxy;
  268. else
  269. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name,
  270. "acl_get_aclpb: Invalid aclpb type %d\n", type );
  271. return NULL;
  272. }
  273. /*
  274. * Create a new proxy acl pblock
  275. *
  276. */
  277. struct acl_pblock *
  278. acl_new_proxy_aclpb( Slapi_PBlock *pb )
  279. {
  280. void *op;
  281. Acl_PBlock *aclpb = NULL;
  282. Acl_PBlock *proxy_aclpb = NULL;
  283. slapi_pblock_get ( pb, SLAPI_OPERATION, &op );
  284. aclpb = (Acl_PBlock *) acl_get_ext ( ACL_EXT_OPERATION, op );
  285. if (NULL == aclpb ) return NULL;
  286. proxy_aclpb = acl__get_aclpb_from_pool();
  287. if (NULL == proxy_aclpb) return NULL;
  288. proxy_aclpb->aclpb_type = ACLPB_TYPE_PROXY;
  289. aclpb->aclpb_proxy = proxy_aclpb;
  290. return proxy_aclpb;
  291. }
  292. static int
  293. acl__handle_config_entry (Slapi_Entry *e, void *callback_data )
  294. {
  295. *(int * )callback_data = slapi_entry_attr_get_int( e, "nsslapd-threadnumber");
  296. return 0;
  297. }
  298. /*
  299. * Create a pool of acl pblock. Created during the ACL plugin
  300. * initialization.
  301. */
  302. int
  303. acl_create_aclpb_pool ()
  304. {
  305. Acl_PBlock *aclpb;
  306. Acl_PBlock *prev_aclpb;
  307. Acl_PBlock *first_aclpb;
  308. int i;
  309. int maxThreads= 0;
  310. slapi_search_internal_callback( "cn=config", LDAP_SCOPE_BASE, "(objectclass=*)",
  311. NULL, 0 /* attrsonly */,
  312. &maxThreads/* callback_data */,
  313. NULL /* controls */,
  314. NULL /* result_callback */,
  315. acl__handle_config_entry,
  316. NULL /* referral_callback */);
  317. /* Create a pool pf aclpb */
  318. maxThreads = 2 * maxThreads;
  319. aclQueue = ( Acl_PBqueue *) slapi_ch_calloc ( 1, sizeof (Acl_PBqueue) );
  320. aclQueue->aclq_lock = PR_NewLock();
  321. if ( NULL == aclQueue->aclq_lock ) {
  322. /* ERROR */
  323. return 1;
  324. }
  325. prev_aclpb = NULL;
  326. first_aclpb = NULL;
  327. for ( i = 0; i < maxThreads; i++ ) {
  328. aclpb = acl__malloc_aclpb ();
  329. if ( 0 == i) first_aclpb = aclpb;
  330. aclpb->aclpb_prev = prev_aclpb;
  331. if ( prev_aclpb ) prev_aclpb->aclpb_next = aclpb;
  332. prev_aclpb = aclpb;
  333. }
  334. /* Since this is the begining, everybody is in free list */
  335. aclQueue->aclq_free = first_aclpb;
  336. aclQueue->aclq_nfree = maxThreads;
  337. return 0;
  338. }
  339. /*
  340. * Get a FREE acl pblock from the pool.
  341. *
  342. */
  343. static Acl_PBlock *
  344. acl__get_aclpb_from_pool ( )
  345. {
  346. Acl_PBlock *aclpb = NULL;
  347. Acl_PBlock *t_aclpb = NULL;
  348. PR_Lock (aclQueue->aclq_lock );
  349. /* Get the first aclpb from the FREE List */
  350. aclpb = aclQueue->aclq_free;
  351. if ( aclpb ) {
  352. t_aclpb = aclpb->aclpb_next;
  353. if ( t_aclpb ) t_aclpb->aclpb_prev = NULL;
  354. aclQueue->aclq_free = t_aclpb;
  355. /* make the this an orphon */
  356. aclpb->aclpb_prev = aclpb->aclpb_next = NULL;
  357. aclQueue->aclq_nfree--;
  358. } else {
  359. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  360. "Unable to find a free aclpb\n");
  361. aclpb = acl__malloc_aclpb ();
  362. }
  363. /* Now move it to the FRONT of busy list */
  364. t_aclpb = aclQueue->aclq_busy;
  365. aclpb->aclpb_next = t_aclpb;
  366. if ( t_aclpb ) t_aclpb->aclpb_prev = aclpb;
  367. aclQueue->aclq_busy = aclpb;
  368. aclQueue->aclq_nbusy++;
  369. PR_Unlock (aclQueue->aclq_lock );
  370. return aclpb;
  371. }
  372. /*
  373. * Put the acl pblock into the FREE pool.
  374. *
  375. */
  376. static int
  377. acl__put_aclpb_back_to_pool ( Acl_PBlock *aclpb )
  378. {
  379. Acl_PBlock *p_aclpb, *n_aclpb;
  380. PR_Lock (aclQueue->aclq_lock );
  381. /* Remove it from the busy list */
  382. n_aclpb = aclpb->aclpb_next;
  383. p_aclpb = aclpb->aclpb_prev;
  384. if ( p_aclpb ) {
  385. p_aclpb->aclpb_next = n_aclpb;
  386. if ( n_aclpb ) n_aclpb->aclpb_prev = p_aclpb;
  387. } else {
  388. aclQueue->aclq_busy = n_aclpb;
  389. if ( n_aclpb ) n_aclpb->aclpb_prev = NULL;
  390. }
  391. aclQueue->aclq_nbusy--;
  392. /* Put back to the FREE list */
  393. aclpb->aclpb_prev = NULL;
  394. n_aclpb = aclQueue->aclq_free;
  395. aclpb->aclpb_next = n_aclpb;
  396. if ( n_aclpb ) n_aclpb->aclpb_prev = aclpb;
  397. aclQueue->aclq_free = aclpb;
  398. aclQueue->aclq_nfree++;
  399. PR_Unlock (aclQueue->aclq_lock );
  400. return 0;
  401. }
  402. /*
  403. * Allocate the basic acl pb
  404. *
  405. */
  406. static Acl_PBlock *
  407. acl__malloc_aclpb ( )
  408. {
  409. Acl_PBlock *aclpb = NULL;
  410. aclpb = ( Acl_PBlock *) slapi_ch_calloc ( 1, sizeof ( Acl_PBlock) );
  411. /* Now set the propert we need for ACL evaluations */
  412. if ((aclpb->aclpb_proplist = PListNew(NULL)) == NULL) {
  413. slapi_log_error (SLAPI_LOG_FATAL, plugin_name,
  414. "Unable to allocate the aclprop PList\n");
  415. return NULL;
  416. }
  417. if (PListInitProp(aclpb->aclpb_proplist, 0, DS_PROP_ACLPB, aclpb, 0) < 0) {
  418. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  419. "Unable to set the ACL PBLOCK in the Plist\n");
  420. return NULL;
  421. }
  422. if (PListInitProp(aclpb->aclpb_proplist, 0, DS_ATTR_USERDN, aclpb, 0) < 0) {
  423. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  424. "Unable to set the USER DN in the Plist\n");
  425. return NULL;
  426. }
  427. if (PListInitProp(aclpb->aclpb_proplist, 0, DS_ATTR_AUTHTYPE, aclpb, 0) < 0) {
  428. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  429. "Unable to set the AUTH TYPE in the Plist\n");
  430. return NULL;
  431. }
  432. if (PListInitProp(aclpb->aclpb_proplist, 0, DS_ATTR_ENTRY, aclpb, 0) < 0) {
  433. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  434. "Unable to set the ENTRY TYPE in the Plist\n");
  435. return NULL;
  436. }
  437. /*
  438. * ACL_ATTR_IP and ACL_ATTR_DNS are initialized lazily in the
  439. * IpGetter and DnsGetter functions.
  440. * They are removed from the aclpb property list at acl__aclpb_done()
  441. * time.
  442. */
  443. /* allocate the acleval struct */
  444. aclpb->aclpb_acleval = (ACLEvalHandle_t *) ACL_EvalNew(NULL, NULL);
  445. if (aclpb->aclpb_acleval == NULL) {
  446. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  447. "Unable to allocate the acleval block\n");
  448. return NULL;
  449. }
  450. /*
  451. * This is a libaccess routine.
  452. * Need to setup subject and resource property information
  453. */
  454. ACL_EvalSetSubject(NULL, aclpb->aclpb_acleval, aclpb->aclpb_proplist);
  455. /* allocate some space for attr name */
  456. aclpb->aclpb_Evalattr = (char *) slapi_ch_malloc (ACLPB_MAX_ATTR_LEN);
  457. aclpb->aclpb_deny_handles = (aci_t **) slapi_ch_calloc (1,
  458. ACLPB_INCR_LIST_HANDLES * sizeof (aci_t *));
  459. aclpb->aclpb_allow_handles = (aci_t **) slapi_ch_calloc (1,
  460. ACLPB_INCR_LIST_HANDLES * sizeof (aci_t *));
  461. aclpb->aclpb_deny_handles_size = ACLPB_INCR_LIST_HANDLES;
  462. aclpb->aclpb_allow_handles_size = ACLPB_INCR_LIST_HANDLES;
  463. /* allocate the array for bases */
  464. aclpb->aclpb_grpsearchbase = (char **)
  465. slapi_ch_malloc (ACLPB_INCR_BASES * sizeof(char *));
  466. aclpb->aclpb_grpsearchbase_size = ACLPB_INCR_BASES;
  467. aclpb->aclpb_numof_bases = 0;
  468. /* Make sure aclpb_search_base is initialized to NULL..tested elsewhere! */
  469. aclpb->aclpb_search_base = NULL;
  470. aclpb->aclpb_authorization_sdn = slapi_sdn_new ();
  471. aclpb->aclpb_curr_entry_sdn = slapi_sdn_new();
  472. aclpb->aclpb_aclContainer = acllist_get_aciContainer_new ();
  473. /* hash table to store macro matched values from targets */
  474. aclpb->aclpb_macro_ht = acl_ht_new();
  475. return aclpb;
  476. }
  477. /* Initializes the aclpb */
  478. void
  479. acl_init_aclpb ( Slapi_PBlock *pb , Acl_PBlock *aclpb, const char *dn, int copy_from_aclcb)
  480. {
  481. struct acl_cblock *aclcb = NULL;
  482. char *authType;
  483. void *conn;
  484. unsigned long op_type;
  485. if ( NULL == aclpb ) {
  486. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name, "acl_init_aclpb:No ACLPB\n");
  487. return;
  488. }
  489. /* See if we have initialized already */
  490. if (aclpb->aclpb_state & ACLPB_INITIALIZED) return;
  491. slapi_pblock_get ( pb, SLAPI_OPERATION_TYPE, &op_type );
  492. if ( op_type == SLAPI_OPERATION_BIND || op_type == SLAPI_OPERATION_UNBIND )
  493. return;
  494. /* We indicate the initialize here becuase, if something goes wrong, it's cleaned up
  495. ** properly.
  496. */
  497. aclpb->aclpb_state = ACLPB_INITIALIZED;
  498. /* We make an anonymous user a non null dn which is empty */
  499. if (dn && *dn != '\0' )
  500. slapi_sdn_set_ndn_byval ( aclpb->aclpb_authorization_sdn, dn );
  501. else
  502. slapi_sdn_set_ndn_byval ( aclpb->aclpb_authorization_sdn, "" );
  503. /* reset scoped entry cache to be empty */
  504. aclpb->aclpb_scoped_entry_anominfo.anom_e_nummatched = 0;
  505. if (PListAssignValue(aclpb->aclpb_proplist, DS_ATTR_USERDN,
  506. slapi_sdn_get_ndn(aclpb->aclpb_authorization_sdn), 0) < 0) {
  507. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  508. "Unable to set the USER DN in the Plist\n");
  509. return;
  510. }
  511. slapi_pblock_get ( pb, SLAPI_OPERATION_AUTHTYPE, &authType );
  512. if (PListAssignValue(aclpb->aclpb_proplist, DS_ATTR_AUTHTYPE, authType, 0) < 0) {
  513. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  514. "Unable to set the AUTH TYPE in the Plist\n");
  515. return;
  516. }
  517. /* PKBxxx: We should be getting it from the OP struct */
  518. slapi_pblock_get ( pb, SLAPI_CONN_CERT, &aclpb->aclpb_clientcert );
  519. /* See if the we have already a cached info about user's group */
  520. aclg_init_userGroup ( aclpb, dn, 0 /* get lock */ );
  521. slapi_pblock_get( pb, SLAPI_BE_MAXNESTLEVEL, &aclpb->aclpb_max_nesting_level );
  522. slapi_pblock_get( pb, SLAPI_SEARCH_SIZELIMIT, &aclpb->aclpb_max_member_sizelimit );
  523. if ( aclpb->aclpb_max_member_sizelimit == 0 ) {
  524. aclpb->aclpb_max_member_sizelimit = SLAPD_DEFAULT_LOOKTHROUGHLIMIT;
  525. }
  526. slapi_pblock_get( pb, SLAPI_OPERATION_TYPE, &aclpb->aclpb_optype );
  527. aclpb->aclpb_signature = acl_get_aclsignature();
  528. aclpb->aclpb_last_cache_result = 0;
  529. aclpb->aclpb_pblock = pb;
  530. PR_ASSERT ( aclpb->aclpb_pblock != NULL );
  531. /* get the connection */
  532. slapi_pblock_get ( pb, SLAPI_CONNECTION, &conn);
  533. aclcb = (struct acl_cblock *) acl_get_ext ( ACL_EXT_CONNECTION, conn );
  534. if (NULL == aclcb || NULL == aclcb->aclcb_lock) {
  535. /* This could happen if the client is dead and we are in
  536. ** process of abondoning this operation
  537. */
  538. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  539. "No CONNECTION extension\n");
  540. } else if ( aclcb->aclcb_state == -1 ) {
  541. /* indicate that we need to update the cache */
  542. aclpb->aclpb_state |= ACLPB_UPD_ACLCB_CACHE;
  543. aclcb->aclcb_state = 0; /* Nore this is ACLCB and not ACLPB */
  544. } else if ( copy_from_aclcb ){
  545. char *cdn;
  546. Slapi_DN *c_sdn; /* client SDN */
  547. /* check if the operation is abandoned or not.*/
  548. if ( slapi_op_abandoned ( pb ) ) {
  549. return;
  550. }
  551. slapi_pblock_get ( pb, SLAPI_CONN_DN, &cdn ); /* We *must* free cdn! */
  552. c_sdn = slapi_sdn_new_dn_passin( cdn );
  553. PR_Lock ( aclcb->aclcb_lock );
  554. /*
  555. * since PR_Lock is taken,
  556. * we can mark the connection extension ok to be destroyed.
  557. */
  558. if ( (aclcb->aclcb_aclsignature != acl_get_aclsignature()) ||
  559. ( (NULL == cdn) && aclcb->aclcb_sdn ) ||
  560. (cdn && (NULL == aclcb->aclcb_sdn )) ||
  561. (cdn && aclcb->aclcb_sdn && ( 0 != slapi_sdn_compare ( c_sdn, aclcb->aclcb_sdn ) ))) {
  562. /* cleanup the aclcb cache */
  563. acl_clean_aclEval_context ( &aclcb->aclcb_eval_context, 0 /*clean*/ );
  564. aclcb->aclcb_state = 0;
  565. aclcb->aclcb_aclsignature = 0;
  566. slapi_sdn_done ( aclcb->aclcb_sdn );
  567. }
  568. slapi_sdn_free ( &c_sdn );
  569. /* COPY the cached information from ACLCB --> ACLPB */
  570. if ( aclcb->aclcb_state & ACLCB_HAS_CACHED_EVALCONTEXT) {
  571. acl_copyEval_context ( aclpb, &aclcb->aclcb_eval_context ,
  572. &aclpb->aclpb_prev_opEval_context, 0 );
  573. aclpb->aclpb_state |= ACLPB_HAS_ACLCB_EVALCONTEXT;
  574. }
  575. PR_Unlock ( aclcb->aclcb_lock );
  576. }
  577. }
  578. /* Cleans up the aclpb */
  579. static void
  580. acl__done_aclpb ( struct acl_pblock *aclpb )
  581. {
  582. int i;
  583. int dump_aclpb_info = 0;
  584. int rc=-1;
  585. char *tmp_ptr=NULL;
  586. /*
  587. ** First, let's do some sanity checks to see if we have everything what
  588. ** it should be.
  589. */
  590. /* Nothing needs to be cleaned up in this case */
  591. if ( !aclpb->aclpb_state & ACLPB_INITIALIZED)
  592. return;
  593. /* Check the state */
  594. if (aclpb->aclpb_state & ~ACLPB_STATE_ALL) {
  595. slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
  596. "The aclpb.state value (%d) is incorrect. Exceeded the limit (%d)\n",
  597. aclpb->aclpb_state, ACLPB_STATE_ALL);
  598. dump_aclpb_info = 1;
  599. }
  600. /* acl__dump_stats ( aclpb, acl__get_aclpb_type(aclpb)); */
  601. /* reset the usergroup cache */
  602. aclg_reset_userGroup ( aclpb );
  603. if ( aclpb->aclpb_res_type & ~ACLPB_RESTYPE_ALL ) {
  604. slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
  605. "The aclpb res_type value (%d) has exceeded. Limit is (%d)\n",
  606. aclpb->aclpb_res_type, ACLPB_RESTYPE_ALL, 0 );
  607. dump_aclpb_info = 1;
  608. }
  609. if ( dump_aclpb_info ) {
  610. const char *ndn;
  611. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name,
  612. "ACLPB value is:%p\n", aclpb, 0,0 );
  613. ndn = slapi_sdn_get_ndn ( aclpb->aclpb_curr_entry_sdn );
  614. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name, "curr_entry:%p num_entries:%d curr_dn:%p\n",
  615. aclpb->aclpb_curr_entry ? (char *) aclpb->aclpb_curr_entry : "NULL",
  616. aclpb->aclpb_num_entries,
  617. ndn ? ndn : "NULL");
  618. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name, "Last attr:%p, Plist:%p acleval: %p\n",
  619. aclpb->aclpb_Evalattr ? aclpb->aclpb_Evalattr : "NULL",
  620. aclpb->aclpb_proplist ? (char *) aclpb->aclpb_proplist : "NULL",
  621. aclpb->aclpb_acleval ? (char *) aclpb->aclpb_acleval : "NULL" );
  622. }
  623. /* Now Free the contents or clean it */
  624. slapi_sdn_done ( aclpb->aclpb_curr_entry_sdn );
  625. if (aclpb->aclpb_Evalattr)
  626. aclpb->aclpb_Evalattr[0] = '\0';
  627. /* deallocate the contents of the base array */
  628. for (i=0; i < aclpb->aclpb_numof_bases; i++) {
  629. if (aclpb->aclpb_grpsearchbase[i])
  630. slapi_ch_free ( (void **)&aclpb->aclpb_grpsearchbase[i] );
  631. }
  632. aclpb->aclpb_numof_bases = 0;
  633. acl_clean_aclEval_context ( &aclpb->aclpb_prev_opEval_context, 0 /*claen*/ );
  634. acl_clean_aclEval_context ( &aclpb->aclpb_prev_entryEval_context, 0 /*clean*/ );
  635. acl_clean_aclEval_context ( &aclpb->aclpb_curr_entryEval_context, 0/*clean*/ );
  636. if ( aclpb->aclpb_client_entry ) slapi_entry_free ( aclpb->aclpb_client_entry );
  637. aclpb->aclpb_client_entry = NULL;
  638. slapi_sdn_done ( aclpb->aclpb_authorization_sdn );
  639. aclpb->aclpb_pblock = NULL;
  640. if ( aclpb->aclpb_search_base )
  641. slapi_ch_free ( (void **) &aclpb->aclpb_search_base );
  642. for ( i=0; i < aclpb->aclpb_num_deny_handles; i++ )
  643. aclpb->aclpb_deny_handles[i] = NULL;
  644. aclpb->aclpb_num_deny_handles = 0;
  645. for ( i=0; i < aclpb->aclpb_num_allow_handles; i++ )
  646. aclpb->aclpb_allow_handles[i] = NULL;
  647. aclpb->aclpb_num_allow_handles = 0;
  648. /* clear results cache */
  649. memset((char*)aclpb->aclpb_cache_result, 0,
  650. sizeof(struct result_cache)*aclpb->aclpb_last_cache_result);
  651. aclpb->aclpb_last_cache_result = 0;
  652. aclpb->aclpb_handles_index[0] = -1;
  653. aclpb->aclpb_base_handles_index[0] = -1;
  654. aclpb->aclpb_stat_acllist_scanned = 0;
  655. aclpb->aclpb_stat_aclres_matched = 0;
  656. aclpb->aclpb_stat_total_entries = 0;
  657. aclpb->aclpb_stat_anom_list_scanned = 0;
  658. aclpb->aclpb_stat_num_copycontext = 0;
  659. aclpb->aclpb_stat_num_copy_attrs = 0;
  660. aclpb->aclpb_stat_num_tmatched_acls = 0;
  661. aclpb->aclpb_clientcert = NULL;
  662. aclpb->aclpb_proxy = NULL;
  663. acllist_done_aciContainer ( aclpb->aclpb_aclContainer );
  664. /*
  665. * Here, decide which things need to be freed/removed/whatever from the
  666. * aclpb_proplist.
  667. */
  668. /*
  669. * The DS_ATTR_DNS property contains the name of the client machine.
  670. *
  671. * The value pointed to by this property is stored in the pblock--it
  672. * points to the SLAPI_CLIENT_DNS object. So, that memory will
  673. * be freed elsewhere.
  674. *
  675. * It's removed here from the aclpb_proplist as it would be an error to
  676. * allow it to persist in the aclpb which is an operation time thing.
  677. * If we leave it here the next time this aclpb gets used, the DnsGetter
  678. * is not called by LASDnsEval/ACL_GetAttribute() as it thinks the
  679. * ACL_ATTR_DNS has already been initialized.
  680. *
  681. */
  682. if ((rc = PListFindValue(aclpb->aclpb_proplist, ACL_ATTR_DNS,
  683. (void **)&tmp_ptr, NULL)) > 0) {
  684. PListDeleteProp(aclpb->aclpb_proplist, rc, NULL);
  685. }
  686. /*
  687. * Remove the DS_ATTR_IP property from the property list.
  688. * The value of this property is just the property pointer
  689. * (an unsigned long) so that gets freed too when we delete the
  690. * property.
  691. * It's removed here from the aclpb_proplist as it would be an error to
  692. * allow it to persist in the aclpb which is an operation time thing.
  693. * If we leave it here the next time this aclpb gets used, the DnsGetter
  694. * is not called by LASIpEval/ACL_GetAttribute() as it thinks the
  695. * ACL_ATTR_IP has already been initialized.
  696. */
  697. if ((rc = PListFindValue(aclpb->aclpb_proplist, ACL_ATTR_IP,
  698. (void **)&tmp_ptr, NULL)) > 0) {
  699. PListDeleteProp(aclpb->aclpb_proplist, rc, NULL);
  700. }
  701. /*
  702. * The DS_ATTR_USERDN value comes from aclpb_authorization_sdn.
  703. * This memory
  704. * is freed above using aclpb_authorization_sdn so we don't need to free it here
  705. * before overwriting the old value.
  706. */
  707. PListAssignValue(aclpb->aclpb_proplist, DS_ATTR_USERDN, NULL, 0);
  708. /*
  709. * The DS_ATTR_AUTHTYPE value is a pointer into the pblock, so
  710. * we do not need to free that memory before overwriting the value.
  711. */
  712. PListAssignValue(aclpb->aclpb_proplist, DS_ATTR_AUTHTYPE, NULL, 0);
  713. /*
  714. * DO NOT overwrite the aclpb pointer--it is initialized at malloc_aclpb
  715. * time and is kept within the aclpb.
  716. *
  717. * PListAssignValue(aclpb->aclpb_proplist, DS_PROP_ACLPB, NULL, 0);
  718. */
  719. /*
  720. * The DS_ATTR_ENTRY value was a pointer to the entry being evaluated
  721. * by the ACL code. That entry comes from outside the context of
  722. * the acl code and so is dealt with out there. Ergo, here we can just
  723. * lose the pointer to that entry.
  724. */
  725. PListAssignValue(aclpb->aclpb_proplist, DS_ATTR_ENTRY, NULL, 0);
  726. aclpb->aclpb_signature = 0;
  727. /* reset scoped entry cache to be empty */
  728. aclpb->aclpb_scoped_entry_anominfo.anom_e_nummatched = 0;
  729. /* Free up any of the string values left in the macro ht and remove
  730. * the entries.*/
  731. acl_ht_free_all_entries_and_values(aclpb->aclpb_macro_ht);
  732. /* Finally, set it to the no use state */
  733. aclpb->aclpb_state = 0;
  734. }
  735. static char *
  736. acl__get_aclpb_type ( Acl_PBlock *aclpb )
  737. {
  738. if (aclpb->aclpb_state & ACLPB_TYPE_PROXY)
  739. return ACLPB_TYPE_PROXY_STR;
  740. return ACLPB_TYPE_MAIN_STR;
  741. }
  742. static void
  743. acl__dump_stats ( struct acl_pblock *aclpb , const char *block_type)
  744. {
  745. int connid = 0;
  746. int opid = 0;
  747. Slapi_PBlock *pb = NULL;
  748. pb = aclpb->aclpb_pblock;
  749. if ( pb ) {
  750. slapi_pblock_get ( pb, SLAPI_CONN_ID, &connid );
  751. slapi_pblock_get ( pb, SLAPI_OPERATION_ID, &opid );
  752. }
  753. /* DUMP STAT INFO */
  754. slapi_log_error( SLAPI_LOG_ACL, plugin_name,
  755. "**** ACL OPERATION STAT BEGIN ( aclpb:%p Block type: %s): Conn:%d Operation:%d *******\n",
  756. aclpb, block_type, connid, opid );
  757. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "\tNumber of entries scanned: %d\n",
  758. aclpb->aclpb_stat_total_entries);
  759. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "\tNumber of times ACL List scanned: %d\n",
  760. aclpb->aclpb_stat_acllist_scanned);
  761. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "\tNumber of ACLs with target matched:%d\n",
  762. aclpb->aclpb_stat_num_tmatched_acls);
  763. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "\tNumber of times acl resource matched:%d\n",
  764. aclpb->aclpb_stat_aclres_matched);
  765. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "\tNumber of times ANOM list scanned:%d\n",
  766. aclpb->aclpb_stat_anom_list_scanned);
  767. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "\tNumber of times Context was copied:%d\n",
  768. aclpb->aclpb_stat_num_copycontext);
  769. slapi_log_error( SLAPI_LOG_ACL, plugin_name, "\tNumber of times Attrs was copied:%d\n",
  770. aclpb->aclpb_stat_num_copy_attrs);
  771. slapi_log_error( SLAPI_LOG_ACL, plugin_name, " **** ACL OPERATION STAT END *******\n");
  772. }
  773. /****************************************************************************/
  774. /* E N D */
  775. /****************************************************************************/