cl5_config.c 28 KB

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