1
0

cl5_config.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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 && 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. if ( slapi_attr_is_last_mod(config_attr)){
  314. continue;
  315. }
  316. /* replace existing value */
  317. if ( strcasecmp (config_attr, CONFIG_CHANGELOG_DIR_ATTRIBUTE ) == 0 )
  318. {
  319. if (config_attr_value && config_attr_value[0] != '\0')
  320. {
  321. slapi_ch_free_string(&config.dir);
  322. config.dir = slapi_ch_strdup(config_attr_value);
  323. replace_bslash (config.dir);
  324. }
  325. else
  326. {
  327. *returncode = 1;
  328. if (returntext)
  329. {
  330. strcpy (returntext, "null changelog directory");
  331. }
  332. goto done;
  333. }
  334. }
  335. else if ( strcasecmp ( config_attr, CONFIG_CHANGELOG_MAXENTRIES_ATTRIBUTE ) == 0 )
  336. {
  337. if (config_attr_value && config_attr_value[0] != '\0')
  338. {
  339. config.maxEntries = atoi (config_attr_value);
  340. }
  341. else
  342. {
  343. config.maxEntries = 0;
  344. }
  345. }
  346. else if ( strcasecmp ( config_attr, CONFIG_CHANGELOG_MAXAGE_ATTRIBUTE ) == 0 )
  347. {
  348. slapi_ch_free_string(&config.maxAge);
  349. config.maxAge = slapi_ch_strdup(config_attr_value);
  350. }
  351. else if ( strcasecmp ( config_attr, CONFIG_CHANGELOG_COMPACTDB_ATTRIBUTE ) == 0 )
  352. {
  353. char *endp = NULL;
  354. long value;
  355. errno = 0;
  356. if (config_attr_value && config_attr_value[0] != '\0')
  357. {
  358. value = strtol(config_attr_value, &endp, 10);
  359. if (*endp != '\0' || errno == ERANGE || value < 0 ) {
  360. if (returntext)
  361. {
  362. PR_snprintf ( returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  363. "%s: invalid value \"%s\", %s must range from 0 to %lld",
  364. CONFIG_CHANGELOG_COMPACTDB_ATTRIBUTE, config_attr_value,
  365. CONFIG_CHANGELOG_COMPACTDB_ATTRIBUTE,
  366. (long long int)LONG_MAX );
  367. *returncode = LDAP_UNWILLING_TO_PERFORM;
  368. goto done;
  369. }
  370. config.compactInterval = value;
  371. }
  372. }
  373. else
  374. {
  375. config.compactInterval = 0;
  376. }
  377. }
  378. else if ( strcasecmp ( config_attr, CONFIG_CHANGELOG_TRIM_ATTRIBUTE ) == 0 )
  379. {
  380. char *endp = NULL;
  381. long value;
  382. errno = 0;
  383. if (config_attr_value && config_attr_value[0] != '\0')
  384. {
  385. value = strtol(config_attr_value, &endp, 10);
  386. if (*endp != '\0' || errno == ERANGE || value < 0 ) {
  387. if (returntext)
  388. {
  389. PR_snprintf ( returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  390. "%s: invalid value \"%s\", %s must range from 0 to %lld",
  391. CONFIG_CHANGELOG_TRIM_ATTRIBUTE, config_attr_value,
  392. CONFIG_CHANGELOG_TRIM_ATTRIBUTE,
  393. (long long int)LONG_MAX );
  394. *returncode = LDAP_UNWILLING_TO_PERFORM;
  395. goto done;
  396. }
  397. config.trimInterval = value;
  398. }
  399. }
  400. else
  401. {
  402. config.trimInterval = CHANGELOGDB_TRIM_INTERVAL;
  403. }
  404. }
  405. else if ( strcasecmp ( config_attr, CONFIG_CHANGELOG_SYMMETRIC_KEY ) == 0 )
  406. {
  407. slapi_ch_free_string(&config.symmetricKey);
  408. config.symmetricKey = slapi_ch_strdup(config_attr_value);
  409. /* Storing the encryption symmetric key */
  410. /* no need to change any changelog configuration */
  411. goto done;
  412. }
  413. else
  414. {
  415. *returncode = LDAP_UNWILLING_TO_PERFORM;
  416. if (returntext)
  417. {
  418. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  419. "Unwilling to apply %s mods while the server is running", config_attr);
  420. }
  421. goto done;
  422. }
  423. }
  424. }
  425. }
  426. /* Undo the reset above for all the modifiable attributes that were not modified
  427. * except config.dir */
  428. if (config.maxEntries == CL5_NUM_IGNORE)
  429. config.maxEntries = originalConfig->maxEntries;
  430. if (config.compactInterval == CL5_NUM_IGNORE)
  431. config.compactInterval = originalConfig->compactInterval;
  432. if (config.trimInterval == CL5_NUM_IGNORE)
  433. config.trimInterval = originalConfig->trimInterval;
  434. if (strcmp (config.maxAge, CL5_STR_IGNORE) == 0) {
  435. slapi_ch_free_string(&config.maxAge);
  436. if (originalConfig->maxAge)
  437. config.maxAge = slapi_ch_strdup(originalConfig->maxAge);
  438. }
  439. /* attempt to change chagelog dir */
  440. if (config.dir)
  441. {
  442. currentDir = cl5GetDir ();
  443. if (currentDir == NULL)
  444. {
  445. /* something is wrong: we should never be here */
  446. *returncode = 1;
  447. if (returntext)
  448. {
  449. strcpy (returntext, "internal failure");
  450. }
  451. goto done;
  452. }
  453. #ifdef _WIN32
  454. if (strcasecmp (currentDir, config.dir) != 0)
  455. #else /* On Unix, path are case sensitive */
  456. if (strcmp (currentDir, config.dir) != 0)
  457. #endif
  458. {
  459. if (!cl5DbDirIsEmpty(config.dir))
  460. {
  461. *returncode = 1;
  462. if (returntext)
  463. {
  464. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  465. "The changelog directory [%s] already exists and is not empty. "
  466. "Please choose a directory that does not exist or is empty.\n",
  467. config.dir);
  468. }
  469. goto done;
  470. }
  471. if (!_is_absolutepath(config.dir) || (CL5_SUCCESS != cl5CreateDirIfNeeded(config.dir)))
  472. {
  473. *returncode = 1;
  474. if (returntext)
  475. {
  476. PL_strncpyz (returntext, "invalid changelog directory or insufficient access", SLAPI_DSE_RETURNTEXT_SIZE);
  477. }
  478. goto done;
  479. }
  480. /* changelog directory changed - need to remove the
  481. previous changelog and create new one */
  482. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name_cl,
  483. "changelog5_config_modify: changelog directory changed; "
  484. "old dir - %s, new dir - %s; recreating changelog.\n",
  485. currentDir, config.dir);
  486. rc = cl5Close ();
  487. if (rc != CL5_SUCCESS)
  488. {
  489. *returncode = 1;
  490. if (returntext)
  491. {
  492. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to close changelog; error - %d", rc);
  493. }
  494. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  495. "changelog5_config_modify: failed to close changelog\n");
  496. goto done;
  497. } else {
  498. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  499. "changelog5_config_modify: closed the changelog\n");
  500. }
  501. rc = cl5Delete (currentDir);
  502. if (rc != CL5_SUCCESS)
  503. {
  504. *returncode = 1;
  505. if (returntext)
  506. {
  507. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to remove changelog; error - %d", rc);
  508. }
  509. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  510. "changelog5_config_modify: failed to remove changelog\n");
  511. goto done;
  512. } else {
  513. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  514. "changelog5_config_modify: deleted the changelog at %s\n", currentDir);
  515. }
  516. rc = cl5Open (config.dir, &config.dbconfig);
  517. if (rc != CL5_SUCCESS)
  518. {
  519. *returncode = 1;
  520. if (returntext)
  521. {
  522. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to restart changelog; error - %d", rc);
  523. }
  524. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  525. "changelog5_config_modify: failed to restart changelog\n");
  526. /* before finishing, let's try to do some error recovery */
  527. if (CL5_SUCCESS != cl5Open(currentDir, &config.dbconfig))
  528. {
  529. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  530. "changelog5_config_modify: failed to restore previous changelog\n");
  531. }
  532. goto done;
  533. } else {
  534. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  535. "changelog5_config_modify: opened the changelog at %s\n", config.dir);
  536. }
  537. }
  538. }
  539. /* one of the changelog parameters is modified */
  540. if (config.maxEntries != CL5_NUM_IGNORE ||
  541. config.trimInterval != CL5_NUM_IGNORE ||
  542. strcmp (config.maxAge, CL5_STR_IGNORE) != 0)
  543. {
  544. rc = cl5ConfigTrimming (config.maxEntries, config.maxAge, config.compactInterval, config.trimInterval);
  545. if (rc != CL5_SUCCESS)
  546. {
  547. *returncode = 1;
  548. if (returntext)
  549. {
  550. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to configure changelog trimming; error - %d", rc);
  551. }
  552. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  553. "changelog5_config_modify: failed to configure changelog trimming\n");
  554. goto done;
  555. }
  556. }
  557. done:;
  558. slapi_rwlock_unlock (s_configLock);
  559. changelog5_config_done (&config);
  560. changelog5_config_free (&originalConfig);
  561. /* slapi_ch_free accepts NULL pointer */
  562. slapi_ch_free ((void**)&currentDir);
  563. if (*returncode == LDAP_SUCCESS)
  564. {
  565. if (returntext)
  566. {
  567. returntext[0] = '\0';
  568. }
  569. return SLAPI_DSE_CALLBACK_OK;
  570. }
  571. return SLAPI_DSE_CALLBACK_ERROR;
  572. }
  573. static int
  574. changelog5_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  575. int *returncode, char *returntext, void *arg)
  576. {
  577. int rc;
  578. char *currentDir = NULL;
  579. *returncode = LDAP_SUCCESS;
  580. /* changelog must be open before it can be deleted */
  581. if (cl5GetState () != CL5_STATE_OPEN)
  582. {
  583. *returncode = 1;
  584. if (returntext)
  585. {
  586. PL_strncpyz(returntext, "changelog is not configured", SLAPI_DSE_RETURNTEXT_SIZE);
  587. }
  588. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  589. "changelog5_config_delete: chagelog is not configured\n");
  590. return SLAPI_DSE_CALLBACK_ERROR;
  591. }
  592. slapi_rwlock_wrlock (s_configLock);
  593. /* changelog must be open before it can be deleted */
  594. if (cl5GetState () != CL5_STATE_OPEN)
  595. {
  596. *returncode = 1;
  597. if (returntext)
  598. {
  599. PL_strncpyz(returntext, "changelog is not configured", SLAPI_DSE_RETURNTEXT_SIZE);
  600. }
  601. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  602. "changelog5_config_delete: chagelog is not configured\n");
  603. goto done;
  604. }
  605. currentDir = cl5GetDir ();
  606. if (currentDir == NULL)
  607. {
  608. /* something is wrong: we should never be here */
  609. *returncode = 1;
  610. if (returntext)
  611. {
  612. PL_strncpyz (returntext, "internal failure", SLAPI_DSE_RETURNTEXT_SIZE);
  613. }
  614. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  615. "changelog5_config_delete: NULL directory\n");
  616. goto done;
  617. }
  618. /* this call will block until all threads using changelog
  619. release changelog by calling cl5RemoveThread () */
  620. rc = cl5Close ();
  621. if (rc != CL5_SUCCESS)
  622. {
  623. *returncode = 1;
  624. if (returntext)
  625. {
  626. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to close changelog; error - %d", rc);
  627. }
  628. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  629. "changelog5_config_delete: failed to close changelog\n");
  630. goto done;
  631. }
  632. rc = cl5Delete (currentDir);
  633. if (rc != CL5_SUCCESS)
  634. {
  635. *returncode = 1;
  636. if (returntext)
  637. {
  638. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to remove changelog; error - %d", rc);
  639. }
  640. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  641. "changelog5_config_delete: failed to remove changelog\n");
  642. goto done;
  643. }
  644. done:;
  645. slapi_rwlock_unlock (s_configLock);
  646. /* slapi_ch_free accepts NULL pointer */
  647. slapi_ch_free ((void**)&currentDir);
  648. if (*returncode == LDAP_SUCCESS)
  649. {
  650. if (returntext)
  651. {
  652. returntext[0] = '\0';
  653. }
  654. return SLAPI_DSE_CALLBACK_OK;
  655. }
  656. return SLAPI_DSE_CALLBACK_ERROR;
  657. }
  658. static int dont_allow_that(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  659. int *returncode, char *returntext, void *arg)
  660. {
  661. *returncode = LDAP_UNWILLING_TO_PERFORM;
  662. return SLAPI_DSE_CALLBACK_ERROR;
  663. }
  664. static changelog5Config * changelog5_dup_config(changelog5Config *config)
  665. {
  666. changelog5Config *dup = (changelog5Config *) slapi_ch_calloc(1, sizeof(changelog5Config));
  667. if (config->dir)
  668. dup->dir = slapi_ch_strdup(config->dir);
  669. if (config->maxAge)
  670. dup->maxAge = slapi_ch_strdup(config->maxAge);
  671. dup->maxEntries = config->maxEntries;
  672. dup->compactInterval = config->compactInterval;
  673. dup->trimInterval = config->trimInterval;
  674. dup->dbconfig.pageSize = config->dbconfig.pageSize;
  675. dup->dbconfig.fileMode = config->dbconfig.fileMode;
  676. dup->dbconfig.maxConcurrentWrites = config->dbconfig.maxConcurrentWrites;
  677. return dup;
  678. }
  679. /*
  680. * Given the changelog configuration entry, extract the configuration directives.
  681. */
  682. static void changelog5_extract_config(Slapi_Entry* entry, changelog5Config *config)
  683. {
  684. char *arg;
  685. memset (config, 0, sizeof (*config));
  686. config->dir = slapi_entry_attr_get_charptr(entry,CONFIG_CHANGELOG_DIR_ATTRIBUTE);
  687. replace_bslash (config->dir);
  688. arg = slapi_entry_attr_get_charptr(entry,CONFIG_CHANGELOG_MAXENTRIES_ATTRIBUTE);
  689. if (arg)
  690. {
  691. config->maxEntries = atoi (arg);
  692. slapi_ch_free_string(&arg);
  693. }
  694. arg = slapi_entry_attr_get_charptr(entry,CONFIG_CHANGELOG_COMPACTDB_ATTRIBUTE);
  695. if (arg)
  696. {
  697. char *endp = NULL;
  698. long value;
  699. errno = 0;
  700. if (arg)
  701. {
  702. value = strtol(arg, &endp, 10);
  703. if (*endp != '\0' || errno == ERANGE || value < 0 ) {
  704. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  705. "changelog5_extract_config: %s: invalid value \"%s\", using default value (%d)\n",
  706. CONFIG_CHANGELOG_COMPACTDB_ATTRIBUTE, arg,
  707. CHANGELOGDB_COMPACT_INTERVAL );
  708. config->compactInterval = CHANGELOGDB_COMPACT_INTERVAL;
  709. } else {
  710. config->compactInterval = value;
  711. }
  712. }
  713. slapi_ch_free_string(&arg);
  714. }
  715. else
  716. {
  717. config->compactInterval = CHANGELOGDB_COMPACT_INTERVAL;
  718. }
  719. arg = slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_TRIM_ATTRIBUTE);
  720. if (arg)
  721. {
  722. char *endp = NULL;
  723. long value;
  724. errno = 0;
  725. if (arg)
  726. {
  727. value = strtol(arg, &endp, 10);
  728. if (*endp != '\0' || errno == ERANGE || value < 0 ) {
  729. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  730. "changelog5_extract_config: %s: invalid value \"%s\", using default value (%d)\n",
  731. CONFIG_CHANGELOG_TRIM_ATTRIBUTE, arg,
  732. CHANGELOGDB_TRIM_INTERVAL );
  733. config->trimInterval = CHANGELOGDB_TRIM_INTERVAL;
  734. } else {
  735. config->trimInterval = value;
  736. }
  737. }
  738. slapi_ch_free_string(&arg);
  739. }
  740. else
  741. {
  742. config->trimInterval = CHANGELOGDB_TRIM_INTERVAL;
  743. }
  744. config->maxAge = slapi_entry_attr_get_charptr(entry,CONFIG_CHANGELOG_MAXAGE_ATTRIBUTE);
  745. /*
  746. * Read the Changelog Internal Configuration Parameters for the Changelog DB
  747. */
  748. arg = slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_MAX_CONCURRENT_WRITES);
  749. if (arg)
  750. {
  751. config->dbconfig.maxConcurrentWrites = atoi (arg);
  752. slapi_ch_free_string(&arg);
  753. }
  754. if ( config->dbconfig.maxConcurrentWrites <= 0 )
  755. {
  756. config->dbconfig.maxConcurrentWrites = CL5_DEFAULT_CONFIG_MAX_CONCURRENT_WRITES;
  757. }
  758. /*
  759. * changelog encryption
  760. */
  761. arg = slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_ENCRYPTION_ALGORITHM);
  762. if (arg)
  763. {
  764. config->dbconfig.encryptionAlgorithm = slapi_ch_strdup(arg);
  765. slapi_ch_free_string(&arg);
  766. }
  767. else
  768. {
  769. config->dbconfig.encryptionAlgorithm = NULL; /* no encryption */
  770. }
  771. /*
  772. * symmetric key
  773. */
  774. arg = slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_SYMMETRIC_KEY);
  775. if (arg)
  776. {
  777. config->dbconfig.symmetricKey = slapi_ch_strdup(arg);
  778. slapi_ch_free_string(&arg);
  779. }
  780. else
  781. {
  782. config->dbconfig.symmetricKey = NULL; /* no symmetric key */
  783. }
  784. }
  785. static void replace_bslash (char *dir)
  786. {
  787. char *bslash;
  788. if (dir == NULL)
  789. return;
  790. bslash = strchr (dir, '\\');
  791. while (bslash)
  792. {
  793. *bslash = '/';
  794. bslash = strchr (bslash, '\\');
  795. }
  796. }
  797. static int notify_replica (Replica *r, void *arg)
  798. {
  799. return replica_log_ruv_elements (r);
  800. }
  801. static int _is_absolutepath (char * dir)
  802. {
  803. if (dir[0] == '/')
  804. return 1;
  805. #if defined(_WIN32)
  806. if (dir[2] == '/' && dir[1] == ':')
  807. return 1;
  808. #endif
  809. return 0;
  810. }