legacy_consumer.c 20 KB

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