cl5_config.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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. if (strcmp (currentDir, config.dir) != 0)
  454. {
  455. if (!cl5DbDirIsEmpty(config.dir))
  456. {
  457. *returncode = 1;
  458. if (returntext)
  459. {
  460. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  461. "The changelog directory [%s] already exists and is not empty. "
  462. "Please choose a directory that does not exist or is empty.\n",
  463. config.dir);
  464. }
  465. goto done;
  466. }
  467. if (!_is_absolutepath(config.dir) || (CL5_SUCCESS != cl5CreateDirIfNeeded(config.dir)))
  468. {
  469. *returncode = 1;
  470. if (returntext)
  471. {
  472. PL_strncpyz (returntext, "invalid changelog directory or insufficient access", SLAPI_DSE_RETURNTEXT_SIZE);
  473. }
  474. goto done;
  475. }
  476. /* changelog directory changed - need to remove the
  477. previous changelog and create new one */
  478. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name_cl,
  479. "changelog5_config_modify: changelog directory changed; "
  480. "old dir - %s, new dir - %s; recreating changelog.\n",
  481. currentDir, config.dir);
  482. rc = cl5Close ();
  483. if (rc != CL5_SUCCESS)
  484. {
  485. *returncode = 1;
  486. if (returntext)
  487. {
  488. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to close changelog; error - %d", rc);
  489. }
  490. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  491. "changelog5_config_modify: failed to close changelog\n");
  492. goto done;
  493. } else {
  494. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  495. "changelog5_config_modify: closed the changelog\n");
  496. }
  497. rc = cl5Delete (currentDir);
  498. if (rc != CL5_SUCCESS)
  499. {
  500. *returncode = 1;
  501. if (returntext)
  502. {
  503. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to remove changelog; error - %d", rc);
  504. }
  505. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  506. "changelog5_config_modify: failed to remove changelog\n");
  507. goto done;
  508. } else {
  509. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  510. "changelog5_config_modify: deleted the changelog at %s\n", currentDir);
  511. }
  512. rc = cl5Open (config.dir, &config.dbconfig);
  513. if (rc != CL5_SUCCESS)
  514. {
  515. *returncode = 1;
  516. if (returntext)
  517. {
  518. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to restart changelog; error - %d", rc);
  519. }
  520. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  521. "changelog5_config_modify: failed to restart changelog\n");
  522. /* before finishing, let's try to do some error recovery */
  523. if (CL5_SUCCESS != cl5Open(currentDir, &config.dbconfig))
  524. {
  525. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  526. "changelog5_config_modify: failed to restore previous changelog\n");
  527. }
  528. goto done;
  529. } else {
  530. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  531. "changelog5_config_modify: opened the changelog at %s\n", config.dir);
  532. }
  533. }
  534. }
  535. /* one of the changelog parameters is modified */
  536. if (config.maxEntries != CL5_NUM_IGNORE ||
  537. config.trimInterval != CL5_NUM_IGNORE ||
  538. strcmp (config.maxAge, CL5_STR_IGNORE) != 0)
  539. {
  540. rc = cl5ConfigTrimming (config.maxEntries, config.maxAge, config.compactInterval, config.trimInterval);
  541. if (rc != CL5_SUCCESS)
  542. {
  543. *returncode = 1;
  544. if (returntext)
  545. {
  546. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to configure changelog trimming; error - %d", rc);
  547. }
  548. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  549. "changelog5_config_modify: failed to configure changelog trimming\n");
  550. goto done;
  551. }
  552. }
  553. done:;
  554. slapi_rwlock_unlock (s_configLock);
  555. changelog5_config_done (&config);
  556. changelog5_config_free (&originalConfig);
  557. /* slapi_ch_free accepts NULL pointer */
  558. slapi_ch_free ((void**)&currentDir);
  559. if (*returncode == LDAP_SUCCESS)
  560. {
  561. if (returntext)
  562. {
  563. returntext[0] = '\0';
  564. }
  565. return SLAPI_DSE_CALLBACK_OK;
  566. }
  567. return SLAPI_DSE_CALLBACK_ERROR;
  568. }
  569. static int
  570. changelog5_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  571. int *returncode, char *returntext, void *arg)
  572. {
  573. int rc;
  574. char *currentDir = NULL;
  575. *returncode = LDAP_SUCCESS;
  576. /* changelog must be open before it can be deleted */
  577. if (cl5GetState () != CL5_STATE_OPEN)
  578. {
  579. *returncode = 1;
  580. if (returntext)
  581. {
  582. PL_strncpyz(returntext, "changelog is not configured", SLAPI_DSE_RETURNTEXT_SIZE);
  583. }
  584. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  585. "changelog5_config_delete: chagelog is not configured\n");
  586. return SLAPI_DSE_CALLBACK_ERROR;
  587. }
  588. slapi_rwlock_wrlock (s_configLock);
  589. /* changelog must be open before it can be deleted */
  590. if (cl5GetState () != CL5_STATE_OPEN)
  591. {
  592. *returncode = 1;
  593. if (returntext)
  594. {
  595. PL_strncpyz(returntext, "changelog is not configured", SLAPI_DSE_RETURNTEXT_SIZE);
  596. }
  597. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  598. "changelog5_config_delete: chagelog is not configured\n");
  599. goto done;
  600. }
  601. currentDir = cl5GetDir ();
  602. if (currentDir == NULL)
  603. {
  604. /* something is wrong: we should never be here */
  605. *returncode = 1;
  606. if (returntext)
  607. {
  608. PL_strncpyz (returntext, "internal failure", SLAPI_DSE_RETURNTEXT_SIZE);
  609. }
  610. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  611. "changelog5_config_delete: NULL directory\n");
  612. goto done;
  613. }
  614. /* this call will block until all threads using changelog
  615. release changelog by calling cl5RemoveThread () */
  616. rc = cl5Close ();
  617. if (rc != CL5_SUCCESS)
  618. {
  619. *returncode = 1;
  620. if (returntext)
  621. {
  622. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to close changelog; error - %d", rc);
  623. }
  624. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  625. "changelog5_config_delete: failed to close changelog\n");
  626. goto done;
  627. }
  628. rc = cl5Delete (currentDir);
  629. if (rc != CL5_SUCCESS)
  630. {
  631. *returncode = 1;
  632. if (returntext)
  633. {
  634. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to remove changelog; error - %d", rc);
  635. }
  636. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  637. "changelog5_config_delete: failed to remove changelog\n");
  638. goto done;
  639. }
  640. done:;
  641. slapi_rwlock_unlock (s_configLock);
  642. /* slapi_ch_free accepts NULL pointer */
  643. slapi_ch_free ((void**)&currentDir);
  644. if (*returncode == LDAP_SUCCESS)
  645. {
  646. if (returntext)
  647. {
  648. returntext[0] = '\0';
  649. }
  650. return SLAPI_DSE_CALLBACK_OK;
  651. }
  652. return SLAPI_DSE_CALLBACK_ERROR;
  653. }
  654. static int dont_allow_that(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  655. int *returncode, char *returntext, void *arg)
  656. {
  657. *returncode = LDAP_UNWILLING_TO_PERFORM;
  658. return SLAPI_DSE_CALLBACK_ERROR;
  659. }
  660. static changelog5Config * changelog5_dup_config(changelog5Config *config)
  661. {
  662. changelog5Config *dup = (changelog5Config *) slapi_ch_calloc(1, sizeof(changelog5Config));
  663. if (config->dir)
  664. dup->dir = slapi_ch_strdup(config->dir);
  665. if (config->maxAge)
  666. dup->maxAge = slapi_ch_strdup(config->maxAge);
  667. dup->maxEntries = config->maxEntries;
  668. dup->compactInterval = config->compactInterval;
  669. dup->trimInterval = config->trimInterval;
  670. dup->dbconfig.pageSize = config->dbconfig.pageSize;
  671. dup->dbconfig.fileMode = config->dbconfig.fileMode;
  672. dup->dbconfig.maxConcurrentWrites = config->dbconfig.maxConcurrentWrites;
  673. return dup;
  674. }
  675. /*
  676. * Given the changelog configuration entry, extract the configuration directives.
  677. */
  678. static void changelog5_extract_config(Slapi_Entry* entry, changelog5Config *config)
  679. {
  680. char *arg;
  681. memset (config, 0, sizeof (*config));
  682. config->dir = slapi_entry_attr_get_charptr(entry,CONFIG_CHANGELOG_DIR_ATTRIBUTE);
  683. replace_bslash (config->dir);
  684. arg = slapi_entry_attr_get_charptr(entry,CONFIG_CHANGELOG_MAXENTRIES_ATTRIBUTE);
  685. if (arg)
  686. {
  687. config->maxEntries = atoi (arg);
  688. slapi_ch_free_string(&arg);
  689. }
  690. arg = slapi_entry_attr_get_charptr(entry,CONFIG_CHANGELOG_COMPACTDB_ATTRIBUTE);
  691. if (arg)
  692. {
  693. char *endp = NULL;
  694. long value;
  695. errno = 0;
  696. if (arg)
  697. {
  698. value = strtol(arg, &endp, 10);
  699. if (*endp != '\0' || errno == ERANGE || value < 0 ) {
  700. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  701. "changelog5_extract_config: %s: invalid value \"%s\", using default value (%d)\n",
  702. CONFIG_CHANGELOG_COMPACTDB_ATTRIBUTE, arg,
  703. CHANGELOGDB_COMPACT_INTERVAL );
  704. config->compactInterval = CHANGELOGDB_COMPACT_INTERVAL;
  705. } else {
  706. config->compactInterval = value;
  707. }
  708. }
  709. slapi_ch_free_string(&arg);
  710. }
  711. else
  712. {
  713. config->compactInterval = CHANGELOGDB_COMPACT_INTERVAL;
  714. }
  715. arg = slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_TRIM_ATTRIBUTE);
  716. if (arg)
  717. {
  718. char *endp = NULL;
  719. long value;
  720. errno = 0;
  721. if (arg)
  722. {
  723. value = strtol(arg, &endp, 10);
  724. if (*endp != '\0' || errno == ERANGE || value < 0 ) {
  725. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  726. "changelog5_extract_config: %s: invalid value \"%s\", using default value (%d)\n",
  727. CONFIG_CHANGELOG_TRIM_ATTRIBUTE, arg,
  728. CHANGELOGDB_TRIM_INTERVAL );
  729. config->trimInterval = CHANGELOGDB_TRIM_INTERVAL;
  730. } else {
  731. config->trimInterval = value;
  732. }
  733. }
  734. slapi_ch_free_string(&arg);
  735. }
  736. else
  737. {
  738. config->trimInterval = CHANGELOGDB_TRIM_INTERVAL;
  739. }
  740. config->maxAge = slapi_entry_attr_get_charptr(entry,CONFIG_CHANGELOG_MAXAGE_ATTRIBUTE);
  741. /*
  742. * Read the Changelog Internal Configuration Parameters for the Changelog DB
  743. */
  744. arg = slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_MAX_CONCURRENT_WRITES);
  745. if (arg)
  746. {
  747. config->dbconfig.maxConcurrentWrites = atoi (arg);
  748. slapi_ch_free_string(&arg);
  749. }
  750. if ( config->dbconfig.maxConcurrentWrites <= 0 )
  751. {
  752. config->dbconfig.maxConcurrentWrites = CL5_DEFAULT_CONFIG_MAX_CONCURRENT_WRITES;
  753. }
  754. /*
  755. * changelog encryption
  756. */
  757. arg = slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_ENCRYPTION_ALGORITHM);
  758. if (arg)
  759. {
  760. config->dbconfig.encryptionAlgorithm = slapi_ch_strdup(arg);
  761. slapi_ch_free_string(&arg);
  762. }
  763. else
  764. {
  765. config->dbconfig.encryptionAlgorithm = NULL; /* no encryption */
  766. }
  767. /*
  768. * symmetric key
  769. */
  770. arg = slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_SYMMETRIC_KEY);
  771. if (arg)
  772. {
  773. config->dbconfig.symmetricKey = slapi_ch_strdup(arg);
  774. slapi_ch_free_string(&arg);
  775. }
  776. else
  777. {
  778. config->dbconfig.symmetricKey = NULL; /* no symmetric key */
  779. }
  780. }
  781. static void replace_bslash (char *dir)
  782. {
  783. char *bslash;
  784. if (dir == NULL)
  785. return;
  786. bslash = strchr (dir, '\\');
  787. while (bslash)
  788. {
  789. *bslash = '/';
  790. bslash = strchr (bslash, '\\');
  791. }
  792. }
  793. static int notify_replica (Replica *r, void *arg)
  794. {
  795. return replica_log_ruv_elements (r);
  796. }
  797. static int _is_absolutepath (char * dir)
  798. {
  799. if (dir[0] == '/')
  800. return 1;
  801. return 0;
  802. }