sync_refresh.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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, PRThread *tid, 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, tid, 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. if (client_cookie) {
  159. rc = sync_persist_startup(tid, session_cookie);
  160. }
  161. if (rc == 0) {
  162. session_cookie = NULL; /* maintained in persist code */
  163. slapi_operation_set_flag(operation, OP_FLAG_SYNC_PERSIST);
  164. }
  165. }
  166. } else {
  167. /* unknown mode, return an error */
  168. rc = 1;
  169. }
  170. error_return:
  171. sync_cookie_free(&client_cookie);
  172. sync_cookie_free(&session_cookie);
  173. slapi_ch_free((void **)&cookie);
  174. }
  175. /* if we sent the entries
  176. * return "error" to abort normal search
  177. */
  178. if ( entries_sent > 0 ) {
  179. return(1);
  180. } else {
  181. return(rc);
  182. }
  183. }
  184. int sync_srch_refresh_post_search(Slapi_PBlock *pb)
  185. {
  186. int rc = 0;
  187. SyncOpInfo *info = sync_get_operation_extension(pb);
  188. if (!info) {
  189. return (0); /* nothing to do */
  190. }
  191. if (info->send_flag & SYNC_FLAG_SEND_INTERMEDIATE) {
  192. rc = sync_intermediate_msg(pb, LDAP_TAG_SYNC_REFRESH_DELETE, info->cookie, NULL);
  193. /* the refresh phase is over, now the post op
  194. * plugins will create the state control
  195. * depending on the operation type, reset flag
  196. */
  197. info->send_flag &= ~SYNC_FLAG_ADD_STATE_CTRL;
  198. /* activate the persistent phase thread*/
  199. sync_persist_startup(info->tid, info->cookie);
  200. }
  201. if (info->send_flag & SYNC_FLAG_ADD_DONE_CTRL) {
  202. LDAPControl **ctrl = (LDAPControl **)slapi_ch_calloc(2, sizeof (LDAPControl *));
  203. char *cookiestr = sync_cookie2str(info->cookie);
  204. sync_create_sync_done_control( &ctrl[0], 0, cookiestr);
  205. slapi_pblock_set(pb, SLAPI_RESCONTROLS, ctrl);
  206. slapi_ch_free((void **)&cookiestr);
  207. }
  208. return(rc);
  209. }
  210. int sync_srch_refresh_pre_entry(Slapi_PBlock *pb)
  211. {
  212. int rc = 0;
  213. SyncOpInfo *info = sync_get_operation_extension(pb);
  214. if (!info) {
  215. rc = 0; /* nothing to do */
  216. } else if (info->send_flag & SYNC_FLAG_ADD_STATE_CTRL) {
  217. Slapi_Entry *e;
  218. slapi_pblock_get(pb, SLAPI_SEARCH_RESULT_ENTRY, &e);
  219. LDAPControl **ctrl = (LDAPControl **)slapi_ch_calloc(2, sizeof (LDAPControl *));
  220. sync_create_state_control(e, &ctrl[0], LDAP_SYNC_ADD, NULL);
  221. slapi_pblock_set(pb, SLAPI_SEARCH_CTRLS, ctrl);
  222. }
  223. return(rc);
  224. }
  225. int sync_srch_refresh_pre_result(Slapi_PBlock *pb)
  226. {
  227. SyncOpInfo *info = sync_get_operation_extension(pb);
  228. if (!info) {
  229. return 0; /* nothing to do */
  230. }
  231. if (info->send_flag & SYNC_FLAG_NO_RESULT ) {
  232. return(1);
  233. } else {
  234. return(0);
  235. }
  236. }
  237. static void
  238. sync_free_update_nodes (Sync_UpdateNode **updates, int count)
  239. {
  240. int i;
  241. for (i=0; i<count;i++) {
  242. if ((*updates)[i].upd_uuid)
  243. slapi_ch_free((void **)&((*updates)[i].upd_uuid));
  244. if ((*updates)[i].upd_e)
  245. slapi_entry_free((*updates)[i].upd_e);
  246. }
  247. slapi_ch_free((void **)updates);
  248. }
  249. int
  250. sync_refresh_update_content(Slapi_PBlock *pb, Sync_Cookie *client_cookie, Sync_Cookie *server_cookie)
  251. {
  252. Slapi_PBlock *seq_pb;
  253. char *filter;
  254. Sync_CallBackData cb_data;
  255. int rc;
  256. int chg_count = server_cookie->cookie_change_info -
  257. client_cookie->cookie_change_info + 1;
  258. cb_data.cb_updates = (Sync_UpdateNode *)slapi_ch_calloc(chg_count, sizeof(Sync_UpdateNode));
  259. seq_pb = slapi_pblock_new();
  260. slapi_pblock_init(seq_pb);
  261. cb_data.orig_pb = pb;
  262. cb_data.change_start = client_cookie->cookie_change_info;
  263. filter = slapi_ch_smprintf("(&(changenumber>=%d)(changenumber<=%d))",
  264. client_cookie->cookie_change_info,
  265. server_cookie->cookie_change_info);
  266. slapi_search_internal_set_pb(
  267. seq_pb,
  268. CL_SRCH_BASE,
  269. LDAP_SCOPE_ONE,
  270. filter,
  271. NULL,
  272. 0,
  273. NULL, NULL,
  274. plugin_get_default_component_id(),
  275. 0);
  276. rc = slapi_search_internal_callback_pb (
  277. seq_pb, &cb_data, NULL, sync_read_entry_from_changelog, NULL);
  278. slapi_pblock_destroy(seq_pb);
  279. /* Now send the deleted entries in a sync info message
  280. * and the modified entries as single entries
  281. */
  282. sync_send_deleted_entries(pb, cb_data.cb_updates, chg_count, server_cookie);
  283. sync_send_modified_entries(pb, cb_data.cb_updates, chg_count);
  284. sync_free_update_nodes(&cb_data.cb_updates, chg_count);
  285. slapi_ch_free((void **)&filter);
  286. return (rc);
  287. }
  288. int
  289. sync_refresh_initial_content(Slapi_PBlock *pb, int sync_persist, PRThread *tid, Sync_Cookie *sc)
  290. {
  291. /* the entries will be sent in the normal search process, but
  292. * - a control has to be sent with each entry
  293. * if sync persist:
  294. * - an intermediate response has to be sent
  295. * - no result message must be sent
  296. *
  297. * else
  298. * - a result message with a sync done control has to be sent
  299. *
  300. * setup on operation extension to take care of in
  301. * pre_entry, pre_result and post_search plugins
  302. */
  303. SyncOpInfo *info;
  304. if (sync_persist) {
  305. info = new_SyncOpInfo
  306. (SYNC_FLAG_ADD_STATE_CTRL |
  307. SYNC_FLAG_SEND_INTERMEDIATE |
  308. SYNC_FLAG_NO_RESULT,
  309. tid,
  310. sc);
  311. } else {
  312. info = new_SyncOpInfo
  313. (SYNC_FLAG_ADD_STATE_CTRL |
  314. SYNC_FLAG_ADD_DONE_CTRL,
  315. tid,
  316. sc);
  317. }
  318. sync_set_operation_extension(pb, info);
  319. return(0);
  320. }
  321. static int
  322. sync_str2chgreq(char *chgtype)
  323. {
  324. if (chgtype == NULL) {
  325. return(-1);
  326. }
  327. if (strcasecmp(chgtype,"add") == 0) {
  328. return(LDAP_REQ_ADD);
  329. } else if (strcasecmp(chgtype,"modify") == 0) {
  330. return(LDAP_REQ_MODIFY);
  331. } else if (strcasecmp(chgtype,"modrdn") == 0) {
  332. return(LDAP_REQ_MODRDN);
  333. } else if (strcasecmp(chgtype,"delete") == 0) {
  334. return(LDAP_REQ_DELETE);
  335. } else {
  336. return(-1);
  337. }
  338. }
  339. static char*
  340. sync_get_attr_value_from_entry( Slapi_Entry *cl_entry, char *attrtype)
  341. {
  342. Slapi_Value *sval=NULL;
  343. const struct berval *value;
  344. char *strvalue = NULL;
  345. if ( NULL != cl_entry ) {
  346. Slapi_Attr *chattr = NULL;
  347. sval = NULL;
  348. value = NULL;
  349. if ( slapi_entry_attr_find( cl_entry, attrtype, &chattr ) == 0 ) {
  350. slapi_attr_first_value( chattr,&sval );
  351. if ( NULL != sval ) {
  352. value = slapi_value_get_berval ( sval );
  353. if( NULL != value && NULL != value->bv_val &&
  354. '\0' != value->bv_val[0]) {
  355. strvalue = slapi_ch_strdup( value->bv_val);
  356. }
  357. }
  358. }
  359. }
  360. return (strvalue);
  361. }
  362. static int
  363. sync_find_ref_by_uuid(Sync_UpdateNode *updates, int stop, char *uniqueid)
  364. {
  365. int rc = -1;
  366. int i;
  367. for (i=0; i<stop; i++) {
  368. if ( updates[i].upd_uuid && (0 == strcmp(uniqueid, updates[i].upd_uuid))) {
  369. rc = i;
  370. break;
  371. }
  372. }
  373. return (rc);
  374. }
  375. static int
  376. sync_is_entry_in_scope(Slapi_PBlock *pb, Slapi_Entry *db_entry)
  377. {
  378. Slapi_Filter *origfilter;
  379. slapi_pblock_get( pb, SLAPI_SEARCH_FILTER, &origfilter );
  380. if (db_entry &&
  381. sync_is_active(db_entry, pb) &&
  382. (slapi_vattr_filter_test( pb, db_entry, origfilter, 1) == 0)) {
  383. return(1);
  384. } else {
  385. return(0);
  386. }
  387. }
  388. Slapi_Entry *
  389. sync_deleted_entry_from_changelog(Slapi_Entry *cl_entry)
  390. {
  391. Slapi_Entry *db_entry = NULL;
  392. char *entrydn = NULL;
  393. char *uniqueid = NULL;
  394. entrydn = sync_get_attr_value_from_entry (cl_entry, CL_ATTR_ENTRYDN);
  395. uniqueid = sync_get_attr_value_from_entry (cl_entry, CL_ATTR_UNIQUEID);
  396. /* when the Retro CL can provide the deleted entry
  397. * the entry will be taken from th RCL.
  398. * For now. just create an entry to holde the nsuniqueid
  399. */
  400. db_entry = slapi_entry_alloc();
  401. slapi_entry_init(db_entry, entrydn, NULL);
  402. slapi_entry_add_string(db_entry, "nsuniqueid", uniqueid);
  403. slapi_ch_free((void**)&uniqueid);
  404. return(db_entry);
  405. }
  406. int
  407. sync_read_entry_from_changelog( Slapi_Entry *cl_entry, void *cb_data)
  408. {
  409. char *uniqueid = NULL;
  410. char *chgtype = NULL;
  411. char *chgnr = NULL;
  412. int chg_req;
  413. int prev = 0;
  414. int index = 0;
  415. Sync_CallBackData *cb = (Sync_CallBackData *) cb_data;
  416. if (cb == NULL) {
  417. return(1);
  418. }
  419. uniqueid = sync_get_attr_value_from_entry (cl_entry, CL_ATTR_UNIQUEID);
  420. if (uniqueid == NULL) {
  421. slapi_log_error (SLAPI_LOG_FATAL, SYNC_PLUGIN_SUBSYSTEM,
  422. "Retro Changelog does not provied nsuniquedid."
  423. "Check RCL plugin configuration." );
  424. return(1);
  425. }
  426. chgtype = sync_get_attr_value_from_entry (cl_entry, CL_ATTR_CHGTYPE);
  427. chgnr = sync_get_attr_value_from_entry (cl_entry, CL_ATTR_CHANGENUMBER);
  428. index = sync_number2int(chgnr) - cb->change_start;
  429. chg_req = sync_str2chgreq(chgtype);
  430. switch (chg_req){
  431. case LDAP_REQ_ADD:
  432. /* nsuniqueid cannot exist, just add reference */
  433. cb->cb_updates[index].upd_chgtype = LDAP_REQ_ADD;
  434. cb->cb_updates[index].upd_uuid = uniqueid;
  435. break;
  436. case LDAP_REQ_MODIFY:
  437. /* check if we have seen this uuid already */
  438. prev = sync_find_ref_by_uuid(cb->cb_updates, index, uniqueid);
  439. if (prev == -1) {
  440. cb->cb_updates[index].upd_chgtype = LDAP_REQ_MODIFY;
  441. cb->cb_updates[index].upd_uuid = uniqueid;
  442. } else {
  443. /* was add or mod, keep it */
  444. cb->cb_updates[index].upd_uuid = 0;
  445. cb->cb_updates[index].upd_chgtype = 0;
  446. slapi_ch_free_string(&uniqueid);
  447. }
  448. break;
  449. case LDAP_REQ_MODRDN:
  450. {
  451. /* if it is a modrdn, we finally need to decide if this will
  452. * trigger a present or delete state, keep the info that
  453. * the entry was subject to a modrdn
  454. */
  455. int new_scope = 0;
  456. int old_scope = 0;
  457. Slapi_DN *original_dn;
  458. char *newsuperior = sync_get_attr_value_from_entry (cl_entry, CL_ATTR_NEWSUPERIOR);
  459. char *entrydn = sync_get_attr_value_from_entry (cl_entry, CL_ATTR_ENTRYDN);
  460. /* if newsuperior is set we need to checkif the entry has been moved into
  461. * or moved out of the scope of the synchronization request
  462. */
  463. original_dn = slapi_sdn_new_dn_byref(entrydn);
  464. old_scope = sync_is_active_scope(original_dn,cb->orig_pb);
  465. slapi_sdn_free(&original_dn);
  466. slapi_ch_free_string(&entrydn);
  467. if (newsuperior) {
  468. Slapi_DN *newbase;
  469. newbase = slapi_sdn_new_dn_byref(newsuperior);
  470. new_scope = sync_is_active_scope(newbase, cb->orig_pb);
  471. slapi_ch_free_string(&newsuperior);
  472. slapi_sdn_free(&newbase);
  473. } else {
  474. /* scope didn't change */
  475. new_scope = old_scope;
  476. }
  477. prev = sync_find_ref_by_uuid(cb->cb_updates, index, uniqueid);
  478. if ( old_scope && new_scope ) {
  479. /* nothing changed, it's just a MOD */
  480. if (prev == -1) {
  481. cb->cb_updates[index].upd_chgtype = LDAP_REQ_MODIFY;
  482. cb->cb_updates[index].upd_uuid = uniqueid;
  483. } else {
  484. cb->cb_updates[index].upd_uuid = 0;
  485. cb->cb_updates[index].upd_chgtype = 0;
  486. slapi_ch_free_string(&uniqueid);
  487. }
  488. } else if ( old_scope ) {
  489. /* it was moved out of scope, handle as DEL */
  490. if (prev == -1) {
  491. cb->cb_updates[index].upd_chgtype = LDAP_REQ_DELETE;
  492. cb->cb_updates[index].upd_uuid = uniqueid;
  493. cb->cb_updates[index].upd_e = sync_deleted_entry_from_changelog(cl_entry);
  494. } else {
  495. cb->cb_updates[prev].upd_chgtype = LDAP_REQ_DELETE;
  496. cb->cb_updates[prev].upd_e = sync_deleted_entry_from_changelog(cl_entry);
  497. slapi_ch_free_string(&uniqueid);
  498. }
  499. } else if ( new_scope ) {
  500. /* moved into scope, handle as ADD */
  501. cb->cb_updates[index].upd_chgtype = LDAP_REQ_ADD;
  502. cb->cb_updates[index].upd_uuid = uniqueid;
  503. } else {
  504. /* nothing to do */
  505. slapi_ch_free_string(&uniqueid);
  506. }
  507. slapi_sdn_free(&original_dn);
  508. break;
  509. }
  510. case LDAP_REQ_DELETE:
  511. /* check if we have seen this uuid already */
  512. prev = sync_find_ref_by_uuid(cb->cb_updates, index, uniqueid);
  513. if (prev == -1) {
  514. cb->cb_updates[index].upd_chgtype = LDAP_REQ_DELETE;
  515. cb->cb_updates[index].upd_uuid = uniqueid;
  516. cb->cb_updates[index].upd_e = sync_deleted_entry_from_changelog(cl_entry);
  517. } else {
  518. /* if it was added since last cookie state, we
  519. * can ignoere it */
  520. if (cb->cb_updates[prev].upd_chgtype == LDAP_REQ_ADD) {
  521. slapi_ch_free_string(&(cb->cb_updates[prev].upd_uuid));
  522. cb->cb_updates[prev].upd_uuid = NULL;
  523. cb->cb_updates[index].upd_uuid = NULL;
  524. } else {
  525. /* ignore previous mod */
  526. cb->cb_updates[index].upd_uuid = NULL;
  527. cb->cb_updates[prev].upd_chgtype = LDAP_REQ_DELETE;
  528. cb->cb_updates[prev].upd_e = sync_deleted_entry_from_changelog(cl_entry);
  529. }
  530. slapi_ch_free_string(&uniqueid);
  531. }
  532. break;
  533. default:
  534. slapi_ch_free_string(&uniqueid);
  535. }
  536. slapi_ch_free_string(&chgtype);
  537. slapi_ch_free_string(&chgnr);
  538. return (0);
  539. }
  540. #define SYNC_MAX_DELETED_UUID_BATCH 50
  541. void
  542. sync_send_deleted_entries(Slapi_PBlock *pb, Sync_UpdateNode *upd, int chg_count, Sync_Cookie *cookie)
  543. {
  544. char *syncUUIDs[SYNC_MAX_DELETED_UUID_BATCH + 1];
  545. int uuid_index = 0;
  546. int index, i;
  547. syncUUIDs[0] = NULL;
  548. for (index=0; index < chg_count; index++) {
  549. if (upd[index].upd_chgtype == LDAP_REQ_DELETE &&
  550. upd[index].upd_uuid ) {
  551. if (uuid_index < SYNC_MAX_DELETED_UUID_BATCH) {
  552. syncUUIDs[uuid_index++] = sync_nsuniqueid2uuid(upd[index].upd_uuid);
  553. } else {
  554. /* max number of uuids to be sent in one sync info message */
  555. syncUUIDs[uuid_index] = NULL;
  556. sync_intermediate_msg (pb, LDAP_TAG_SYNC_ID_SET, cookie, &syncUUIDs[0]);
  557. for (i=0; i<uuid_index;i++) {
  558. slapi_ch_free((void **)&syncUUIDs[i]);
  559. syncUUIDs[i] = NULL;
  560. }
  561. uuid_index = 0;
  562. }
  563. }
  564. }
  565. if (uuid_index > 0 && syncUUIDs[uuid_index-1]) {
  566. /* more entries to send */
  567. syncUUIDs[uuid_index] = NULL;
  568. sync_intermediate_msg (pb, LDAP_TAG_SYNC_ID_SET, cookie, &syncUUIDs[0]);
  569. for (i=0; i<uuid_index;i++) {
  570. slapi_ch_free((void **)&syncUUIDs[i]);
  571. syncUUIDs[i] = NULL;
  572. }
  573. }
  574. }
  575. void
  576. sync_send_modified_entries(Slapi_PBlock *pb, Sync_UpdateNode *upd, int chg_count)
  577. {
  578. int index;
  579. for (index=0; index < chg_count; index++) {
  580. if (upd[index].upd_chgtype != LDAP_REQ_DELETE &&
  581. upd[index].upd_uuid )
  582. sync_send_entry_from_changelog(pb, upd[index].upd_chgtype, upd[index].upd_uuid);
  583. }
  584. }
  585. int
  586. sync_send_entry_from_changelog(Slapi_PBlock *pb,int chg_req, char *uniqueid)
  587. {
  588. Slapi_Entry *db_entry = NULL;
  589. int chg_type = LDAP_SYNC_ADD;
  590. int rv;
  591. Slapi_PBlock *search_pb = NULL;
  592. Slapi_Entry **entries = NULL;
  593. char *origbase;
  594. char *filter = slapi_ch_smprintf("(nsuniqueid=%s)",uniqueid);
  595. slapi_pblock_get( pb, SLAPI_ORIGINAL_TARGET_DN, &origbase );
  596. search_pb = slapi_pblock_new();
  597. slapi_search_internal_set_pb(search_pb, origbase,
  598. LDAP_SCOPE_SUBTREE, filter,
  599. NULL, 0, NULL, NULL, plugin_get_default_component_id(), 0);
  600. slapi_search_internal_pb(search_pb);
  601. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &rv);
  602. if ( rv == LDAP_SUCCESS) {
  603. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  604. if (entries)
  605. db_entry = *entries; /* there can only be one */
  606. }
  607. if (db_entry && sync_is_entry_in_scope(pb, db_entry)) {
  608. LDAPControl **ctrl = (LDAPControl **)slapi_ch_calloc(2, sizeof (LDAPControl *));
  609. sync_create_state_control(db_entry, &ctrl[0], chg_type, NULL);
  610. slapi_send_ldap_search_entry (pb, db_entry, ctrl, NULL, 0);
  611. ldap_controls_free(ctrl);
  612. }
  613. slapi_free_search_results_internal(search_pb);
  614. slapi_pblock_destroy(search_pb);
  615. slapi_ch_free((void **)&filter);
  616. return (0);
  617. }
  618. static SyncOpInfo*
  619. new_SyncOpInfo(int flag, PRThread *tid, Sync_Cookie *cookie) {
  620. SyncOpInfo *spec = (SyncOpInfo *)slapi_ch_calloc(1, sizeof(SyncOpInfo));
  621. spec->send_flag = flag;
  622. spec->cookie = cookie;
  623. spec->tid = tid;
  624. return spec;
  625. }
  626. /* consumer operation extension constructor */
  627. static void *
  628. sync_operation_extension_ctor(void *object, void *parent)
  629. {
  630. /* we only set the extension value explicitly if the
  631. client requested the control - see deref_pre_search */
  632. return NULL; /* we don't set anything in the ctor */
  633. }
  634. /* consumer operation extension destructor */
  635. static void
  636. sync_delete_SyncOpInfo(SyncOpInfo **info)
  637. {
  638. if (info && *info) {
  639. sync_cookie_free(&((*info)->cookie));
  640. slapi_ch_free((void **)info);
  641. }
  642. }
  643. static void
  644. sync_operation_extension_dtor(void *ext, void *object, void *parent)
  645. {
  646. SyncOpInfo *spec = (SyncOpInfo *)ext;
  647. sync_delete_SyncOpInfo(&spec);
  648. }
  649. static SyncOpInfo *
  650. sync_get_operation_extension(Slapi_PBlock *pb)
  651. {
  652. Slapi_Operation *op;
  653. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  654. return (SyncOpInfo *)slapi_get_object_extension(sync_extension_type,
  655. op, sync_extension_handle);
  656. }
  657. static void
  658. sync_set_operation_extension(Slapi_PBlock *pb, SyncOpInfo *spec)
  659. {
  660. Slapi_Operation *op;
  661. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  662. slapi_set_object_extension(sync_extension_type, op,
  663. sync_extension_handle, (void *)spec);
  664. }
  665. int
  666. sync_register_operation_extension(void)
  667. {
  668. return slapi_register_object_extension(SYNC_PLUGIN_SUBSYSTEM,
  669. SLAPI_EXT_OPERATION,
  670. sync_operation_extension_ctor,
  671. sync_operation_extension_dtor,
  672. &sync_extension_type,
  673. &sync_extension_handle);
  674. }
  675. int
  676. sync_unregister_operation_entension(void)
  677. {
  678. int rc = slapi_unregister_object_extension(SYNC_PLUGIN_SUBSYSTEM,
  679. SLAPI_EXT_OPERATION,
  680. &sync_extension_type,
  681. &sync_extension_handle);
  682. return rc;
  683. }