sync_refresh.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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) 2013 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. #include "sync.h"
  38. static SyncOpInfo *new_SyncOpInfo(int flag, Sync_Cookie *cookie);
  39. static int sync_extension_type;
  40. static int sync_extension_handle;
  41. static SyncOpInfo *sync_get_operation_extension(Slapi_PBlock *pb);
  42. static void sync_set_operation_extension(Slapi_PBlock *pb, SyncOpInfo *spec);
  43. static int sync_find_ref_by_uuid(Sync_UpdateNode *updates, int stop, char *uniqueid);
  44. static void sync_free_update_nodes (Sync_UpdateNode **updates, int count);
  45. Slapi_Entry *sync_deleted_entry_from_changelog(Slapi_Entry *cl_entry);
  46. static int sync_feature_allowed (Slapi_PBlock *pb);
  47. static int sync_feature_allowed (Slapi_PBlock *pb)
  48. {
  49. int isroot = 0;
  50. int ldapcode = LDAP_SUCCESS;
  51. slapi_pblock_get(pb, SLAPI_REQUESTOR_ISROOT, &isroot);
  52. if ( !isroot) {
  53. char *dn;
  54. Slapi_Entry *feature = NULL;
  55. /* Fetch the feature entry and see if the requestor is allowed access. */
  56. dn = slapi_ch_smprintf("dn: oid=%s,cn=features,cn=config", LDAP_CONTROL_SYNC);
  57. if ((feature = slapi_str2entry(dn,0)) != NULL) {
  58. char *dummy_attr = "1.1";
  59. ldapcode = slapi_access_allowed(pb, feature, dummy_attr, NULL, SLAPI_ACL_READ);
  60. }
  61. /* If the feature entry does not exist, deny use of the control. Only
  62. * the root DN will be allowed to use the control in this case. */
  63. if ((feature == NULL) || (ldapcode != LDAP_SUCCESS)) {
  64. ldapcode = LDAP_INSUFFICIENT_ACCESS;
  65. }
  66. slapi_ch_free((void **)&dn);
  67. slapi_entry_free(feature);
  68. }
  69. return(ldapcode);
  70. }
  71. int sync_srch_refresh_pre_search(Slapi_PBlock *pb)
  72. {
  73. LDAPControl **requestcontrols;
  74. struct berval *psbvp;
  75. Sync_Cookie *client_cookie = NULL;
  76. Sync_Cookie *session_cookie = NULL;
  77. int rc = 0;
  78. int sync_persist = 0;
  79. PRThread *tid = NULL;
  80. int entries_sent = 0;
  81. slapi_pblock_get (pb, SLAPI_REQCONTROLS, &requestcontrols);
  82. if ( slapi_control_present( requestcontrols, LDAP_CONTROL_SYNC, &psbvp, NULL )){
  83. char *cookie = NULL;
  84. int mode = 1;
  85. int refresh = 0;
  86. if ( sync_parse_control_value( psbvp, &mode,
  87. &refresh, &cookie ) != LDAP_SUCCESS )
  88. {
  89. rc = 1;
  90. goto error_return;
  91. } else {
  92. /* control is valid, check if usere is allowed to perform sync searches */
  93. rc = sync_feature_allowed(pb);
  94. if (rc) {
  95. sync_result_err(pb,rc,NULL);
  96. goto error_return;
  97. }
  98. }
  99. if ( mode == 1 || mode == 3 )
  100. {
  101. /* we need to return a cookie in the result message
  102. * indicating a state to be used in future sessions
  103. * as starting point - create it now
  104. */
  105. session_cookie = sync_cookie_create(pb);
  106. /*
  107. * if mode is persist we need to setup the persit handler
  108. * to catch the mods while the refresh is done
  109. */
  110. if ( mode == 3 )
  111. {
  112. tid = sync_persist_add(pb);
  113. if ( tid )
  114. sync_persist = 1;
  115. else {
  116. rc = LDAP_UNWILLING_TO_PERFORM;
  117. sync_result_err(pb,rc,"Too many active synchronization sessions");
  118. goto error_return;
  119. }
  120. }
  121. /*
  122. * now handl the refresh request
  123. * there are two scenarios
  124. * 1. no cookie is provided this means send all entries matching the search request
  125. * 2. a cookie is provided: send all entries changed since the cookie was issued
  126. * -- return an error if the cookie is invalid
  127. * -- return e-syncRefreshRequired if the data referenced in the cookie are no
  128. * longer in the history
  129. */
  130. if (cookie &&
  131. ( client_cookie = sync_cookie_parse (cookie))) {
  132. if (sync_cookie_isvalid(client_cookie, session_cookie)) {
  133. rc = sync_refresh_update_content(pb, client_cookie, session_cookie);
  134. if (rc == 0)
  135. entries_sent = 1;
  136. if (sync_persist)
  137. rc = sync_intermediate_msg(pb, LDAP_TAG_SYNC_REFRESH_DELETE, session_cookie, NULL);
  138. else
  139. rc = sync_result_msg(pb, session_cookie);
  140. } else {
  141. rc = E_SYNC_REFRESH_REQUIRED;
  142. sync_result_err(pb,rc, "Invalid session cookie");
  143. }
  144. } else {
  145. rc = sync_refresh_initial_content (pb, sync_persist, session_cookie);
  146. if (rc == 0 && !sync_persist)
  147. /* maintained in postop code */
  148. session_cookie = NULL;
  149. /* if persis it will be handed over to persist code */
  150. }
  151. if ( rc ) {
  152. if (sync_persist)
  153. sync_persist_terminate (tid);
  154. goto error_return;
  155. } else if (sync_persist){
  156. Slapi_Operation *operation;
  157. slapi_pblock_get(pb, SLAPI_OPERATION, &operation);
  158. rc = sync_persist_startup(tid, session_cookie);
  159. if (rc == 0) {
  160. session_cookie = NULL; /* maintained in persist code */
  161. slapi_operation_set_flag(operation, OP_FLAG_SYNC_PERSIST);
  162. }
  163. }
  164. } else {
  165. /* unknown mode, return an error */
  166. rc = 1;
  167. }
  168. error_return:
  169. sync_cookie_free(&client_cookie);
  170. sync_cookie_free(&session_cookie);
  171. slapi_ch_free((void **)&cookie);
  172. }
  173. /* if we sent the entries
  174. * return "error" to abort normal search
  175. */
  176. if ( entries_sent > 0 ) {
  177. return(1);
  178. } else {
  179. return(rc);
  180. }
  181. }
  182. int sync_srch_refresh_post_search(Slapi_PBlock *pb)
  183. {
  184. int rc = 0;
  185. SyncOpInfo *info = sync_get_operation_extension(pb);
  186. if (!info) {
  187. return (0); /* nothing to do */
  188. }
  189. if (info->send_flag & SYNC_FLAG_SEND_INTERMEDIATE) {
  190. rc = sync_intermediate_msg(pb, LDAP_TAG_SYNC_REFRESH_DELETE, info->cookie, NULL);
  191. /* the refresh phase is over, now the post op
  192. * plugins will create the state control
  193. * depending on the operation type, reset flag
  194. */
  195. info->send_flag &= ~SYNC_FLAG_ADD_STATE_CTRL;
  196. }
  197. if (info->send_flag & SYNC_FLAG_ADD_DONE_CTRL) {
  198. LDAPControl **ctrl = (LDAPControl **)slapi_ch_calloc(2, sizeof (LDAPControl *));
  199. char *cookiestr = sync_cookie2str(info->cookie);
  200. sync_create_sync_done_control( &ctrl[0], 0, cookiestr);
  201. slapi_pblock_set(pb, SLAPI_RESCONTROLS, ctrl);
  202. slapi_ch_free((void **)&cookiestr);
  203. }
  204. return(rc);
  205. }
  206. int sync_srch_refresh_pre_entry(Slapi_PBlock *pb)
  207. {
  208. int rc = 0;
  209. SyncOpInfo *info = sync_get_operation_extension(pb);
  210. if (!info) {
  211. rc = 0; /* nothing to do */
  212. } else if (info->send_flag & SYNC_FLAG_ADD_STATE_CTRL) {
  213. Slapi_Entry *e;
  214. slapi_pblock_get(pb, SLAPI_SEARCH_RESULT_ENTRY, &e);
  215. LDAPControl **ctrl = (LDAPControl **)slapi_ch_calloc(2, sizeof (LDAPControl *));
  216. sync_create_state_control(e, &ctrl[0], LDAP_SYNC_ADD, NULL);
  217. slapi_pblock_set(pb, SLAPI_SEARCH_CTRLS, ctrl);
  218. }
  219. return(rc);
  220. }
  221. int sync_srch_refresh_pre_result(Slapi_PBlock *pb)
  222. {
  223. SyncOpInfo *info = sync_get_operation_extension(pb);
  224. if (!info) {
  225. return 0; /* nothing to do */
  226. }
  227. if (info->send_flag & SYNC_FLAG_NO_RESULT ) {
  228. return(1);
  229. } else {
  230. return(0);
  231. }
  232. }
  233. static void
  234. sync_free_update_nodes (Sync_UpdateNode **updates, int count)
  235. {
  236. int i;
  237. for (i=0; i<count;i++) {
  238. if ((*updates)[i].upd_uuid)
  239. slapi_ch_free((void **)&((*updates)[i].upd_uuid));
  240. if ((*updates)[i].upd_e)
  241. slapi_entry_free((*updates)[i].upd_e);
  242. }
  243. slapi_ch_free((void **)updates);
  244. }
  245. int
  246. sync_refresh_update_content(Slapi_PBlock *pb, Sync_Cookie *client_cookie, Sync_Cookie *server_cookie)
  247. {
  248. Slapi_PBlock *seq_pb;
  249. char *filter;
  250. Sync_CallBackData cb_data;
  251. int rc;
  252. int chg_count = server_cookie->cookie_change_info -
  253. client_cookie->cookie_change_info + 1;
  254. cb_data.cb_updates = (Sync_UpdateNode *)slapi_ch_calloc(chg_count, sizeof(Sync_UpdateNode));
  255. seq_pb = slapi_pblock_new();
  256. slapi_pblock_init(seq_pb);
  257. cb_data.orig_pb = pb;
  258. cb_data.change_start = client_cookie->cookie_change_info;
  259. filter = slapi_ch_smprintf("(&(changenumber>=%d)(changenumber<=%d))",
  260. client_cookie->cookie_change_info,
  261. server_cookie->cookie_change_info);
  262. slapi_search_internal_set_pb(
  263. seq_pb,
  264. CL_SRCH_BASE,
  265. LDAP_SCOPE_ONE,
  266. filter,
  267. NULL,
  268. 0,
  269. NULL, NULL,
  270. plugin_get_default_component_id(),
  271. 0);
  272. rc = slapi_search_internal_callback_pb (
  273. seq_pb, &cb_data, NULL, sync_read_entry_from_changelog, NULL);
  274. slapi_pblock_destroy(seq_pb);
  275. /* Now send the deleted entries in a sync info message
  276. * and the modified entries as single entries
  277. */
  278. sync_send_deleted_entries(pb, cb_data.cb_updates, chg_count, server_cookie);
  279. sync_send_modified_entries(pb, cb_data.cb_updates, chg_count);
  280. sync_free_update_nodes(&cb_data.cb_updates, chg_count);
  281. slapi_ch_free((void **)&filter);
  282. return (rc);
  283. }
  284. int
  285. sync_refresh_initial_content(Slapi_PBlock *pb, int sync_persist, Sync_Cookie *sc)
  286. {
  287. /* the entries will be sent in the normal search process, but
  288. * - a control has to be sent with each entry
  289. * if sync persist:
  290. * - an intermediate response has to be sent
  291. * - no result message must be sent
  292. *
  293. * else
  294. * - a result message with a sync done control has to be sent
  295. *
  296. * setup on operation extension to take care of in
  297. * pre_entry, pre_result and post_search plugins
  298. */
  299. SyncOpInfo *info;
  300. if (sync_persist) {
  301. info = new_SyncOpInfo
  302. (SYNC_FLAG_ADD_STATE_CTRL |
  303. SYNC_FLAG_SEND_INTERMEDIATE |
  304. SYNC_FLAG_NO_RESULT,
  305. sc);
  306. } else {
  307. info = new_SyncOpInfo
  308. (SYNC_FLAG_ADD_STATE_CTRL |
  309. SYNC_FLAG_ADD_DONE_CTRL,
  310. sc);
  311. }
  312. sync_set_operation_extension(pb, info);
  313. return(0);
  314. }
  315. static int
  316. sync_str2chgreq(char *chgtype)
  317. {
  318. if (chgtype == NULL) {
  319. return(-1);
  320. }
  321. if (strcasecmp(chgtype,"add") == 0) {
  322. return(LDAP_REQ_ADD);
  323. } else if (strcasecmp(chgtype,"modify") == 0) {
  324. return(LDAP_REQ_MODIFY);
  325. } else if (strcasecmp(chgtype,"modrdn") == 0) {
  326. return(LDAP_REQ_MODRDN);
  327. } else if (strcasecmp(chgtype,"delete") == 0) {
  328. return(LDAP_REQ_DELETE);
  329. } else {
  330. return(-1);
  331. }
  332. }
  333. static char*
  334. sync_get_attr_value_from_entry( Slapi_Entry *cl_entry, char *attrtype)
  335. {
  336. Slapi_Value *sval=NULL;
  337. const struct berval *value;
  338. char *strvalue = NULL;
  339. if ( NULL != cl_entry ) {
  340. Slapi_Attr *chattr = NULL;
  341. sval = NULL;
  342. value = NULL;
  343. if ( slapi_entry_attr_find( cl_entry, attrtype, &chattr ) == 0 ) {
  344. slapi_attr_first_value( chattr,&sval );
  345. if ( NULL != sval ) {
  346. value = slapi_value_get_berval ( sval );
  347. if( NULL != value && NULL != value->bv_val &&
  348. '\0' != value->bv_val[0]) {
  349. strvalue = slapi_ch_strdup( value->bv_val);
  350. }
  351. }
  352. }
  353. }
  354. return (strvalue);
  355. }
  356. static int
  357. sync_find_ref_by_uuid(Sync_UpdateNode *updates, int stop, char *uniqueid)
  358. {
  359. int rc = -1;
  360. int i;
  361. for (i=0; i<stop; i++) {
  362. if ( updates[i].upd_uuid && (0 == strcmp(uniqueid, updates[i].upd_uuid))) {
  363. rc = i;
  364. break;
  365. }
  366. }
  367. return (rc);
  368. }
  369. static int
  370. sync_is_entry_in_scope(Slapi_PBlock *pb, Slapi_Entry *db_entry)
  371. {
  372. Slapi_Filter *origfilter;
  373. slapi_pblock_get( pb, SLAPI_SEARCH_FILTER, &origfilter );
  374. if (db_entry &&
  375. sync_is_active(db_entry, pb) &&
  376. (slapi_vattr_filter_test( pb, db_entry, origfilter, 1) == 0)) {
  377. return(1);
  378. } else {
  379. return(0);
  380. }
  381. }
  382. Slapi_Entry *
  383. sync_deleted_entry_from_changelog(Slapi_Entry *cl_entry)
  384. {
  385. Slapi_Entry *db_entry = NULL;
  386. char *entrydn = NULL;
  387. char *uniqueid = NULL;
  388. entrydn = sync_get_attr_value_from_entry (cl_entry, CL_ATTR_ENTRYDN);
  389. uniqueid = sync_get_attr_value_from_entry (cl_entry, CL_ATTR_UNIQUEID);
  390. /* when the Retro CL can provide the deleted entry
  391. * the entry will be taken from th RCL.
  392. * For now. just create an entry to holde the nsuniqueid
  393. */
  394. db_entry = slapi_entry_alloc();
  395. slapi_entry_init(db_entry, entrydn, NULL);
  396. slapi_entry_add_string(db_entry, "nsuniqueid", uniqueid);
  397. slapi_ch_free((void**)&uniqueid);
  398. return(db_entry);
  399. }
  400. int
  401. sync_read_entry_from_changelog( Slapi_Entry *cl_entry, void *cb_data)
  402. {
  403. char *uniqueid = NULL;
  404. char *chgtype = NULL;
  405. char *chgnr = NULL;
  406. int chg_req;
  407. int prev = 0;
  408. int index = 0;
  409. Sync_CallBackData *cb = (Sync_CallBackData *) cb_data;
  410. if (cb == NULL) {
  411. return(1);
  412. }
  413. uniqueid = sync_get_attr_value_from_entry (cl_entry, CL_ATTR_UNIQUEID);
  414. chgtype = sync_get_attr_value_from_entry (cl_entry, CL_ATTR_CHGTYPE);
  415. chgnr = sync_get_attr_value_from_entry (cl_entry, CL_ATTR_CHANGENUMBER);
  416. if (uniqueid == NULL) {
  417. slapi_log_error (SLAPI_LOG_FATAL, SYNC_PLUGIN_SUBSYSTEM,
  418. "Retro Changelog does not provied nsuniquedid."
  419. "Check RCL plugin configuration." );
  420. return(1);
  421. }
  422. index = sync_number2int(chgnr) - cb->change_start;
  423. chg_req = sync_str2chgreq(chgtype);
  424. switch (chg_req){
  425. case LDAP_REQ_ADD:
  426. /* nsuniqueid cannot exist, just add reference */
  427. cb->cb_updates[index].upd_chgtype = LDAP_REQ_ADD;
  428. cb->cb_updates[index].upd_uuid = uniqueid;
  429. break;
  430. case LDAP_REQ_MODIFY:
  431. /* check if we have seen this uuid already */
  432. prev = sync_find_ref_by_uuid(cb->cb_updates, index, uniqueid);
  433. if (prev == -1) {
  434. cb->cb_updates[index].upd_chgtype = LDAP_REQ_MODIFY;
  435. cb->cb_updates[index].upd_uuid = uniqueid;
  436. } else {
  437. /* was add or mod, keep it */
  438. cb->cb_updates[index].upd_uuid = 0;
  439. cb->cb_updates[index].upd_chgtype = 0;
  440. slapi_ch_free((void **)&uniqueid);
  441. }
  442. break;
  443. case LDAP_REQ_MODRDN:
  444. {
  445. /* if it is a modrdn, we finally need to decide if this will
  446. * trigger a present or delete state, keep the info that
  447. * the entry was subject to a modrdn
  448. */
  449. int new_scope = 0;
  450. int old_scope = 0;
  451. Slapi_DN *original_dn;
  452. char *newsuperior = sync_get_attr_value_from_entry (cl_entry, CL_ATTR_NEWSUPERIOR);
  453. char *entrydn = sync_get_attr_value_from_entry (cl_entry, CL_ATTR_ENTRYDN);
  454. /* if newsuperior is set we need to checkif the entry has been moved into
  455. * or moved out of the scope of the synchronization request
  456. */
  457. original_dn = slapi_sdn_new_dn_byref(entrydn);
  458. old_scope = sync_is_active_scope(original_dn,cb->orig_pb);
  459. slapi_sdn_free(&original_dn);
  460. slapi_ch_free((void **)&entrydn);
  461. if (newsuperior) {
  462. Slapi_DN *newbase;
  463. newbase = slapi_sdn_new_dn_byref(newsuperior);
  464. new_scope = sync_is_active_scope(newbase, cb->orig_pb);
  465. slapi_ch_free((void **)&newsuperior);
  466. slapi_sdn_free(&newbase);
  467. } else {
  468. /* scope didn't change */
  469. new_scope = old_scope;
  470. }
  471. prev = sync_find_ref_by_uuid(cb->cb_updates, index, uniqueid);
  472. if ( old_scope && new_scope ) {
  473. /* nothing changed, it's just a MOD */
  474. if (prev == -1) {
  475. cb->cb_updates[index].upd_chgtype = LDAP_REQ_MODIFY;
  476. cb->cb_updates[index].upd_uuid = uniqueid;
  477. } else {
  478. cb->cb_updates[index].upd_uuid = 0;
  479. cb->cb_updates[index].upd_chgtype = 0;
  480. slapi_ch_free((void **)&uniqueid);
  481. }
  482. } else if ( old_scope ) {
  483. /* it was moved out of scope, handle as DEL */
  484. if (prev == -1) {
  485. cb->cb_updates[index].upd_chgtype = LDAP_REQ_DELETE;
  486. cb->cb_updates[index].upd_uuid = uniqueid;
  487. cb->cb_updates[index].upd_e = sync_deleted_entry_from_changelog(cl_entry);
  488. } else {
  489. cb->cb_updates[prev].upd_chgtype = LDAP_REQ_DELETE;
  490. cb->cb_updates[prev].upd_e = sync_deleted_entry_from_changelog(cl_entry);
  491. slapi_ch_free((void **)&uniqueid);
  492. }
  493. } else if ( new_scope ) {
  494. /* moved into scope, handle as ADD */
  495. cb->cb_updates[index].upd_chgtype = LDAP_REQ_ADD;
  496. cb->cb_updates[index].upd_uuid = uniqueid;
  497. } else {
  498. /* nothing to do */
  499. }
  500. slapi_sdn_free(&original_dn);
  501. break;
  502. }
  503. case LDAP_REQ_DELETE:
  504. /* check if we have seen this uuid already */
  505. prev = sync_find_ref_by_uuid(cb->cb_updates, index, uniqueid);
  506. if (prev == -1) {
  507. cb->cb_updates[index].upd_chgtype = LDAP_REQ_DELETE;
  508. cb->cb_updates[index].upd_uuid = uniqueid;
  509. cb->cb_updates[index].upd_e = sync_deleted_entry_from_changelog(cl_entry);
  510. } else {
  511. /* if it was added since last cookie state, we
  512. * can ignoere it */
  513. if (cb->cb_updates[prev].upd_chgtype == LDAP_REQ_ADD) {
  514. slapi_ch_free((void **)&(cb->cb_updates[prev].upd_uuid));
  515. slapi_ch_free((void **)&uniqueid);
  516. cb->cb_updates[prev].upd_uuid = NULL;
  517. cb->cb_updates[index].upd_uuid = NULL;
  518. } else {
  519. /* ignore previous mod */
  520. cb->cb_updates[index].upd_uuid = NULL;
  521. cb->cb_updates[prev].upd_chgtype = LDAP_REQ_DELETE;
  522. cb->cb_updates[prev].upd_e = sync_deleted_entry_from_changelog(cl_entry);
  523. }
  524. }
  525. break;
  526. }
  527. slapi_ch_free((void **)&chgtype);
  528. slapi_ch_free((void **)&chgnr);
  529. return (0);
  530. }
  531. #define SYNC_MAX_DELETED_UUID_BATCH 50
  532. void
  533. sync_send_deleted_entries(Slapi_PBlock *pb, Sync_UpdateNode *upd, int chg_count, Sync_Cookie *cookie)
  534. {
  535. char *syncUUIDs[SYNC_MAX_DELETED_UUID_BATCH + 1];
  536. int uuid_index = 0;
  537. int index, i;
  538. syncUUIDs[0] = NULL;
  539. for (index=0; index < chg_count; index++) {
  540. if (upd[index].upd_chgtype == LDAP_REQ_DELETE &&
  541. upd[index].upd_uuid ) {
  542. if (uuid_index < SYNC_MAX_DELETED_UUID_BATCH) {
  543. syncUUIDs[uuid_index++] = sync_nsuniqueid2uuid(upd[index].upd_uuid);
  544. } else {
  545. /* max number of uuids to be sent in one sync info message */
  546. syncUUIDs[uuid_index] = NULL;
  547. sync_intermediate_msg (pb, LDAP_TAG_SYNC_ID_SET, cookie, &syncUUIDs[0]);
  548. for (i=0; i<uuid_index;i++) {
  549. slapi_ch_free((void **)&syncUUIDs[i]);
  550. syncUUIDs[i] = NULL;
  551. }
  552. uuid_index = 0;
  553. }
  554. }
  555. }
  556. if (uuid_index > 0 && syncUUIDs[uuid_index-1]) {
  557. /* more entries to send */
  558. syncUUIDs[uuid_index] = NULL;
  559. sync_intermediate_msg (pb, LDAP_TAG_SYNC_ID_SET, cookie, &syncUUIDs[0]);
  560. for (i=0; i<uuid_index;i++) {
  561. slapi_ch_free((void **)&syncUUIDs[i]);
  562. syncUUIDs[i] = NULL;
  563. }
  564. }
  565. }
  566. void
  567. sync_send_modified_entries(Slapi_PBlock *pb, Sync_UpdateNode *upd, int chg_count)
  568. {
  569. int index;
  570. for (index=0; index < chg_count; index++) {
  571. if (upd[index].upd_chgtype != LDAP_REQ_DELETE &&
  572. upd[index].upd_uuid )
  573. sync_send_entry_from_changelog(pb, upd[index].upd_chgtype, upd[index].upd_uuid);
  574. }
  575. }
  576. int
  577. sync_send_entry_from_changelog(Slapi_PBlock *pb,int chg_req, char *uniqueid)
  578. {
  579. Slapi_Entry *db_entry = NULL;
  580. int chg_type = LDAP_SYNC_ADD;
  581. int rv;
  582. Slapi_PBlock *search_pb = NULL;
  583. Slapi_Entry **entries = NULL;
  584. char *origbase;
  585. char *filter = slapi_ch_smprintf("(nsuniqueid=%s)",uniqueid);
  586. slapi_pblock_get( pb, SLAPI_ORIGINAL_TARGET_DN, &origbase );
  587. search_pb = slapi_pblock_new();
  588. slapi_search_internal_set_pb(search_pb, origbase,
  589. LDAP_SCOPE_SUBTREE, filter,
  590. NULL, 0, NULL, NULL, plugin_get_default_component_id(), 0);
  591. slapi_search_internal_pb(search_pb);
  592. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &rv);
  593. if ( rv == LDAP_SUCCESS) {
  594. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  595. if (entries)
  596. db_entry = *entries; /* there can only be one */
  597. }
  598. if (db_entry && sync_is_entry_in_scope(pb, db_entry)) {
  599. LDAPControl **ctrl = (LDAPControl **)slapi_ch_calloc(2, sizeof (LDAPControl *));
  600. sync_create_state_control(db_entry, &ctrl[0], chg_type, NULL);
  601. slapi_send_ldap_search_entry (pb, db_entry, ctrl, NULL, 0);
  602. ldap_controls_free(ctrl);
  603. }
  604. slapi_free_search_results_internal(search_pb);
  605. slapi_pblock_destroy(search_pb);
  606. slapi_ch_free((void **)&filter);
  607. return (0);
  608. }
  609. static SyncOpInfo*
  610. new_SyncOpInfo(int flag, Sync_Cookie *cookie) {
  611. SyncOpInfo *spec = (SyncOpInfo *)slapi_ch_calloc(1, sizeof(SyncOpInfo));
  612. spec->send_flag = flag;
  613. spec->cookie = cookie;
  614. return spec;
  615. }
  616. /* consumer operation extension constructor */
  617. static void *
  618. sync_operation_extension_ctor(void *object, void *parent)
  619. {
  620. /* we only set the extension value explicitly if the
  621. client requested the control - see deref_pre_search */
  622. return NULL; /* we don't set anything in the ctor */
  623. }
  624. /* consumer operation extension destructor */
  625. static void
  626. sync_delete_SyncOpInfo(SyncOpInfo **info)
  627. {
  628. if (info && *info) {
  629. sync_cookie_free(&((*info)->cookie));
  630. slapi_ch_free((void **)info);
  631. }
  632. }
  633. static void
  634. sync_operation_extension_dtor(void *ext, void *object, void *parent)
  635. {
  636. SyncOpInfo *spec = (SyncOpInfo *)ext;
  637. sync_delete_SyncOpInfo(&spec);
  638. }
  639. static SyncOpInfo *
  640. sync_get_operation_extension(Slapi_PBlock *pb)
  641. {
  642. Slapi_Operation *op;
  643. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  644. return (SyncOpInfo *)slapi_get_object_extension(sync_extension_type,
  645. op, sync_extension_handle);
  646. }
  647. static void
  648. sync_set_operation_extension(Slapi_PBlock *pb, SyncOpInfo *spec)
  649. {
  650. Slapi_Operation *op;
  651. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  652. slapi_set_object_extension(sync_extension_type, op,
  653. sync_extension_handle, (void *)spec);
  654. }
  655. int
  656. sync_register_operation_extension(void)
  657. {
  658. return slapi_register_object_extension(SYNC_PLUGIN_SUBSYSTEM,
  659. SLAPI_EXT_OPERATION,
  660. sync_operation_extension_ctor,
  661. sync_operation_extension_dtor,
  662. &sync_extension_type,
  663. &sync_extension_handle);
  664. }