1
0

cl5_config.c 25 KB

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