1
0

legacy_consumer.c 22 KB

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