roles_plugin.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. /*
  42. Code to implement server roles features
  43. */
  44. #include "slap.h"
  45. #include "vattr_spi.h"
  46. #include "roles_cache.h"
  47. #define DEFINE_STATECHANGE_STATICS 1
  48. #include "statechange.h"
  49. #define STATECHANGE_ROLES_ID "Roles"
  50. #define STATECHANGE_ROLES_CONFG_FILTER "objectclass=nsRoleDefinition"
  51. #define STATECHANGE_ROLES_ENTRY_FILTER "objectclass=*"
  52. #define ROLES_PLUGIN_SUBSYSTEM "roles-plugin" /* for logging */
  53. static void * roles_plugin_identity = NULL;
  54. static Slapi_PluginDesc pdesc = { "roles",
  55. VENDOR, DS_PACKAGE_VERSION, "roles plugin" };
  56. #ifdef _WIN32
  57. int *module_ldap_debug = 0;
  58. void plugin_init_debug_level(int *level_ptr)
  59. {
  60. module_ldap_debug = level_ptr;
  61. }
  62. #endif
  63. static int roles_start( Slapi_PBlock *pb );
  64. static int roles_post_op( Slapi_PBlock *pb );
  65. static int roles_close( Slapi_PBlock *pb );
  66. static void roles_set_plugin_identity(void * identity);
  67. int
  68. roles_postop_init ( Slapi_PBlock *pb )
  69. {
  70. int rc = 0;
  71. Slapi_Entry *plugin_entry = NULL;
  72. char *plugin_type = NULL;
  73. int postadd = SLAPI_PLUGIN_POST_ADD_FN;
  74. int postmod = SLAPI_PLUGIN_POST_MODIFY_FN;
  75. int postmdn = SLAPI_PLUGIN_POST_MODRDN_FN;
  76. int postdel = SLAPI_PLUGIN_POST_DELETE_FN;
  77. if ((slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_ENTRY, &plugin_entry) == 0) &&
  78. plugin_entry &&
  79. (plugin_type = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-plugintype")) &&
  80. plugin_type && strstr(plugin_type, "betxn")) {
  81. postadd = SLAPI_PLUGIN_BE_TXN_POST_ADD_FN;
  82. postmod = SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN;
  83. postmdn = SLAPI_PLUGIN_BE_TXN_POST_MODRDN_FN;
  84. postdel = SLAPI_PLUGIN_BE_TXN_POST_DELETE_FN;
  85. }
  86. slapi_ch_free_string(&plugin_type);
  87. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  88. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  89. slapi_pblock_set(pb, postmod, (void *)roles_post_op ) != 0 ||
  90. slapi_pblock_set(pb, postmdn, (void *)roles_post_op ) != 0 ||
  91. slapi_pblock_set(pb, postadd, (void *) roles_post_op ) != 0 ||
  92. slapi_pblock_set(pb, postdel, (void *) roles_post_op ) != 0 )
  93. {
  94. slapi_log_error( SLAPI_LOG_FATAL, ROLES_PLUGIN_SUBSYSTEM,
  95. "roles_postop_init: failed to register plugin\n" );
  96. rc = -1;
  97. }
  98. return rc;
  99. }
  100. int
  101. roles_internalpostop_init ( Slapi_PBlock *pb )
  102. {
  103. int rc = 0;
  104. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  105. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  106. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
  107. (void *)roles_post_op ) != 0 ||
  108. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
  109. (void *)roles_post_op ) != 0 ||
  110. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
  111. (void *) roles_post_op ) != 0 ||
  112. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
  113. (void *) roles_post_op ) != 0 )
  114. {
  115. slapi_log_error( SLAPI_LOG_FATAL, ROLES_PLUGIN_SUBSYSTEM,
  116. "roles_internalpostop_init: failed to register plugin\n" );
  117. rc = -1;
  118. }
  119. return rc;
  120. }
  121. /* roles_init
  122. ----------
  123. Initialization of the plugin
  124. */
  125. int roles_init( Slapi_PBlock *pb )
  126. {
  127. int rc = 0;
  128. void *plugin_identity = NULL;
  129. Slapi_Entry *plugin_entry = NULL;
  130. int is_betxn = 0;
  131. const char *plugin_type = "postoperation";
  132. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM,
  133. "=> roles_init\n" );
  134. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  135. PR_ASSERT (plugin_identity);
  136. roles_set_plugin_identity(plugin_identity);
  137. if ((slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_ENTRY, &plugin_entry) == 0) &&
  138. plugin_entry) {
  139. is_betxn = slapi_entry_attr_get_bool(plugin_entry, "nsslapd-pluginbetxn");
  140. }
  141. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  142. (void *)SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  143. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  144. (void *)&pdesc ) != 0 ||
  145. slapi_pblock_set( pb, SLAPI_PLUGIN_START_FN,
  146. (void *)roles_start ) != 0 ||
  147. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  148. (void *) roles_close ) != 0 )
  149. {
  150. slapi_log_error( SLAPI_LOG_FATAL, ROLES_PLUGIN_SUBSYSTEM,
  151. "roles_init failed\n" );
  152. rc = -1;
  153. goto bailout;
  154. }
  155. if (is_betxn) {
  156. plugin_type = "betxnpostoperation";
  157. }
  158. rc = slapi_register_plugin(plugin_type, 1 /* Enabled */,
  159. "roles_postop_init", roles_postop_init,
  160. "Roles postoperation plugin", NULL,
  161. plugin_identity);
  162. if ( rc < 0 ) {
  163. goto bailout;
  164. }
  165. if (!is_betxn) {
  166. rc = slapi_register_plugin("internalpostoperation", 1 /* Enabled */,
  167. "roles_internalpostop_init", roles_internalpostop_init,
  168. "Roles internalpostoperation plugin", NULL,
  169. plugin_identity);
  170. }
  171. bailout:
  172. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM,
  173. "<= roles_init %d\n", rc );
  174. return rc;
  175. }
  176. /* roles_start
  177. -----------
  178. kexcoff: cache build at init or at startup ?
  179. */
  180. static int roles_start( Slapi_PBlock *pb )
  181. {
  182. int rc = 0;
  183. void **statechange_api;
  184. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM,
  185. "=> roles_start\n" );
  186. roles_cache_init();
  187. /* from Pete Rowley for vcache
  188. * PLUGIN DEPENDENCY ON STATECHANGE PLUGIN
  189. *
  190. * register objectclasses which indicate a
  191. * role configuration entry, and therefore
  192. * a globally significant change for the vcache
  193. */
  194. if(!slapi_apib_get_interface(StateChange_v1_0_GUID, &statechange_api))
  195. {
  196. statechange_register(statechange_api,
  197. STATECHANGE_ROLES_ID,
  198. NULL,
  199. STATECHANGE_ROLES_CONFG_FILTER,
  200. &vattr_global_invalidate,
  201. (notify_callback) statechange_vattr_cache_invalidator_callback(statechange_api));
  202. }
  203. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM,
  204. "<= roles_start %d\n", rc );
  205. return rc;
  206. }
  207. /* roles_close
  208. -----------
  209. kexcoff: ??
  210. */
  211. static int roles_close( Slapi_PBlock *pb )
  212. {
  213. void **statechange_api;
  214. int rc = 0;
  215. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM,
  216. "=> roles_close\n" );
  217. roles_cache_stop();
  218. if(!slapi_apib_get_interface(StateChange_v1_0_GUID, &statechange_api))
  219. {
  220. statechange_unregister(statechange_api,
  221. NULL,
  222. STATECHANGE_ROLES_CONFG_FILTER,
  223. (notify_callback) statechange_vattr_cache_invalidator_callback(statechange_api));
  224. }
  225. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM,
  226. "<= roles_close %d\n", rc );
  227. return rc;
  228. }
  229. /* roles_sp_get_value
  230. ------------------
  231. Enumerate the values of the role attribute.
  232. We do this by first locating all the roles which are in scope
  233. Then we iterate over the in-scope roles calling Slapi_Role_Check().
  234. For those which pass the check, we add their DN to the attribute's value set.
  235. */
  236. int roles_sp_get_value(vattr_sp_handle *handle,
  237. vattr_context *c,
  238. Slapi_Entry *e,
  239. char *type,
  240. Slapi_ValueSet** results,
  241. int *type_name_disposition,
  242. char** actual_type_name,
  243. int flags,
  244. int *free_flags,
  245. void *hint)
  246. {
  247. int rc = -1;
  248. rc = roles_cache_listroles_ext(c, e, 1, results);
  249. if (rc == 0)
  250. {
  251. *free_flags = SLAPI_VIRTUALATTRS_RETURNED_COPIES;
  252. *actual_type_name = slapi_ch_strdup(NSROLEATTR);
  253. if (type_name_disposition)
  254. {
  255. *type_name_disposition = SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_EXACTLY_OR_ALIAS;
  256. }
  257. }
  258. /* Need to check the return code here because the caller
  259. doesn't understand roles return codes */
  260. return rc;
  261. }
  262. /* roles_sp_compare_value
  263. ----------------------
  264. Compare the value of the role attribute with a presented value.
  265. Return true or false to the client.
  266. */
  267. int roles_sp_compare_value(vattr_sp_handle *handle, vattr_context *c, Slapi_Entry *e, char *type, Slapi_Value *test_this, int* result,int flags, void *hint)
  268. {
  269. int rv;
  270. Slapi_DN the_dn;
  271. /* Extract the role's DN from the value passed in */
  272. /* possible problem here - slapi_value_get_string returns a pointer to the
  273. raw bv_val in the value, which is not guaranteed to be null terminated,
  274. but probably is for any value passed into this function */
  275. slapi_sdn_init_dn_byref(&the_dn,slapi_value_get_string(test_this));
  276. rv = roles_check(e,&the_dn,result);
  277. slapi_sdn_done(&the_dn);
  278. return rv;
  279. }
  280. int roles_sp_list_types(vattr_sp_handle *handle,Slapi_Entry *e,vattr_type_list_context *type_context,int flags)
  281. {
  282. static char* test_type_name = NSROLEATTR;
  283. int ret =0;
  284. if ( 0 == ( flags & SLAPI_VIRTUALATTRS_LIST_OPERATIONAL_ATTRS )) {
  285. /*
  286. * Operational attributes were NOT requested. Since the only
  287. * attribute type we service is nsRole which IS operational,
  288. * there is nothing for us to do in this case.
  289. */
  290. return 0;
  291. }
  292. ret = roles_cache_listroles(e, 0, NULL);
  293. if(ret == 0)
  294. {
  295. vattr_type_thang thang = {0};
  296. thang.type_name = test_type_name;
  297. thang.type_flags = SLAPI_ATTR_FLAG_OPATTR;
  298. slapi_vattrspi_add_type(type_context,&thang,SLAPI_VIRTUALATTRS_REQUEST_POINTERS);
  299. }
  300. return 0;
  301. }
  302. /* What do we do on shutdown ? */
  303. int roles_sp_cleanup()
  304. {
  305. return 0;
  306. }
  307. /* roles_post_op
  308. -----------
  309. Catch all for all post operations that change entries
  310. in some way - this simply notifies the cache of a
  311. change - the cache decides if action is necessary
  312. */
  313. static int roles_post_op( Slapi_PBlock *pb )
  314. {
  315. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "--> roles_post_op\n");
  316. roles_cache_change_notify(pb);
  317. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "<-- roles_post_op\n");
  318. return SLAPI_PLUGIN_SUCCESS; /* always succeed */
  319. }
  320. static void roles_set_plugin_identity(void * identity)
  321. {
  322. roles_plugin_identity=identity;
  323. }
  324. void * roles_get_plugin_identity()
  325. {
  326. return roles_plugin_identity;
  327. }