aclinit.c 15 KB

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