sync_refresh.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2013 Red Hat, Inc.
  3. * All rights reserved.
  4. *
  5. * License: GPL (version 3 or any later version).
  6. * See LICENSE for details.
  7. * END COPYRIGHT BLOCK **/
  8. #include "sync.h"
  9. static SyncOpInfo *new_SyncOpInfo(int flag, PRThread *tid, Sync_Cookie *cookie);
  10. static int sync_extension_type;
  11. static int sync_extension_handle;
  12. static PRBool allow_openldap_compat;
  13. static SyncOpInfo *sync_get_operation_extension(Slapi_PBlock *pb);
  14. static void sync_set_operation_extension(Slapi_PBlock *pb, SyncOpInfo *spec);
  15. static int sync_find_ref_by_uuid(Sync_UpdateNode *updates, int stop, char *uniqueid);
  16. static void sync_free_update_nodes(Sync_UpdateNode **updates, int count);
  17. Slapi_Entry *sync_deleted_entry_from_changelog(Slapi_Entry *cl_entry);
  18. static int sync_feature_allowed(Slapi_PBlock *pb);
  19. static int
  20. sync_feature_allowed(Slapi_PBlock *pb)
  21. {
  22. int isroot = 0;
  23. int ldapcode = LDAP_SUCCESS;
  24. slapi_pblock_get(pb, SLAPI_REQUESTOR_ISROOT, &isroot);
  25. if (!isroot) {
  26. char *dn;
  27. Slapi_Entry *feature = NULL;
  28. /* Fetch the feature entry and see if the requestor is allowed access. */
  29. dn = slapi_ch_smprintf("dn: oid=%s,cn=features,cn=config", LDAP_CONTROL_SYNC);
  30. if ((feature = slapi_str2entry(dn, 0)) != NULL) {
  31. char *dummy_attr = "1.1";
  32. ldapcode = slapi_access_allowed(pb, feature, dummy_attr, NULL, SLAPI_ACL_READ);
  33. }
  34. /* If the feature entry does not exist, deny use of the control. Only
  35. * the root DN will be allowed to use the control in this case. */
  36. if ((feature == NULL) || (ldapcode != LDAP_SUCCESS)) {
  37. ldapcode = LDAP_INSUFFICIENT_ACCESS;
  38. }
  39. slapi_ch_free((void **)&dn);
  40. slapi_entry_free(feature);
  41. }
  42. return (ldapcode);
  43. }
  44. int
  45. sync_srch_refresh_pre_search(Slapi_PBlock *pb)
  46. {
  47. LDAPControl **requestcontrols;
  48. struct berval *psbvp;
  49. Sync_Cookie *client_cookie = NULL;
  50. Sync_Cookie *session_cookie = NULL;
  51. int rc = 0;
  52. int sync_persist = 0;
  53. PRThread *tid = NULL;
  54. int entries_sent = 0;
  55. slapi_pblock_get(pb, SLAPI_REQCONTROLS, &requestcontrols);
  56. if (slapi_control_present(requestcontrols, LDAP_CONTROL_SYNC, &psbvp, NULL)) {
  57. char *cookie = NULL;
  58. int32_t mode = 1;
  59. int32_t refresh = 0;
  60. PRBool cookie_refresh = PR_FALSE;
  61. if (sync_parse_control_value(psbvp, &mode,
  62. &refresh, &cookie) != LDAP_SUCCESS) {
  63. rc = 1;
  64. goto error_return;
  65. } else {
  66. /* control is valid, check if usere is allowed to perform sync searches */
  67. rc = sync_feature_allowed(pb);
  68. if (rc) {
  69. sync_result_err(pb, rc, NULL);
  70. goto error_return;
  71. }
  72. }
  73. if (mode == 1 || mode == 3) {
  74. /*
  75. * OpenLDAP violates rfc4533 by sending a "rid=" in it's initial cookie sync, even
  76. * when using their changelog mode. As a result, we parse the cookie to handle this
  77. * shenangians to determine if this is valid.
  78. */
  79. client_cookie = sync_cookie_parse(cookie, &cookie_refresh, &allow_openldap_compat);
  80. /*
  81. * we need to return a cookie in the result message
  82. * indicating a state to be used in future sessions
  83. * as starting point - create it now. We need to provide
  84. * the client_cookie so we understand if we are in
  85. * openldap mode or not, and to get the 'rid' of the
  86. * consumer.
  87. */
  88. session_cookie = sync_cookie_create(pb, client_cookie);
  89. if (session_cookie == NULL) {
  90. /* In some rare case access to the retroCL fails.
  91. * It can happen when retroCL is just created and
  92. * does not contain any record.
  93. * As we are not able to retrieve the last changenumber
  94. * just return a failure.
  95. * Another option would be to set cookie_change_info=0
  96. * if we can not retrieve any record in retroCL
  97. * (in sync_cookie_create)
  98. */
  99. slapi_log_err(SLAPI_LOG_ERR, SYNC_PLUGIN_SUBSYSTEM,
  100. "sync_srch_refresh_pre_search - fails to create a session cookie\n");
  101. rc = LDAP_OPERATIONS_ERROR;
  102. sync_result_err(pb, rc, "Unable to create a session cookie: last changelog record unreachable");
  103. goto error_return;
  104. }
  105. /*
  106. * if mode is persist we need to setup the persit handler
  107. * to catch the mods while the refresh is done
  108. */
  109. if (mode == 3) {
  110. if (client_cookie && client_cookie->openldap_compat == PR_TRUE) {
  111. /* We don't allow this. */
  112. rc = LDAP_UNWILLING_TO_PERFORM;
  113. sync_result_err(pb, rc, "Invalid session state, openldap compat not supported with persistence");
  114. goto error_return;
  115. }
  116. /* Launch the thread. */
  117. tid = sync_persist_add(pb);
  118. if (tid)
  119. sync_persist = 1;
  120. else {
  121. rc = LDAP_UNWILLING_TO_PERFORM;
  122. sync_result_err(pb, rc, "Too many active synchronization sessions");
  123. goto error_return;
  124. }
  125. }
  126. /*
  127. * now handle the refresh request
  128. * there are two scenarios
  129. * 1. no cookie is provided this means send all entries matching the search request
  130. * 2. a cookie is provided: send all entries changed since the cookie was issued
  131. * -- return an error if the cookie is invalid
  132. * -- return e-syncRefreshRequired if the data referenced in the cookie are no
  133. * longer in the history
  134. */
  135. if (!cookie_refresh) {
  136. if (sync_cookie_isvalid(client_cookie, session_cookie)) {
  137. rc = sync_refresh_update_content(pb, client_cookie, session_cookie);
  138. if (rc == 0) {
  139. entries_sent = 1;
  140. }
  141. if (sync_persist) {
  142. rc = sync_intermediate_msg(pb, LDAP_TAG_SYNC_REFRESH_DELETE, session_cookie, NULL);
  143. } else {
  144. rc = sync_result_msg(pb, session_cookie);
  145. }
  146. } else {
  147. rc = E_SYNC_REFRESH_REQUIRED;
  148. sync_result_err(pb, rc, "Invalid session cookie");
  149. }
  150. } else {
  151. rc = sync_refresh_initial_content(pb, sync_persist, tid, session_cookie);
  152. if (rc == 0 && !sync_persist) {
  153. /* maintained in postop code */
  154. session_cookie = NULL;
  155. }
  156. /* if persis it will be handed over to persist code */
  157. }
  158. if (rc) {
  159. if (sync_persist) {
  160. sync_persist_terminate(tid);
  161. }
  162. goto error_return;
  163. } else if (sync_persist) {
  164. Slapi_Operation *operation;
  165. slapi_pblock_get(pb, SLAPI_OPERATION, &operation);
  166. if (client_cookie) {
  167. rc = sync_persist_startup(tid, session_cookie);
  168. }
  169. if (rc == 0) {
  170. session_cookie = NULL; /* maintained in persist code */
  171. slapi_operation_set_flag(operation, OP_FLAG_SYNC_PERSIST);
  172. }
  173. }
  174. } else {
  175. /* unknown mode, return an error */
  176. rc = 1;
  177. }
  178. error_return:
  179. sync_cookie_free(&client_cookie);
  180. sync_cookie_free(&session_cookie);
  181. slapi_ch_free((void **)&cookie);
  182. }
  183. /* if we sent the entries
  184. * return "error" to abort normal search
  185. */
  186. if (entries_sent > 0) {
  187. return (1);
  188. } else {
  189. return (rc);
  190. }
  191. }
  192. int
  193. sync_srch_refresh_post_search(Slapi_PBlock *pb)
  194. {
  195. int rc = 0;
  196. SyncOpInfo *info = sync_get_operation_extension(pb);
  197. if (!info) {
  198. return (0); /* nothing to do */
  199. }
  200. if (info->send_flag & SYNC_FLAG_SEND_INTERMEDIATE) {
  201. rc = sync_intermediate_msg(pb, LDAP_TAG_SYNC_REFRESH_DELETE, info->cookie, NULL);
  202. /* the refresh phase is over, now the post op
  203. * plugins will create the state control
  204. * depending on the operation type, reset flag
  205. */
  206. info->send_flag &= ~SYNC_FLAG_ADD_STATE_CTRL;
  207. /* activate the persistent phase thread*/
  208. sync_persist_startup(info->tid, info->cookie);
  209. }
  210. if (info->send_flag & SYNC_FLAG_ADD_DONE_CTRL) {
  211. LDAPControl **ctrl = (LDAPControl **)slapi_ch_calloc(2, sizeof(LDAPControl *));
  212. char *cookiestr = sync_cookie2str(info->cookie);
  213. /*
  214. * RFC4533
  215. * If refreshDeletes of syncDoneValue is FALSE, the new copy includes
  216. * all changed entries returned by the reissued Sync Operation, as well
  217. * as all unchanged entries identified as being present by the reissued
  218. * Sync Operation, but whose content is provided by the previous Sync
  219. * Operation. The unchanged entries not identified as being present are
  220. * deleted from the client content. They had been either deleted,
  221. * moved, or otherwise scoped-out from the content.
  222. *
  223. * If refreshDeletes of syncDoneValue is TRUE, the new copy includes all
  224. * changed entries returned by the reissued Sync Operation, as well as
  225. * all other entries of the previous copy except for those that are
  226. * identified as having been deleted from the content.
  227. *
  228. * Confused yet? Don't worry so am I. I have no idea what this means or
  229. * what it will do. The best I can see from wireshark is that if refDel is
  230. * false, then anything *not* present will be purged from the change that
  231. * was supplied. Which probably says a lot about how confusing syncrepl is
  232. * that we've hardcoded this to false for literally years and no one has
  233. * complained, probably because every client is broken in their own ways
  234. * as no one can actually interpret that dense statement above.
  235. *
  236. * Point is, if we set refresh to true for openldap mode, it works, and if
  237. * it's false, the moment we send a single intermediate delete message, we
  238. * delete literally everything 🔥.
  239. *
  240. * See README.md for more about how this works.
  241. */
  242. if (info->cookie->openldap_compat) {
  243. sync_create_sync_done_control(&ctrl[0], 1, cookiestr);
  244. } else {
  245. sync_create_sync_done_control(&ctrl[0], 0, cookiestr);
  246. }
  247. slapi_pblock_set(pb, SLAPI_RESCONTROLS, ctrl);
  248. slapi_ch_free((void **)&cookiestr);
  249. }
  250. return (rc);
  251. }
  252. int
  253. sync_srch_refresh_pre_entry(Slapi_PBlock *pb)
  254. {
  255. int rc = 0;
  256. SyncOpInfo *info = sync_get_operation_extension(pb);
  257. if (!info) {
  258. rc = 0; /* nothing to do */
  259. } else if (info->send_flag & SYNC_FLAG_ADD_STATE_CTRL) {
  260. Slapi_Entry *e;
  261. PRBool openldap_compat = PR_FALSE;
  262. if (info->cookie) {
  263. openldap_compat = info->cookie->openldap_compat;
  264. }
  265. slapi_pblock_get(pb, SLAPI_SEARCH_RESULT_ENTRY, &e);
  266. LDAPControl **ctrl = (LDAPControl **)slapi_ch_calloc(2, sizeof(LDAPControl *));
  267. rc = sync_create_state_control(e, &ctrl[0], LDAP_SYNC_ADD, NULL, openldap_compat);
  268. slapi_pblock_set(pb, SLAPI_SEARCH_CTRLS, ctrl);
  269. }
  270. return (rc);
  271. }
  272. int
  273. sync_srch_refresh_pre_result(Slapi_PBlock *pb)
  274. {
  275. SyncOpInfo *info = sync_get_operation_extension(pb);
  276. if (!info) {
  277. return 0; /* nothing to do */
  278. }
  279. if (info->send_flag & SYNC_FLAG_NO_RESULT) {
  280. return (1);
  281. } else {
  282. return (0);
  283. }
  284. }
  285. static void
  286. sync_free_update_nodes(Sync_UpdateNode **updates, int count)
  287. {
  288. int i;
  289. for (i = 0; i < count; i++) {
  290. /* ch free checks for null for us. */
  291. slapi_ch_free((void **)&((*updates)[i].upd_uuid));
  292. slapi_ch_free((void **)&((*updates)[i].upd_euuid));
  293. if ((*updates)[i].upd_e) {
  294. slapi_entry_free((*updates)[i].upd_e);
  295. }
  296. }
  297. slapi_ch_free((void **)updates);
  298. }
  299. int
  300. sync_refresh_update_content(Slapi_PBlock *pb, Sync_Cookie *client_cookie, Sync_Cookie *server_cookie)
  301. {
  302. Slapi_PBlock *seq_pb;
  303. char *filter;
  304. Sync_CallBackData cb_data;
  305. int rc = LDAP_SUCCESS;
  306. PR_ASSERT(client_cookie);
  307. /*
  308. * We have nothing to send, move along.
  309. * Should be caught by cookie is valid though if the server < client, but if
  310. * they are equal, we return.
  311. */
  312. PR_ASSERT(server_cookie->cookie_change_info >= client_cookie->cookie_change_info);
  313. if (server_cookie->cookie_change_info == client_cookie->cookie_change_info) {
  314. return rc;
  315. }
  316. int chg_count = (server_cookie->cookie_change_info - client_cookie->cookie_change_info) + 1;
  317. PR_ASSERT(chg_count > 0);
  318. cb_data.cb_updates = (Sync_UpdateNode *)slapi_ch_calloc(chg_count, sizeof(Sync_UpdateNode));
  319. seq_pb = slapi_pblock_new();
  320. slapi_pblock_init(seq_pb);
  321. cb_data.orig_pb = pb;
  322. cb_data.change_start = client_cookie->cookie_change_info;
  323. cb_data.openldap_compat = server_cookie->openldap_compat;
  324. /*
  325. * The client has already seen up to AND including change_info, so this should
  326. * should reflect that. originally was:
  327. *
  328. * filter = slapi_ch_smprintf("(&(changenumber>=%lu)(changenumber<=%lu))",
  329. * client_cookie->cookie_change_info,
  330. * server_cookie->cookie_change_info);
  331. *
  332. * which would create a situation where if the previous cn was say 5, and the next
  333. * is 6, we'd get both 5 and 6, even though the client has already seen 5. But worse
  334. * if 5 was an "add" of the entry, and 6 was a "delete" of the same entry then sync
  335. * would over-optimise and remove the sync value because it things the add/delete was
  336. * in the same operation so we'd never send it. But the client HAD seen the add, and
  337. * now we'd never send the delete so this would be a bug. This created some confusion
  338. * for me in the tests, but the sync repl tests now correctly work and reflect the behaviour
  339. * expected.
  340. */
  341. if (server_cookie->openldap_compat) {
  342. /* In openldap compat we only want items that have an entryuuid, else we can't sync them */
  343. filter = slapi_ch_smprintf("(&(changenumber>=%lu)(changenumber<=%lu)(" CL_ATTR_ENTRYUUID "=*))",
  344. client_cookie->cookie_change_info + 1,
  345. server_cookie->cookie_change_info);
  346. } else {
  347. filter = slapi_ch_smprintf("(&(changenumber>=%lu)(changenumber<=%lu))",
  348. client_cookie->cookie_change_info + 1,
  349. server_cookie->cookie_change_info);
  350. }
  351. slapi_search_internal_set_pb(
  352. seq_pb,
  353. CL_SRCH_BASE,
  354. LDAP_SCOPE_ONE,
  355. filter,
  356. NULL,
  357. 0,
  358. NULL, NULL,
  359. plugin_get_default_component_id(),
  360. 0);
  361. rc = slapi_search_internal_callback_pb(
  362. seq_pb, &cb_data, NULL, sync_read_entry_from_changelog, NULL);
  363. slapi_pblock_destroy(seq_pb);
  364. /* Now send the deleted entries in a sync info message
  365. * and the modified entries as single entries
  366. */
  367. sync_send_deleted_entries(pb, cb_data.cb_updates, chg_count, server_cookie);
  368. sync_send_modified_entries(pb, cb_data.cb_updates, chg_count, server_cookie);
  369. sync_free_update_nodes(&cb_data.cb_updates, chg_count);
  370. slapi_ch_free((void **)&filter);
  371. return (rc);
  372. }
  373. int
  374. sync_refresh_initial_content(Slapi_PBlock *pb, int sync_persist, PRThread *tid, Sync_Cookie *sc)
  375. {
  376. /* the entries will be sent in the normal search process, but
  377. * - a control has to be sent with each entry
  378. * if sync persist:
  379. * - an intermediate response has to be sent
  380. * - no result message must be sent
  381. *
  382. * else
  383. * - a result message with a sync done control has to be sent
  384. *
  385. * setup on operation extension to take care of in
  386. * pre_entry, pre_result and post_search plugins
  387. */
  388. SyncOpInfo *info;
  389. if (sc->openldap_compat == PR_TRUE) {
  390. /*
  391. * If this is true we need to adjust the filter to
  392. * include a wrapping entryuuid condition. This is
  393. * because openldap demands entryuuid == syncuuid so
  394. * we must only send entries with an entryuuid.
  395. */
  396. struct slapi_filter *filter = NULL;
  397. slapi_pblock_get(pb, SLAPI_SEARCH_FILTER, (void *)&filter);
  398. PR_ASSERT(filter);
  399. /* We need to alloc this due to how str2filter manips the str. If it's
  400. * static we cause a segfault because it's in a protected section.
  401. */
  402. char *buf = slapi_ch_strdup("(entryUUID=*)");
  403. struct slapi_filter *euuid_filter = slapi_str2filter(buf);
  404. PR_ASSERT(euuid_filter);
  405. struct slapi_filter *wrapped_filter = slapi_filter_join(LDAP_FILTER_AND, filter, euuid_filter);
  406. PR_ASSERT(wrapped_filter);
  407. slapi_pblock_set(pb, SLAPI_SEARCH_FILTER, (void *)wrapped_filter);
  408. slapi_ch_free_string(&buf);
  409. }
  410. if (sync_persist) {
  411. info = new_SyncOpInfo(SYNC_FLAG_ADD_STATE_CTRL |
  412. SYNC_FLAG_SEND_INTERMEDIATE |
  413. SYNC_FLAG_NO_RESULT,
  414. tid,
  415. sc);
  416. } else {
  417. info = new_SyncOpInfo(SYNC_FLAG_ADD_STATE_CTRL |
  418. SYNC_FLAG_ADD_DONE_CTRL,
  419. tid,
  420. sc);
  421. }
  422. sync_set_operation_extension(pb, info);
  423. return (0);
  424. }
  425. static int
  426. sync_str2chgreq(char *chgtype)
  427. {
  428. if (chgtype == NULL) {
  429. return (-1);
  430. }
  431. if (strcasecmp(chgtype, "add") == 0) {
  432. return (LDAP_REQ_ADD);
  433. } else if (strcasecmp(chgtype, "modify") == 0) {
  434. return (LDAP_REQ_MODIFY);
  435. } else if (strcasecmp(chgtype, "modrdn") == 0) {
  436. return (LDAP_REQ_MODRDN);
  437. } else if (strcasecmp(chgtype, "delete") == 0) {
  438. return (LDAP_REQ_DELETE);
  439. } else {
  440. return (-1);
  441. }
  442. }
  443. static char *
  444. sync_get_attr_value_from_entry(Slapi_Entry *cl_entry, char *attrtype)
  445. {
  446. Slapi_Value *sval = NULL;
  447. const struct berval *value;
  448. char *strvalue = NULL;
  449. if (NULL != cl_entry) {
  450. Slapi_Attr *chattr = NULL;
  451. sval = NULL;
  452. value = NULL;
  453. if (slapi_entry_attr_find(cl_entry, attrtype, &chattr) == 0) {
  454. slapi_attr_first_value(chattr, &sval);
  455. if (NULL != sval) {
  456. value = slapi_value_get_berval(sval);
  457. if (NULL != value && NULL != value->bv_val &&
  458. '\0' != value->bv_val[0]) {
  459. strvalue = slapi_ch_strdup(value->bv_val);
  460. }
  461. }
  462. }
  463. }
  464. return (strvalue);
  465. }
  466. static int
  467. sync_find_ref_by_uuid(Sync_UpdateNode *updates, int stop, char *uniqueid)
  468. {
  469. int rc = -1;
  470. int i;
  471. for (i = 0; i < stop; i++) {
  472. if (updates[i].upd_uuid && (0 == strcmp(uniqueid, updates[i].upd_uuid))) {
  473. rc = i;
  474. break;
  475. }
  476. }
  477. return (rc);
  478. }
  479. static int
  480. sync_is_entry_in_scope(Slapi_PBlock *pb, Slapi_Entry *db_entry)
  481. {
  482. Slapi_Filter *origfilter;
  483. slapi_pblock_get(pb, SLAPI_SEARCH_FILTER, &origfilter);
  484. if (db_entry &&
  485. sync_is_active(db_entry, pb) &&
  486. (slapi_vattr_filter_test(pb, db_entry, origfilter, 1) == 0)) {
  487. return (1);
  488. } else {
  489. return (0);
  490. }
  491. }
  492. Slapi_Entry *
  493. sync_deleted_entry_from_changelog(Slapi_Entry *cl_entry)
  494. {
  495. Slapi_Entry *db_entry = NULL;
  496. char *entrydn = NULL;
  497. char *uniqueid = NULL;
  498. entrydn = sync_get_attr_value_from_entry(cl_entry, CL_ATTR_ENTRYDN);
  499. uniqueid = sync_get_attr_value_from_entry(cl_entry, CL_ATTR_UNIQUEID);
  500. /* when the Retro CL can provide the deleted entry
  501. * the entry will be taken from th RCL.
  502. * For now. just create an entry to holde the nsuniqueid
  503. */
  504. db_entry = slapi_entry_alloc();
  505. slapi_entry_init(db_entry, entrydn, NULL);
  506. slapi_entry_add_string(db_entry, "nsuniqueid", uniqueid);
  507. slapi_ch_free((void **)&uniqueid);
  508. return (db_entry);
  509. }
  510. int
  511. sync_read_entry_from_changelog(Slapi_Entry *cl_entry, void *cb_data)
  512. {
  513. char *uniqueid = NULL;
  514. char *entryuuid = NULL;
  515. char *chgtype = NULL;
  516. char *chgnr = NULL;
  517. int chg_req;
  518. int prev = 0;
  519. int index = 0;
  520. unsigned long chgnum = 0;
  521. Sync_CallBackData *cb = (Sync_CallBackData *)cb_data;
  522. if (cb == NULL) {
  523. return (1);
  524. }
  525. uniqueid = sync_get_attr_value_from_entry(cl_entry, CL_ATTR_UNIQUEID);
  526. if (uniqueid == NULL) {
  527. slapi_log_err(SLAPI_LOG_ERR, SYNC_PLUGIN_SUBSYSTEM,
  528. "sync_read_entry_from_changelog - Retro Changelog does not provide nsuniquedid."
  529. "Check 'cn=Retro Changelog Plugin,cn=plugins,cn=config' contains 'nsslapd-attribute: nsuniqueid:targetUniqueId'\n");
  530. return (1);
  531. }
  532. /* If we were requested to do openldap mode, get the targetEntryUuid too */
  533. if (cb->openldap_compat == PR_TRUE) {
  534. entryuuid = sync_get_attr_value_from_entry(cl_entry, CL_ATTR_ENTRYUUID);
  535. if (entryuuid == NULL) {
  536. slapi_log_err(SLAPI_LOG_ERR, SYNC_PLUGIN_SUBSYSTEM,
  537. "sync_read_entry_from_changelog - Retro Changelog does not provide entryuuid."
  538. "Check 'cn=Retro Changelog Plugin,cn=plugins,cn=config' contains 'nsslapd-attribute: entryuuid:targetEntryUUID'\n");
  539. return (1);
  540. }
  541. }
  542. chgnr = sync_get_attr_value_from_entry(cl_entry, CL_ATTR_CHANGENUMBER);
  543. chgnum = sync_number2ulong(chgnr);
  544. if (SYNC_INVALID_CHANGENUM == chgnum) {
  545. slapi_log_err(SLAPI_LOG_ERR, SYNC_PLUGIN_SUBSYSTEM,
  546. "sync_read_entry_from_changelog - Change number provided by Retro Changelog is invalid: %s\n", chgnr);
  547. slapi_ch_free_string(&chgnr);
  548. slapi_ch_free_string(&uniqueid);
  549. slapi_ch_free_string(&entryuuid);
  550. return (1);
  551. }
  552. if (chgnum < cb->change_start) {
  553. slapi_log_err(SLAPI_LOG_ERR, SYNC_PLUGIN_SUBSYSTEM,
  554. "sync_read_entry_from_changelog - "
  555. "Change number provided by Retro Changelog %s is less than the initial number %lu\n",
  556. chgnr, cb->change_start);
  557. slapi_ch_free_string(&chgnr);
  558. slapi_ch_free_string(&uniqueid);
  559. slapi_ch_free_string(&entryuuid);
  560. return (1);
  561. }
  562. index = chgnum - cb->change_start;
  563. chgtype = sync_get_attr_value_from_entry(cl_entry, CL_ATTR_CHGTYPE);
  564. chg_req = sync_str2chgreq(chgtype);
  565. switch (chg_req) {
  566. case LDAP_REQ_ADD:
  567. slapi_log_err(SLAPI_LOG_PLUGIN, SYNC_PLUGIN_SUBSYSTEM, "sync_read_entry_from_changelog - %s LDAP_REQ_ADD\n", uniqueid);
  568. /* nsuniqueid cannot exist, just add reference */
  569. cb->cb_updates[index].upd_chgtype = LDAP_REQ_ADD;
  570. cb->cb_updates[index].upd_uuid = uniqueid;
  571. cb->cb_updates[index].upd_euuid = entryuuid;
  572. break;
  573. case LDAP_REQ_MODIFY:
  574. /* check if we have seen this uuid already */
  575. prev = sync_find_ref_by_uuid(cb->cb_updates, index, uniqueid);
  576. if (prev == -1) {
  577. slapi_log_err(SLAPI_LOG_PLUGIN, SYNC_PLUGIN_SUBSYSTEM, "sync_read_entry_from_changelog - %s LDAP_REQ_MODIFY\n", uniqueid);
  578. cb->cb_updates[index].upd_chgtype = LDAP_REQ_MODIFY;
  579. cb->cb_updates[index].upd_uuid = uniqueid;
  580. cb->cb_updates[index].upd_euuid = entryuuid;
  581. } else {
  582. /* was add or mod, keep it */
  583. slapi_log_err(SLAPI_LOG_PLUGIN, SYNC_PLUGIN_SUBSYSTEM, "sync_read_entry_from_changelog - %s LDAP_REQ_MODIFY (already queued)\n", uniqueid);
  584. cb->cb_updates[index].upd_uuid = NULL;
  585. cb->cb_updates[index].upd_euuid = NULL;
  586. cb->cb_updates[index].upd_chgtype = 0;
  587. slapi_ch_free_string(&uniqueid);
  588. slapi_ch_free_string(&entryuuid);
  589. }
  590. break;
  591. case LDAP_REQ_MODRDN: {
  592. /* if it is a modrdn, we finally need to decide if this will
  593. * trigger a present or delete state, keep the info that
  594. * the entry was subject to a modrdn
  595. */
  596. int new_scope = 0;
  597. int old_scope = 0;
  598. Slapi_DN *original_dn;
  599. char *newsuperior = sync_get_attr_value_from_entry(cl_entry, CL_ATTR_NEWSUPERIOR);
  600. char *entrydn = sync_get_attr_value_from_entry(cl_entry, CL_ATTR_ENTRYDN);
  601. /* if newsuperior is set we need to checkif the entry has been moved into
  602. * or moved out of the scope of the synchronization request
  603. */
  604. original_dn = slapi_sdn_new_dn_byref(entrydn);
  605. old_scope = sync_is_active_scope(original_dn, cb->orig_pb);
  606. slapi_sdn_free(&original_dn);
  607. slapi_ch_free_string(&entrydn);
  608. if (newsuperior) {
  609. Slapi_DN *newbase;
  610. newbase = slapi_sdn_new_dn_byref(newsuperior);
  611. new_scope = sync_is_active_scope(newbase, cb->orig_pb);
  612. slapi_ch_free_string(&newsuperior);
  613. slapi_sdn_free(&newbase);
  614. } else {
  615. /* scope didn't change */
  616. new_scope = old_scope;
  617. }
  618. prev = sync_find_ref_by_uuid(cb->cb_updates, index, uniqueid);
  619. if (old_scope && new_scope) {
  620. /* nothing changed, it's just a MOD */
  621. if (prev == -1) {
  622. slapi_log_err(SLAPI_LOG_PLUGIN, SYNC_PLUGIN_SUBSYSTEM, "sync_read_entry_from_changelog - %s LDAP_REQ_MODRDN\n", uniqueid);
  623. cb->cb_updates[index].upd_chgtype = LDAP_REQ_MODIFY;
  624. cb->cb_updates[index].upd_uuid = uniqueid;
  625. cb->cb_updates[index].upd_euuid = entryuuid;
  626. } else {
  627. slapi_log_err(SLAPI_LOG_PLUGIN, SYNC_PLUGIN_SUBSYSTEM, "sync_read_entry_from_changelog - %s LDAP_REQ_MODRDN (already queued)\n", uniqueid);
  628. cb->cb_updates[index].upd_uuid = NULL;
  629. cb->cb_updates[index].upd_euuid = NULL;
  630. cb->cb_updates[index].upd_chgtype = 0;
  631. slapi_ch_free_string(&uniqueid);
  632. slapi_ch_free_string(&entryuuid);
  633. }
  634. } else if (old_scope) {
  635. /* it was moved out of scope, handle as DEL */
  636. if (prev == -1) {
  637. slapi_log_err(SLAPI_LOG_PLUGIN, SYNC_PLUGIN_SUBSYSTEM, "sync_read_entry_from_changelog - %s LDAP_REQ_MODRDN -> LDAP_REQ_DELETE\n", uniqueid);
  638. cb->cb_updates[index].upd_chgtype = LDAP_REQ_DELETE;
  639. cb->cb_updates[index].upd_uuid = uniqueid;
  640. cb->cb_updates[index].upd_euuid = entryuuid;
  641. cb->cb_updates[index].upd_e = sync_deleted_entry_from_changelog(cl_entry);
  642. } else {
  643. slapi_log_err(SLAPI_LOG_PLUGIN, SYNC_PLUGIN_SUBSYSTEM, "sync_read_entry_from_changelog - %s LDAP_REQ_MODRDN -> LDAP_REQ_DELETE (already queued)\n", uniqueid);
  644. cb->cb_updates[prev].upd_chgtype = LDAP_REQ_DELETE;
  645. cb->cb_updates[prev].upd_e = sync_deleted_entry_from_changelog(cl_entry);
  646. slapi_ch_free_string(&uniqueid);
  647. slapi_ch_free_string(&entryuuid);
  648. }
  649. } else if (new_scope) {
  650. /* moved into scope, handle as ADD */
  651. slapi_log_err(SLAPI_LOG_PLUGIN, SYNC_PLUGIN_SUBSYSTEM, "sync_read_entry_from_changelog - %s LDAP_REQ_MODRDN -> LDAP_REQ_ADD\n", uniqueid);
  652. cb->cb_updates[index].upd_chgtype = LDAP_REQ_ADD;
  653. cb->cb_updates[index].upd_uuid = uniqueid;
  654. cb->cb_updates[index].upd_euuid = entryuuid;
  655. } else {
  656. /* nothing to do */
  657. slapi_ch_free_string(&uniqueid);
  658. slapi_ch_free_string(&entryuuid);
  659. }
  660. slapi_sdn_free(&original_dn);
  661. break;
  662. }
  663. case LDAP_REQ_DELETE:
  664. /* check if we have seen this uuid already */
  665. prev = sync_find_ref_by_uuid(cb->cb_updates, index, uniqueid);
  666. if (prev == -1) {
  667. slapi_log_err(SLAPI_LOG_PLUGIN, SYNC_PLUGIN_SUBSYSTEM, "sync_read_entry_from_changelog - %s LDAP_REQ_DELETE\n", uniqueid);
  668. cb->cb_updates[index].upd_chgtype = LDAP_REQ_DELETE;
  669. cb->cb_updates[index].upd_uuid = uniqueid;
  670. cb->cb_updates[index].upd_euuid = entryuuid;
  671. cb->cb_updates[index].upd_e = sync_deleted_entry_from_changelog(cl_entry);
  672. } else {
  673. /* if it was added since last cookie state, we
  674. * can ignore it */
  675. if (cb->cb_updates[prev].upd_chgtype == LDAP_REQ_ADD) {
  676. slapi_log_err(SLAPI_LOG_PLUGIN, SYNC_PLUGIN_SUBSYSTEM, "sync_read_entry_from_changelog - %s LDAP_REQ_DELETE -> NO-OP\n", uniqueid);
  677. slapi_ch_free_string(&(cb->cb_updates[prev].upd_uuid));
  678. cb->cb_updates[prev].upd_uuid = NULL;
  679. cb->cb_updates[prev].upd_euuid = NULL;
  680. cb->cb_updates[index].upd_uuid = NULL;
  681. cb->cb_updates[index].upd_euuid = NULL;
  682. } else {
  683. /* ignore previous mod */
  684. slapi_log_err(SLAPI_LOG_PLUGIN, SYNC_PLUGIN_SUBSYSTEM, "sync_read_entry_from_changelog - %s LDAP_REQ_DELETE (already queued, updating)\n", uniqueid);
  685. cb->cb_updates[index].upd_uuid = NULL;
  686. cb->cb_updates[index].upd_euuid = NULL;
  687. cb->cb_updates[prev].upd_chgtype = LDAP_REQ_DELETE;
  688. cb->cb_updates[prev].upd_e = sync_deleted_entry_from_changelog(cl_entry);
  689. }
  690. slapi_ch_free_string(&uniqueid);
  691. slapi_ch_free_string(&entryuuid);
  692. }
  693. break;
  694. default:
  695. slapi_ch_free_string(&uniqueid);
  696. slapi_ch_free_string(&entryuuid);
  697. }
  698. slapi_ch_free_string(&chgtype);
  699. slapi_ch_free_string(&chgnr);
  700. return (0);
  701. }
  702. #define SYNC_MAX_DELETED_UUID_BATCH 50
  703. void
  704. sync_send_deleted_entries(Slapi_PBlock *pb, Sync_UpdateNode *upd, int chg_count, Sync_Cookie *cookie)
  705. {
  706. char *syncUUIDs[SYNC_MAX_DELETED_UUID_BATCH + 1] = {0};
  707. struct berval *ber_syncUUIDs[SYNC_MAX_DELETED_UUID_BATCH + 1] = {0};
  708. size_t uuid_index = 0;
  709. PR_ASSERT(cookie);
  710. syncUUIDs[0] = NULL;
  711. for (size_t index = 0; index < chg_count; index++) {
  712. if (upd[index].upd_chgtype == LDAP_REQ_DELETE && upd[index].upd_uuid) {
  713. if (uuid_index < SYNC_MAX_DELETED_UUID_BATCH) {
  714. if (upd[index].upd_euuid) {
  715. /* Only occurs in openldap mode, swap to the entryuuid */
  716. syncUUIDs[uuid_index] = sync_entryuuid2uuid(upd[index].upd_euuid);
  717. } else {
  718. /* Normal mode */
  719. syncUUIDs[uuid_index] = sync_nsuniqueid2uuid(upd[index].upd_uuid);
  720. }
  721. uuid_index++;
  722. } else {
  723. /* max number of uuids to be sent in one sync info message */
  724. syncUUIDs[uuid_index] = NULL;
  725. for (size_t i = 0; i < uuid_index; i++) {
  726. ber_syncUUIDs[i] = (struct berval *) slapi_ch_malloc(sizeof(struct berval));
  727. ber_syncUUIDs[i]->bv_val = syncUUIDs[i];
  728. ber_syncUUIDs[i]->bv_len = 16;
  729. }
  730. sync_intermediate_msg(pb, LDAP_TAG_SYNC_ID_SET, cookie, ber_syncUUIDs);
  731. for (size_t i = 0; i < uuid_index; i++) {
  732. slapi_ch_free((void **)&syncUUIDs[i]);
  733. slapi_ch_free((void **)&ber_syncUUIDs[i]);
  734. syncUUIDs[i] = NULL;
  735. }
  736. uuid_index = 0;
  737. }
  738. }
  739. }
  740. if (uuid_index > 0 && syncUUIDs[uuid_index - 1]) {
  741. /* more entries to send */
  742. syncUUIDs[uuid_index] = NULL;
  743. for (size_t i = 0; i < uuid_index; i++) {
  744. ber_syncUUIDs[i] = (struct berval *) slapi_ch_malloc(sizeof(struct berval));
  745. ber_syncUUIDs[i]->bv_val = syncUUIDs[i];
  746. ber_syncUUIDs[i]->bv_len = 16;
  747. }
  748. sync_intermediate_msg(pb, LDAP_TAG_SYNC_ID_SET, cookie, ber_syncUUIDs);
  749. for (size_t i = 0; i < uuid_index; i++) {
  750. slapi_ch_free((void **)&syncUUIDs[i]);
  751. slapi_ch_free((void **)&ber_syncUUIDs[i]);
  752. syncUUIDs[i] = NULL;
  753. }
  754. }
  755. }
  756. void
  757. sync_send_modified_entries(Slapi_PBlock *pb, Sync_UpdateNode *upd, int chg_count, Sync_Cookie *cookie)
  758. {
  759. for (size_t index = 0; index < chg_count; index++) {
  760. if (upd[index].upd_chgtype != LDAP_REQ_DELETE && upd[index].upd_uuid) {
  761. sync_send_entry_from_changelog(pb, upd[index].upd_chgtype, upd[index].upd_uuid, cookie);
  762. }
  763. }
  764. }
  765. int
  766. sync_send_entry_from_changelog(Slapi_PBlock *pb, int chg_req __attribute__((unused)), char *uniqueid, Sync_Cookie *cookie)
  767. {
  768. Slapi_Entry *db_entry = NULL;
  769. int chg_type = LDAP_SYNC_ADD;
  770. int rv = LDAP_SUCCESS;
  771. Slapi_PBlock *search_pb = NULL;
  772. Slapi_Entry **entries = NULL;
  773. char *origbase;
  774. char *filter = slapi_ch_smprintf("(nsuniqueid=%s)", uniqueid);
  775. slapi_pblock_get(pb, SLAPI_ORIGINAL_TARGET_DN, &origbase);
  776. search_pb = slapi_pblock_new();
  777. slapi_search_internal_set_pb(search_pb, origbase,
  778. LDAP_SCOPE_SUBTREE, filter,
  779. NULL, 0, NULL, NULL, plugin_get_default_component_id(), 0);
  780. slapi_search_internal_pb(search_pb);
  781. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &rv);
  782. if (rv == LDAP_SUCCESS) {
  783. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  784. if (entries) {
  785. db_entry = *entries; /* there can only be one */
  786. }
  787. }
  788. if (db_entry && sync_is_entry_in_scope(pb, db_entry)) {
  789. LDAPControl **ctrl = (LDAPControl **)slapi_ch_calloc(2, sizeof(LDAPControl *));
  790. rv = sync_create_state_control(db_entry, &ctrl[0], chg_type, NULL, cookie->openldap_compat);
  791. if (rv != LDAP_SUCCESS) {
  792. ldap_controls_free(ctrl);
  793. slapi_log_err(SLAPI_LOG_ERR, SYNC_PLUGIN_SUBSYSTEM, "Terminating sync_send_entry_from_changelog due to error code -> %d\n", rv);
  794. goto senddone;
  795. }
  796. slapi_send_ldap_search_entry(pb, db_entry, ctrl, NULL, 0);
  797. ldap_controls_free(ctrl);
  798. }
  799. senddone:
  800. slapi_free_search_results_internal(search_pb);
  801. slapi_pblock_destroy(search_pb);
  802. slapi_ch_free((void **)&filter);
  803. return rv;
  804. }
  805. static SyncOpInfo *
  806. new_SyncOpInfo(int flag, PRThread *tid, Sync_Cookie *cookie)
  807. {
  808. SyncOpInfo *spec = (SyncOpInfo *)slapi_ch_calloc(1, sizeof(SyncOpInfo));
  809. spec->send_flag = flag;
  810. spec->cookie = cookie;
  811. spec->tid = tid;
  812. return spec;
  813. }
  814. /* consumer operation extension constructor */
  815. static void *
  816. sync_operation_extension_ctor(void *object __attribute__((unused)), void *parent __attribute__((unused)))
  817. {
  818. /* we only set the extension value explicitly if the
  819. client requested the control - see deref_pre_search */
  820. return NULL; /* we don't set anything in the ctor */
  821. }
  822. /* consumer operation extension destructor */
  823. static void
  824. sync_delete_SyncOpInfo(SyncOpInfo **info)
  825. {
  826. if (info && *info) {
  827. sync_cookie_free(&((*info)->cookie));
  828. slapi_ch_free((void **)info);
  829. }
  830. }
  831. static void
  832. sync_operation_extension_dtor(void *ext, void *object __attribute__((unused)), void *parent __attribute__((unused)))
  833. {
  834. SyncOpInfo *spec = (SyncOpInfo *)ext;
  835. sync_delete_SyncOpInfo(&spec);
  836. }
  837. static SyncOpInfo *
  838. sync_get_operation_extension(Slapi_PBlock *pb)
  839. {
  840. Slapi_Operation *op;
  841. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  842. return (SyncOpInfo *)slapi_get_object_extension(sync_extension_type,
  843. op, sync_extension_handle);
  844. }
  845. static void
  846. sync_set_operation_extension(Slapi_PBlock *pb, SyncOpInfo *spec)
  847. {
  848. Slapi_Operation *op;
  849. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  850. slapi_set_object_extension(sync_extension_type, op,
  851. sync_extension_handle, (void *)spec);
  852. }
  853. void
  854. sync_register_allow_openldap_compat(PRBool allow)
  855. {
  856. /* This is synced by virtue of the plugin locking/loading. */
  857. allow_openldap_compat = allow;
  858. }
  859. int
  860. sync_register_operation_extension(void)
  861. {
  862. return slapi_register_object_extension(SYNC_PLUGIN_SUBSYSTEM,
  863. SLAPI_EXT_OPERATION,
  864. sync_operation_extension_ctor,
  865. sync_operation_extension_dtor,
  866. &sync_extension_type,
  867. &sync_extension_handle);
  868. }
  869. int
  870. sync_unregister_operation_entension(void)
  871. {
  872. int rc = slapi_unregister_object_extension(SYNC_PLUGIN_SUBSYSTEM,
  873. SLAPI_EXT_OPERATION,
  874. &sync_extension_type,
  875. &sync_extension_handle);
  876. return rc;
  877. }