cos.c 7.2 KB

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