aclinit.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. #include "acl.h"
  39. static int __aclinit__RegisterLases(void);
  40. static int __aclinit__RegisterAttributes(void);
  41. static int __aclinit_handler(Slapi_Entry *e, void *callback_data);
  42. /***************************************************************************
  43. *
  44. * aclinit_main()
  45. * Main routine which is called at the server boot up time.
  46. *
  47. * 1) Reads all the ACI entries from the database and creates
  48. * the ACL list.
  49. * 2) Registers all the LASes and the GetAttrs supported by the DS.
  50. * 3) Generates anonymous profiles.
  51. * 4) Registers proxy control
  52. * 5) Creates aclpb pool
  53. *
  54. * Input:
  55. * None.
  56. *
  57. * Returns:
  58. * 0 -- no error
  59. * 1 -- Error
  60. *
  61. * Error Handling:
  62. * If any error found during the ACL generation, error is logged.
  63. *
  64. **************************************************************************/
  65. static int acl_initialized = 0;
  66. int
  67. aclinit_main()
  68. {
  69. Slapi_PBlock *pb;
  70. int rv;
  71. Slapi_DN *sdn;
  72. void *node;
  73. if (acl_initialized) {
  74. /* There is no need to do anything more */
  75. return 0;
  76. }
  77. /* Initialize the LIBACCESS ACL library */
  78. if (ACL_Init() != 0) {
  79. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  80. "ACL Library Initialization failed\n",0,0,0);
  81. return 1;
  82. }
  83. /* register all the LASes supported by the DS */
  84. if (ACL_ERR == __aclinit__RegisterLases()) {
  85. /* Error is already logged */
  86. return 1;
  87. }
  88. /* Register all the Attrs */
  89. if (ACL_ERR == __aclinit__RegisterAttributes()) {
  90. /* Error is already logged */
  91. return 1;
  92. }
  93. /*
  94. * Register to get backend state changes so we can add/remove
  95. * acis from backends that come up and go down.
  96. */
  97. slapi_register_backend_state_change((void *) NULL, acl_be_state_change_fnc);
  98. /* register the extensions */
  99. /* ONREPL Moved to the acl_init function because extensions
  100. need to be registered before any operations are issued
  101. if ( 0 != acl_init_ext() ) {
  102. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name,
  103. "Unable to initialize the extensions\n");
  104. return 1;
  105. } */
  106. /* create the mutex array */
  107. if ( 0 != aclext_alloc_lockarray ( ) ) {
  108. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name,
  109. "Unable to create the mutext array\n");
  110. return 1;
  111. }
  112. /* Allocate the pool */
  113. if ( 0 != acl_create_aclpb_pool () ) {
  114. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name,
  115. "Unable to create the acl private pool\n");
  116. return 1;
  117. }
  118. /*
  119. * Now read all the ACLs from all the backends and put it
  120. * in a list
  121. */
  122. /* initialize the ACLLIST sub-system */
  123. if ( 0 != (rv = acllist_init ( ))) {
  124. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name,
  125. "Unable to initialize the plugin:%d\n", rv );
  126. return 1;
  127. }
  128. /* Initialize the anonymous profile i.e., generate it */
  129. rv = aclanom_init ();
  130. pb = slapi_pblock_new();
  131. /*
  132. * search for the aci_attr_type attributes of all entries.
  133. *
  134. * slapi_get_fist_suffix() and slapi_get_next_suffix() do not return the
  135. * rootdse entry so we search for acis in there explicitly here.
  136. */
  137. sdn = slapi_sdn_new_dn_byval("");
  138. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  139. "Searching for all acis(scope base) at suffix ''\n");
  140. aclinit_search_and_update_aci ( 0, /* thisbeonly */
  141. sdn, /* base */
  142. NULL, /* be name*/
  143. LDAP_SCOPE_BASE, ACL_ADD_ACIS,
  144. DO_TAKE_ACLCACHE_WRITELOCK);
  145. slapi_sdn_free(&sdn);
  146. sdn = slapi_get_first_suffix( &node, 1 );
  147. while (sdn)
  148. {
  149. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  150. "Searching for all acis(scope subtree) at suffix '%s'\n",
  151. slapi_sdn_get_dn(sdn) );
  152. aclinit_search_and_update_aci ( 0, /* thisbeonly */
  153. sdn, /* base */
  154. NULL, /* be name*/
  155. LDAP_SCOPE_SUBTREE, ACL_ADD_ACIS,
  156. DO_TAKE_ACLCACHE_WRITELOCK);
  157. sdn = slapi_get_next_suffix( &node, 1 );
  158. }
  159. /* Initialize it. */
  160. acl_initialized = 1;
  161. /* generate the signatures */
  162. acl_set_aclsignature ( aclutil_gen_signature ( 100 ) );
  163. /* Initialize the user-group cache */
  164. rv = aclgroup_init ( );
  165. aclanom_gen_anomProfile (DO_TAKE_ACLCACHE_READLOCK);
  166. /* Register both of the proxied authorization controls (version 1 and 2) */
  167. slapi_register_supported_control( LDAP_CONTROL_PROXYAUTH,
  168. SLAPI_OPERATION_SEARCH | SLAPI_OPERATION_COMPARE
  169. | SLAPI_OPERATION_ADD | SLAPI_OPERATION_DELETE
  170. | SLAPI_OPERATION_MODIFY | SLAPI_OPERATION_MODDN
  171. | SLAPI_OPERATION_EXTENDED );
  172. slapi_register_supported_control( LDAP_CONTROL_PROXIEDAUTH,
  173. SLAPI_OPERATION_SEARCH | SLAPI_OPERATION_COMPARE
  174. | SLAPI_OPERATION_ADD | SLAPI_OPERATION_DELETE
  175. | SLAPI_OPERATION_MODIFY | SLAPI_OPERATION_MODDN
  176. | SLAPI_OPERATION_EXTENDED );
  177. slapi_pblock_destroy ( pb );
  178. return 0;
  179. }
  180. /*
  181. * This routine is the one that scans for acis and either adds them
  182. * to the internal cache (op==ACL_ADD_ACIS) or deletes them
  183. * (op==ACL_REMOVE_ACIS).
  184. *
  185. * If thisbeonly is 0 the search
  186. * is conducted on the base with the specifed scope and be_name is ignored.
  187. * This is used at startup time where we iterate over all suffixes, searching
  188. * for all the acis in the DIT to load the ACL cache.
  189. *
  190. * If thisbeonly is 1 then then a be_name must be specified.
  191. * In this case we will search in that backend ONLY.
  192. * This is used in the case where a backend is turned on and off--in this
  193. * case we only want to add/remove the acis in that particular backend and
  194. * not for example in any backends below that one.
  195. */
  196. int
  197. aclinit_search_and_update_aci ( int thisbeonly, const Slapi_DN *base,
  198. char *be_name, int scope, int op,
  199. acl_lock_flag_t lock_flag )
  200. {
  201. char *attrs[2] = { "aci", NULL };
  202. /* Tell __aclinit_handler whether it's an add or a delete */
  203. Slapi_PBlock *aPb;
  204. LDAPControl **ctrls=NULL;
  205. int retval;
  206. struct berval *bval;
  207. aclinit_handler_callback_data_t call_back_data;
  208. PR_ASSERT( lock_flag == DONT_TAKE_ACLCACHE_WRITELOCK ||
  209. lock_flag == DO_TAKE_ACLCACHE_WRITELOCK);
  210. if ( thisbeonly && be_name == NULL) {
  211. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name,
  212. "Error: This be_name must be specified.\n", 0, 0, 0);
  213. return -1;
  214. }
  215. /*
  216. * We need to explicitly request (objectclass=ldapsubentry)
  217. * in order to get all the subentry acis too.
  218. * Note that subentries can be added under subentries (although its not
  219. * recommended) so that
  220. * there may be non-trivial acis under a subentry.
  221. */
  222. /* Use new search internal API */
  223. /* and never retrieve aci from a remote server */
  224. aPb = slapi_pblock_new ();
  225. /*
  226. * Set up the control to say "Only get acis from this Backend--
  227. * there may be more backends under this one.
  228. */
  229. if ( thisbeonly ) {
  230. bval = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  231. bval->bv_len = strlen(be_name) + 1;
  232. bval->bv_val = slapi_ch_strdup(be_name);
  233. ctrls = (LDAPControl **)slapi_ch_calloc( 2, sizeof(LDAPControl *));
  234. ctrls[0] = NULL;
  235. ctrls[1] = NULL;
  236. retval = slapi_build_control_from_berval(
  237. MTN_CONTROL_USE_ONE_BACKEND_OID,
  238. bval,
  239. 1 /* is critical */,
  240. ctrls);
  241. }
  242. slapi_search_internal_set_pb ( aPb,
  243. slapi_sdn_get_dn(base),
  244. scope,
  245. "(|(aci=*)(objectclass=ldapsubentry))",
  246. attrs,
  247. 0 /* attrsonly */,
  248. ctrls /* controls: SLAPI_ARGCONTROLS */,
  249. NULL /* uniqueid */,
  250. aclplugin_get_identity (ACL_PLUGIN_IDENTITY),
  251. SLAPI_OP_FLAG_NEVER_CHAIN /* actions : get local aci only */);
  252. if (thisbeonly) {
  253. slapi_pblock_set(aPb, SLAPI_REQCONTROLS, ctrls);
  254. }
  255. call_back_data.op = op;
  256. call_back_data.retCode = 0;
  257. call_back_data.lock_flag = lock_flag;
  258. slapi_search_internal_callback_pb(aPb,
  259. &call_back_data /* callback_data */,
  260. NULL/* result_callback */,
  261. __aclinit_handler,
  262. NULL /* referral_callback */);
  263. if (thisbeonly) {
  264. slapi_ch_free((void **)&bval);
  265. }
  266. /*
  267. * This frees the control oid, the bv_val and the control itself and the
  268. * ctrls array mem by caling ldap_controls_free()--so we
  269. * don't need to do it ourselves.
  270. */
  271. slapi_pblock_destroy (aPb);
  272. return call_back_data.retCode;
  273. }
  274. /***************************************************************************
  275. *
  276. * __aclinit_handler
  277. *
  278. * For each entry, finds if there is any ACL in thet entry. If there is
  279. * then the ACL is processed and stored in the ACL LIST.
  280. *
  281. *
  282. * Input:
  283. *
  284. *
  285. * Returns:
  286. * None.
  287. *
  288. * Error Handling:
  289. * If any error found during the ACL generation, the ACL is
  290. * logged. Also, set in the callback_data so that caller can act upon it.
  291. *
  292. **************************************************************************/
  293. static int
  294. __aclinit_handler ( Slapi_Entry *e, void *callback_data)
  295. {
  296. Slapi_Attr *attr;
  297. aclinit_handler_callback_data_t *call_back_data =
  298. (aclinit_handler_callback_data_t*)callback_data;
  299. Slapi_DN *e_sdn;
  300. int rv;
  301. Slapi_Value *sval=NULL;
  302. call_back_data->retCode = 0; /* assume success--if there's an error we overwrite it */
  303. if (e != NULL) {
  304. e_sdn = slapi_entry_get_sdn ( e );
  305. /*
  306. * Take the write lock around all the mods--so that
  307. * other operations will see the acicache either before the whole mod
  308. * or after but not, as it was before, during the mod.
  309. * This is in line with the LDAP concept of the operation
  310. * on the whole entry being the atomic unit.
  311. *
  312. */
  313. if ( call_back_data->op == ACL_ADD_ACIS ) {
  314. slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
  315. "Adding acis for entry '%s'\n", slapi_sdn_get_dn(e_sdn));
  316. slapi_entry_attr_find ( e, aci_attr_type, &attr );
  317. if ( attr ) {
  318. const struct berval *attrValue;
  319. int i;
  320. if ( call_back_data->lock_flag == DO_TAKE_ACLCACHE_WRITELOCK) {
  321. acllist_acicache_WRITE_LOCK();
  322. }
  323. i= slapi_attr_first_value ( attr, &sval );
  324. while(i != -1) {
  325. attrValue = slapi_value_get_berval(sval);
  326. if ( 0 != (rv=acllist_insert_aci_needsLock (e_sdn, attrValue))) {
  327. aclutil_print_err(rv, e_sdn, attrValue, NULL);
  328. /* We got an error; Log it and then march along */
  329. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name,
  330. "Error: This (%s) ACL will not be considered for evaluation"
  331. " because of syntax errors.\n",
  332. attrValue->bv_val ? attrValue->bv_val: "NULL", 0, 0);
  333. call_back_data->retCode = rv;
  334. }
  335. i= slapi_attr_next_value( attr, i, &sval );
  336. }/* while */
  337. if ( call_back_data->lock_flag == DO_TAKE_ACLCACHE_WRITELOCK) {
  338. acllist_acicache_WRITE_UNLOCK();
  339. }
  340. }
  341. } else if (call_back_data->op == ACL_REMOVE_ACIS) {
  342. /* Here we are deleting the acis. */
  343. slapi_log_error ( SLAPI_LOG_ACL, plugin_name, "Removing acis\n");
  344. if ( call_back_data->lock_flag == DO_TAKE_ACLCACHE_WRITELOCK) {
  345. acllist_acicache_WRITE_LOCK();
  346. }
  347. if ( 0 != (rv=acllist_remove_aci_needsLock(e_sdn, NULL))) {
  348. aclutil_print_err(rv, e_sdn, NULL, NULL);
  349. /* We got an error; Log it and then march along */
  350. slapi_log_error ( SLAPI_LOG_FATAL, plugin_name,
  351. "Error: ACls not deleted from %s\n",
  352. e_sdn, 0, 0);
  353. call_back_data->retCode = rv;
  354. }
  355. if ( call_back_data->lock_flag == DO_TAKE_ACLCACHE_WRITELOCK) {
  356. acllist_acicache_WRITE_UNLOCK();
  357. }
  358. }
  359. }
  360. /*
  361. * If we get here it's success.
  362. * The call_back_data->error is the error code that counts as it's the
  363. * one that the original caller will see--this routine is called off a callbacl.
  364. */
  365. return ACL_FALSE; /* "local" error code--it's 0 */
  366. }
  367. /***************************************************************************
  368. *
  369. * __acl__RegisterAttributes
  370. *
  371. * Register all the attributes supported by the DS.
  372. *
  373. * Input:
  374. * None.
  375. *
  376. * Returns:
  377. * ACL_OK - No error
  378. * ACL_ERR - in case of errror
  379. *
  380. * Error Handling:
  381. * None.
  382. *
  383. **************************************************************************/
  384. static int
  385. __aclinit__RegisterAttributes(void)
  386. {
  387. ACLMethod_t methodinfo;
  388. NSErr_t errp;
  389. int rv;
  390. memset (&errp, 0, sizeof(NSErr_t));
  391. rv = ACL_MethodRegister(&errp, DS_METHOD, &methodinfo);
  392. if (rv < 0) {
  393. acl_print_acllib_err(&errp, NULL);
  394. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  395. "Unable to Register the methods\n", 0,0,0);
  396. return ACL_ERR;
  397. }
  398. rv = ACL_MethodSetDefault (&errp, methodinfo);
  399. if (rv < 0) {
  400. acl_print_acllib_err(&errp, NULL);
  401. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  402. "Unable to Set the default method\n", 0,0,0);
  403. return ACL_ERR;
  404. }
  405. rv = ACL_AttrGetterRegister(&errp, ACL_ATTR_IP, DS_LASIpGetter,
  406. methodinfo, ACL_DBTYPE_ANY, ACL_AT_FRONT, NULL);
  407. if (rv < 0) {
  408. acl_print_acllib_err(&errp, NULL);
  409. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  410. "Unable to Register Attr ip\n", 0,0,0);
  411. return ACL_ERR;
  412. }
  413. rv = ACL_AttrGetterRegister(&errp, ACL_ATTR_DNS, DS_LASDnsGetter,
  414. methodinfo, ACL_DBTYPE_ANY, ACL_AT_FRONT, NULL);
  415. if (rv < 0) {
  416. acl_print_acllib_err(&errp, NULL);
  417. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  418. "Unable to Register Attr dns\n", 0,0,0);
  419. return ACL_ERR;
  420. }
  421. return ACL_OK;
  422. }
  423. /***************************************************************************
  424. *
  425. * __acl__RegisterLases
  426. * Register all the LASes supported by the DS.
  427. *
  428. * The DS doesnot support user/group. We have defined our own LAS
  429. * so that we can display/print an error when the LAS is invoked.
  430. * Input:
  431. * None.
  432. *
  433. * Returns:
  434. * ACL_OK - No error
  435. * ACL_ERR - in case of errror
  436. *
  437. * Error Handling:
  438. * None.
  439. *
  440. **************************************************************************/
  441. static int
  442. __aclinit__RegisterLases(void)
  443. {
  444. if (ACL_LasRegister(NULL, DS_LAS_USER, (LASEvalFunc_t) DS_LASUserEval,
  445. (LASFlushFunc_t) NULL) < 0) {
  446. slapi_log_error (SLAPI_LOG_FATAL, plugin_name,
  447. "Unable to register USER Las\n",0,0,0);
  448. return ACL_ERR;
  449. }
  450. if (ACL_LasRegister(NULL, DS_LAS_GROUP, (LASEvalFunc_t) DS_LASGroupEval,
  451. (LASFlushFunc_t) NULL) < 0) {
  452. slapi_log_error (SLAPI_LOG_FATAL, plugin_name,
  453. "Unable to register GROUP Las\n",0,0,0);
  454. return ACL_ERR;
  455. }
  456. if (ACL_LasRegister(NULL, DS_LAS_GROUPDN, (LASEvalFunc_t)DS_LASGroupDnEval,
  457. (LASFlushFunc_t)NULL) < 0) {
  458. slapi_log_error (SLAPI_LOG_FATAL, plugin_name,
  459. "Unable to register GROUPDN Las\n",0,0,0);
  460. return ACL_ERR;
  461. }
  462. if (ACL_LasRegister(NULL, DS_LAS_ROLEDN, (LASEvalFunc_t)DS_LASRoleDnEval,
  463. (LASFlushFunc_t)NULL) < 0) {
  464. slapi_log_error (SLAPI_LOG_FATAL, plugin_name,
  465. "Unable to register ROLEDN Las\n",0,0,0);
  466. return ACL_ERR;
  467. }
  468. if (ACL_LasRegister(NULL, DS_LAS_USERDN, (LASEvalFunc_t)DS_LASUserDnEval,
  469. (LASFlushFunc_t)NULL) < 0) {
  470. slapi_log_error (SLAPI_LOG_FATAL, plugin_name,
  471. "Unable to register USERDN Las\n",0,0,0);
  472. return ACL_ERR;
  473. }
  474. if (ACL_LasRegister(NULL, DS_LAS_USERDNATTR,
  475. (LASEvalFunc_t)DS_LASUserDnAttrEval,
  476. (LASFlushFunc_t)NULL) < 0) {
  477. slapi_log_error (SLAPI_LOG_FATAL, plugin_name,
  478. "Unable to register USERDNATTR Las\n",0,0,0);
  479. return ACL_ERR;
  480. }
  481. if (ACL_LasRegister(NULL, DS_LAS_AUTHMETHOD,
  482. (LASEvalFunc_t)DS_LASAuthMethodEval,
  483. (LASFlushFunc_t)NULL) < 0) {
  484. slapi_log_error (SLAPI_LOG_FATAL, plugin_name,
  485. "Unable to register CLIENTAUTHTYPE Las\n",0,0,0);
  486. return ACL_ERR;
  487. }
  488. if (ACL_LasRegister(NULL, DS_LAS_GROUPDNATTR,
  489. (LASEvalFunc_t)DS_LASGroupDnAttrEval,
  490. (LASFlushFunc_t)NULL) < 0) {
  491. slapi_log_error (SLAPI_LOG_FATAL, plugin_name,
  492. "Unable to register GROUPDNATTR Las\n",0,0,0);
  493. return ACL_ERR;
  494. }
  495. if (ACL_LasRegister(NULL, DS_LAS_USERATTR,
  496. (LASEvalFunc_t)DS_LASUserAttrEval,
  497. (LASFlushFunc_t)NULL) < 0) {
  498. slapi_log_error (SLAPI_LOG_FATAL, plugin_name,
  499. "Unable to register USERATTR Las\n",0,0,0);
  500. return ACL_ERR;
  501. }
  502. return ACL_OK;
  503. }