retrocl_create.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 "retrocl.h"
  13. /*
  14. The changelog is created by
  15. - changing the node in the dse tree which represents the changelog plugin
  16. to enabled on,
  17. - shutting down the server,
  18. - starting the server.
  19. */
  20. /******************************/
  21. /*
  22. * Function: retrocl_create_be
  23. *
  24. * Returns: LDAP_
  25. *
  26. * Arguments: location in file system to put changelog, or NULL for default
  27. *
  28. * Description:
  29. * add an entry of class nsBackendInstance below cn=ldbm,cn=plugins,cn=config
  30. *
  31. */
  32. static int
  33. retrocl_create_be(const char *bedir)
  34. {
  35. Slapi_PBlock *pb = NULL;
  36. Slapi_Entry *e;
  37. struct berval *vals[2];
  38. struct berval val;
  39. int rc;
  40. vals[0] = &val;
  41. vals[1] = NULL;
  42. e = slapi_entry_alloc();
  43. /* RETROCL_LDBM_DN is no need to be normalized. */
  44. slapi_entry_set_dn(e, slapi_ch_strdup(RETROCL_LDBM_DN));
  45. /* Set the objectclass attribute */
  46. val.bv_val = "top";
  47. val.bv_len = 3;
  48. slapi_entry_add_values(e, "objectclass", vals);
  49. /* Set the objectclass attribute */
  50. val.bv_val = "extensibleObject";
  51. val.bv_len = strlen(val.bv_val);
  52. slapi_entry_add_values(e, "objectclass", vals);
  53. /* Set the objectclass attribute */
  54. val.bv_val = "nsBackendInstance";
  55. val.bv_len = strlen(val.bv_val);
  56. slapi_entry_add_values(e, "objectclass", vals);
  57. val.bv_val = "changelog";
  58. val.bv_len = strlen(val.bv_val);
  59. slapi_entry_add_values(e, "cn", vals);
  60. val.bv_val = RETROCL_BE_CACHESIZE;
  61. val.bv_len = strlen(val.bv_val);
  62. slapi_entry_add_values(e, "nsslapd-cachesize", vals);
  63. val.bv_val = RETROCL_CHANGELOG_DN;
  64. val.bv_len = strlen(val.bv_val);
  65. slapi_entry_add_values(e, "nsslapd-suffix", vals);
  66. val.bv_val = RETROCL_BE_CACHEMEMSIZE;
  67. val.bv_len = strlen(val.bv_val);
  68. slapi_entry_add_values(e, "nsslapd-cachememsize", vals);
  69. val.bv_val = "off";
  70. val.bv_len = strlen(val.bv_val);
  71. slapi_entry_add_values(e, "nsslapd-readonly", vals);
  72. if (bedir) {
  73. val.bv_val = (char *)bedir; /* cast const */
  74. val.bv_len = strlen(val.bv_val);
  75. slapi_entry_add_values(e, "nsslapd-directory", vals);
  76. }
  77. pb = slapi_pblock_new();
  78. slapi_add_entry_internal_set_pb(pb, e, NULL /* controls */,
  79. g_plg_identity[PLUGIN_RETROCL],
  80. 0 /* actions */);
  81. slapi_add_internal_pb(pb);
  82. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  83. slapi_pblock_destroy(pb);
  84. if (rc == 0) {
  85. slapi_log_err(SLAPI_LOG_PLUGIN, RETROCL_PLUGIN_NAME,
  86. "created changelog database node\n");
  87. } else if (rc == LDAP_ALREADY_EXISTS) {
  88. slapi_log_err(SLAPI_LOG_PLUGIN, RETROCL_PLUGIN_NAME,
  89. "changelog database node already existed\n");
  90. } else {
  91. slapi_log_err(SLAPI_LOG_ERR, RETROCL_PLUGIN_NAME,
  92. "Changelog LDBM backend could not be created (%d)\n", rc);
  93. return rc;
  94. }
  95. /* we need the changenumber indexed */
  96. e = slapi_entry_alloc();
  97. /* RETROCL_INDEX_DN is no need to be normalized. */
  98. slapi_entry_set_dn(e, slapi_ch_strdup(RETROCL_INDEX_DN));
  99. /* Set the objectclass attribute */
  100. val.bv_val = "top";
  101. val.bv_len = 3;
  102. slapi_entry_add_values(e, "objectclass", vals);
  103. /* Set the objectclass attribute */
  104. val.bv_val = "nsIndex";
  105. val.bv_len = strlen(val.bv_val);
  106. slapi_entry_add_values(e, "objectclass", vals);
  107. val.bv_val = "changenumber";
  108. val.bv_len = strlen(val.bv_val);
  109. slapi_entry_add_values(e, "cn", vals);
  110. val.bv_val = "false";
  111. val.bv_len = strlen(val.bv_val);
  112. slapi_entry_add_values(e, "nssystemindex", vals);
  113. val.bv_val = "eq";
  114. val.bv_len = strlen(val.bv_val);
  115. slapi_entry_add_values(e, "nsindextype", vals);
  116. val.bv_val = "integerOrderingMatch";
  117. val.bv_len = strlen(val.bv_val);
  118. slapi_entry_add_values(e, "nsMatchingRule", vals);
  119. pb = slapi_pblock_new();
  120. slapi_add_entry_internal_set_pb(pb, e, NULL /* controls */,
  121. g_plg_identity[PLUGIN_RETROCL],
  122. 0 /* actions */);
  123. slapi_add_internal_pb(pb);
  124. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  125. slapi_pblock_destroy(pb);
  126. if (rc == 0) {
  127. slapi_log_err(SLAPI_LOG_PLUGIN, RETROCL_PLUGIN_NAME,
  128. "created changenumber index node\n");
  129. } else if (rc == LDAP_ALREADY_EXISTS) {
  130. slapi_log_err(SLAPI_LOG_PLUGIN, RETROCL_PLUGIN_NAME,
  131. "changelog index node already existed\n");
  132. } else {
  133. slapi_log_err(SLAPI_LOG_ERR, RETROCL_PLUGIN_NAME,
  134. "Changelog LDBM backend changenumber index could not be created (%d)\n", rc);
  135. return rc;
  136. }
  137. return rc;
  138. }
  139. /*
  140. * Function: retrocl_create_config
  141. *
  142. * Returns: LDAP_
  143. *
  144. * Arguments: none
  145. *
  146. * Description:
  147. * This function is called if there was no mapping tree node or backend for
  148. * cn=changelog.
  149. */
  150. int
  151. retrocl_create_config(void)
  152. {
  153. Slapi_PBlock *pb = NULL;
  154. Slapi_Entry *e;
  155. struct berval *vals[2];
  156. struct berval val;
  157. int rc;
  158. char *mappingtree_dn = NULL;
  159. vals[0] = &val;
  160. vals[1] = NULL;
  161. /* Assume the mapping tree node is missing. It doesn't hurt to
  162. * attempt to add it if it already exists. You will see a warning
  163. * in the errors file when the referenced backend does not exist.
  164. */
  165. e = slapi_entry_alloc();
  166. /* This function converts the old DN style to the new one. */
  167. mappingtree_dn = slapi_create_dn_string("%s", RETROCL_MAPPINGTREE_DN);
  168. if (NULL == mappingtree_dn) {
  169. slapi_log_err(SLAPI_LOG_PLUGIN, RETROCL_PLUGIN_NAME,
  170. "retrocl_create_config: failed to normalize "
  171. "mappingtree dn %s\n",
  172. RETROCL_MAPPINGTREE_DN);
  173. return LDAP_PARAM_ERROR;
  174. }
  175. slapi_entry_set_dn(e, mappingtree_dn); /* mappingtree_dn is consumed */
  176. /* Set the objectclass attribute */
  177. val.bv_val = "top";
  178. val.bv_len = 3;
  179. slapi_entry_add_values(e, "objectclass", vals);
  180. /* Set the objectclass attribute */
  181. val.bv_val = "extensibleObject";
  182. val.bv_len = strlen(val.bv_val);
  183. slapi_entry_add_values(e, "objectclass", vals);
  184. /* Set the objectclass attribute */
  185. val.bv_val = "nsMappingTree";
  186. val.bv_len = strlen(val.bv_val);
  187. slapi_entry_add_values(e, "objectclass", vals);
  188. val.bv_val = "backend";
  189. val.bv_len = strlen(val.bv_val);
  190. slapi_entry_add_values(e, "nsslapd-state", vals);
  191. val.bv_val = RETROCL_CHANGELOG_DN;
  192. val.bv_len = strlen(val.bv_val);
  193. slapi_entry_add_values(e, "cn", vals);
  194. val.bv_val = "changelog";
  195. val.bv_len = strlen(val.bv_val);
  196. slapi_entry_add_values(e, "nsslapd-backend", vals);
  197. pb = slapi_pblock_new();
  198. slapi_add_entry_internal_set_pb(pb, e, NULL /* controls */,
  199. g_plg_identity[PLUGIN_RETROCL],
  200. 0 /* actions */);
  201. slapi_add_internal_pb(pb);
  202. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  203. slapi_pblock_destroy(pb);
  204. if (rc == 0) {
  205. slapi_log_err(SLAPI_LOG_PLUGIN, RETROCL_PLUGIN_NAME,
  206. "created changelog mapping tree node\n");
  207. } else if (rc == LDAP_ALREADY_EXISTS) {
  208. slapi_log_err(SLAPI_LOG_PLUGIN, RETROCL_PLUGIN_NAME,
  209. "changelog mapping tree node already existed\n");
  210. } else {
  211. slapi_log_err(SLAPI_LOG_ERR, RETROCL_PLUGIN_NAME,
  212. "cn=\"cn=changelog\",cn=mapping tree,cn=config could not be created (%d)\n", rc);
  213. return rc;
  214. }
  215. retrocl_be_changelog = slapi_be_select_by_instance_name("changelog");
  216. if (retrocl_be_changelog == NULL) {
  217. /* This is not the nsslapd-changelogdir from cn=changelog4,cn=config */
  218. char *bedir;
  219. bedir = retrocl_get_config_str(CONFIG_CHANGELOG_DIRECTORY_ATTRIBUTE);
  220. if (bedir == NULL) {
  221. /* none specified */
  222. }
  223. rc = retrocl_create_be(bedir);
  224. slapi_ch_free_string(&bedir);
  225. if (rc != LDAP_SUCCESS && rc != LDAP_ALREADY_EXISTS) {
  226. return rc;
  227. }
  228. retrocl_be_changelog = slapi_be_select_by_instance_name("changelog");
  229. }
  230. return LDAP_SUCCESS;
  231. }
  232. /******************************/
  233. /* Function: retrocl_create_cle
  234. *
  235. * Arguments: none
  236. * Returns: nothing
  237. * Description: Attempts to create the cn=changelog entry which might already
  238. * exist.
  239. */
  240. void
  241. retrocl_create_cle(void)
  242. {
  243. Slapi_PBlock *pb = NULL;
  244. Slapi_Entry *e;
  245. int rc;
  246. struct berval *vals[2];
  247. struct berval val;
  248. vals[0] = &val;
  249. vals[1] = NULL;
  250. e = slapi_entry_alloc();
  251. slapi_entry_set_dn(e, slapi_ch_strdup(RETROCL_CHANGELOG_DN));
  252. /* Set the objectclass attribute */
  253. val.bv_val = "top";
  254. val.bv_len = strlen(val.bv_val);
  255. slapi_entry_add_values(e, "objectclass", vals);
  256. /* Set the objectclass attribute */
  257. val.bv_val = "nsContainer";
  258. val.bv_len = strlen(val.bv_val);
  259. slapi_entry_add_values(e, "objectclass", vals);
  260. /* Set the objectclass attribute */
  261. val.bv_val = "changelog";
  262. val.bv_len = strlen(val.bv_val);
  263. slapi_entry_add_values(e, "cn", vals);
  264. pb = slapi_pblock_new();
  265. slapi_add_entry_internal_set_pb(pb, e, NULL /* controls */,
  266. g_plg_identity[PLUGIN_RETROCL],
  267. 0 /* actions */);
  268. slapi_add_internal_pb(pb);
  269. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  270. slapi_pblock_destroy(pb);
  271. if (rc == 0) {
  272. slapi_log_err(SLAPI_LOG_PLUGIN, RETROCL_PLUGIN_NAME,
  273. "created cn=changelog\n");
  274. } else if (rc == LDAP_ALREADY_EXISTS) {
  275. slapi_log_err(SLAPI_LOG_PLUGIN, RETROCL_PLUGIN_NAME,
  276. "cn=changelog already existed\n");
  277. } else {
  278. slapi_log_err(SLAPI_LOG_ERR, RETROCL_PLUGIN_NAME,
  279. "cn=changelog could not be created (%d)\n", rc);
  280. }
  281. }