cl5_config.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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. /* cl5_config.c - functions to process changelog configuration
  42. */
  43. #include <string.h>
  44. #include <prio.h>
  45. #include "repl5.h"
  46. #include "cl5.h"
  47. #include "cl5_clcache.h" /* To configure the Changelog Cache */
  48. #include "intrinsics.h" /* JCMREPL - Is this bad? */
  49. #ifdef TEST_CL5
  50. #include "cl5_test.h"
  51. #endif
  52. #include "nspr.h"
  53. #include "plstr.h"
  54. #define CONFIG_BASE "cn=changelog5,cn=config" /*"cn=changelog,cn=supplier,cn=replication5.0,cn=replication,cn=config"*/
  55. #define CONFIG_FILTER "(objectclass=*)"
  56. static PRRWLock *s_configLock; /* guarantees that only on thread at a time
  57. modifies changelog configuration */
  58. /* Forward Declartions */
  59. static int changelog5_config_add (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  60. static int changelog5_config_modify (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  61. static int changelog5_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  62. static int dont_allow_that(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e, int *returncode, char *returntext, void *arg);
  63. static void changelog5_extract_config(Slapi_Entry* entry, changelog5Config *config);
  64. static changelog5Config * changelog5_dup_config(changelog5Config *config);
  65. static void replace_bslash (char *dir);
  66. static int notify_replica (Replica *r, void *arg);
  67. static int _is_absolutepath (char *dir);
  68. int changelog5_config_init()
  69. {
  70. /* The FE DSE *must* be initialised before we get here */
  71. /* create the configuration lock */
  72. s_configLock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "config_lock");
  73. if (s_configLock == NULL)
  74. {
  75. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  76. "changelog5_config_init: failed to create configurationlock; "
  77. "NSPR error - %d\n",PR_GetError ());
  78. return 1;
  79. }
  80. slapi_config_register_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_BASE,
  81. CONFIG_FILTER, changelog5_config_add, NULL);
  82. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_BASE,
  83. CONFIG_FILTER, changelog5_config_modify, NULL);
  84. slapi_config_register_callback(SLAPI_OPERATION_MODRDN, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_BASE,
  85. CONFIG_FILTER, dont_allow_that, NULL);
  86. slapi_config_register_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_BASE,
  87. CONFIG_FILTER, changelog5_config_delete, NULL);
  88. return 0;
  89. }
  90. void changelog5_config_cleanup()
  91. {
  92. slapi_config_remove_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_BASE,
  93. CONFIG_FILTER, changelog5_config_add);
  94. slapi_config_remove_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_BASE,
  95. CONFIG_FILTER, changelog5_config_modify);
  96. slapi_config_remove_callback(SLAPI_OPERATION_MODRDN, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_BASE,
  97. CONFIG_FILTER, dont_allow_that);
  98. slapi_config_remove_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_BASE,
  99. CONFIG_FILTER, changelog5_config_delete);
  100. if (s_configLock)
  101. {
  102. PR_DestroyRWLock (s_configLock);
  103. s_configLock = NULL;
  104. }
  105. }
  106. int changelog5_read_config (changelog5Config *config)
  107. {
  108. int rc = LDAP_SUCCESS;
  109. Slapi_PBlock *pb;
  110. pb = slapi_pblock_new ();
  111. slapi_search_internal_set_pb (pb, CONFIG_BASE, LDAP_SCOPE_BASE, CONFIG_FILTER, NULL, 0, NULL,
  112. NULL, repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  113. slapi_search_internal_pb (pb);
  114. slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_RESULT, &rc );
  115. if ( LDAP_SUCCESS == rc )
  116. {
  117. Slapi_Entry **entries = NULL;
  118. slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
  119. if ( NULL != entries && NULL != entries[0])
  120. {
  121. /* Extract the config info from the changelog entry */
  122. changelog5_extract_config(entries[0], config);
  123. }
  124. }
  125. else
  126. {
  127. memset (config, 0, sizeof (*config));
  128. rc = LDAP_SUCCESS;
  129. }
  130. slapi_free_search_results_internal(pb);
  131. slapi_pblock_destroy(pb);
  132. return rc;
  133. }
  134. void changelog5_config_done (changelog5Config *config)
  135. {
  136. if (config) {
  137. /* slapi_ch_free_string accepts NULL pointer */
  138. slapi_ch_free_string (&config->maxAge);
  139. slapi_ch_free_string (&config->dir);
  140. }
  141. }
  142. void changelog5_config_free (changelog5Config **config)
  143. {
  144. changelog5_config_done(*config);
  145. slapi_ch_free((void **)config);
  146. }
  147. static int
  148. changelog5_config_add (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  149. int *returncode, char *returntext, void *arg)
  150. {
  151. int rc;
  152. changelog5Config config;
  153. *returncode = LDAP_SUCCESS;
  154. PR_RWLock_Wlock (s_configLock);
  155. /* we already have a configured changelog - don't need to do anything
  156. since add operation will fail */
  157. if (cl5GetState () == CL5_STATE_OPEN)
  158. {
  159. *returncode = 1;
  160. if (returntext)
  161. {
  162. strcpy (returntext, "attempt to add changelog when it already exists");
  163. }
  164. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  165. "changelog5_config_add: changelog already exist; "
  166. "request ignored\n");
  167. goto done;
  168. }
  169. changelog5_extract_config(e, &config);
  170. if (config.dir == NULL)
  171. {
  172. *returncode = 1;
  173. if (returntext)
  174. {
  175. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "NULL changelog directory");
  176. }
  177. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  178. "changelog5_config_add: NULL changelog directory\n");
  179. goto done;
  180. }
  181. if (!cl5DbDirIsEmpty(config.dir))
  182. {
  183. *returncode = 1;
  184. if (returntext)
  185. {
  186. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  187. "The changelog directory [%s] already exists and is not empty. "
  188. "Please choose a directory that does not exist or is empty.\n",
  189. config.dir);
  190. }
  191. goto done;
  192. }
  193. /* start the changelog */
  194. rc = cl5Open (config.dir, &config.dbconfig);
  195. if (rc != CL5_SUCCESS)
  196. {
  197. *returncode = 1;
  198. if (returntext)
  199. {
  200. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to start changelog; error - %d", rc);
  201. }
  202. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  203. "changelog5_config_add: failed to start changelog\n");
  204. goto done;
  205. }
  206. /* set trimming parameters */
  207. rc = cl5ConfigTrimming (config.maxEntries, config.maxAge);
  208. if (rc != CL5_SUCCESS)
  209. {
  210. *returncode = 1;
  211. if (returntext)
  212. {
  213. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to configure changelog trimming; error - %d", rc);
  214. }
  215. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  216. "changelog5_config_add: failed to configure changelog trimming\n");
  217. goto done;
  218. }
  219. /* notify all the replicas that the changelog is configured
  220. so that the can log dummy changes if necessary. */
  221. replica_enumerate_replicas (notify_replica, NULL);
  222. #ifdef TEST_CL5
  223. testChangelog (TEST_ITERATION);
  224. #endif
  225. done:;
  226. PR_RWLock_Unlock (s_configLock);
  227. changelog5_config_done (&config);
  228. if (*returncode == LDAP_SUCCESS)
  229. {
  230. if (returntext)
  231. {
  232. returntext[0] = '\0';
  233. }
  234. return SLAPI_DSE_CALLBACK_OK;
  235. }
  236. return SLAPI_DSE_CALLBACK_ERROR;
  237. }
  238. static int
  239. changelog5_config_modify (Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  240. int *returncode, char *returntext, void *arg)
  241. {
  242. int rc= 0;
  243. LDAPMod **mods;
  244. int i;
  245. changelog5Config config;
  246. changelog5Config * originalConfig = NULL;
  247. char *currentDir = NULL;
  248. *returncode = LDAP_SUCCESS;
  249. /* changelog must be open before its parameters can be modified */
  250. if (cl5GetState() != CL5_STATE_OPEN)
  251. {
  252. if (returntext)
  253. {
  254. strcpy (returntext, "changelog is not configured");
  255. }
  256. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  257. "changelog5_config_modify: changelog is not configured\n");
  258. return SLAPI_DSE_CALLBACK_ERROR;
  259. }
  260. PR_RWLock_Wlock (s_configLock);
  261. /* changelog must be open before its parameters can be modified */
  262. if (cl5GetState() != CL5_STATE_OPEN)
  263. {
  264. *returncode = 1;
  265. if (returntext)
  266. {
  267. strcpy (returntext, "changelog is not configured");
  268. }
  269. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  270. "changelog5_config_modify: changelog is not configured\n");
  271. goto done;
  272. }
  273. /*
  274. * Extract all the original configuration: This is needed to ensure that the configuration
  275. * is trully reloaded. This was not needed before 091401 because the changelog configuration
  276. * was always hardcoded (NULL was being passed to cl5Open). Now we need to ensure we pass to
  277. * cl5Open the proper configuration...
  278. */
  279. changelog5_extract_config(e, &config);
  280. originalConfig = changelog5_dup_config(&config);
  281. /* Reset all the attributes that have been potentially modified by the current MODIFY operation */
  282. slapi_ch_free_string(&config.dir);
  283. config.dir = NULL;
  284. config.maxEntries = CL5_NUM_IGNORE;
  285. slapi_ch_free_string(&config.maxAge);
  286. config.maxAge = slapi_ch_strdup(CL5_STR_IGNORE);
  287. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  288. for (i = 0; mods[i] != NULL; i++)
  289. {
  290. if (mods[i]->mod_op & LDAP_MOD_DELETE)
  291. {
  292. /* We don't support deleting changelog attributes */
  293. }
  294. else
  295. {
  296. int j;
  297. for (j = 0; ((mods[i]->mod_values[j]) && (LDAP_SUCCESS == rc)); j++)
  298. {
  299. char *config_attr, *config_attr_value;
  300. config_attr = (char *) mods[i]->mod_type;
  301. config_attr_value = (char *) mods[i]->mod_bvalues[j]->bv_val;
  302. #define ATTR_MODIFIERSNAME "modifiersname"
  303. #define ATTR_MODIFYTIMESTAMP "modifytimestamp"
  304. if ( strcasecmp ( config_attr, ATTR_MODIFIERSNAME ) == 0 ) {
  305. continue;
  306. }
  307. if ( strcasecmp ( config_attr, ATTR_MODIFYTIMESTAMP ) == 0 ) {
  308. continue;
  309. }
  310. /* replace existing value */
  311. if ( strcasecmp (config_attr, CONFIG_CHANGELOG_DIR_ATTRIBUTE ) == 0 )
  312. {
  313. if (config_attr_value && config_attr_value[0] != '\0')
  314. {
  315. slapi_ch_free_string(&config.dir);
  316. config.dir = slapi_ch_strdup(config_attr_value);
  317. replace_bslash (config.dir);
  318. }
  319. else
  320. {
  321. *returncode = 1;
  322. if (returntext)
  323. {
  324. strcpy (returntext, "null changelog directory");
  325. }
  326. goto done;
  327. }
  328. }
  329. else if ( strcasecmp ( config_attr, CONFIG_CHANGELOG_MAXENTRIES_ATTRIBUTE ) == 0 )
  330. {
  331. if (config_attr_value && config_attr_value[0] != '\0')
  332. {
  333. config.maxEntries = atoi (config_attr_value);
  334. }
  335. else
  336. {
  337. config.maxEntries = 0;
  338. }
  339. }
  340. else if ( strcasecmp ( config_attr, CONFIG_CHANGELOG_MAXAGE_ATTRIBUTE ) == 0 )
  341. {
  342. slapi_ch_free_string(&config.maxAge);
  343. config.maxAge = slapi_ch_strdup(config_attr_value);
  344. }
  345. else
  346. {
  347. *returncode = LDAP_UNWILLING_TO_PERFORM;
  348. if (returntext)
  349. {
  350. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  351. "Unwilling to apply %s mods while the server is running", config_attr);
  352. }
  353. goto done;
  354. }
  355. }
  356. }
  357. }
  358. /* Undo the reset above for all the modifiable attributes that were not modified
  359. * except config.dir */
  360. if (config.maxEntries == CL5_NUM_IGNORE)
  361. config.maxEntries = originalConfig->maxEntries;
  362. if (strcmp (config.maxAge, CL5_STR_IGNORE) == 0) {
  363. slapi_ch_free_string(&config.maxAge);
  364. if (originalConfig->maxAge)
  365. config.maxAge = slapi_ch_strdup(originalConfig->maxAge);
  366. }
  367. /* attempt to change chagelog dir */
  368. if (config.dir)
  369. {
  370. currentDir = cl5GetDir ();
  371. if (currentDir == NULL)
  372. {
  373. /* something is wrong: we should never be here */
  374. *returncode = 1;
  375. if (returntext)
  376. {
  377. strcpy (returntext, "internal failure");
  378. }
  379. goto done;
  380. }
  381. #ifdef _WIN32
  382. if (strcasecmp (currentDir, config.dir) != 0)
  383. #else /* On Unix, path are case sensitive */
  384. if (strcmp (currentDir, config.dir) != 0)
  385. #endif
  386. {
  387. if (!cl5DbDirIsEmpty(config.dir))
  388. {
  389. *returncode = 1;
  390. if (returntext)
  391. {
  392. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  393. "The changelog directory [%s] already exists and is not empty. "
  394. "Please choose a directory that does not exist or is empty.\n",
  395. config.dir);
  396. }
  397. goto done;
  398. }
  399. if (!_is_absolutepath(config.dir) || (CL5_SUCCESS != cl5CreateDirIfNeeded(config.dir)))
  400. {
  401. *returncode = 1;
  402. if (returntext)
  403. {
  404. PL_strncpyz (returntext, "invalid changelog directory or insufficient access", SLAPI_DSE_RETURNTEXT_SIZE);
  405. }
  406. goto done;
  407. }
  408. /* changelog directory changed - need to remove the
  409. previous changelog and create new one */
  410. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name_cl,
  411. "changelog5_config_modify: changelog directory changed; "
  412. "old dir - %s, new dir - %s; recreating changelog.\n",
  413. currentDir, config.dir);
  414. rc = cl5Close ();
  415. if (rc != CL5_SUCCESS)
  416. {
  417. *returncode = 1;
  418. if (returntext)
  419. {
  420. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to close changelog; error - %d", rc);
  421. }
  422. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  423. "changelog5_config_modify: failed to close changelog\n");
  424. goto done;
  425. } else {
  426. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  427. "changelog5_config_modify: closed the changelog\n");
  428. }
  429. rc = cl5Delete (currentDir);
  430. if (rc != CL5_SUCCESS)
  431. {
  432. *returncode = 1;
  433. if (returntext)
  434. {
  435. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to remove changelog; error - %d", rc);
  436. }
  437. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  438. "changelog5_config_modify: failed to remove changelog\n");
  439. goto done;
  440. } else {
  441. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  442. "changelog5_config_modify: deleted the changelog at %s\n", currentDir);
  443. }
  444. rc = cl5Open (config.dir, &config.dbconfig);
  445. if (rc != CL5_SUCCESS)
  446. {
  447. *returncode = 1;
  448. if (returntext)
  449. {
  450. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to restart changelog; error - %d", rc);
  451. }
  452. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  453. "changelog5_config_modify: failed to restart changelog\n");
  454. /* before finishing, let's try to do some error recovery */
  455. if (CL5_SUCCESS != cl5Open(currentDir, &config.dbconfig))
  456. {
  457. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  458. "changelog5_config_modify: failed to restore previous changelog\n");
  459. }
  460. goto done;
  461. } else {
  462. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  463. "changelog5_config_modify: opened the changelog at %s\n", config.dir);
  464. }
  465. }
  466. }
  467. /* one of the changelog parameters is modified */
  468. if (config.maxEntries != CL5_NUM_IGNORE ||
  469. strcmp (config.maxAge, CL5_STR_IGNORE) != 0)
  470. {
  471. rc = cl5ConfigTrimming (config.maxEntries, config.maxAge);
  472. if (rc != CL5_SUCCESS)
  473. {
  474. *returncode = 1;
  475. if (returntext)
  476. {
  477. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to configure changelog trimming; error - %d", rc);
  478. }
  479. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  480. "changelog5_config_modify: failed to configure changelog trimming\n");
  481. goto done;
  482. }
  483. }
  484. done:;
  485. PR_RWLock_Unlock (s_configLock);
  486. changelog5_config_done (&config);
  487. changelog5_config_free (&originalConfig);
  488. /* slapi_ch_free accepts NULL pointer */
  489. slapi_ch_free ((void**)&currentDir);
  490. if (*returncode == LDAP_SUCCESS)
  491. {
  492. if (returntext)
  493. {
  494. returntext[0] = '\0';
  495. }
  496. return SLAPI_DSE_CALLBACK_OK;
  497. }
  498. return SLAPI_DSE_CALLBACK_ERROR;
  499. }
  500. static int
  501. changelog5_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  502. int *returncode, char *returntext, void *arg)
  503. {
  504. int rc;
  505. char *currentDir = NULL;
  506. *returncode = LDAP_SUCCESS;
  507. /* changelog must be open before it can be deleted */
  508. if (cl5GetState () != CL5_STATE_OPEN)
  509. {
  510. *returncode = 1;
  511. if (returntext)
  512. {
  513. PL_strncpyz(returntext, "changelog is not configured", SLAPI_DSE_RETURNTEXT_SIZE);
  514. }
  515. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  516. "changelog5_config_delete: chagelog is not configured\n");
  517. return SLAPI_DSE_CALLBACK_ERROR;
  518. }
  519. PR_RWLock_Wlock (s_configLock);
  520. /* changelog must be open before it can be deleted */
  521. if (cl5GetState () != CL5_STATE_OPEN)
  522. {
  523. *returncode = 1;
  524. if (returntext)
  525. {
  526. PL_strncpyz(returntext, "changelog is not configured", SLAPI_DSE_RETURNTEXT_SIZE);
  527. }
  528. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  529. "changelog5_config_delete: chagelog is not configured\n");
  530. goto done;
  531. }
  532. currentDir = cl5GetDir ();
  533. if (currentDir == NULL)
  534. {
  535. /* something is wrong: we should never be here */
  536. *returncode = 1;
  537. if (returntext)
  538. {
  539. PL_strncpyz (returntext, "internal failure", SLAPI_DSE_RETURNTEXT_SIZE);
  540. }
  541. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  542. "changelog5_config_delete: NULL directory\n");
  543. goto done;
  544. }
  545. /* this call will block until all threads using changelog
  546. release changelog by calling cl5RemoveThread () */
  547. rc = cl5Close ();
  548. if (rc != CL5_SUCCESS)
  549. {
  550. *returncode = 1;
  551. if (returntext)
  552. {
  553. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to close changelog; error - %d", rc);
  554. }
  555. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  556. "changelog5_config_delete: failed to close changelog\n");
  557. goto done;
  558. }
  559. rc = cl5Delete (currentDir);
  560. if (rc != CL5_SUCCESS)
  561. {
  562. *returncode = 1;
  563. if (returntext)
  564. {
  565. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to remove changelog; error - %d", rc);
  566. }
  567. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  568. "changelog5_config_delete: failed to remove changelog\n");
  569. goto done;
  570. }
  571. done:;
  572. PR_RWLock_Unlock (s_configLock);
  573. /* slapi_ch_free accepts NULL pointer */
  574. slapi_ch_free ((void**)&currentDir);
  575. if (*returncode == LDAP_SUCCESS)
  576. {
  577. if (returntext)
  578. {
  579. returntext[0] = '\0';
  580. }
  581. return SLAPI_DSE_CALLBACK_OK;
  582. }
  583. return SLAPI_DSE_CALLBACK_ERROR;
  584. }
  585. static int dont_allow_that(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  586. int *returncode, char *returntext, void *arg)
  587. {
  588. *returncode = LDAP_UNWILLING_TO_PERFORM;
  589. return SLAPI_DSE_CALLBACK_ERROR;
  590. }
  591. static changelog5Config * changelog5_dup_config(changelog5Config *config)
  592. {
  593. changelog5Config *dup = (changelog5Config *) slapi_ch_calloc(1, sizeof(changelog5Config));
  594. if (config->dir)
  595. dup->dir = slapi_ch_strdup(config->dir);
  596. if (config->maxAge)
  597. dup->maxAge = slapi_ch_strdup(config->maxAge);
  598. dup->maxEntries = config->maxEntries;
  599. dup->dbconfig.pageSize = config->dbconfig.pageSize;
  600. dup->dbconfig.fileMode = config->dbconfig.fileMode;
  601. dup->dbconfig.maxConcurrentWrites = config->dbconfig.maxConcurrentWrites;
  602. return dup;
  603. }
  604. /*
  605. * Given the changelog configuration entry, extract the configuration directives.
  606. */
  607. static void changelog5_extract_config(Slapi_Entry* entry, changelog5Config *config)
  608. {
  609. char *arg;
  610. memset (config, 0, sizeof (*config));
  611. config->dir = slapi_entry_attr_get_charptr(entry,CONFIG_CHANGELOG_DIR_ATTRIBUTE);
  612. replace_bslash (config->dir);
  613. arg= slapi_entry_attr_get_charptr(entry,CONFIG_CHANGELOG_MAXENTRIES_ATTRIBUTE);
  614. if (arg)
  615. {
  616. config->maxEntries = atoi (arg);
  617. slapi_ch_free_string(&arg);
  618. }
  619. config->maxAge = slapi_entry_attr_get_charptr(entry,CONFIG_CHANGELOG_MAXAGE_ATTRIBUTE);
  620. /*
  621. * Read the Changelog Internal Configuration Parameters for the Changelog DB
  622. * (db cache size, db settings...)
  623. */
  624. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_MAX_CONCURRENT_WRITES);
  625. if (arg)
  626. {
  627. config->dbconfig.maxConcurrentWrites = atoi (arg);
  628. slapi_ch_free_string(&arg);
  629. }
  630. if ( config->dbconfig.maxConcurrentWrites <= 0 )
  631. {
  632. config->dbconfig.maxConcurrentWrites = CL5_DEFAULT_CONFIG_MAX_CONCURRENT_WRITES;
  633. }
  634. }
  635. static void replace_bslash (char *dir)
  636. {
  637. char *bslash;
  638. if (dir == NULL)
  639. return;
  640. bslash = strchr (dir, '\\');
  641. while (bslash)
  642. {
  643. *bslash = '/';
  644. bslash = strchr (bslash, '\\');
  645. }
  646. }
  647. static int notify_replica (Replica *r, void *arg)
  648. {
  649. return replica_log_ruv_elements (r);
  650. }
  651. static int _is_absolutepath (char * dir)
  652. {
  653. if (dir[0] == '/')
  654. return 1;
  655. #if defined(_WIN32)
  656. if (dir[2] == '/' && dir[1] == ':')
  657. return 1;
  658. #endif
  659. return 0;
  660. }