cl5_config.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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. /* start the changelog */
  182. rc = cl5Open (config.dir, &config.dbconfig);
  183. if (rc != CL5_SUCCESS)
  184. {
  185. *returncode = 1;
  186. if (returntext)
  187. {
  188. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to start changelog; error - %d", rc);
  189. }
  190. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  191. "changelog5_config_add: failed to start changelog\n");
  192. goto done;
  193. }
  194. /* set trimming parameters */
  195. rc = cl5ConfigTrimming (config.maxEntries, config.maxAge);
  196. if (rc != CL5_SUCCESS)
  197. {
  198. *returncode = 1;
  199. if (returntext)
  200. {
  201. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to configure changelog trimming; error - %d", rc);
  202. }
  203. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  204. "changelog5_config_add: failed to configure changelog trimming\n");
  205. goto done;
  206. }
  207. /* notify all the replicas that the changelog is configured
  208. so that the can log dummy changes if necessary. */
  209. replica_enumerate_replicas (notify_replica, NULL);
  210. #ifdef TEST_CL5
  211. testChangelog (TEST_ITERATION);
  212. #endif
  213. done:;
  214. PR_RWLock_Unlock (s_configLock);
  215. changelog5_config_done (&config);
  216. if (*returncode == LDAP_SUCCESS)
  217. {
  218. if (returntext)
  219. {
  220. returntext[0] = '\0';
  221. }
  222. return SLAPI_DSE_CALLBACK_OK;
  223. }
  224. return SLAPI_DSE_CALLBACK_ERROR;
  225. }
  226. static int
  227. changelog5_config_modify (Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  228. int *returncode, char *returntext, void *arg)
  229. {
  230. int rc= 0;
  231. LDAPMod **mods;
  232. int i;
  233. changelog5Config config;
  234. changelog5Config * originalConfig = NULL;
  235. char *currentDir = NULL;
  236. *returncode = LDAP_SUCCESS;
  237. /* changelog must be open before its parameters can be modified */
  238. if (cl5GetState() != CL5_STATE_OPEN)
  239. {
  240. if (returntext)
  241. {
  242. strcpy (returntext, "changelog is not configured");
  243. }
  244. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  245. "changelog5_config_modify: changelog is not configured\n");
  246. return SLAPI_DSE_CALLBACK_ERROR;
  247. }
  248. PR_RWLock_Wlock (s_configLock);
  249. /* changelog must be open before its parameters can be modified */
  250. if (cl5GetState() != CL5_STATE_OPEN)
  251. {
  252. *returncode = 1;
  253. if (returntext)
  254. {
  255. strcpy (returntext, "changelog is not configured");
  256. }
  257. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  258. "changelog5_config_modify: changelog is not configured\n");
  259. goto done;
  260. }
  261. /*
  262. * Extract all the original configuration: This is needed to ensure that the configuration
  263. * is trully reloaded. This was not needed before 091401 because the changelog configuration
  264. * was always hardcoded (NULL was being passed to cl5Open). Now we need to ensure we pass to
  265. * cl5Open the proper configuration...
  266. */
  267. changelog5_extract_config(e, &config);
  268. originalConfig = changelog5_dup_config(&config);
  269. /* Reset all the attributes that have been potentially modified by the current MODIFY operation */
  270. slapi_ch_free_string(&config.dir);
  271. config.dir = NULL;
  272. config.maxEntries = CL5_NUM_IGNORE;
  273. slapi_ch_free_string(&config.maxAge);
  274. config.maxAge = slapi_ch_strdup(CL5_STR_IGNORE);
  275. config.dbconfig.maxChCacheEntries = 0;
  276. config.dbconfig.maxChCacheSize = CL5_NUM_IGNORE;
  277. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  278. for (i = 0; mods[i] != NULL; i++)
  279. {
  280. if (mods[i]->mod_op & LDAP_MOD_DELETE)
  281. {
  282. /* We don't support deleting changelog attributes */
  283. }
  284. else
  285. {
  286. int j;
  287. for (j = 0; ((mods[i]->mod_values[j]) && (LDAP_SUCCESS == rc)); j++)
  288. {
  289. char *config_attr, *config_attr_value;
  290. config_attr = (char *) mods[i]->mod_type;
  291. config_attr_value = (char *) mods[i]->mod_bvalues[j]->bv_val;
  292. #define ATTR_MODIFIERSNAME "modifiersname"
  293. #define ATTR_MODIFYTIMESTAMP "modifytimestamp"
  294. if ( strcasecmp ( config_attr, ATTR_MODIFIERSNAME ) == 0 ) {
  295. continue;
  296. }
  297. if ( strcasecmp ( config_attr, ATTR_MODIFYTIMESTAMP ) == 0 ) {
  298. continue;
  299. }
  300. /* replace existing value */
  301. if ( strcasecmp (config_attr, CONFIG_CHANGELOG_DIR_ATTRIBUTE ) == 0 )
  302. {
  303. if (config_attr_value && config_attr_value[0] != '\0')
  304. {
  305. slapi_ch_free_string(&config.dir);
  306. config.dir = slapi_ch_strdup(config_attr_value);
  307. replace_bslash (config.dir);
  308. }
  309. else
  310. {
  311. *returncode = 1;
  312. if (returntext)
  313. {
  314. strcpy (returntext, "null changelog directory");
  315. }
  316. goto done;
  317. }
  318. }
  319. else if ( strcasecmp ( config_attr, CONFIG_CHANGELOG_MAXENTRIES_ATTRIBUTE ) == 0 )
  320. {
  321. if (config_attr_value && config_attr_value[0] != '\0')
  322. {
  323. config.maxEntries = atoi (config_attr_value);
  324. }
  325. else
  326. {
  327. config.maxEntries = 0;
  328. }
  329. }
  330. else if ( strcasecmp ( config_attr, CONFIG_CHANGELOG_MAXAGE_ATTRIBUTE ) == 0 )
  331. {
  332. slapi_ch_free_string(&config.maxAge);
  333. config.maxAge = slapi_ch_strdup(config_attr_value);
  334. }
  335. else if ( strcasecmp ( config_attr, CONFIG_CHANGELOG_CACHESIZE ) == 0 )
  336. { /* The Changelog Cache Size parameters can be modified online without a need for restart */
  337. if (config_attr_value && config_attr_value[0] != '\0')
  338. {
  339. config.dbconfig.maxChCacheEntries = atoi (config_attr_value);
  340. }
  341. else
  342. {
  343. config.dbconfig.maxChCacheEntries = 0;
  344. }
  345. }
  346. else if ( strcasecmp ( config_attr, CONFIG_CHANGELOG_CACHEMEMSIZE ) == 0 )
  347. { /* The Changelog Cache Size parameters can be modified online without a need for restart */
  348. if (config_attr_value && config_attr_value[0] != '\0')
  349. {
  350. config.dbconfig.maxChCacheSize = atoi (config_attr_value);
  351. }
  352. else
  353. {
  354. config.dbconfig.maxChCacheSize = 0;
  355. }
  356. }
  357. else
  358. {
  359. *returncode = LDAP_UNWILLING_TO_PERFORM;
  360. if (returntext)
  361. {
  362. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  363. "Unwilling to apply %s mods while the server is running", config_attr);
  364. }
  365. goto done;
  366. }
  367. }
  368. }
  369. }
  370. /* Undo the reset above for all the modifiable attributes that were not modified
  371. * except config.dir */
  372. if (config.maxEntries == CL5_NUM_IGNORE)
  373. config.maxEntries = originalConfig->maxEntries;
  374. if (strcmp (config.maxAge, CL5_STR_IGNORE) == 0) {
  375. slapi_ch_free_string(&config.maxAge);
  376. if (originalConfig->maxAge)
  377. config.maxAge = slapi_ch_strdup(originalConfig->maxAge);
  378. }
  379. if (config.dbconfig.maxChCacheEntries == 0)
  380. config.dbconfig.maxChCacheEntries = originalConfig->dbconfig.maxChCacheEntries;
  381. if (config.dbconfig.maxChCacheSize == CL5_NUM_IGNORE)
  382. config.dbconfig.maxChCacheSize = originalConfig->dbconfig.maxChCacheSize;
  383. /* attempt to change chagelog dir */
  384. if (config.dir)
  385. {
  386. currentDir = cl5GetDir ();
  387. if (currentDir == NULL)
  388. {
  389. /* something is wrong: we should never be here */
  390. *returncode = 1;
  391. if (returntext)
  392. {
  393. strcpy (returntext, "internal failure");
  394. }
  395. goto done;
  396. }
  397. #ifdef _WIN32
  398. if (strcasecmp (currentDir, config.dir) != 0)
  399. #else /* On Unix, path are case sensitive */
  400. if (strcmp (currentDir, config.dir) != 0)
  401. #endif
  402. {
  403. if (!_is_absolutepath(config.dir) || (CL5_SUCCESS != cl5CreateDirIfNeeded(config.dir)))
  404. {
  405. *returncode = 1;
  406. if (returntext)
  407. {
  408. PL_strncpyz (returntext, "invalid changelog directory or insufficient access", SLAPI_DSE_RETURNTEXT_SIZE);
  409. }
  410. goto done;
  411. }
  412. /* changelog directory changed - need to remove the
  413. previous changelog and create new one */
  414. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name_cl,
  415. "changelog5_config_modify: changelog directory changed; "
  416. "old dir - %s, new dir - %s; recreating changelog.\n",
  417. currentDir, config.dir);
  418. /* this call will block until all threads using changelog
  419. release changelog by calling cl5RemoveThread () */
  420. rc = cl5Close ();
  421. if (rc != CL5_SUCCESS)
  422. {
  423. *returncode = 1;
  424. if (returntext)
  425. {
  426. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to close changelog; error - %d", rc);
  427. }
  428. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  429. "changelog5_config_modify: failed to close changelog\n");
  430. goto done;
  431. }
  432. rc = cl5Delete (currentDir);
  433. if (rc != CL5_SUCCESS)
  434. {
  435. *returncode = 1;
  436. if (returntext)
  437. {
  438. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to remove changelog; error - %d", rc);
  439. }
  440. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  441. "changelog5_config_modify: failed to remove changelog\n");
  442. goto done;
  443. }
  444. rc = cl5Open (config.dir, &config.dbconfig);
  445. if (rc != CL5_SUCCESS)
  446. {
  447. *returncode = 1;
  448. if (returntext)
  449. {
  450. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to restart changelog; error - %d", rc);
  451. }
  452. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  453. "changelog5_config_modify: failed to restart changelog\n");
  454. /* before finishing, let's try to do some error recovery */
  455. if (CL5_SUCCESS != cl5Open(currentDir, &config.dbconfig)) {
  456. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  457. "changelog5_config_modify: failed to restore previous changelog\n");
  458. }
  459. goto done;
  460. }
  461. }
  462. }
  463. /* one of the changelog parameters is modified */
  464. if (config.maxEntries != CL5_NUM_IGNORE ||
  465. strcmp (config.maxAge, CL5_STR_IGNORE) != 0)
  466. {
  467. rc = cl5ConfigTrimming (config.maxEntries, config.maxAge);
  468. if (rc != CL5_SUCCESS)
  469. {
  470. *returncode = 1;
  471. if (returntext)
  472. {
  473. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to configure changelog trimming; error - %d", rc);
  474. }
  475. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  476. "changelog5_config_modify: failed to configure changelog trimming\n");
  477. goto done;
  478. }
  479. }
  480. if (config.dbconfig.maxChCacheEntries != 0 || config.dbconfig.maxChCacheSize != CL5_NUM_IGNORE)
  481. clcache_set_config(&config.dbconfig);
  482. done:;
  483. PR_RWLock_Unlock (s_configLock);
  484. changelog5_config_done (&config);
  485. changelog5_config_free (&originalConfig);
  486. /* slapi_ch_free accepts NULL pointer */
  487. slapi_ch_free ((void**)&currentDir);
  488. if (*returncode == LDAP_SUCCESS)
  489. {
  490. if (returntext)
  491. {
  492. returntext[0] = '\0';
  493. }
  494. return SLAPI_DSE_CALLBACK_OK;
  495. }
  496. return SLAPI_DSE_CALLBACK_ERROR;
  497. }
  498. static int
  499. changelog5_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  500. int *returncode, char *returntext, void *arg)
  501. {
  502. int rc;
  503. char *currentDir = NULL;
  504. *returncode = LDAP_SUCCESS;
  505. /* changelog must be open before it can be deleted */
  506. if (cl5GetState () != CL5_STATE_OPEN)
  507. {
  508. *returncode = 1;
  509. if (returntext)
  510. {
  511. PL_strncpyz(returntext, "changelog is not configured", SLAPI_DSE_RETURNTEXT_SIZE);
  512. }
  513. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  514. "changelog5_config_delete: chagelog is not configured\n");
  515. return SLAPI_DSE_CALLBACK_ERROR;
  516. }
  517. PR_RWLock_Wlock (s_configLock);
  518. /* changelog must be open before it can be deleted */
  519. if (cl5GetState () != CL5_STATE_OPEN)
  520. {
  521. *returncode = 1;
  522. if (returntext)
  523. {
  524. PL_strncpyz(returntext, "changelog is not configured", SLAPI_DSE_RETURNTEXT_SIZE);
  525. }
  526. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  527. "changelog5_config_delete: chagelog is not configured\n");
  528. goto done;
  529. }
  530. currentDir = cl5GetDir ();
  531. if (currentDir == NULL)
  532. {
  533. /* something is wrong: we should never be here */
  534. *returncode = 1;
  535. if (returntext)
  536. {
  537. PL_strncpyz (returntext, "internal failure", SLAPI_DSE_RETURNTEXT_SIZE);
  538. }
  539. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  540. "changelog5_config_delete: NULL directory\n");
  541. goto done;
  542. }
  543. /* this call will block until all threads using changelog
  544. release changelog by calling cl5RemoveThread () */
  545. rc = cl5Close ();
  546. if (rc != CL5_SUCCESS)
  547. {
  548. *returncode = 1;
  549. if (returntext)
  550. {
  551. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to close changelog; error - %d", rc);
  552. }
  553. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  554. "changelog5_config_delete: failed to close changelog\n");
  555. goto done;
  556. }
  557. rc = cl5Delete (currentDir);
  558. if (rc != CL5_SUCCESS)
  559. {
  560. *returncode = 1;
  561. if (returntext)
  562. {
  563. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to remove changelog; error - %d", rc);
  564. }
  565. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  566. "changelog5_config_delete: failed to remove changelog\n");
  567. goto done;
  568. }
  569. done:;
  570. PR_RWLock_Unlock (s_configLock);
  571. /* slapi_ch_free accepts NULL pointer */
  572. slapi_ch_free ((void**)&currentDir);
  573. if (*returncode == LDAP_SUCCESS)
  574. {
  575. if (returntext)
  576. {
  577. returntext[0] = '\0';
  578. }
  579. return SLAPI_DSE_CALLBACK_OK;
  580. }
  581. return SLAPI_DSE_CALLBACK_ERROR;
  582. }
  583. static int dont_allow_that(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  584. int *returncode, char *returntext, void *arg)
  585. {
  586. *returncode = LDAP_UNWILLING_TO_PERFORM;
  587. return SLAPI_DSE_CALLBACK_ERROR;
  588. }
  589. static changelog5Config * changelog5_dup_config(changelog5Config *config)
  590. {
  591. changelog5Config *dup = (changelog5Config *) slapi_ch_calloc(1, sizeof(changelog5Config));
  592. if (config->dir)
  593. dup->dir = slapi_ch_strdup(config->dir);
  594. if (config->maxAge)
  595. dup->maxAge = slapi_ch_strdup(config->maxAge);
  596. dup->maxEntries = config->maxEntries;
  597. /*memcpy((void *) &dup->dbconfig, (const void *) &config->dbconfig, sizeof(CL5DBConfig));*/
  598. dup->dbconfig.cacheSize = config->dbconfig.cacheSize;
  599. dup->dbconfig.durableTrans = config->dbconfig.durableTrans;
  600. dup->dbconfig.checkpointInterval = config->dbconfig.checkpointInterval;
  601. dup->dbconfig.circularLogging = config->dbconfig.circularLogging;
  602. dup->dbconfig.pageSize = config->dbconfig.pageSize;
  603. dup->dbconfig.logfileSize = config->dbconfig.logfileSize;
  604. dup->dbconfig.maxTxnSize = config->dbconfig.maxTxnSize;
  605. dup->dbconfig.fileMode = config->dbconfig.fileMode;
  606. dup->dbconfig.verbose = config->dbconfig.verbose;
  607. dup->dbconfig.debug = config->dbconfig.debug;
  608. dup->dbconfig.tricklePercentage = config->dbconfig.tricklePercentage;
  609. dup->dbconfig.spinCount = config->dbconfig.spinCount;
  610. dup->dbconfig.maxChCacheEntries = config->dbconfig.maxChCacheEntries;
  611. dup->dbconfig.maxChCacheSize = config->dbconfig.maxChCacheSize;
  612. dup->dbconfig.nb_lock_config = config->dbconfig.nb_lock_config;
  613. return dup;
  614. }
  615. /*
  616. * Given the changelog configuration entry, extract the configuration directives.
  617. */
  618. static void changelog5_extract_config(Slapi_Entry* entry, changelog5Config *config)
  619. {
  620. char *arg;
  621. memset (config, 0, sizeof (*config));
  622. config->dir = slapi_entry_attr_get_charptr(entry,CONFIG_CHANGELOG_DIR_ATTRIBUTE);
  623. replace_bslash (config->dir);
  624. arg= slapi_entry_attr_get_charptr(entry,CONFIG_CHANGELOG_MAXENTRIES_ATTRIBUTE);
  625. if (arg)
  626. {
  627. config->maxEntries = atoi (arg);
  628. slapi_ch_free_string(&arg);
  629. }
  630. config->maxAge = slapi_entry_attr_get_charptr(entry,CONFIG_CHANGELOG_MAXAGE_ATTRIBUTE);
  631. /*
  632. * Read the Changelog Internal Configuration Parameters for the Changelog DB
  633. * (db cache size, db settings...)
  634. */
  635. /* Set configuration default values first... */
  636. config->dbconfig.cacheSize = CL5_DEFAULT_CONFIG_DB_DBCACHESIZE;
  637. config->dbconfig.durableTrans = CL5_DEFAULT_CONFIG_DB_DURABLE_TRANSACTIONS;
  638. config->dbconfig.checkpointInterval = CL5_DEFAULT_CONFIG_DB_CHECKPOINT_INTERVAL;
  639. config->dbconfig.circularLogging = CL5_DEFAULT_CONFIG_DB_CIRCULAR_LOGGING;
  640. config->dbconfig.pageSize = CL5_DEFAULT_CONFIG_DB_PAGE_SIZE;
  641. config->dbconfig.logfileSize = CL5_DEFAULT_CONFIG_DB_LOGFILE_SIZE;
  642. config->dbconfig.maxTxnSize = CL5_DEFAULT_CONFIG_DB_TXN_MAX;
  643. config->dbconfig.verbose = CL5_DEFAULT_CONFIG_DB_VERBOSE;
  644. config->dbconfig.debug = CL5_DEFAULT_CONFIG_DB_DEBUG;
  645. config->dbconfig.tricklePercentage = CL5_DEFAULT_CONFIG_DB_TRICKLE_PERCENTAGE;
  646. config->dbconfig.spinCount = CL5_DEFAULT_CONFIG_DB_SPINCOUNT;
  647. config->dbconfig.nb_lock_config = CL5_DEFAULT_CONFIG_NB_LOCK;
  648. /* Now read from the entry to override default values if needed */
  649. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_DBCACHESIZE);
  650. if (arg)
  651. {
  652. size_t theSize = atoi (arg);
  653. if (theSize > CL5_MIN_DB_DBCACHESIZE)
  654. config->dbconfig.cacheSize = theSize;
  655. else {
  656. config->dbconfig.cacheSize = CL5_MIN_DB_DBCACHESIZE;
  657. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  658. "Warning: Changelog dbcache size too small. "
  659. "Increasing the Memory Size to %d bytes\n",
  660. CL5_MIN_DB_DBCACHESIZE);
  661. }
  662. slapi_ch_free_string(&arg);
  663. }
  664. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_DURABLE_TRANSACTIONS);
  665. if (arg)
  666. {
  667. config->dbconfig.durableTrans = atoi (arg);
  668. slapi_ch_free_string(&arg);
  669. }
  670. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_CHECKPOINT_INTERVAL);
  671. if (arg)
  672. {
  673. config->dbconfig.checkpointInterval = atoi (arg);
  674. slapi_ch_free_string(&arg);
  675. }
  676. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_CIRCULAR_LOGGING);
  677. if (arg)
  678. {
  679. config->dbconfig.circularLogging = atoi (arg);
  680. slapi_ch_free_string(&arg);
  681. }
  682. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_PAGE_SIZE);
  683. if (arg)
  684. {
  685. config->dbconfig.pageSize = atoi (arg);
  686. slapi_ch_free_string(&arg);
  687. }
  688. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_LOGFILE_SIZE);
  689. if (arg)
  690. {
  691. config->dbconfig.logfileSize = atoi (arg);
  692. slapi_ch_free_string(&arg);
  693. }
  694. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_MAXTXN_SIZE);
  695. if (arg)
  696. {
  697. config->dbconfig.maxTxnSize = atoi (arg);
  698. slapi_ch_free_string(&arg);
  699. }
  700. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_VERBOSE);
  701. if (arg)
  702. {
  703. config->dbconfig.verbose = atoi (arg);
  704. slapi_ch_free_string(&arg);
  705. }
  706. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_DEBUG);
  707. if (arg)
  708. {
  709. config->dbconfig.debug = atoi (arg);
  710. slapi_ch_free_string(&arg);
  711. }
  712. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_TRICKLE_PERCENTAGE);
  713. if (arg)
  714. {
  715. config->dbconfig.tricklePercentage = atoi (arg);
  716. slapi_ch_free_string(&arg);
  717. }
  718. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_SPINCOUNT);
  719. if (arg)
  720. {
  721. config->dbconfig.spinCount = atoi (arg);
  722. slapi_ch_free_string(&arg);
  723. }
  724. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_MAX_CONCURRENT_WRITES);
  725. if (arg)
  726. {
  727. config->dbconfig.maxConcurrentWrites = atoi (arg);
  728. slapi_ch_free_string(&arg);
  729. }
  730. if ( config->dbconfig.maxConcurrentWrites <= 0 )
  731. {
  732. config->dbconfig.maxConcurrentWrites = CL5_DEFAULT_CONFIG_MAX_CONCURRENT_WRITES;
  733. }
  734. /*
  735. * Read the Changelog Internal Configuration Parameters for the Changelog Cache
  736. */
  737. /* Set configuration default values first... */
  738. config->dbconfig.maxChCacheEntries = CL5_DEFAULT_CONFIG_CACHESIZE;
  739. config->dbconfig.maxChCacheSize = CL5_DEFAULT_CONFIG_CACHEMEMSIZE;
  740. /* Now read from the entry to override default values if needed */
  741. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_CACHESIZE);
  742. if (arg)
  743. {
  744. config->dbconfig.maxChCacheEntries = atoi (arg);
  745. slapi_ch_free_string(&arg);
  746. }
  747. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_CACHEMEMSIZE);
  748. if (arg)
  749. {
  750. config->dbconfig.maxChCacheSize = atoi (arg);
  751. slapi_ch_free_string(&arg);
  752. }
  753. arg = slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_NB_LOCK);
  754. if (arg)
  755. {
  756. size_t theSize = atoi(arg);
  757. if (theSize < CL5_MIN_NB_LOCK)
  758. {
  759. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  760. "Warning: Changelog %s value is too low (%d). Set to minimal value instead (%d)\n",
  761. CONFIG_CHANGELOG_NB_LOCK, theSize, CL5_MIN_NB_LOCK);
  762. config->dbconfig.nb_lock_config = CL5_MIN_NB_LOCK;
  763. }
  764. else
  765. {
  766. config->dbconfig.nb_lock_config = theSize;
  767. }
  768. slapi_ch_free_string(&arg);
  769. }
  770. clcache_set_config(&config->dbconfig);
  771. }
  772. static void replace_bslash (char *dir)
  773. {
  774. char *bslash;
  775. if (dir == NULL)
  776. return;
  777. bslash = strchr (dir, '\\');
  778. while (bslash)
  779. {
  780. *bslash = '/';
  781. bslash = strchr (bslash, '\\');
  782. }
  783. }
  784. static int notify_replica (Replica *r, void *arg)
  785. {
  786. return replica_log_ruv_elements (r);
  787. }
  788. static int _is_absolutepath (char * dir)
  789. {
  790. if (dir[0] == '/')
  791. return 1;
  792. #if defined(_WIN32)
  793. if (dir[2] == '/' && dir[1] == ':')
  794. return 1;
  795. #endif
  796. return 0;
  797. }