roles_plugin.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. PLUGIN_MAGIC_VENDOR_STR, PRODUCTTEXT, "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. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  72. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  73. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_MODIFY_FN,
  74. (void *)roles_post_op ) != 0 ||
  75. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_MODRDN_FN,
  76. (void *)roles_post_op ) != 0 ||
  77. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_ADD_FN,
  78. (void *) roles_post_op ) != 0 ||
  79. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_DELETE_FN,
  80. (void *) roles_post_op ) != 0 )
  81. {
  82. slapi_log_error( SLAPI_LOG_FATAL, ROLES_PLUGIN_SUBSYSTEM,
  83. "roles_postop_init: failed to register plugin\n" );
  84. rc = -1;
  85. }
  86. return rc;
  87. }
  88. int
  89. roles_internalpostop_init ( Slapi_PBlock *pb )
  90. {
  91. int rc = 0;
  92. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  93. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  94. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
  95. (void *)roles_post_op ) != 0 ||
  96. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
  97. (void *)roles_post_op ) != 0 ||
  98. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
  99. (void *) roles_post_op ) != 0 ||
  100. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
  101. (void *) roles_post_op ) != 0 )
  102. {
  103. slapi_log_error( SLAPI_LOG_FATAL, ROLES_PLUGIN_SUBSYSTEM,
  104. "roles_internalpostop_init: failed to register plugin\n" );
  105. rc = -1;
  106. }
  107. return rc;
  108. }
  109. /* roles_init
  110. ----------
  111. Initialization of the plugin
  112. */
  113. int roles_init( Slapi_PBlock *pb )
  114. {
  115. int rc = 0;
  116. void *plugin_identity = NULL;
  117. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM,
  118. "=> roles_init\n" );
  119. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  120. PR_ASSERT (plugin_identity);
  121. roles_set_plugin_identity(plugin_identity);
  122. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  123. (void *)SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  124. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  125. (void *)&pdesc ) != 0 ||
  126. slapi_pblock_set( pb, SLAPI_PLUGIN_START_FN,
  127. (void *)roles_start ) != 0 ||
  128. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  129. (void *) roles_close ) != 0 )
  130. {
  131. slapi_log_error( SLAPI_LOG_FATAL, ROLES_PLUGIN_SUBSYSTEM,
  132. "roles_init failed\n" );
  133. rc = -1;
  134. goto bailout;
  135. }
  136. rc = slapi_register_plugin("postoperation", 1 /* Enabled */,
  137. "roles_postop_init", roles_postop_init,
  138. "Roles postoperation plugin", NULL,
  139. plugin_identity);
  140. if ( rc < 0 ) {
  141. goto bailout;
  142. }
  143. rc = slapi_register_plugin("internalpostoperation", 1 /* Enabled */,
  144. "roles_internalpostop_init", roles_internalpostop_init,
  145. "Roles internalpostoperation plugin", NULL,
  146. plugin_identity);
  147. bailout:
  148. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM,
  149. "<= roles_init %d\n", rc );
  150. return rc;
  151. }
  152. /* roles_start
  153. -----------
  154. kexcoff: cache build at init or at startup ?
  155. */
  156. static int roles_start( Slapi_PBlock *pb )
  157. {
  158. int rc = 0;
  159. void **statechange_api;
  160. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM,
  161. "=> roles_start\n" );
  162. roles_cache_init();
  163. /* from Pete Rowley for vcache
  164. * PLUGIN DEPENDENCY ON STATECHANGE PLUGIN
  165. *
  166. * register objectclasses which indicate a
  167. * role configuration entry, and therefore
  168. * a globally significant change for the vcache
  169. */
  170. if(!slapi_apib_get_interface(StateChange_v1_0_GUID, &statechange_api))
  171. {
  172. statechange_register(statechange_api, STATECHANGE_ROLES_ID, NULL, STATECHANGE_ROLES_CONFG_FILTER, &vattr_global_invalidate, (notify_callback) statechange_vattr_cache_invalidator_callback(statechange_api));
  173. }
  174. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM,
  175. "<= roles_start %d\n", rc );
  176. return rc;
  177. }
  178. /* roles_close
  179. -----------
  180. kexcoff: ??
  181. */
  182. static int roles_close( Slapi_PBlock *pb )
  183. {
  184. int rc = 0;
  185. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM,
  186. "=> roles_close\n" );
  187. roles_cache_stop();
  188. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM,
  189. "<= roles_close %d\n", rc );
  190. return rc;
  191. }
  192. /* roles_sp_get_value
  193. ------------------
  194. Enumerate the values of the role attribute.
  195. We do this by first locating all the roles which are in scope
  196. Then we iterate over the in-scope roles calling Slapi_Role_Check().
  197. For those which pass the check, we add their DN to the attribute's value set.
  198. */
  199. int roles_sp_get_value(vattr_sp_handle *handle,
  200. vattr_context *c,
  201. Slapi_Entry *e,
  202. char *type,
  203. Slapi_ValueSet** results,
  204. int *type_name_disposition,
  205. char** actual_type_name,
  206. int flags,
  207. int *free_flags,
  208. void *hint)
  209. {
  210. int rc = -1;
  211. rc = roles_cache_listroles_ext(c, e, 1, results);
  212. if (rc == 0)
  213. {
  214. *free_flags = SLAPI_VIRTUALATTRS_RETURNED_COPIES;
  215. *actual_type_name = strdup(NSROLEATTR);
  216. if (type_name_disposition)
  217. {
  218. *type_name_disposition = SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_EXACTLY_OR_ALIAS;
  219. }
  220. }
  221. /* Need to check the return code here because the caller
  222. doesn't understand roles return codes */
  223. return rc;
  224. }
  225. /* roles_sp_compare_value
  226. ----------------------
  227. Compare the value of the role attribute with a presented value.
  228. Return true or false to the client.
  229. */
  230. 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)
  231. {
  232. int rv;
  233. Slapi_DN the_dn;
  234. /* Extract the role's DN from the value passed in */
  235. /* possible problem here - slapi_value_get_string returns a pointer to the
  236. raw bv_val in the value, which is not guaranteed to be null terminated,
  237. but probably is for any value passed into this function */
  238. slapi_sdn_init_dn_byref(&the_dn,slapi_value_get_string(test_this));
  239. rv = roles_check(e,&the_dn,result);
  240. slapi_sdn_done(&the_dn);
  241. return rv;
  242. }
  243. int roles_sp_list_types(vattr_sp_handle *handle,Slapi_Entry *e,vattr_type_list_context *type_context,int flags)
  244. {
  245. static char* test_type_name = NSROLEATTR;
  246. int ret =0;
  247. if ( 0 == ( flags & SLAPI_VIRTUALATTRS_LIST_OPERATIONAL_ATTRS )) {
  248. /*
  249. * Operational attributes were NOT requested. Since the only
  250. * attribute type we service is nsRole which IS operational,
  251. * there is nothing for us to do in this case.
  252. */
  253. return 0;
  254. }
  255. ret = roles_cache_listroles(e, 0, NULL);
  256. if(ret == 0)
  257. {
  258. vattr_type_thang thang = {0};
  259. thang.type_name = test_type_name;
  260. thang.type_flags = SLAPI_ATTR_FLAG_OPATTR;
  261. slapi_vattrspi_add_type(type_context,&thang,SLAPI_VIRTUALATTRS_REQUEST_POINTERS);
  262. }
  263. return 0;
  264. }
  265. /* What do we do on shutdown ? */
  266. int roles_sp_cleanup()
  267. {
  268. return 0;
  269. }
  270. /* roles_post_op
  271. -----------
  272. Catch all for all post operations that change entries
  273. in some way - this simply notifies the cache of a
  274. change - the cache decides if action is necessary
  275. */
  276. static int roles_post_op( Slapi_PBlock *pb )
  277. {
  278. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "--> roles_post_op\n");
  279. roles_cache_change_notify(pb);
  280. slapi_log_error( SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "<-- roles_post_op\n");
  281. return 0; /* always succeed */
  282. }
  283. static void roles_set_plugin_identity(void * identity)
  284. {
  285. roles_plugin_identity=identity;
  286. }
  287. void * roles_get_plugin_identity()
  288. {
  289. return roles_plugin_identity;
  290. }