cl5_config.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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);
  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. slapi_ch_free_string(&config.maxAge);
  295. config.maxAge = slapi_ch_strdup(CL5_STR_IGNORE);
  296. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  297. for (i = 0; mods[i] != NULL; i++)
  298. {
  299. if (mods[i]->mod_op & LDAP_MOD_DELETE)
  300. {
  301. /* We don't support deleting changelog attributes */
  302. }
  303. else
  304. {
  305. int j;
  306. for (j = 0; ((mods[i]->mod_values[j]) && (LDAP_SUCCESS == rc)); j++)
  307. {
  308. char *config_attr, *config_attr_value;
  309. config_attr = (char *) mods[i]->mod_type;
  310. config_attr_value = (char *) mods[i]->mod_bvalues[j]->bv_val;
  311. #define ATTR_MODIFIERSNAME "modifiersname"
  312. #define ATTR_MODIFYTIMESTAMP "modifytimestamp"
  313. if ( strcasecmp ( config_attr, ATTR_MODIFIERSNAME ) == 0 ) {
  314. continue;
  315. }
  316. if ( strcasecmp ( config_attr, ATTR_MODIFYTIMESTAMP ) == 0 ) {
  317. continue;
  318. }
  319. /* replace existing value */
  320. if ( strcasecmp (config_attr, CONFIG_CHANGELOG_DIR_ATTRIBUTE ) == 0 )
  321. {
  322. if (config_attr_value && config_attr_value[0] != '\0')
  323. {
  324. slapi_ch_free_string(&config.dir);
  325. config.dir = slapi_ch_strdup(config_attr_value);
  326. replace_bslash (config.dir);
  327. }
  328. else
  329. {
  330. *returncode = 1;
  331. if (returntext)
  332. {
  333. strcpy (returntext, "null changelog directory");
  334. }
  335. goto done;
  336. }
  337. }
  338. else if ( strcasecmp ( config_attr, CONFIG_CHANGELOG_MAXENTRIES_ATTRIBUTE ) == 0 )
  339. {
  340. if (config_attr_value && config_attr_value[0] != '\0')
  341. {
  342. config.maxEntries = atoi (config_attr_value);
  343. }
  344. else
  345. {
  346. config.maxEntries = 0;
  347. }
  348. }
  349. else if ( strcasecmp ( config_attr, CONFIG_CHANGELOG_MAXAGE_ATTRIBUTE ) == 0 )
  350. {
  351. slapi_ch_free_string(&config.maxAge);
  352. config.maxAge = slapi_ch_strdup(config_attr_value);
  353. }
  354. else if ( strcasecmp ( config_attr, CONFIG_CHANGELOG_SYMMETRIC_KEY ) == 0 )
  355. {
  356. slapi_ch_free_string(&config.symmetricKey);
  357. config.symmetricKey = slapi_ch_strdup(config_attr_value);
  358. /* Storing the encryption symmetric key */
  359. /* no need to change any changelog configuration */
  360. goto done;
  361. }
  362. else
  363. {
  364. *returncode = LDAP_UNWILLING_TO_PERFORM;
  365. if (returntext)
  366. {
  367. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  368. "Unwilling to apply %s mods while the server is running", config_attr);
  369. }
  370. goto done;
  371. }
  372. }
  373. }
  374. }
  375. /* Undo the reset above for all the modifiable attributes that were not modified
  376. * except config.dir */
  377. if (config.maxEntries == CL5_NUM_IGNORE)
  378. config.maxEntries = originalConfig->maxEntries;
  379. if (strcmp (config.maxAge, CL5_STR_IGNORE) == 0) {
  380. slapi_ch_free_string(&config.maxAge);
  381. if (originalConfig->maxAge)
  382. config.maxAge = slapi_ch_strdup(originalConfig->maxAge);
  383. }
  384. /* attempt to change chagelog dir */
  385. if (config.dir)
  386. {
  387. currentDir = cl5GetDir ();
  388. if (currentDir == NULL)
  389. {
  390. /* something is wrong: we should never be here */
  391. *returncode = 1;
  392. if (returntext)
  393. {
  394. strcpy (returntext, "internal failure");
  395. }
  396. goto done;
  397. }
  398. #ifdef _WIN32
  399. if (strcasecmp (currentDir, config.dir) != 0)
  400. #else /* On Unix, path are case sensitive */
  401. if (strcmp (currentDir, config.dir) != 0)
  402. #endif
  403. {
  404. if (!cl5DbDirIsEmpty(config.dir))
  405. {
  406. *returncode = 1;
  407. if (returntext)
  408. {
  409. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  410. "The changelog directory [%s] already exists and is not empty. "
  411. "Please choose a directory that does not exist or is empty.\n",
  412. config.dir);
  413. }
  414. goto done;
  415. }
  416. if (!_is_absolutepath(config.dir) || (CL5_SUCCESS != cl5CreateDirIfNeeded(config.dir)))
  417. {
  418. *returncode = 1;
  419. if (returntext)
  420. {
  421. PL_strncpyz (returntext, "invalid changelog directory or insufficient access", SLAPI_DSE_RETURNTEXT_SIZE);
  422. }
  423. goto done;
  424. }
  425. /* changelog directory changed - need to remove the
  426. previous changelog and create new one */
  427. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name_cl,
  428. "changelog5_config_modify: changelog directory changed; "
  429. "old dir - %s, new dir - %s; recreating changelog.\n",
  430. currentDir, config.dir);
  431. rc = cl5Close ();
  432. if (rc != CL5_SUCCESS)
  433. {
  434. *returncode = 1;
  435. if (returntext)
  436. {
  437. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to close changelog; error - %d", rc);
  438. }
  439. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  440. "changelog5_config_modify: failed to close changelog\n");
  441. goto done;
  442. } else {
  443. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  444. "changelog5_config_modify: closed the changelog\n");
  445. }
  446. rc = cl5Delete (currentDir);
  447. if (rc != CL5_SUCCESS)
  448. {
  449. *returncode = 1;
  450. if (returntext)
  451. {
  452. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to remove changelog; error - %d", rc);
  453. }
  454. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  455. "changelog5_config_modify: failed to remove changelog\n");
  456. goto done;
  457. } else {
  458. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  459. "changelog5_config_modify: deleted the changelog at %s\n", currentDir);
  460. }
  461. rc = cl5Open (config.dir, &config.dbconfig);
  462. if (rc != CL5_SUCCESS)
  463. {
  464. *returncode = 1;
  465. if (returntext)
  466. {
  467. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to restart changelog; error - %d", rc);
  468. }
  469. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  470. "changelog5_config_modify: failed to restart changelog\n");
  471. /* before finishing, let's try to do some error recovery */
  472. if (CL5_SUCCESS != cl5Open(currentDir, &config.dbconfig))
  473. {
  474. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  475. "changelog5_config_modify: failed to restore previous changelog\n");
  476. }
  477. goto done;
  478. } else {
  479. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  480. "changelog5_config_modify: opened the changelog at %s\n", config.dir);
  481. }
  482. }
  483. }
  484. /* one of the changelog parameters is modified */
  485. if (config.maxEntries != CL5_NUM_IGNORE ||
  486. strcmp (config.maxAge, CL5_STR_IGNORE) != 0)
  487. {
  488. rc = cl5ConfigTrimming (config.maxEntries, config.maxAge);
  489. if (rc != CL5_SUCCESS)
  490. {
  491. *returncode = 1;
  492. if (returntext)
  493. {
  494. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to configure changelog trimming; error - %d", rc);
  495. }
  496. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  497. "changelog5_config_modify: failed to configure changelog trimming\n");
  498. goto done;
  499. }
  500. }
  501. done:;
  502. slapi_rwlock_unlock (s_configLock);
  503. changelog5_config_done (&config);
  504. changelog5_config_free (&originalConfig);
  505. /* slapi_ch_free accepts NULL pointer */
  506. slapi_ch_free ((void**)&currentDir);
  507. if (*returncode == LDAP_SUCCESS)
  508. {
  509. if (returntext)
  510. {
  511. returntext[0] = '\0';
  512. }
  513. return SLAPI_DSE_CALLBACK_OK;
  514. }
  515. return SLAPI_DSE_CALLBACK_ERROR;
  516. }
  517. static int
  518. changelog5_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  519. int *returncode, char *returntext, void *arg)
  520. {
  521. int rc;
  522. char *currentDir = NULL;
  523. *returncode = LDAP_SUCCESS;
  524. /* changelog must be open before it can be deleted */
  525. if (cl5GetState () != CL5_STATE_OPEN)
  526. {
  527. *returncode = 1;
  528. if (returntext)
  529. {
  530. PL_strncpyz(returntext, "changelog is not configured", SLAPI_DSE_RETURNTEXT_SIZE);
  531. }
  532. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  533. "changelog5_config_delete: chagelog is not configured\n");
  534. return SLAPI_DSE_CALLBACK_ERROR;
  535. }
  536. slapi_rwlock_wrlock (s_configLock);
  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_FATAL, repl_plugin_name_cl,
  546. "changelog5_config_delete: chagelog is not configured\n");
  547. goto done;
  548. }
  549. currentDir = cl5GetDir ();
  550. if (currentDir == NULL)
  551. {
  552. /* something is wrong: we should never be here */
  553. *returncode = 1;
  554. if (returntext)
  555. {
  556. PL_strncpyz (returntext, "internal failure", SLAPI_DSE_RETURNTEXT_SIZE);
  557. }
  558. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  559. "changelog5_config_delete: NULL directory\n");
  560. goto done;
  561. }
  562. /* this call will block until all threads using changelog
  563. release changelog by calling cl5RemoveThread () */
  564. rc = cl5Close ();
  565. if (rc != CL5_SUCCESS)
  566. {
  567. *returncode = 1;
  568. if (returntext)
  569. {
  570. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to close changelog; error - %d", rc);
  571. }
  572. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  573. "changelog5_config_delete: failed to close changelog\n");
  574. goto done;
  575. }
  576. rc = cl5Delete (currentDir);
  577. if (rc != CL5_SUCCESS)
  578. {
  579. *returncode = 1;
  580. if (returntext)
  581. {
  582. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to remove changelog; error - %d", rc);
  583. }
  584. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  585. "changelog5_config_delete: failed to remove changelog\n");
  586. goto done;
  587. }
  588. done:;
  589. slapi_rwlock_unlock (s_configLock);
  590. /* slapi_ch_free accepts NULL pointer */
  591. slapi_ch_free ((void**)&currentDir);
  592. if (*returncode == LDAP_SUCCESS)
  593. {
  594. if (returntext)
  595. {
  596. returntext[0] = '\0';
  597. }
  598. return SLAPI_DSE_CALLBACK_OK;
  599. }
  600. return SLAPI_DSE_CALLBACK_ERROR;
  601. }
  602. static int dont_allow_that(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  603. int *returncode, char *returntext, void *arg)
  604. {
  605. *returncode = LDAP_UNWILLING_TO_PERFORM;
  606. return SLAPI_DSE_CALLBACK_ERROR;
  607. }
  608. static changelog5Config * changelog5_dup_config(changelog5Config *config)
  609. {
  610. changelog5Config *dup = (changelog5Config *) slapi_ch_calloc(1, sizeof(changelog5Config));
  611. if (config->dir)
  612. dup->dir = slapi_ch_strdup(config->dir);
  613. if (config->maxAge)
  614. dup->maxAge = slapi_ch_strdup(config->maxAge);
  615. dup->maxEntries = config->maxEntries;
  616. dup->dbconfig.pageSize = config->dbconfig.pageSize;
  617. dup->dbconfig.fileMode = config->dbconfig.fileMode;
  618. dup->dbconfig.maxConcurrentWrites = config->dbconfig.maxConcurrentWrites;
  619. return dup;
  620. }
  621. /*
  622. * Given the changelog configuration entry, extract the configuration directives.
  623. */
  624. static void changelog5_extract_config(Slapi_Entry* entry, changelog5Config *config)
  625. {
  626. char *arg;
  627. memset (config, 0, sizeof (*config));
  628. config->dir = slapi_entry_attr_get_charptr(entry,CONFIG_CHANGELOG_DIR_ATTRIBUTE);
  629. replace_bslash (config->dir);
  630. arg= slapi_entry_attr_get_charptr(entry,CONFIG_CHANGELOG_MAXENTRIES_ATTRIBUTE);
  631. if (arg)
  632. {
  633. config->maxEntries = atoi (arg);
  634. slapi_ch_free_string(&arg);
  635. }
  636. config->maxAge = slapi_entry_attr_get_charptr(entry,CONFIG_CHANGELOG_MAXAGE_ATTRIBUTE);
  637. /*
  638. * Read the Changelog Internal Configuration Parameters for the Changelog DB
  639. */
  640. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_MAX_CONCURRENT_WRITES);
  641. if (arg)
  642. {
  643. config->dbconfig.maxConcurrentWrites = atoi (arg);
  644. slapi_ch_free_string(&arg);
  645. }
  646. if ( config->dbconfig.maxConcurrentWrites <= 0 )
  647. {
  648. config->dbconfig.maxConcurrentWrites = CL5_DEFAULT_CONFIG_MAX_CONCURRENT_WRITES;
  649. }
  650. /*
  651. * changelog encryption
  652. */
  653. arg = slapi_entry_attr_get_charptr(entry,
  654. CONFIG_CHANGELOG_ENCRYPTION_ALGORITHM);
  655. if (arg)
  656. {
  657. config->dbconfig.encryptionAlgorithm = slapi_ch_strdup(arg);
  658. slapi_ch_free_string(&arg);
  659. }
  660. else
  661. {
  662. config->dbconfig.encryptionAlgorithm = NULL; /* no encryption */
  663. }
  664. /*
  665. * symmetric key
  666. */
  667. arg = slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_SYMMETRIC_KEY);
  668. if (arg)
  669. {
  670. config->dbconfig.symmetricKey = slapi_ch_strdup(arg);
  671. slapi_ch_free_string(&arg);
  672. }
  673. else
  674. {
  675. config->dbconfig.symmetricKey = NULL; /* no symmetric key */
  676. }
  677. }
  678. static void replace_bslash (char *dir)
  679. {
  680. char *bslash;
  681. if (dir == NULL)
  682. return;
  683. bslash = strchr (dir, '\\');
  684. while (bslash)
  685. {
  686. *bslash = '/';
  687. bslash = strchr (bslash, '\\');
  688. }
  689. }
  690. static int notify_replica (Replica *r, void *arg)
  691. {
  692. return replica_log_ruv_elements (r);
  693. }
  694. static int _is_absolutepath (char * dir)
  695. {
  696. if (dir[0] == '/')
  697. return 1;
  698. #if defined(_WIN32)
  699. if (dir[2] == '/' && dir[1] == ':')
  700. return 1;
  701. #endif
  702. return 0;
  703. }