cl5_config.c 23 KB

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