acl_ext.c 30 KB

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