legacy_consumer.c 22 KB

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