1
0

sync_util.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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 struct berval * create_syncinfo_value( int type, const char *cookie, const char **uuids);
  10. static char * sync_cookie_get_server_info(Slapi_PBlock *pb);
  11. static char * sync_cookie_get_client_info(Slapi_PBlock *pb);
  12. /*
  13. * Parse the value from an LDAPv3 sync request control. They look
  14. * like this:
  15. *
  16. * syncRequestValue ::= SEQUENCE {
  17. * mode ENUMERATED {
  18. * -- 0 unused
  19. * refreshOnly (1),
  20. * -- 2 reserved
  21. * refreshAndPersist (3)
  22. * },
  23. * cookie syncCookie OPTIONAL,
  24. * reloadHint BOOLEAN DEFAULT FALSE
  25. * }
  26. *
  27. * Return an LDAP error code (LDAP_SUCCESS if all goes well).
  28. *
  29. */
  30. int
  31. sync_parse_control_value( struct berval *psbvp, ber_int_t *mode, int *reload, char **cookie )
  32. {
  33. int rc= LDAP_SUCCESS;
  34. if ( psbvp->bv_len == 0 || psbvp->bv_val == NULL )
  35. {
  36. rc= LDAP_PROTOCOL_ERROR;
  37. }
  38. else
  39. {
  40. BerElement *ber= ber_init( psbvp );
  41. if ( ber == NULL )
  42. {
  43. rc= LDAP_OPERATIONS_ERROR;
  44. }
  45. else
  46. {
  47. if ( ber_scanf( ber, "{e", mode ) == LBER_ERROR )
  48. {
  49. rc= LDAP_PROTOCOL_ERROR;
  50. } else {
  51. ber_tag_t tag;
  52. ber_len_t len;
  53. tag = ber_peek_tag( ber, &len );
  54. if ( tag == LDAP_TAG_SYNC_COOKIE ) {
  55. rc = ber_scanf( ber, "a", cookie );
  56. tag = ber_peek_tag( ber, &len );
  57. }
  58. if (rc != LBER_ERROR && tag == LDAP_TAG_RELOAD_HINT ) {
  59. rc = ber_scanf( ber, "b", reload );
  60. }
  61. if (rc != LBER_ERROR) {
  62. rc = ber_scanf( ber, "}");
  63. }
  64. if (rc == LBER_ERROR) {
  65. rc= LDAP_PROTOCOL_ERROR;
  66. };
  67. }
  68. /* the ber encoding is no longer needed */
  69. ber_free(ber,1);
  70. }
  71. }
  72. return( rc );
  73. }
  74. char *
  75. sync_nsuniqueid2uuid(const char *nsuniqueid)
  76. {
  77. char *uuid;
  78. char u[17];
  79. u[0] = slapi_str_to_u8(nsuniqueid);
  80. u[1] = slapi_str_to_u8(nsuniqueid+2);
  81. u[2] = slapi_str_to_u8(nsuniqueid+4);
  82. u[3] = slapi_str_to_u8(nsuniqueid+6);
  83. u[4] = slapi_str_to_u8(nsuniqueid+9);
  84. u[5] = slapi_str_to_u8(nsuniqueid+11);
  85. u[6] = slapi_str_to_u8(nsuniqueid+13);
  86. u[7] = slapi_str_to_u8(nsuniqueid+15);
  87. u[8] = slapi_str_to_u8(nsuniqueid+18);
  88. u[9] = slapi_str_to_u8(nsuniqueid+20);
  89. u[10] = slapi_str_to_u8(nsuniqueid+22);
  90. u[11] = slapi_str_to_u8(nsuniqueid+24);
  91. u[12] = slapi_str_to_u8(nsuniqueid+27);
  92. u[13] = slapi_str_to_u8(nsuniqueid+29);
  93. u[14] = slapi_str_to_u8(nsuniqueid+31);
  94. u[15] = slapi_str_to_u8(nsuniqueid+33);
  95. u[16] = '\0';
  96. uuid = slapi_ch_malloc(sizeof(u));
  97. memcpy(uuid, u, sizeof(u));
  98. return(uuid);
  99. }
  100. /*
  101. * syncStateValue ::= SEQUENCE {
  102. * state ENUMERATED {
  103. * present (0),
  104. * add (1),
  105. * modify (2),
  106. * delete (3)
  107. * },
  108. * entryUUID syncUUID,
  109. * cookie syncCookie OPTIONAL
  110. * }
  111. *
  112. */
  113. int
  114. sync_create_state_control( Slapi_Entry *e, LDAPControl **ctrlp, int type, Sync_Cookie *cookie)
  115. {
  116. int rc;
  117. BerElement *ber;
  118. struct berval *bvp;
  119. char *uuid;
  120. Slapi_Attr *attr;
  121. Slapi_Value *val;
  122. if ( type == LDAP_SYNC_NONE || ctrlp == NULL || ( ber = der_alloc()) == NULL ) {
  123. return( LDAP_OPERATIONS_ERROR );
  124. }
  125. *ctrlp = NULL;
  126. slapi_entry_attr_find(e, SLAPI_ATTR_UNIQUEID, &attr);
  127. slapi_attr_first_value(attr, &val);
  128. uuid = sync_nsuniqueid2uuid(slapi_value_get_string(val));
  129. if (( rc = ber_printf( ber, "{eo", type , uuid, 16 )) != -1 ) {
  130. if (cookie) {
  131. char *cookiestr = sync_cookie2str(cookie);
  132. rc = ber_printf( ber, "s}", cookiestr);
  133. slapi_ch_free((void **)&cookiestr);
  134. } else {
  135. rc = ber_printf( ber, "}");
  136. }
  137. }
  138. if (rc != -1) {
  139. rc = ber_flatten( ber, &bvp );
  140. }
  141. ber_free( ber, 1 );
  142. slapi_ch_free((void **)&uuid);
  143. if ( rc == -1 ) {
  144. return( LDAP_OPERATIONS_ERROR );
  145. }
  146. *ctrlp = (LDAPControl *)slapi_ch_malloc( sizeof( LDAPControl ));
  147. (*ctrlp)->ldctl_iscritical = 0;
  148. (*ctrlp)->ldctl_oid = slapi_ch_strdup( LDAP_CONTROL_SYNC_STATE );
  149. (*ctrlp)->ldctl_value = *bvp; /* struct copy */
  150. bvp->bv_val = NULL;
  151. ber_bvfree( bvp );
  152. return (LDAP_SUCCESS);
  153. }
  154. /*
  155. * syncDoneValue ::= SEQUENCE {
  156. * cookie syncCookie OPTIONAL
  157. * refreshDeletes BOOLEAN DEFAULT FALSE
  158. * }
  159. *
  160. */
  161. int
  162. sync_create_sync_done_control( LDAPControl **ctrlp, int refresh, char *cookie)
  163. {
  164. int rc;
  165. BerElement *ber;
  166. struct berval *bvp;
  167. if ( ctrlp == NULL || ( ber = der_alloc()) == NULL ) {
  168. return( LDAP_OPERATIONS_ERROR );
  169. }
  170. *ctrlp = NULL;
  171. if (cookie) {
  172. if (( rc = ber_printf( ber, "{s", cookie)) != -1 ) {
  173. if (refresh) {
  174. rc = ber_printf( ber, "e}", refresh);
  175. } else {
  176. rc = ber_printf( ber, "}");
  177. }
  178. }
  179. } else {
  180. if (refresh) {
  181. rc = ber_printf( ber, "{e}", refresh);
  182. } else {
  183. rc = ber_printf( ber, "{}");
  184. }
  185. }
  186. if (rc != -1) {
  187. rc = ber_flatten( ber, &bvp );
  188. }
  189. ber_free( ber, 1 );
  190. if ( rc == -1 ) {
  191. return( LDAP_OPERATIONS_ERROR );
  192. }
  193. *ctrlp = (LDAPControl *)slapi_ch_malloc( sizeof( LDAPControl ));
  194. (*ctrlp)->ldctl_iscritical = 0;
  195. (*ctrlp)->ldctl_oid = slapi_ch_strdup( LDAP_CONTROL_SYNC_DONE );
  196. (*ctrlp)->ldctl_value = *bvp; /* struct copy */
  197. bvp->bv_val = NULL;
  198. ber_bvfree( bvp );
  199. return (LDAP_SUCCESS);
  200. }
  201. char *
  202. sync_cookie2str(Sync_Cookie *cookie)
  203. {
  204. char *cookiestr = NULL;
  205. if (cookie) {
  206. cookiestr = slapi_ch_smprintf("%s#%s#%lu",
  207. cookie->cookie_server_signature,
  208. cookie->cookie_client_signature,
  209. cookie->cookie_change_info);
  210. }
  211. return(cookiestr);
  212. }
  213. int
  214. sync_intermediate_msg (Slapi_PBlock *pb, int tag, Sync_Cookie *cookie, char **uuids)
  215. {
  216. int rc;
  217. struct berval *syncInfo;
  218. LDAPControl *ctrlp = NULL;
  219. char *cookiestr = sync_cookie2str(cookie);
  220. syncInfo = create_syncinfo_value(tag, cookiestr, (const char **)uuids);
  221. rc = slapi_send_ldap_intermediate( pb, &ctrlp, LDAP_SYNC_INFO, syncInfo);
  222. slapi_ch_free((void **)&cookiestr);
  223. ber_bvfree(syncInfo);
  224. return (rc);
  225. }
  226. int sync_result_msg (Slapi_PBlock *pb, Sync_Cookie *cookie)
  227. {
  228. int rc = 0;
  229. char *cookiestr = sync_cookie2str(cookie);
  230. LDAPControl **ctrl = (LDAPControl **)slapi_ch_calloc(2, sizeof (LDAPControl *));
  231. sync_create_sync_done_control( &ctrl[0], 0, cookiestr);
  232. slapi_pblock_set(pb, SLAPI_RESCONTROLS, ctrl);
  233. slapi_send_ldap_result(pb, 0, NULL, NULL, 0, NULL );
  234. slapi_ch_free((void **)&cookiestr);
  235. return (rc);
  236. }
  237. int sync_result_err (Slapi_PBlock *pb, int err, char *msg)
  238. {
  239. int rc = 0;
  240. slapi_send_ldap_result(pb, err, NULL, msg, 0, NULL );
  241. return (rc);
  242. }
  243. static struct berval *
  244. create_syncinfo_value( int type, const char *cookie, const char **uuids)
  245. {
  246. BerElement *ber;
  247. struct berval *bvp = NULL;
  248. if ( ( ber = der_alloc() ) == NULL ) {
  249. return( NULL );
  250. }
  251. switch (type) {
  252. case LDAP_TAG_SYNC_NEW_COOKIE:
  253. ber_printf(ber, "to", type, cookie);
  254. break;
  255. case LDAP_TAG_SYNC_REFRESH_DELETE:
  256. case LDAP_TAG_SYNC_REFRESH_PRESENT:
  257. ber_printf(ber, "t{", type);
  258. if (cookie)
  259. ber_printf(ber, "s", cookie);
  260. /* ber_printf(ber, "b",1); */
  261. ber_printf(ber, "}");
  262. break;
  263. case LDAP_TAG_SYNC_ID_SET:
  264. ber_printf(ber, "t{", type);
  265. if (cookie)
  266. ber_printf(ber, "s", cookie);
  267. if (uuids)
  268. ber_printf(ber, "b[v]", 1, uuids);
  269. ber_printf(ber, "}");
  270. break;
  271. default:
  272. break;
  273. }
  274. ber_flatten( ber, &bvp );
  275. ber_free( ber, 1 );
  276. return( bvp );
  277. }
  278. static int
  279. sync_handle_cnum_entry(Slapi_Entry *e, void *cb_data)
  280. {
  281. int rc = 0;
  282. Sync_CallBackData *cb = (Sync_CallBackData *)cb_data;
  283. Slapi_Value *sval=NULL;
  284. const struct berval *value;
  285. cb->changenr = 0;
  286. if ( NULL != e ) {
  287. Slapi_Attr *chattr = NULL;
  288. sval = NULL;
  289. value = NULL;
  290. if ( slapi_entry_attr_find( e, CL_ATTR_CHANGENUMBER, &chattr ) == 0 ) {
  291. slapi_attr_first_value( chattr,&sval );
  292. if ( NULL != sval ) {
  293. value = slapi_value_get_berval ( sval );
  294. if (value && value->bv_val && ('\0' != value->bv_val[0])) {
  295. cb->changenr = sync_number2ulong(value->bv_val);
  296. if (SYNC_INVALID_CHANGENUM != cb->changenr) {
  297. cb->cb_err = 0; /* changenr successfully set */
  298. }
  299. }
  300. }
  301. }
  302. }
  303. return (rc);
  304. }
  305. /*
  306. * a cookie is used to synchronize client server sessions,
  307. * it consist of three parts
  308. * -- server id, client should only sync with one server
  309. * -- client id, client should use same bind dn, and srch params
  310. * -- change info, kind of state info like csn, ruv,
  311. * in the first implementation use changenumber from retro cl
  312. *
  313. * syntax: <server-id>#<client-id>#change
  314. *
  315. */
  316. static char *
  317. sync_cookie_get_server_info(Slapi_PBlock *pb)
  318. {
  319. char *info_enc;
  320. int rc = 0;
  321. Slapi_Entry **entries;
  322. Slapi_PBlock *srch_pb = NULL;
  323. char *host = NULL;
  324. char *port = NULL;
  325. char *server_attrs[] = {"nsslapd-localhost", "nsslapd-port", NULL};
  326. srch_pb = slapi_pblock_new ();
  327. slapi_search_internal_set_pb (srch_pb, "cn=config", LDAP_SCOPE_BASE,
  328. "objectclass=*", server_attrs, 0, NULL, NULL,
  329. plugin_get_default_component_id(), 0);
  330. slapi_search_internal_pb (srch_pb);
  331. slapi_pblock_get(srch_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  332. if (rc != 0)
  333. {
  334. slapi_log_error(SLAPI_LOG_FATAL, SYNC_PLUGIN_SUBSYSTEM, "sync_cookie_get_server_info: "
  335. "unable to read server configuration: error %d\n", rc);
  336. }
  337. else
  338. {
  339. slapi_pblock_get(srch_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  340. if (NULL == entries || NULL == entries[0])
  341. {
  342. slapi_log_error(SLAPI_LOG_FATAL, SYNC_PLUGIN_SUBSYSTEM, "sync_cookie_get_server_info: "
  343. "server configuration missing\n");
  344. rc = -1;
  345. }
  346. else
  347. {
  348. host = slapi_entry_attr_get_charptr(entries[0], "nsslapd-localhost");
  349. port = slapi_entry_attr_get_charptr(entries[0], "nsslapd-port");
  350. }
  351. }
  352. info_enc = slapi_ch_smprintf("%s:%s",host?host:"nohost",port?port:"noport");
  353. slapi_ch_free ((void**)&host);
  354. slapi_ch_free ((void**)&port);
  355. slapi_free_search_results_internal(srch_pb);
  356. slapi_pblock_destroy (srch_pb);
  357. return(info_enc);
  358. }
  359. static char *
  360. sync_cookie_get_client_info(Slapi_PBlock *pb)
  361. {
  362. char *targetdn;
  363. char *strfilter;
  364. char *clientdn;
  365. char *clientinfo;
  366. slapi_pblock_get(pb, SLAPI_TARGET_DN, &targetdn);
  367. slapi_pblock_get(pb, SLAPI_SEARCH_STRFILTER, &strfilter);
  368. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &clientdn);
  369. clientinfo = slapi_ch_smprintf("%s:%s:%s",clientdn,targetdn,strfilter);
  370. return (clientinfo);
  371. }
  372. static unsigned long
  373. sync_cookie_get_change_number(int lastnr, const char *uniqueid)
  374. {
  375. Slapi_PBlock *srch_pb;
  376. Slapi_Entry **entries;
  377. Slapi_Entry *cl_entry;
  378. int rv;
  379. unsigned long newnr = SYNC_INVALID_CHANGENUM;
  380. char *filter = slapi_ch_smprintf("(&(changenumber>=%d)(targetuniqueid=%s))",lastnr,uniqueid);
  381. srch_pb = slapi_pblock_new();
  382. slapi_search_internal_set_pb(srch_pb, CL_SRCH_BASE, LDAP_SCOPE_SUBTREE, filter,
  383. NULL, 0, NULL, NULL, plugin_get_default_component_id(), 0);
  384. slapi_search_internal_pb(srch_pb);
  385. slapi_pblock_get(srch_pb, SLAPI_PLUGIN_INTOP_RESULT, &rv);
  386. if (rv == LDAP_SUCCESS) {
  387. slapi_pblock_get(srch_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  388. if (entries && *entries) {
  389. Slapi_Attr *attr;
  390. Slapi_Value *val;
  391. cl_entry = *entries; /* only use teh first one */
  392. slapi_entry_attr_find(cl_entry, CL_ATTR_CHANGENUMBER, &attr);
  393. slapi_attr_first_value(attr, &val);
  394. newnr = sync_number2ulong((char *)slapi_value_get_string(val));
  395. }
  396. }
  397. slapi_free_search_results_internal(srch_pb);
  398. slapi_pblock_destroy(srch_pb);
  399. slapi_ch_free((void **)&filter);
  400. return (newnr);
  401. }
  402. static int
  403. sync_cookie_get_change_info(Sync_CallBackData *scbd)
  404. {
  405. Slapi_PBlock *seq_pb;
  406. char *base;
  407. char *attrname;
  408. int rc;
  409. base = slapi_ch_strdup("cn=changelog");
  410. attrname = slapi_ch_strdup("changenumber");
  411. seq_pb = slapi_pblock_new();
  412. slapi_pblock_init(seq_pb);
  413. slapi_seq_internal_set_pb(seq_pb, base, SLAPI_SEQ_LAST, attrname, NULL, NULL, 0, 0,
  414. plugin_get_default_component_id(), 0);
  415. rc = slapi_seq_internal_callback_pb (seq_pb, scbd, NULL, sync_handle_cnum_entry, NULL);
  416. slapi_pblock_destroy(seq_pb);
  417. slapi_ch_free((void **)&attrname);
  418. slapi_ch_free((void **)&base);
  419. return (rc);
  420. }
  421. Sync_Cookie *
  422. sync_cookie_create (Slapi_PBlock *pb)
  423. {
  424. Sync_CallBackData scbd;
  425. int rc;
  426. Sync_Cookie *sc = (Sync_Cookie *)slapi_ch_calloc(1, sizeof(Sync_Cookie));
  427. scbd.cb_err = SYNC_CALLBACK_PREINIT;
  428. rc = sync_cookie_get_change_info (&scbd);
  429. if (rc == 0) {
  430. sc->cookie_server_signature = sync_cookie_get_server_info(pb);
  431. sc->cookie_client_signature = sync_cookie_get_client_info(pb);
  432. if (scbd.cb_err == SYNC_CALLBACK_PREINIT) {
  433. /* changenr is not initialized. */
  434. sc->cookie_change_info = 0;
  435. } else {
  436. sc->cookie_change_info = scbd.changenr;
  437. }
  438. } else {
  439. slapi_ch_free ((void **)&sc);
  440. sc = NULL;
  441. }
  442. return (sc);
  443. }
  444. void sync_cookie_update (Sync_Cookie *sc, Slapi_Entry *ec)
  445. {
  446. const char *uniqueid = NULL;
  447. Slapi_Attr *attr;
  448. Slapi_Value *val;
  449. slapi_entry_attr_find(ec, SLAPI_ATTR_UNIQUEID, &attr);
  450. slapi_attr_first_value(attr, &val);
  451. uniqueid = slapi_value_get_string(val);
  452. sc->cookie_change_info = sync_cookie_get_change_number (sc->cookie_change_info, uniqueid);
  453. }
  454. Sync_Cookie *
  455. sync_cookie_parse (char *cookie)
  456. {
  457. char *p, *q;
  458. Sync_Cookie *sc = NULL;
  459. if (cookie == NULL || *cookie == '\0' ) {
  460. return NULL;
  461. }
  462. /*
  463. * Format of cookie: server_signature#client_signature#change_info_number
  464. * If the cookie is malformed, NULL is returned.
  465. */
  466. p = q = cookie;
  467. p = strchr(q, '#');
  468. if (p) {
  469. *p = '\0';
  470. sc = (Sync_Cookie *)slapi_ch_calloc(1, sizeof(Sync_Cookie));
  471. sc->cookie_server_signature = slapi_ch_strdup(q);
  472. q = p + 1;
  473. p = strchr(q, '#');
  474. if (p) {
  475. *p = '\0';
  476. sc->cookie_client_signature = slapi_ch_strdup(q);
  477. sc->cookie_change_info = sync_number2ulong(p+1);
  478. if (SYNC_INVALID_CHANGENUM == sc->cookie_change_info) {
  479. goto error_return;
  480. }
  481. } else {
  482. goto error_return;
  483. }
  484. }
  485. return (sc);
  486. error_return:
  487. slapi_ch_free_string(&(sc->cookie_client_signature));
  488. slapi_ch_free_string(&(sc->cookie_server_signature));
  489. slapi_ch_free((void **)&sc);
  490. return NULL;
  491. }
  492. int
  493. sync_cookie_isvalid (Sync_Cookie *testcookie, Sync_Cookie *refcookie)
  494. {
  495. /* client and server info must match */
  496. if ((testcookie && refcookie) &&
  497. (strcmp(testcookie->cookie_client_signature,refcookie->cookie_client_signature) ||
  498. strcmp(testcookie->cookie_server_signature,refcookie->cookie_server_signature) ||
  499. testcookie->cookie_change_info == -1 ||
  500. testcookie->cookie_change_info > refcookie->cookie_change_info)) {
  501. return (0);
  502. }
  503. /* could add an additional check if the requested state in client cookie is still
  504. * available. Accept any state request for now.
  505. */
  506. return (1);
  507. }
  508. void
  509. sync_cookie_free (Sync_Cookie **freecookie)
  510. {
  511. if (*freecookie) {
  512. slapi_ch_free((void **)&((*freecookie)->cookie_client_signature));
  513. slapi_ch_free((void **)&((*freecookie)->cookie_server_signature));
  514. slapi_ch_free((void **)freecookie);
  515. }
  516. }
  517. int sync_is_active_scope (const Slapi_DN *dn, Slapi_PBlock *pb)
  518. {
  519. int rc;
  520. char *origbase = NULL;
  521. Slapi_DN *base = NULL;
  522. int scope;
  523. slapi_pblock_get( pb, SLAPI_ORIGINAL_TARGET_DN, &origbase );
  524. slapi_pblock_get( pb, SLAPI_SEARCH_TARGET_SDN, &base );
  525. slapi_pblock_get( pb, SLAPI_SEARCH_SCOPE, &scope );
  526. if (NULL == base) {
  527. base = slapi_sdn_new_dn_byref(origbase);
  528. slapi_pblock_set(pb, SLAPI_SEARCH_TARGET_SDN, base);
  529. }
  530. if ( slapi_sdn_scope_test(dn, base, scope )) {
  531. rc = 1;
  532. } else {
  533. rc = 0;
  534. }
  535. return (rc);
  536. }
  537. int sync_is_active (Slapi_Entry *e, Slapi_PBlock *pb)
  538. {
  539. if ( pb == NULL ) {
  540. /* not yet initialized */
  541. return(0);
  542. } else {
  543. /* check id entry is in scope of sync request */
  544. return(sync_is_active_scope( slapi_entry_get_sdn_const(e),pb));
  545. }
  546. }
  547. Slapi_PBlock *
  548. sync_pblock_copy(Slapi_PBlock *src)
  549. {
  550. Slapi_Operation *operation;
  551. Slapi_Operation *operation_new;
  552. Slapi_Connection *connection;
  553. int *scope;
  554. int *deref;
  555. int *filter_normalized;
  556. char *fstr;
  557. char **attrs, **attrs_dup;
  558. char **reqattrs, **reqattrs_dup;
  559. int *attrsonly;
  560. int *isroot;
  561. int *sizelimit;
  562. int *timelimit;
  563. struct slapdplugin *pi;
  564. ber_int_t msgid;
  565. ber_tag_t tag;
  566. slapi_pblock_get( src, SLAPI_OPERATION, &operation );
  567. slapi_pblock_get( src, SLAPI_CONNECTION, &connection );
  568. slapi_pblock_get( src, SLAPI_SEARCH_SCOPE, &scope );
  569. slapi_pblock_get( src, SLAPI_SEARCH_DEREF, &deref );
  570. slapi_pblock_get( src, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED, &filter_normalized );
  571. slapi_pblock_get( src, SLAPI_SEARCH_STRFILTER, &fstr );
  572. slapi_pblock_get( src, SLAPI_SEARCH_ATTRS, &attrs );
  573. slapi_pblock_get( src, SLAPI_SEARCH_REQATTRS, &reqattrs );
  574. slapi_pblock_get( src, SLAPI_SEARCH_ATTRSONLY, &attrsonly );
  575. slapi_pblock_get( src, SLAPI_REQUESTOR_ISROOT, &isroot );
  576. slapi_pblock_get( src, SLAPI_SEARCH_SIZELIMIT, &sizelimit );
  577. slapi_pblock_get( src, SLAPI_SEARCH_TIMELIMIT, &timelimit );
  578. slapi_pblock_get( src, SLAPI_PLUGIN, &pi);
  579. Slapi_PBlock *dest = slapi_pblock_new();
  580. operation_new = slapi_operation_new(0);
  581. msgid = slapi_operation_get_msgid(operation);
  582. slapi_operation_set_msgid(operation_new, msgid);
  583. tag = slapi_operation_get_tag(operation);
  584. slapi_operation_set_tag(operation_new, tag);
  585. slapi_pblock_set( dest, SLAPI_OPERATION, operation_new );
  586. slapi_pblock_set( dest, SLAPI_CONNECTION, connection );
  587. slapi_pblock_set( dest, SLAPI_SEARCH_SCOPE, &scope );
  588. slapi_pblock_set( dest, SLAPI_SEARCH_DEREF, &deref );
  589. slapi_pblock_set( dest, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED, &filter_normalized );
  590. slapi_pblock_set( dest, SLAPI_SEARCH_STRFILTER, slapi_ch_strdup(fstr) );
  591. attrs_dup = slapi_ch_array_dup(attrs);
  592. reqattrs_dup = slapi_ch_array_dup(reqattrs);
  593. slapi_pblock_set( dest, SLAPI_SEARCH_ATTRS, attrs_dup );
  594. slapi_pblock_set( dest, SLAPI_SEARCH_REQATTRS, reqattrs_dup );
  595. slapi_pblock_set( dest, SLAPI_SEARCH_ATTRSONLY, &attrsonly );
  596. slapi_pblock_set( dest, SLAPI_REQUESTOR_ISROOT, &isroot );
  597. slapi_pblock_set( dest, SLAPI_SEARCH_SIZELIMIT, &sizelimit );
  598. slapi_pblock_set( dest, SLAPI_SEARCH_TIMELIMIT, &timelimit );
  599. slapi_pblock_set( dest, SLAPI_PLUGIN, pi);
  600. return dest;
  601. }
  602. int
  603. sync_number2int(char *chgnrstr)
  604. {
  605. char *end;
  606. int nr;
  607. nr = (int)strtoul(chgnrstr, &end, 10);
  608. if ( *end == '\0') {
  609. return (nr);
  610. } else {
  611. return (-1);
  612. }
  613. }
  614. unsigned long
  615. sync_number2ulong(char *chgnrstr)
  616. {
  617. char *end;
  618. unsigned long nr;
  619. nr = strtoul(chgnrstr, &end, 10);
  620. if ( *end == '\0') {
  621. return (nr);
  622. } else {
  623. return SYNC_INVALID_CHANGENUM;
  624. }
  625. }