cl5_config.c 23 KB

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