test_repl_session_plugin.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2010 Red Hat, Inc.
  3. * All rights reserved.
  4. *
  5. * License: GPL (version 3 or any later version).
  6. * See LICENSE for details.
  7. * END COPYRIGHT BLOCK **/
  8. #include "slapi-plugin.h"
  9. #include "repl-session-plugin.h"
  10. #include <string.h>
  11. #define REPL_SESSION_v1_0_GUID "210D7559-566B-41C6-9B03-5523BDF30880"
  12. static char *test_repl_session_plugin_name = "test_repl_session_api";
  13. /*
  14. * Plugin identifiers
  15. */
  16. static Slapi_PluginDesc test_repl_session_pdesc = {
  17. "test-repl-session-plugin",
  18. "Test Vendor",
  19. "1.0",
  20. "test replication session plugin"};
  21. static Slapi_ComponentId *test_repl_session_plugin_id = NULL;
  22. /*
  23. * Replication Session Callbacks
  24. */
  25. /*
  26. * This is called on a sender when a replication agreement is
  27. * initialized at startup. A cookie can be allocated at this
  28. * time which is passed to other callbacks on the sender side.
  29. */
  30. static void *
  31. test_repl_session_plugin_agmt_init_cb(const Slapi_DN *repl_subtree)
  32. {
  33. char *cookie = NULL;
  34. slapi_log_err(SLAPI_LOG_DEBUG, test_repl_session_plugin_name,
  35. "test_repl_session_plugin_init_cb() called for suffix \"%s\".\n",
  36. slapi_sdn_get_ndn(repl_subtree));
  37. /* allocate a string and set as the cookie */
  38. cookie = slapi_ch_smprintf("cookie test");
  39. slapi_log_err(SLAPI_LOG_DEBUG, test_repl_session_plugin_name,
  40. "test_repl_session_plugin_init_cb(): Setting cookie: \"%s\".\n",
  41. cookie);
  42. return cookie;
  43. }
  44. /*
  45. * This is called on a sender when we are about to acquire a
  46. * replica. This callback can allocate some extra data to
  47. * be sent to the replica in the start replication request.
  48. * This memory will be free'd by the replication plug-in
  49. * after it is sent. A guid string must be set that is to
  50. * be used by the receiving side to ensure that the data is
  51. * from the same replication session plug-in.
  52. *
  53. * Returning non-0 will abort the replication session. This
  54. * results in the sender going into incremental backoff mode.
  55. */
  56. static int
  57. test_repl_session_plugin_pre_acquire_cb(void *cookie, const Slapi_DN *repl_subtree, int is_total, char **data_guid, struct berval **data)
  58. {
  59. int rc = 0;
  60. slapi_log_err(SLAPI_LOG_DEBUG, test_repl_session_plugin_name,
  61. "test_repl_session_plugin_pre_acquire_cb() called for suffix \"%s\", "
  62. "is_total: \"%s\", cookie: \"%s\".\n",
  63. slapi_sdn_get_ndn(repl_subtree),
  64. is_total ? "TRUE" : "FALSE", cookie ? (char *)cookie : "NULL");
  65. /* allocate some data to be sent to the replica */
  66. *data_guid = slapi_ch_smprintf("test-guid");
  67. *data = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  68. (*data)->bv_val = slapi_ch_smprintf("test-data");
  69. (*data)->bv_len = strlen((*data)->bv_val) + 1;
  70. slapi_log_err(SLAPI_LOG_DEBUG, test_repl_session_plugin_name,
  71. "test_repl_session_plugin_pre_acquire_cb() sending data: guid: \"%s\" data: \"%s\".\n",
  72. *data_guid, (*data)->bv_val);
  73. return rc;
  74. }
  75. /*
  76. * This is called on a replica when we are about to reply to
  77. * a start replication request from a sender. This callback
  78. * can allocate some extra data to be sent to the sender in
  79. * the start replication response. This memory will be free'd
  80. * by the replication plug-in after it is sent. A guid string
  81. * must be set that is to be used by the receiving side to ensure
  82. * that the data is from the same replication session plug-in.
  83. *
  84. * Returning non-0 will abort the replication session. This
  85. * results in the sender going into incremental backoff mode.
  86. */
  87. static int
  88. test_repl_session_plugin_reply_acquire_cb(const char *repl_subtree, int is_total, char **data_guid, struct berval **data)
  89. {
  90. int rc = 0;
  91. slapi_log_err(SLAPI_LOG_DEBUG, test_repl_session_plugin_name,
  92. "test_repl_session_plugin_reply_acquire_cb() called for suffix \"%s\", is_total: \"%s\".\n",
  93. repl_subtree, is_total ? "TRUE" : "FALSE");
  94. /* allocate some data to be sent to the sender */
  95. *data_guid = slapi_ch_smprintf("test-reply-guid");
  96. *data = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  97. (*data)->bv_val = slapi_ch_smprintf("test-reply-data");
  98. (*data)->bv_len = strlen((*data)->bv_val) + 1;
  99. slapi_log_err(SLAPI_LOG_DEBUG, test_repl_session_plugin_name,
  100. "test_repl_session_plugin_reply_acquire_cb() sending data: guid: \"%s\" data: \"%s\".\n",
  101. *data_guid, (*data)->bv_val);
  102. return rc;
  103. }
  104. /*
  105. * This is called on a sender when it receives a reply to a
  106. * start replication extop that we sent to a replica. Any
  107. * extra data sent by a replication session callback on the
  108. * replica will be set here as the data parameter. The data_guid
  109. * should be checked first to ensure that the sending side is
  110. * using the same replication session plug-in before making any
  111. * assumptions about the contents of the data parameter. You
  112. * should not free data_guid or data. The replication plug-in
  113. * will take care of freeing this memory.
  114. *
  115. * Returning non-0 will abort the replication session. This
  116. * results in the sender going into incremental backoff mode.
  117. */
  118. static int
  119. test_repl_session_plugin_post_acquire_cb(void *cookie, const Slapi_DN *repl_subtree, int is_total, const char *data_guid, const struct berval *data)
  120. {
  121. int rc = 0;
  122. slapi_log_err(SLAPI_LOG_DEBUG, test_repl_session_plugin_name,
  123. "test_repl_session_plugin_post_acquire_cb() called for suffix \"%s\", "
  124. "is_total: \"%s\" cookie: \"%s\".\n",
  125. slapi_sdn_get_ndn(repl_subtree),
  126. is_total ? "TRUE" : "FALSE", cookie ? (char *)cookie : "NULL");
  127. /* log any extra data that was sent from the replica */
  128. if (data_guid && data) {
  129. slapi_log_err(SLAPI_LOG_DEBUG, test_repl_session_plugin_name,
  130. "test_repl_session_plugin_post_acquire_cb() received data: guid: \"%s\" data: \"%s\".\n",
  131. data_guid, data->bv_val);
  132. }
  133. return rc;
  134. }
  135. /*
  136. * This is called on a replica when it receives a start replication
  137. * extended operation from a sender. If the replication session
  138. * plug-in on the sender sent any extra data, it will be set here
  139. * as the data parameter. The data_guid should be checked first to
  140. * ensure that the sending side is using the same replication session
  141. * plug-in before making any assumptions about the contents of the
  142. * data parameter. You should not free data_guid or data. The
  143. * replication plug-in will take care of freeing this memory.
  144. *
  145. * Returning non-0 will abort the replication session. This
  146. * results in the sender going into incremental backoff mode.
  147. */
  148. static int
  149. test_repl_session_plugin_recv_acquire_cb(const char *repl_subtree, int is_total, const char *data_guid, const struct berval *data)
  150. {
  151. int rc = 0;
  152. slapi_log_err(SLAPI_LOG_DEBUG, test_repl_session_plugin_name,
  153. "test_repl_session_plugin_recv_acquire_cb() called for suffix \"%s\", is_total: \"%s\".\n",
  154. repl_subtree, is_total ? "TRUE" : "FALSE");
  155. /* log any extra data that was sent from the sender */
  156. if (data_guid && data) {
  157. slapi_log_err(SLAPI_LOG_DEBUG, test_repl_session_plugin_name,
  158. "test_repl_session_plugin_recv_acquire_cb() received data: guid: \"%s\" data: \"%s\".\n",
  159. data_guid, data->bv_val);
  160. }
  161. return rc;
  162. }
  163. /*
  164. * This is called on a sender when a replication agreement is
  165. * destroyed. Any cookie allocated when the agreement was initialized
  166. * should be free'd here.
  167. */
  168. static void
  169. test_repl_session_plugin_destroy_cb(void *cookie, const Slapi_DN *repl_subtree)
  170. {
  171. slapi_log_err(SLAPI_LOG_DEBUG, test_repl_session_plugin_name,
  172. "test_repl_session_plugin_destroy_cb() called for suffix \"%s\".\n",
  173. slapi_sdn_get_ndn(repl_subtree));
  174. /* free cookie */
  175. slapi_ch_free_string((char **)&cookie);
  176. return;
  177. }
  178. /*
  179. * Callback list for registering API
  180. */
  181. static void *test_repl_session_api[] = {
  182. NULL, /* reserved for api broker use, must be zero */
  183. test_repl_session_plugin_agmt_init_cb,
  184. test_repl_session_plugin_pre_acquire_cb,
  185. test_repl_session_plugin_reply_acquire_cb,
  186. test_repl_session_plugin_post_acquire_cb,
  187. test_repl_session_plugin_recv_acquire_cb,
  188. test_repl_session_plugin_destroy_cb};
  189. /*
  190. * Plug-in framework functions
  191. */
  192. static int
  193. test_repl_session_plugin_start(Slapi_PBlock *pb)
  194. {
  195. slapi_log_err(SLAPI_LOG_PLUGIN, test_repl_session_plugin_name,
  196. "--> test_repl_session_plugin_start -- begin\n");
  197. slapi_log_err(SLAPI_LOG_PLUGIN, test_repl_session_plugin_name,
  198. "<-- test_repl_session_plugin_start -- end\n");
  199. return 0;
  200. }
  201. static int
  202. test_repl_session_plugin_close(Slapi_PBlock *pb)
  203. {
  204. slapi_log_err(SLAPI_LOG_PLUGIN, test_repl_session_plugin_name,
  205. "--> test_repl_session_plugin_close -- begin\n");
  206. slapi_apib_unregister(REPL_SESSION_v1_0_GUID);
  207. slapi_log_err(SLAPI_LOG_PLUGIN, test_repl_session_plugin_name,
  208. "<-- test_repl_session_plugin_close -- end\n");
  209. return 0;
  210. }
  211. int
  212. test_repl_session_plugin_init(Slapi_PBlock *pb)
  213. {
  214. slapi_log_err(SLAPI_LOG_PLUGIN, test_repl_session_plugin_name,
  215. "--> test_repl_session_plugin_init -- begin\n");
  216. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  217. SLAPI_PLUGIN_VERSION_01) != 0 ||
  218. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  219. (void *)test_repl_session_plugin_start) != 0 ||
  220. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  221. (void *)test_repl_session_plugin_close) != 0 ||
  222. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  223. (void *)&test_repl_session_pdesc) != 0) {
  224. slapi_log_err(SLAPI_LOG_ERR, test_repl_session_plugin_name,
  225. "<-- test_repl_session_plugin_init -- failed to register plugin -- end\n");
  226. return -1;
  227. }
  228. if (slapi_apib_register(REPL_SESSION_v1_0_GUID, test_repl_session_api)) {
  229. slapi_log_err(SLAPI_LOG_DEBUG, test_repl_session_plugin_name,
  230. "<-- test_repl_session_plugin_start -- failed to register repl_session api -- end\n");
  231. return -1;
  232. }
  233. /* Retrieve and save the plugin identity to later pass to
  234. internal operations */
  235. if (slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &test_repl_session_plugin_id) != 0) {
  236. slapi_log_err(SLAPI_LOG_ERR, test_repl_session_plugin_name,
  237. "<-- test_repl_session_plugin_init -- failed to retrieve plugin identity -- end\n");
  238. return -1;
  239. }
  240. slapi_log_err(SLAPI_LOG_PLUGIN, test_repl_session_plugin_name,
  241. "<-- test_repl_session_plugin_init -- end\n");
  242. return 0;
  243. }
  244. /*
  245. dn: cn=Test Replication Session API,cn=plugins,cn=config
  246. objectclass: top
  247. objectclass: nsSlapdPlugin
  248. objectclass: extensibleObject
  249. cn: Test Replication Session API
  250. nsslapd-pluginpath: libtestreplsession-plugin
  251. nsslapd-plugininitfunc: test_repl_session_plugin_init
  252. nsslapd-plugintype: preoperation
  253. nsslapd-pluginenabled: on
  254. nsslapd-plugin-depends-on-type: database
  255. nsslapd-plugin-depends-on-named: Multisupplier Replication Plugin
  256. */