cos.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 <stdio.h>
  42. #include <string.h>
  43. #include "portable.h"
  44. #include "nspr.h"
  45. #include "slapi-plugin.h"
  46. #include "slapi-private.h"
  47. #include "cos_cache.h"
  48. #include "vattr_spi.h"
  49. /* get file mode flags for unix */
  50. #ifndef _WIN32
  51. #include <sys/stat.h>
  52. #endif
  53. /*** secret slapd stuff ***/
  54. /*
  55. these are required here because they are not available
  56. in any public header. They must exactly match their
  57. counterparts in the server or they will fail to work
  58. correctly.
  59. */
  60. /*** from proto-slap.h ***/
  61. int slapd_log_error_proc( char *subsystem, char *fmt, ... );
  62. /*** end secrets ***/
  63. #define COS_PLUGIN_SUBSYSTEM "cos-plugin" /* used for logging */
  64. /* subrelease in the following version info is for odd-ball cos releases
  65. * which do not fit into a general release, this can be used for beta releases
  66. * and other (this version stuff is really to help outside applications which
  67. * may wish to update cos decide whether the cos version they want to update to
  68. * is a higher release than the installed plugin)
  69. *
  70. * note: release origin is 00 for directory server
  71. * sub-release should be:
  72. * 50 for initial RTM products
  73. * from 0 increasing for alpha/beta releases
  74. * from 51 increasing for patch releases
  75. */
  76. #define COS_VERSION 0x00050050 /* version format: 0x release origin 00 major 05 minor 00 sub-release 00 */
  77. /* other function prototypes */
  78. int cos_init( Slapi_PBlock *pb );
  79. int cos_compute(computed_attr_context *c,char* type,Slapi_Entry *e,slapi_compute_output_t outputfn);
  80. int cos_start( Slapi_PBlock *pb );
  81. int cos_close( Slapi_PBlock *pb );
  82. int cos_post_op( Slapi_PBlock *pb );
  83. static Slapi_PluginDesc pdesc = { "cos", VENDOR, DS_PACKAGE_VERSION,
  84. "class of service plugin" };
  85. static void * cos_plugin_identity = NULL;
  86. #ifdef _WIN32
  87. int *module_ldap_debug = 0;
  88. void plugin_init_debug_level(int *level_ptr)
  89. {
  90. module_ldap_debug = level_ptr;
  91. }
  92. #endif
  93. /*
  94. ** Plugin identity mgmt
  95. */
  96. void cos_set_plugin_identity(void * identity)
  97. {
  98. cos_plugin_identity=identity;
  99. }
  100. void * cos_get_plugin_identity()
  101. {
  102. return cos_plugin_identity;
  103. }
  104. int cos_version()
  105. {
  106. return COS_VERSION;
  107. }
  108. /*
  109. * cos_postop_init: registering cos_post_op
  110. * cos_post_op just calls cos_cache_change_notify, which does not have any
  111. * backend operations. Thus, no need to be in transaction. Rather, it is
  112. * harmful if putting in the transaction since tring to hold change_lock
  113. * inside of transaction would cause a deadlock.
  114. */
  115. int
  116. cos_postop_init ( Slapi_PBlock *pb )
  117. {
  118. int rc = 0;
  119. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  120. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_ADD_FN, (void *)cos_post_op ) != 0 ||
  121. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_DELETE_FN, (void *)cos_post_op ) != 0 ||
  122. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_MODIFY_FN, (void *)cos_post_op ) != 0 ||
  123. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_MODRDN_FN, (void *)cos_post_op ) != 0 )
  124. {
  125. slapi_log_error( SLAPI_LOG_FATAL, COS_PLUGIN_SUBSYSTEM,
  126. "cos_postop_init: failed to register plugin\n" );
  127. rc = -1;
  128. }
  129. return rc;
  130. }
  131. int
  132. cos_internalpostop_init ( Slapi_PBlock *pb )
  133. {
  134. int rc = 0;
  135. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  136. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  137. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
  138. (void *)cos_post_op ) != 0 ||
  139. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
  140. (void *)cos_post_op ) != 0 ||
  141. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
  142. (void *) cos_post_op ) != 0 ||
  143. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
  144. (void *) cos_post_op ) != 0 )
  145. {
  146. slapi_log_error( SLAPI_LOG_FATAL, COS_PLUGIN_SUBSYSTEM,
  147. "cos_internalpostop_init: failed to register plugin\n" );
  148. rc = -1;
  149. }
  150. return rc;
  151. }
  152. /*
  153. cos_init
  154. --------
  155. adds our callbacks to the list
  156. */
  157. int cos_init( Slapi_PBlock *pb )
  158. {
  159. int ret = 0;
  160. void * plugin_identity=NULL;
  161. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_init\n",0,0,0);
  162. /*
  163. ** Store the plugin identity for later use.
  164. ** Used for internal operations
  165. */
  166. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  167. PR_ASSERT (plugin_identity);
  168. cos_set_plugin_identity(plugin_identity);
  169. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  170. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN, (void *) cos_start ) != 0 ||
  171. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN, (void *) cos_close ) != 0 ||
  172. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION, (void *)&pdesc ) != 0 )
  173. {
  174. slapi_log_error( SLAPI_LOG_FATAL, COS_PLUGIN_SUBSYSTEM,
  175. "cos_init: failed to register plugin\n" );
  176. ret = -1;
  177. goto bailout;
  178. }
  179. ret = slapi_register_plugin("postoperation", 1 /* Enabled */,
  180. "cos_postop_init", cos_postop_init,
  181. "Class of Service postoperation plugin", NULL,
  182. plugin_identity);
  183. if ( ret < 0 ) {
  184. goto bailout;
  185. }
  186. ret = slapi_register_plugin("internalpostoperation", 1 /* Enabled */,
  187. "cos_internalpostop_init", cos_internalpostop_init,
  188. "Class of Service internalpostoperation plugin", NULL,
  189. plugin_identity);
  190. bailout:
  191. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_init\n",0,0,0);
  192. return ret;
  193. }
  194. /*
  195. cos_start
  196. ---------
  197. This function registers the computed attribute evaluator
  198. and inits the cos cache.
  199. It is called after cos_init.
  200. */
  201. int cos_start( Slapi_PBlock *pb )
  202. {
  203. int ret = 0;
  204. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_start\n",0,0,0);
  205. if( !cos_cache_init() )
  206. {
  207. LDAPDebug( LDAP_DEBUG_PLUGIN, "cos: ready for service\n",0,0,0);
  208. }
  209. else
  210. {
  211. /* problems we are hosed */
  212. cos_cache_stop();
  213. LDAPDebug( LDAP_DEBUG_ANY, "cos_start: failed to initialise\n",0,0,0);
  214. ret = -1;
  215. }
  216. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_start\n",0,0,0);
  217. return ret;
  218. }
  219. /*
  220. cos_close
  221. ---------
  222. closes down the cache
  223. */
  224. int cos_close( Slapi_PBlock *pb )
  225. {
  226. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_close\n",0,0,0);
  227. cos_cache_stop();
  228. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_close\n",0,0,0);
  229. return 0;
  230. }
  231. /*
  232. cos_compute
  233. -----------
  234. called when evaluating named attributes in a search
  235. and attributes remain unfound in the entry,
  236. this function checks the attribute for a match with
  237. those in the class of service definitions, and if a
  238. match is found, adds the attribute and value to the
  239. output list
  240. returns
  241. 0 on success
  242. 1 on outright failure
  243. -1 when doesn't know about attribute
  244. */
  245. int cos_compute(computed_attr_context *c,char* type,Slapi_Entry *e,slapi_compute_output_t outputfn)
  246. {
  247. int ret = -1;
  248. return ret;
  249. }
  250. /*
  251. cos_post_op
  252. -----------
  253. Catch all for all post operations that change entries
  254. in some way - this simply notifies the cache of a
  255. change - the cache decides if action is necessary
  256. */
  257. int cos_post_op( Slapi_PBlock *pb )
  258. {
  259. LDAPDebug( LDAP_DEBUG_TRACE, "--> cos_post_op\n",0,0,0);
  260. cos_cache_change_notify(pb);
  261. LDAPDebug( LDAP_DEBUG_TRACE, "<-- cos_post_op\n",0,0,0);
  262. return 0; /* always succeed */
  263. }