cl5_config.c 25 KB

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