sync_util.c 18 KB

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