1
0

acl_ext.c 30 KB

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