legacy_consumer.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /*
  13. * repl_legacy_consumer.c - support for legacy replication (consumer-side)
  14. *
  15. * Support for legacy replication involves correctly dealing with
  16. * the addition and removal of attribute types "copiedFrom" and
  17. * "copyingFrom". The behavior is:
  18. * 1) If a copiedFrom appears in an entry, and that entry is the root
  19. * of a replicated area, then put the backend into "refer on update"
  20. * mode and install a referral corresponding to the URL contained
  21. * in the copiedFrom attribute. This referral overrides the mode
  22. * of the replica, e.g. if it was previously an updateable replica,
  23. * it now becomes read-only except for the updatedn.
  24. * 2) If a copiedFrom disappears from an entry, or the entry containing
  25. * the copiedFrom is removed, restore the backend to the state
  26. * determined by the DS 5.0 replica configuration.
  27. * 3) If a "copyingFrom" referral appears in an entry, and that entry
  28. * is the root of a replicated area, then put the backend into
  29. * "refer all operations" mode and install a referral corresponding
  30. * to the URL contained in the copyingFrom attribute. This referral
  31. * overrides the mode of the replica, e.g if it was previously an
  32. * updateable replica, it now becomes read-only and refers all
  33. * operations except for the updatedn.
  34. * 4) If a copyingFrom disappears from an entry, or the entry containing
  35. * the copyingFrom is removed, restore the backend to the state
  36. * determined by the DS 5.0 replica configuration.
  37. */
  38. #include "repl5.h"
  39. #include "repl.h"
  40. /* Forward Declarations */
  41. static int legacy_consumer_config_add (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  42. static int legacy_consumer_config_modify (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  43. static int legacy_consumer_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  44. static int legacy_consumer_extract_config(Slapi_Entry* entry, char *returntext);
  45. static int legacy_consumer_read_config ();
  46. static void legacy_consumer_encode_pw (Slapi_Entry *e);
  47. static void set_legacy_purl (Slapi_PBlock *pb, const char *purl);
  48. static int get_legacy_referral (Slapi_Entry *e, char **referral, char **state);
  49. /* LEGACY_CONSUMER_CONFIG_DN is no need to be normalized. */
  50. #define LEGACY_CONSUMER_CONFIG_DN "cn=legacy consumer," REPL_CONFIG_TOP
  51. #define LEGACY_CONSUMER_FILTER "(objectclass=*)"
  52. /* Configuration parameters local to this module */
  53. static Slapi_DN *legacy_consumer_replicationdn = NULL;
  54. static char *legacy_consumer_replicationpw = NULL;
  55. /* Lock which protects the above config parameters */
  56. Slapi_RWLock *legacy_consumer_config_lock = NULL;
  57. static PRBool
  58. target_is_a_replica_root(Slapi_PBlock *pb, const Slapi_DN **root)
  59. {
  60. Slapi_DN *sdn = NULL;
  61. PRBool return_value = PR_FALSE;
  62. Object *repl_obj;
  63. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn);
  64. if (NULL == sdn) {
  65. return return_value;
  66. }
  67. repl_obj = replica_get_replica_from_dn(sdn);
  68. if (NULL != repl_obj)
  69. {
  70. Replica *r = object_get_data(repl_obj);
  71. *root = replica_get_root(r);
  72. return_value = PR_TRUE;
  73. object_release(repl_obj);
  74. }
  75. else
  76. {
  77. *root = NULL;
  78. return_value = PR_FALSE;
  79. }
  80. return return_value;
  81. }
  82. static int
  83. parse_cfstring(const char *cfstring, char **referral, char **generation, char **lastreplayed)
  84. {
  85. int return_value = -1;
  86. char *ref, *gen, *lastplayed;
  87. if (cfstring != NULL)
  88. {
  89. char *tmp;
  90. char *cfcopy = slapi_ch_strdup(cfstring);
  91. ref = cfcopy;
  92. tmp = strchr(cfcopy, ' ');
  93. if (NULL != tmp)
  94. {
  95. *tmp++ = '\0';
  96. while ('\0' != *tmp && ' ' == *tmp) tmp++;
  97. gen = tmp;
  98. tmp = strchr(gen, ' ');
  99. if (NULL != tmp)
  100. {
  101. *tmp++ = '\0';
  102. while ('\0' != *tmp && ' ' == *tmp) tmp++;
  103. lastplayed = tmp;
  104. return_value = 0;
  105. }
  106. }
  107. if (return_value == 0)
  108. {
  109. if (referral)
  110. *referral = slapi_ch_strdup(ref);
  111. if (generation)
  112. *generation = slapi_ch_strdup(gen);
  113. if (lastreplayed)
  114. *lastreplayed = slapi_ch_strdup(lastplayed);
  115. }
  116. slapi_ch_free((void **)&cfcopy);
  117. }
  118. return return_value;
  119. }
  120. /*
  121. * This is called from the consumer post-op plugin point.
  122. * It's called if:
  123. * 1) The operation is an add or modify operation, and a
  124. * copiedfrom/copyingfrom was found in the entry/mods, or
  125. * 2) the operation is a delete operation, or
  126. * 3) the operation is a moddn operation.
  127. */
  128. void
  129. process_legacy_cf(Slapi_PBlock *pb)
  130. {
  131. consumer_operation_extension *opext;
  132. Slapi_Operation *op;
  133. char *referral_array[2] = {0};
  134. char *referral;
  135. char *state;
  136. int rc;
  137. const Slapi_DN *replica_root_sdn = NULL;
  138. Slapi_Entry *e;
  139. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  140. opext = (consumer_operation_extension*) repl_con_get_ext (REPL_CON_EXT_OP, op);
  141. if (opext->has_cf)
  142. {
  143. PR_ASSERT (operation_get_type (op) == SLAPI_OPERATION_ADD ||
  144. operation_get_type (op) == SLAPI_OPERATION_MODIFY);
  145. if ((PR_FALSE == target_is_a_replica_root(pb, &replica_root_sdn)) ||
  146. (NULL == replica_root_sdn)){
  147. return;
  148. }
  149. slapi_pblock_get (pb, SLAPI_ENTRY_POST_OP, &e);
  150. PR_ASSERT (e);
  151. if (NULL == e)
  152. return;
  153. rc = get_legacy_referral (e, &referral, &state);
  154. if (rc == 0)
  155. {
  156. referral_array[0] = referral;
  157. referral_array[1] = NULL;
  158. repl_set_mtn_state_and_referrals(replica_root_sdn, state, NULL, NULL,
  159. referral_array);
  160. /* set partial url in the replica_object */
  161. set_legacy_purl (pb, referral);
  162. slapi_ch_free((void **)&referral);
  163. }
  164. }
  165. }
  166. void legacy_consumer_be_state_change (void *handle, char *be_name,
  167. int old_be_state, int new_be_state)
  168. {
  169. Object *r_obj;
  170. Replica *r;
  171. /* we only interested when a backend is coming online */
  172. if (new_be_state == SLAPI_BE_STATE_ON)
  173. {
  174. r_obj = replica_get_for_backend (be_name);
  175. if (r_obj)
  176. {
  177. r = (Replica*)object_get_data (r_obj);
  178. PR_ASSERT (r);
  179. if (replica_is_legacy_consumer (r))
  180. legacy_consumer_init_referrals (r);
  181. object_release (r_obj);
  182. }
  183. }
  184. }
  185. static int
  186. dont_allow_that(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e, int *returncode, char *returntext, void *arg)
  187. {
  188. *returncode = LDAP_UNWILLING_TO_PERFORM;
  189. return SLAPI_DSE_CALLBACK_ERROR;
  190. }
  191. int
  192. legacy_consumer_config_init()
  193. {
  194. /* The FE DSE *must* be initialised before we get here */
  195. int rc;
  196. if ((legacy_consumer_config_lock = slapi_new_rwlock()) == NULL) {
  197. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  198. "Failed to create legacy_consumer config read-write lock\n");
  199. exit(1);
  200. }
  201. rc = legacy_consumer_read_config ();
  202. if (rc != LDAP_SUCCESS)
  203. {
  204. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  205. "Failed to initialize legacy replication configuration\n");
  206. return 1;
  207. }
  208. slapi_config_register_callback(SLAPI_OPERATION_ADD,DSE_FLAG_PREOP,LEGACY_CONSUMER_CONFIG_DN,LDAP_SCOPE_SUBTREE,LEGACY_CONSUMER_FILTER,legacy_consumer_config_add,NULL);
  209. slapi_config_register_callback(SLAPI_OPERATION_MODIFY,DSE_FLAG_PREOP,LEGACY_CONSUMER_CONFIG_DN,LDAP_SCOPE_SUBTREE,LEGACY_CONSUMER_FILTER,legacy_consumer_config_modify,NULL);
  210. slapi_config_register_callback(SLAPI_OPERATION_MODRDN,DSE_FLAG_PREOP,LEGACY_CONSUMER_CONFIG_DN,LDAP_SCOPE_SUBTREE,LEGACY_CONSUMER_FILTER,dont_allow_that,NULL);
  211. slapi_config_register_callback(SLAPI_OPERATION_DELETE,DSE_FLAG_PREOP,LEGACY_CONSUMER_CONFIG_DN,LDAP_SCOPE_SUBTREE,LEGACY_CONSUMER_FILTER,legacy_consumer_config_delete,NULL);
  212. return 0;
  213. }
  214. static int
  215. legacy_consumer_config_add (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg)
  216. {
  217. int rc;
  218. rc = legacy_consumer_extract_config(e, returntext);
  219. if (rc != LDAP_SUCCESS)
  220. {
  221. *returncode = rc;
  222. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Failed to configure legacy replication\n");
  223. return SLAPI_DSE_CALLBACK_ERROR;
  224. }
  225. /* make sure that the password is encoded */
  226. legacy_consumer_encode_pw(e);
  227. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "legacy_consumer_config_add: "
  228. "successfully configured legacy consumer credentials\n");
  229. return SLAPI_DSE_CALLBACK_OK;
  230. }
  231. #define config_copy_strval( s ) s ? slapi_ch_strdup (s) : NULL;
  232. static int
  233. legacy_consumer_config_modify (Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e, int *returncode, char *returntext, void *arg)
  234. {
  235. int rc= 0;
  236. LDAPMod **mods;
  237. int not_allowed = 0;
  238. int i;
  239. if (returntext)
  240. {
  241. returntext[0] = '\0';
  242. }
  243. *returncode = LDAP_SUCCESS;
  244. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  245. slapi_rwlock_wrlock (legacy_consumer_config_lock);
  246. for (i = 0; mods && (mods[i] && (!not_allowed)); i++)
  247. {
  248. if (mods[i]->mod_op & LDAP_MOD_DELETE)
  249. {
  250. /* We don't support deleting an attribute from cn=config */
  251. }
  252. else
  253. {
  254. int j;
  255. for (j = 0; ((mods[i]->mod_values[j]) && (LDAP_SUCCESS == rc)); j++)
  256. {
  257. char *config_attr, *config_attr_value;
  258. int mod_type;
  259. config_attr = (char *) mods[i]->mod_type;
  260. config_attr_value = (char *) mods[i]->mod_bvalues[j]->bv_val;
  261. /* replace existing value */
  262. mod_type = mods[i]->mod_op & ~LDAP_MOD_BVALUES;
  263. if ( strcasecmp (config_attr, CONFIG_LEGACY_REPLICATIONDN_ATTRIBUTE ) == 0 )
  264. {
  265. if (legacy_consumer_replicationdn)
  266. slapi_sdn_free (&legacy_consumer_replicationdn);
  267. if (mod_type == LDAP_MOD_REPLACE)
  268. {
  269. if (config_attr_value)
  270. legacy_consumer_replicationdn = slapi_sdn_new_dn_byval (config_attr_value);
  271. }
  272. else if (mod_type == LDAP_MOD_DELETE)
  273. {
  274. legacy_consumer_replicationdn = NULL;
  275. }
  276. else if (mod_type == LDAP_MOD_ADD)
  277. {
  278. if (legacy_consumer_replicationdn != NULL)
  279. {
  280. not_allowed = 1;
  281. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name,
  282. "Multiple replicationdns not permitted." );
  283. }
  284. else
  285. {
  286. if (config_attr_value)
  287. legacy_consumer_replicationdn = slapi_sdn_new_dn_byval (config_attr_value);
  288. }
  289. }
  290. }
  291. else if ( strcasecmp ( config_attr, CONFIG_LEGACY_REPLICATIONPW_ATTRIBUTE ) == 0 )
  292. {
  293. if (mod_type == LDAP_MOD_REPLACE)
  294. {
  295. slapi_ch_free_string(&legacy_consumer_replicationpw);
  296. legacy_consumer_replicationpw = config_copy_strval(config_attr_value);
  297. }
  298. else if (mod_type == LDAP_MOD_DELETE)
  299. {
  300. slapi_ch_free_string(&legacy_consumer_replicationpw);
  301. }
  302. else if (mod_type == LDAP_MOD_ADD)
  303. {
  304. if (legacy_consumer_replicationpw != NULL)
  305. {
  306. not_allowed = 1;
  307. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name,
  308. "Multiple replicationpws not permitted." );
  309. }
  310. else
  311. {
  312. slapi_ch_free_string(&legacy_consumer_replicationpw);
  313. legacy_consumer_replicationpw = config_copy_strval(config_attr_value);
  314. }
  315. }
  316. }
  317. }
  318. }
  319. }
  320. slapi_rwlock_unlock (legacy_consumer_config_lock);
  321. if (not_allowed)
  322. {
  323. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name,
  324. "Failed to modify legacy replication configuration\n" );
  325. *returncode= LDAP_CONSTRAINT_VIOLATION;
  326. return SLAPI_DSE_CALLBACK_ERROR;
  327. }
  328. /* make sure that the password is encoded */
  329. legacy_consumer_encode_pw (e);
  330. return SLAPI_DSE_CALLBACK_OK;
  331. }
  332. static int
  333. legacy_consumer_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg)
  334. {
  335. slapi_rwlock_wrlock (legacy_consumer_config_lock);
  336. if (legacy_consumer_replicationdn)
  337. slapi_sdn_free (&legacy_consumer_replicationdn);
  338. slapi_ch_free_string(&legacy_consumer_replicationpw);
  339. legacy_consumer_replicationdn = NULL;
  340. slapi_rwlock_unlock (legacy_consumer_config_lock);
  341. *returncode = LDAP_SUCCESS;
  342. return SLAPI_DSE_CALLBACK_OK;
  343. }
  344. /*
  345. * Given the changelog configuration entry, extract the configuration directives.
  346. */
  347. static int
  348. legacy_consumer_extract_config(Slapi_Entry* entry, char *returntext)
  349. {
  350. int rc = LDAP_SUCCESS; /* OK */
  351. char *arg;
  352. slapi_rwlock_wrlock (legacy_consumer_config_lock);
  353. arg= slapi_entry_attr_get_charptr(entry,CONFIG_LEGACY_REPLICATIONDN_ATTRIBUTE);
  354. if (arg)
  355. legacy_consumer_replicationdn = slapi_sdn_new_dn_passin (arg);
  356. arg= slapi_entry_attr_get_charptr(entry,CONFIG_LEGACY_REPLICATIONPW_ATTRIBUTE);
  357. slapi_ch_free_string(&legacy_consumer_replicationpw);
  358. legacy_consumer_replicationpw = arg;
  359. slapi_rwlock_unlock (legacy_consumer_config_lock);
  360. return rc;
  361. }
  362. static int
  363. legacy_consumer_read_config ()
  364. {
  365. int rc = LDAP_SUCCESS;
  366. int scope= LDAP_SCOPE_BASE;
  367. Slapi_PBlock *pb;
  368. pb = slapi_pblock_new ();
  369. slapi_search_internal_set_pb (pb, LEGACY_CONSUMER_CONFIG_DN, scope,
  370. "(objectclass=*)", NULL /*attrs*/, 0 /* attrs only */,
  371. NULL /* controls */, NULL /* uniqueid */,
  372. repl_get_plugin_identity(PLUGIN_LEGACY_REPLICATION), 0 /* actions */);
  373. slapi_search_internal_pb (pb);
  374. slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_RESULT, &rc );
  375. if ( LDAP_SUCCESS == rc )
  376. {
  377. Slapi_Entry **entries = NULL;
  378. slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
  379. if ( NULL != entries && NULL != entries[0])
  380. {
  381. /* Extract the config info from the changelog entry */
  382. rc = legacy_consumer_extract_config(entries[0], NULL);
  383. }
  384. }
  385. else
  386. {
  387. rc = LDAP_SUCCESS;
  388. }
  389. slapi_free_search_results_internal(pb);
  390. slapi_pblock_destroy(pb);
  391. return rc;
  392. }
  393. int
  394. legacy_consumer_is_replicationdn(const char *dn)
  395. {
  396. int return_value = 0; /* Assume not */
  397. if (NULL != dn && '\0' != dn[0])
  398. {
  399. if (NULL != legacy_consumer_replicationdn)
  400. {
  401. Slapi_DN *sdn = slapi_sdn_new_dn_byref (dn);
  402. if (slapi_sdn_compare (legacy_consumer_replicationdn, sdn) == 0) {
  403. return_value = 1;
  404. }
  405. slapi_sdn_free (&sdn);
  406. }
  407. }
  408. return return_value;
  409. }
  410. int
  411. legacy_consumer_is_replicationpw(struct berval *pwval)
  412. {
  413. int return_value = 0; /* Assume not */
  414. if (NULL != pwval && NULL != pwval->bv_val)
  415. {
  416. if (NULL != legacy_consumer_replicationpw &&
  417. '\0' != legacy_consumer_replicationpw[0]) {
  418. struct berval *pwvals[2];
  419. struct berval config_pw;
  420. config_pw.bv_val = legacy_consumer_replicationpw;
  421. config_pw.bv_len = strlen(legacy_consumer_replicationpw);
  422. pwvals[0] = &config_pw;
  423. pwvals[1] = NULL;
  424. return_value = slapi_pw_find(pwvals, pwval) == 0;
  425. }
  426. }
  427. return return_value;
  428. }
  429. static void
  430. legacy_consumer_encode_pw (Slapi_Entry *e)
  431. {
  432. char *updatepw = slapi_entry_attr_get_charptr(e,
  433. CONFIG_LEGACY_REPLICATIONPW_ATTRIBUTE);
  434. int is_encoded;
  435. char *encoded_value = NULL;
  436. if (updatepw != NULL)
  437. {
  438. is_encoded = slapi_is_encoded (updatepw);
  439. if (!is_encoded)
  440. {
  441. encoded_value = slapi_encode (updatepw, "SHA");
  442. }
  443. if (encoded_value)
  444. {
  445. slapi_entry_attr_set_charptr(e,
  446. CONFIG_LEGACY_REPLICATIONPW_ATTRIBUTE, encoded_value);
  447. }
  448. }
  449. }
  450. static void
  451. set_legacy_purl (Slapi_PBlock *pb, const char *purl)
  452. {
  453. Object *r_obj;
  454. Replica *r;
  455. r_obj = replica_get_replica_for_op (pb);
  456. PR_ASSERT (r_obj);
  457. r = (Replica*)object_get_data (r_obj);
  458. PR_ASSERT (r && replica_is_legacy_consumer(r));
  459. replica_set_legacy_purl (r, purl);
  460. object_release (r_obj);
  461. }
  462. /* this function get referrals from an entry.
  463. Returns 0 if successful
  464. 1 if no referrals are present
  465. -1 in case of error
  466. */
  467. static int
  468. get_legacy_referral (Slapi_Entry *e, char **referral, char **state)
  469. {
  470. char* pat = "ldap://%s";
  471. const char *val = NULL;
  472. char *hostport;
  473. int rc = 1;
  474. Slapi_Attr *attr;
  475. const Slapi_Value *sval;
  476. PR_ASSERT (e && referral && state);
  477. /* Find any copiedFrom/copyingFrom attributes -
  478. copyingFrom has priority */
  479. if (slapi_entry_attr_find(e, type_copyingFrom, &attr) == 0)
  480. {
  481. slapi_attr_first_value(attr, (Slapi_Value **)&sval);
  482. val = slapi_value_get_string(sval);
  483. *state = STATE_REFERRAL;
  484. }
  485. else if (slapi_entry_attr_find(e, type_copiedFrom, &attr) == 0)
  486. {
  487. slapi_attr_first_value(attr, (Slapi_Value **)&sval);
  488. val = slapi_value_get_string(sval);
  489. *state = STATE_UPDATE_REFERRAL;
  490. }
  491. if (val)
  492. {
  493. rc = parse_cfstring(val, &hostport, NULL, NULL);
  494. if (rc != 0)
  495. {
  496. const char *target_dn = slapi_entry_get_dn_const(e);
  497. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Warning: a copiedFrom "
  498. "or copyingFrom attribute was added to or removed from an "
  499. "entry that is not the root of a replicated area. It is possible "
  500. "that a legacy replication supplier is incorrectly configured "
  501. "to supply updates to the subtree rooted at %s\n",
  502. target_dn == NULL ? "null" : target_dn);
  503. }
  504. else
  505. {
  506. *referral = slapi_ch_smprintf (pat, hostport);
  507. slapi_ch_free ((void**)&hostport);
  508. }
  509. }
  510. else
  511. {
  512. rc = 1; /* no copiedFrom or copyingFrom int the entry */
  513. }
  514. return rc;
  515. }
  516. /* this function is called during server startup or when replica's data
  517. is reloaded. It sets up referrals in the mapping tree based on the
  518. copiedFrom and copyingFrom attributes. It also sets up partial url in
  519. the replica object used to update RUV.
  520. Returns 0 if successful and -1 otherwise
  521. */
  522. int
  523. legacy_consumer_init_referrals (Replica *r)
  524. {
  525. Slapi_PBlock *pb;
  526. const Slapi_DN *root_sdn;
  527. const char *root_dn;
  528. char *attrs[] = {"copiedFrom", "copyingFrom"};
  529. int rc;
  530. Slapi_Entry **entries = NULL;
  531. char *referral = NULL;
  532. char *referral_array[2];
  533. char *state = NULL;
  534. PR_ASSERT (r);
  535. pb = slapi_pblock_new ();
  536. PR_ASSERT (pb);
  537. root_sdn = replica_get_root(r);
  538. PR_ASSERT (root_sdn);
  539. root_dn = slapi_sdn_get_ndn(root_sdn);
  540. PR_ASSERT (root_dn);
  541. slapi_search_internal_set_pb (pb, root_dn, LDAP_SCOPE_BASE, "objectclass=*",attrs,
  542. 0 /* attrsonly */, NULL /* controls */,
  543. NULL /* uniqueid */,
  544. repl_get_plugin_identity (PLUGIN_LEGACY_REPLICATION),
  545. 0 /* flags */);
  546. slapi_search_internal_pb (pb);
  547. slapi_pblock_get (pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  548. if (rc != LDAP_SUCCESS)
  549. {
  550. if (rc == LDAP_REFERRAL)
  551. {
  552. /* We are in referral mode, probably because ORC failed */
  553. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "legacy_consumer_init_referrals "
  554. "data for replica %s is in referral mode due to failed "
  555. "initialization. Replica need to be reinitialized\n",
  556. root_dn);
  557. rc = 0;
  558. }
  559. else
  560. {
  561. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "legacy_consumer_init_referrals "
  562. "failed to obtain root entry for replica %s; LDAP error - %d\n",
  563. root_dn, rc);
  564. rc = -1;
  565. }
  566. goto done;
  567. }
  568. slapi_pblock_get (pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  569. PR_ASSERT (entries && entries[0]);
  570. rc = get_legacy_referral (entries[0], &referral, &state);
  571. if (rc == 0)
  572. {
  573. referral_array[0] = referral;
  574. referral_array[1] = NULL;
  575. repl_set_mtn_state_and_referrals(root_sdn, state, NULL, NULL, referral_array);
  576. /* set purtial url in the replica_object */
  577. replica_set_legacy_purl (r, referral);
  578. slapi_ch_free((void **)&referral);
  579. }
  580. else if (rc == 1) /* no referrals - treat as success */
  581. {
  582. rc = 0;
  583. }
  584. slapi_free_search_results_internal (pb);
  585. done:
  586. slapi_pblock_destroy (pb);
  587. return rc;
  588. }