acl_ext.c 31 KB

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