sync_util.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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. #include "slap.h" /* for LDAP_TAG_SK_REVERSE */
  10. static struct berval *create_syncinfo_value(int type, const char *cookie, struct berval **uuids);
  11. static char *sync_cookie_get_server_info(Slapi_PBlock *pb);
  12. static char *sync_cookie_get_client_info(Slapi_PBlock *pb);
  13. static void sync_ulong2olcsn(unsigned long chgnr, char *buf);
  14. static unsigned long sync_olcsn2ulong(char *csn);
  15. #define CSN_OFFSET 4102448461
  16. /*
  17. * Parse the value from an LDAPv3 sync request control. They look
  18. * like this:
  19. *
  20. * syncRequestValue ::= SEQUENCE {
  21. * mode ENUMERATED {
  22. * -- 0 unused
  23. * refreshOnly (1),
  24. * -- 2 reserved
  25. * refreshAndPersist (3)
  26. * },
  27. * cookie syncCookie OPTIONAL,
  28. * reloadHint BOOLEAN DEFAULT FALSE
  29. * }
  30. *
  31. * Return an LDAP error code (LDAP_SUCCESS if all goes well).
  32. *
  33. */
  34. int
  35. sync_parse_control_value(struct berval *psbvp, ber_int_t *mode, int *reload, char **cookie)
  36. {
  37. int rc = LDAP_SUCCESS;
  38. if (psbvp->bv_len == 0 || psbvp->bv_val == NULL) {
  39. rc = LDAP_PROTOCOL_ERROR;
  40. } else {
  41. BerElement *ber = ber_init(psbvp);
  42. if (ber == NULL) {
  43. rc = LDAP_OPERATIONS_ERROR;
  44. } else {
  45. if (ber_scanf(ber, "{e", mode) == LBER_ERROR) {
  46. rc = LDAP_PROTOCOL_ERROR;
  47. } else {
  48. ber_tag_t tag;
  49. ber_len_t len;
  50. tag = ber_peek_tag(ber, &len);
  51. if (tag == LDAP_TAG_SYNC_COOKIE) {
  52. rc = ber_scanf(ber, "a", cookie);
  53. tag = ber_peek_tag(ber, &len);
  54. }
  55. if (rc != LBER_ERROR && tag == LDAP_TAG_RELOAD_HINT) {
  56. rc = ber_scanf(ber, "b", reload);
  57. }
  58. if (rc != LBER_ERROR) {
  59. rc = ber_scanf(ber, "}");
  60. }
  61. if (rc == LBER_ERROR) {
  62. rc = LDAP_PROTOCOL_ERROR;
  63. };
  64. }
  65. /* the ber encoding is no longer needed */
  66. ber_free(ber, 1);
  67. }
  68. }
  69. return (rc);
  70. }
  71. char *
  72. sync_entryuuid2uuid(const char *entryuuid)
  73. {
  74. char *uuid;
  75. char u[17] = {0};
  76. u[0] = slapi_str_to_u8(entryuuid);
  77. u[1] = slapi_str_to_u8(entryuuid + 2);
  78. u[2] = slapi_str_to_u8(entryuuid + 4);
  79. u[3] = slapi_str_to_u8(entryuuid + 6);
  80. u[4] = slapi_str_to_u8(entryuuid + 9);
  81. u[5] = slapi_str_to_u8(entryuuid + 11);
  82. u[6] = slapi_str_to_u8(entryuuid + 14);
  83. u[7] = slapi_str_to_u8(entryuuid + 16);
  84. u[8] = slapi_str_to_u8(entryuuid + 19);
  85. u[9] = slapi_str_to_u8(entryuuid + 21);
  86. u[10] = slapi_str_to_u8(entryuuid + 24);
  87. u[11] = slapi_str_to_u8(entryuuid + 26);
  88. u[12] = slapi_str_to_u8(entryuuid + 28);
  89. u[13] = slapi_str_to_u8(entryuuid + 30);
  90. u[14] = slapi_str_to_u8(entryuuid + 32);
  91. u[15] = slapi_str_to_u8(entryuuid + 34);
  92. uuid = slapi_ch_malloc(sizeof(u));
  93. memcpy(uuid, u, sizeof(u));
  94. return (uuid);
  95. }
  96. char *
  97. sync_nsuniqueid2uuid(const char *nsuniqueid)
  98. {
  99. char *uuid;
  100. char u[17];
  101. u[0] = slapi_str_to_u8(nsuniqueid);
  102. u[1] = slapi_str_to_u8(nsuniqueid + 2);
  103. u[2] = slapi_str_to_u8(nsuniqueid + 4);
  104. u[3] = slapi_str_to_u8(nsuniqueid + 6);
  105. u[4] = slapi_str_to_u8(nsuniqueid + 9);
  106. u[5] = slapi_str_to_u8(nsuniqueid + 11);
  107. u[6] = slapi_str_to_u8(nsuniqueid + 13);
  108. u[7] = slapi_str_to_u8(nsuniqueid + 15);
  109. u[8] = slapi_str_to_u8(nsuniqueid + 18);
  110. u[9] = slapi_str_to_u8(nsuniqueid + 20);
  111. u[10] = slapi_str_to_u8(nsuniqueid + 22);
  112. u[11] = slapi_str_to_u8(nsuniqueid + 24);
  113. u[12] = slapi_str_to_u8(nsuniqueid + 27);
  114. u[13] = slapi_str_to_u8(nsuniqueid + 29);
  115. u[14] = slapi_str_to_u8(nsuniqueid + 31);
  116. u[15] = slapi_str_to_u8(nsuniqueid + 33);
  117. u[16] = '\0';
  118. uuid = slapi_ch_malloc(sizeof(u));
  119. memcpy(uuid, u, sizeof(u));
  120. return (uuid);
  121. }
  122. /*
  123. * syncStateValue ::= SEQUENCE {
  124. * state ENUMERATED {
  125. * present (0),
  126. * add (1),
  127. * modify (2),
  128. * delete (3)
  129. * },
  130. * entryUUID syncUUID,
  131. * cookie syncCookie OPTIONAL
  132. * }
  133. *
  134. */
  135. int
  136. sync_create_state_control(Slapi_Entry *e, LDAPControl **ctrlp, int type, Sync_Cookie *cookie, PRBool openldap_compat)
  137. {
  138. int rc;
  139. BerElement *ber;
  140. struct berval *bvp;
  141. char *uuid;
  142. Slapi_Attr *attr = NULL;
  143. Slapi_Value *val = NULL;
  144. if (type == LDAP_SYNC_NONE || ctrlp == NULL || (ber = der_alloc()) == NULL) {
  145. return (LDAP_OPERATIONS_ERROR);
  146. }
  147. *ctrlp = NULL;
  148. if (openldap_compat) {
  149. slapi_entry_attr_find(e, SLAPI_ATTR_ENTRYUUID, &attr);
  150. if (!attr) {
  151. /*
  152. * We can't proceed from here. We are in openldap mode, but some entries don't
  153. * have their UUID. This means that the tree could be corrupted on the openldap
  154. * server, so we have to stop now.
  155. */
  156. slapi_log_err(SLAPI_LOG_ERR, SYNC_PLUGIN_SUBSYSTEM,
  157. "sync_create_state_control - Some entries are missing entryUUID. Unable to proceed. You may need to re-run the entryuuid fixup\n");
  158. return (LDAP_OPERATIONS_ERROR);
  159. }
  160. slapi_attr_first_value(attr, &val);
  161. uuid = sync_entryuuid2uuid(slapi_value_get_string(val));
  162. } else {
  163. slapi_entry_attr_find(e, SLAPI_ATTR_UNIQUEID, &attr);
  164. slapi_attr_first_value(attr, &val);
  165. if ((attr == NULL) || (val == NULL)) {
  166. /* It may happen with entries in special backends
  167. * such like cn=config, cn=shema, cn=monitor...
  168. */
  169. slapi_log_err(SLAPI_LOG_ERR, SYNC_PLUGIN_SUBSYSTEM,
  170. "sync_create_state_control - Entries are missing nsuniqueid. Unable to proceed.\n");
  171. return (LDAP_OPERATIONS_ERROR);
  172. }
  173. uuid = sync_nsuniqueid2uuid(slapi_value_get_string(val));
  174. }
  175. if ((rc = ber_printf(ber, "{eo", type, uuid, 16)) != -1) {
  176. if (cookie) {
  177. char *cookiestr = sync_cookie2str(cookie);
  178. rc = ber_printf(ber, "s}", cookiestr);
  179. slapi_ch_free((void **)&cookiestr);
  180. } else {
  181. rc = ber_printf(ber, "}");
  182. }
  183. }
  184. if (rc != -1) {
  185. rc = ber_flatten(ber, &bvp);
  186. }
  187. ber_free(ber, 1);
  188. slapi_ch_free((void **)&uuid);
  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_STATE);
  195. (*ctrlp)->ldctl_value = *bvp; /* struct copy */
  196. bvp->bv_val = NULL;
  197. ber_bvfree(bvp);
  198. return (LDAP_SUCCESS);
  199. }
  200. /*
  201. * syncDoneValue ::= SEQUENCE {
  202. * cookie syncCookie OPTIONAL
  203. * refreshDeletes BOOLEAN DEFAULT FALSE
  204. * }
  205. *
  206. */
  207. int
  208. sync_create_sync_done_control(LDAPControl **ctrlp, int refresh, char *cookie)
  209. {
  210. int rc;
  211. BerElement *ber;
  212. struct berval *bvp;
  213. if (ctrlp == NULL || (ber = der_alloc()) == NULL) {
  214. return (LDAP_OPERATIONS_ERROR);
  215. }
  216. *ctrlp = NULL;
  217. if (cookie) {
  218. if ((rc = ber_printf(ber, "{s", cookie)) != -1) {
  219. if (refresh) {
  220. rc = ber_printf(ber, "b}", refresh);
  221. } else {
  222. rc = ber_printf(ber, "}");
  223. }
  224. }
  225. } else {
  226. if (refresh) {
  227. rc = ber_printf(ber, "{b}", refresh);
  228. } else {
  229. rc = ber_printf(ber, "{}");
  230. }
  231. }
  232. if (rc != -1) {
  233. rc = ber_flatten(ber, &bvp);
  234. }
  235. ber_free(ber, 1);
  236. if (rc == -1) {
  237. return (LDAP_OPERATIONS_ERROR);
  238. }
  239. *ctrlp = (LDAPControl *)slapi_ch_malloc(sizeof(LDAPControl));
  240. (*ctrlp)->ldctl_iscritical = 0;
  241. (*ctrlp)->ldctl_oid = slapi_ch_strdup(LDAP_CONTROL_SYNC_DONE);
  242. (*ctrlp)->ldctl_value = *bvp; /* struct copy */
  243. bvp->bv_val = NULL;
  244. ber_bvfree(bvp);
  245. return (LDAP_SUCCESS);
  246. }
  247. char *
  248. sync_cookie2str(Sync_Cookie *cookie)
  249. {
  250. char *cookiestr = NULL;
  251. if (cookie) {
  252. if (cookie->openldap_compat) {
  253. char buf[16] = {0};
  254. sync_ulong2olcsn(cookie->cookie_change_info, buf);
  255. cookiestr = slapi_ch_smprintf("%s,csn=%s.000000Z#000000#000#000000",
  256. cookie->cookie_client_signature,
  257. buf);
  258. } else {
  259. cookiestr = slapi_ch_smprintf("%s#%s#%lu",
  260. cookie->cookie_server_signature,
  261. cookie->cookie_client_signature,
  262. cookie->cookie_change_info);
  263. }
  264. }
  265. return (cookiestr);
  266. }
  267. int
  268. sync_intermediate_msg(Slapi_PBlock *pb, int tag, Sync_Cookie *cookie, struct berval **uuids)
  269. {
  270. int rc;
  271. struct berval *syncInfo;
  272. LDAPControl *ctrlp = NULL;
  273. char *cookiestr = sync_cookie2str(cookie);
  274. syncInfo = create_syncinfo_value(tag, cookiestr, uuids);
  275. rc = slapi_send_ldap_intermediate(pb, &ctrlp, LDAP_SYNC_INFO, syncInfo);
  276. slapi_ch_free((void **)&cookiestr);
  277. ber_bvfree(syncInfo);
  278. return (rc);
  279. }
  280. int
  281. sync_result_msg(Slapi_PBlock *pb, Sync_Cookie *cookie)
  282. {
  283. int rc = 0;
  284. char *cookiestr = sync_cookie2str(cookie);
  285. LDAPControl **ctrl = (LDAPControl **)slapi_ch_calloc(2, sizeof(LDAPControl *));
  286. if (cookie && cookie->openldap_compat) {
  287. sync_create_sync_done_control(&ctrl[0], 1, cookiestr);
  288. } else {
  289. sync_create_sync_done_control(&ctrl[0], 0, cookiestr);
  290. }
  291. slapi_pblock_set(pb, SLAPI_RESCONTROLS, ctrl);
  292. slapi_send_ldap_result(pb, 0, NULL, NULL, 0, NULL);
  293. slapi_ch_free((void **)&cookiestr);
  294. return (rc);
  295. }
  296. int
  297. sync_result_err(Slapi_PBlock *pb, int err, char *msg)
  298. {
  299. int rc = 0;
  300. slapi_send_ldap_result(pb, err, NULL, msg, 0, NULL);
  301. return (rc);
  302. }
  303. static struct berval *
  304. create_syncinfo_value(int type, const char *cookie, struct berval **uuids)
  305. {
  306. BerElement *ber;
  307. struct berval *bvp = NULL;
  308. if ((ber = der_alloc()) == NULL) {
  309. return (NULL);
  310. }
  311. /*
  312. * ber_tag_t is an unsigned integer of at least 32 bits
  313. * used to represent a BER tag. It is commonly equivalent
  314. * to a unsigned long.
  315. * ...
  316. * ber_printf(...)
  317. * t
  318. * Tag of the next element. A pointer to a ber_tag_t should be supplied.
  319. */
  320. ber_tag_t btag = (ber_tag_t)type;
  321. switch (type) {
  322. case LDAP_TAG_SYNC_NEW_COOKIE:
  323. ber_printf(ber, "to", btag, cookie);
  324. break;
  325. case LDAP_TAG_SYNC_REFRESH_DELETE:
  326. case LDAP_TAG_SYNC_REFRESH_PRESENT:
  327. ber_printf(ber, "t{", btag);
  328. if (cookie) {
  329. ber_printf(ber, "s", cookie);
  330. }
  331. /* ber_printf(ber, "b",1); */
  332. ber_printf(ber, "}");
  333. break;
  334. case LDAP_TAG_SYNC_ID_SET:
  335. ber_printf(ber, "t{", btag);
  336. if (cookie) {
  337. ber_printf(ber, "s", cookie);
  338. }
  339. if (uuids) {
  340. ber_printf(ber, "b[V]", 1, uuids);
  341. }
  342. ber_printf(ber, "}");
  343. break;
  344. default:
  345. break;
  346. }
  347. ber_flatten(ber, &bvp);
  348. ber_free(ber, 1);
  349. return (bvp);
  350. }
  351. static int
  352. sync_handle_cnum_entry(Slapi_Entry *e, void *cb_data)
  353. {
  354. int rc = 0;
  355. Sync_CallBackData *cb = (Sync_CallBackData *)cb_data;
  356. Slapi_Value *sval = NULL;
  357. const struct berval *value;
  358. cb->changenr = 0;
  359. if (NULL != e) {
  360. Slapi_Attr *chattr = NULL;
  361. sval = NULL;
  362. value = NULL;
  363. if (slapi_entry_attr_find(e, CL_ATTR_CHANGENUMBER, &chattr) == 0) {
  364. slapi_attr_first_value(chattr, &sval);
  365. if (NULL != sval) {
  366. value = slapi_value_get_berval(sval);
  367. if (value && value->bv_val && ('\0' != value->bv_val[0])) {
  368. cb->changenr = sync_number2ulong(value->bv_val);
  369. if (SYNC_INVALID_CHANGENUM != cb->changenr) {
  370. cb->cb_err = 0; /* changenr successfully set */
  371. }
  372. }
  373. }
  374. }
  375. }
  376. return (rc);
  377. }
  378. /*
  379. * a cookie is used to synchronize client server sessions,
  380. * it consist of three parts
  381. * -- server id, client should only sync with one server
  382. * -- client id, client should use same bind dn, and srch params
  383. * -- change info, kind of state info like csn, ruv,
  384. * in the first implementation use changenumber from retro cl
  385. *
  386. * syntax: <server-id>#<client-id>#change
  387. *
  388. */
  389. static char *
  390. sync_cookie_get_server_info(Slapi_PBlock *pb __attribute__((unused)))
  391. {
  392. char *info_enc;
  393. int rc = 0;
  394. Slapi_Entry **entries;
  395. Slapi_PBlock *srch_pb = NULL;
  396. const char *host = NULL;
  397. const char *port = NULL;
  398. char *server_attrs[] = {"nsslapd-localhost", "nsslapd-port", NULL};
  399. srch_pb = slapi_pblock_new();
  400. slapi_search_internal_set_pb(srch_pb, "cn=config", LDAP_SCOPE_BASE,
  401. "objectclass=*", server_attrs, 0, NULL, NULL,
  402. plugin_get_default_component_id(), 0);
  403. slapi_search_internal_pb(srch_pb);
  404. slapi_pblock_get(srch_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  405. if (rc != 0) {
  406. slapi_log_err(SLAPI_LOG_ERR, SYNC_PLUGIN_SUBSYSTEM, "sync_cookie_get_server_info - "
  407. "Unable to read server configuration: error %d\n",
  408. rc);
  409. } else {
  410. slapi_pblock_get(srch_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  411. if (NULL == entries || NULL == entries[0]) {
  412. slapi_log_err(SLAPI_LOG_ERR, SYNC_PLUGIN_SUBSYSTEM, "sync_cookie_get_server_info -"
  413. "Server configuration missing\n");
  414. rc = -1;
  415. } else {
  416. host = slapi_entry_attr_get_ref(entries[0], "nsslapd-localhost");
  417. port = slapi_entry_attr_get_ref(entries[0], "nsslapd-port");
  418. }
  419. }
  420. info_enc = slapi_ch_smprintf("%s:%s", host ? host : "nohost", port ? port : "noport");
  421. slapi_free_search_results_internal(srch_pb);
  422. slapi_pblock_destroy(srch_pb);
  423. return (info_enc);
  424. }
  425. static char *
  426. sync_cookie_get_client_info(Slapi_PBlock *pb)
  427. {
  428. char *targetdn;
  429. char *strfilter;
  430. char *clientdn;
  431. char *clientinfo;
  432. slapi_pblock_get(pb, SLAPI_TARGET_DN, &targetdn);
  433. slapi_pblock_get(pb, SLAPI_SEARCH_STRFILTER, &strfilter);
  434. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &clientdn);
  435. clientinfo = slapi_ch_smprintf("%s:%s:%s", clientdn, targetdn, strfilter);
  436. return (clientinfo);
  437. }
  438. /* This is used with internal search that reverse the order
  439. * of returned entries. So to get
  440. */
  441. static LDAPControl *
  442. sync_build_sort_control(const char *attr)
  443. {
  444. LDAPControl *ctrl;
  445. BerElement *ber;
  446. int rc;
  447. ber = ber_alloc();
  448. if (NULL == ber)
  449. return NULL;
  450. rc = ber_printf(ber, "{{stb}}", attr, LDAP_TAG_SK_REVERSE, 1);
  451. if (-1 == rc) {
  452. ber_free(ber, 1);
  453. return NULL;
  454. }
  455. rc = slapi_build_control(LDAP_CONTROL_SORTREQUEST, ber, 1, &ctrl);
  456. ber_free(ber, 1);
  457. if (LDAP_SUCCESS != rc)
  458. return NULL;
  459. return ctrl;
  460. }
  461. static unsigned long
  462. sync_cookie_get_change_number(int lastnr, const char *uniqueid)
  463. {
  464. Slapi_PBlock *srch_pb;
  465. Slapi_Entry **entries;
  466. Slapi_Entry *cl_entry;
  467. int rv;
  468. unsigned long newnr = SYNC_INVALID_CHANGENUM;
  469. LDAPControl **ctrls = NULL;
  470. ctrls = (LDAPControl **)slapi_ch_calloc(2, sizeof(LDAPControl *));
  471. char *filter = slapi_ch_smprintf("(&(changenumber>=%d)(targetuniqueid=%s))", lastnr + 1, uniqueid);
  472. ctrls[0] = sync_build_sort_control("changenumber");
  473. srch_pb = slapi_pblock_new();
  474. slapi_search_internal_set_pb(srch_pb, CL_SRCH_BASE, LDAP_SCOPE_SUBTREE, filter,
  475. NULL, 0, ctrls, NULL, plugin_get_default_component_id(), 0);
  476. slapi_search_internal_pb(srch_pb);
  477. slapi_pblock_get(srch_pb, SLAPI_PLUGIN_INTOP_RESULT, &rv);
  478. if (rv == LDAP_SUCCESS) {
  479. slapi_pblock_get(srch_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  480. if (entries && *entries) {
  481. Slapi_Attr *attr;
  482. Slapi_Value *val;
  483. cl_entry = *entries; /* only use teh first one */
  484. slapi_entry_attr_find(cl_entry, CL_ATTR_CHANGENUMBER, &attr);
  485. slapi_attr_first_value(attr, &val);
  486. newnr = sync_number2ulong((char *)slapi_value_get_string(val));
  487. }
  488. #if DEBUG
  489. slapi_log_err(SLAPI_LOG_PLUGIN, SYNC_PLUGIN_SUBSYSTEM, "sync_cookie_get_change_number looks for \"%s\"\n",
  490. filter);
  491. for (size_t i = 0; entries[i]; i++) {
  492. Slapi_Attr *attr;
  493. Slapi_Value *val;
  494. char *entrydn;
  495. unsigned long nr;
  496. slapi_entry_attr_find(entries[i], CL_ATTR_ENTRYDN, &attr);
  497. slapi_attr_first_value(attr, &val);
  498. entrydn = (char *)slapi_value_get_string(val);
  499. slapi_entry_attr_find(entries[i], CL_ATTR_CHANGENUMBER, &attr);
  500. slapi_attr_first_value(attr, &val);
  501. nr = sync_number2ulong((char *)slapi_value_get_string(val));
  502. slapi_log_err(SLAPI_LOG_PLUGIN, SYNC_PLUGIN_SUBSYSTEM, "sync_cookie_get_change_number after %d: %d %s\n",
  503. lastnr, (int) nr, entrydn);
  504. }
  505. #endif
  506. }
  507. slapi_free_search_results_internal(srch_pb);
  508. slapi_pblock_destroy(srch_pb);
  509. slapi_ch_free((void **)&filter);
  510. return (newnr);
  511. }
  512. static int
  513. sync_cookie_get_change_info(Sync_CallBackData *scbd)
  514. {
  515. Slapi_PBlock *seq_pb;
  516. char *base;
  517. char *attrname;
  518. int rc;
  519. base = slapi_ch_strdup("cn=changelog");
  520. attrname = slapi_ch_strdup("changenumber");
  521. seq_pb = slapi_pblock_new();
  522. slapi_pblock_init(seq_pb);
  523. slapi_seq_internal_set_pb(seq_pb, base, SLAPI_SEQ_LAST, attrname, NULL, NULL, 0, 0,
  524. plugin_get_default_component_id(), 0);
  525. rc = slapi_seq_internal_callback_pb(seq_pb, scbd, NULL, sync_handle_cnum_entry, NULL);
  526. slapi_pblock_destroy(seq_pb);
  527. slapi_ch_free((void **)&attrname);
  528. slapi_ch_free((void **)&base);
  529. return (rc);
  530. }
  531. Sync_Cookie *
  532. sync_cookie_create(Slapi_PBlock *pb, Sync_Cookie *client_cookie)
  533. {
  534. Sync_CallBackData scbd = {0};
  535. int rc = 0;
  536. Sync_Cookie *sc = (Sync_Cookie *)slapi_ch_calloc(1, sizeof(Sync_Cookie));
  537. scbd.cb_err = SYNC_CALLBACK_PREINIT;
  538. rc = sync_cookie_get_change_info(&scbd);
  539. if (rc == 0) {
  540. /* If the client is in openldap compat, we need to generate the same. */
  541. if (client_cookie && client_cookie->openldap_compat) {
  542. sc->openldap_compat = client_cookie->openldap_compat;
  543. sc->cookie_client_signature = slapi_ch_strdup(client_cookie->cookie_client_signature);
  544. sc->cookie_server_signature = NULL;
  545. } else {
  546. sc->openldap_compat = PR_FALSE;
  547. sc->cookie_server_signature = sync_cookie_get_server_info(pb);
  548. sc->cookie_client_signature = sync_cookie_get_client_info(pb);
  549. }
  550. if (scbd.cb_err == SYNC_CALLBACK_PREINIT) {
  551. /* changenr is not initialized. */
  552. sc->cookie_change_info = 0;
  553. } else {
  554. sc->cookie_change_info = scbd.changenr;
  555. }
  556. } else {
  557. slapi_ch_free((void **)&sc);
  558. sc = NULL;
  559. }
  560. return (sc);
  561. }
  562. void
  563. sync_cookie_update(Sync_Cookie *sc, Slapi_Entry *ec)
  564. {
  565. const char *uniqueid = NULL;
  566. Slapi_Attr *attr;
  567. Slapi_Value *val;
  568. slapi_entry_attr_find(ec, SLAPI_ATTR_UNIQUEID, &attr);
  569. slapi_attr_first_value(attr, &val);
  570. uniqueid = slapi_value_get_string(val);
  571. sc->cookie_change_info = sync_cookie_get_change_number(sc->cookie_change_info, uniqueid);
  572. }
  573. Sync_Cookie *
  574. sync_cookie_parse(char *cookie, PRBool *cookie_refresh, PRBool *allow_openldap_compat)
  575. {
  576. char *p = NULL;
  577. char *q = NULL;
  578. Sync_Cookie *sc = NULL;
  579. /* This is an rfc compliant initial refresh request */
  580. if (cookie == NULL || *cookie == '\0') {
  581. *cookie_refresh = PR_TRUE;
  582. return NULL;
  583. }
  584. /* get ready to parse. */
  585. p = q = cookie;
  586. sc = (Sync_Cookie *)slapi_ch_calloc(1, sizeof(Sync_Cookie));
  587. if (strncmp(cookie, "rid=", 4) == 0) {
  588. if (*allow_openldap_compat != PR_TRUE) {
  589. slapi_log_err(SLAPI_LOG_ERR, SYNC_PLUGIN_SUBSYSTEM, "sync_cookie_parse - An openldap sync request was made, but " SYNC_ALLOW_OPENLDAP_COMPAT " is false\n");
  590. slapi_log_err(SLAPI_LOG_ERR, SYNC_PLUGIN_SUBSYSTEM, "sync_cookie_parse - To enable this run 'dsconf <instance> plugin contentsync set --allow-openldap on'\n");
  591. goto error_return;
  592. }
  593. /*
  594. * We are in openldap mode.
  595. * The cookies are:
  596. * rid=123,csn=20200525051329.534174Z#000000#000#000000
  597. */
  598. sc->openldap_compat = PR_TRUE;
  599. p = strchr(q, ',');
  600. if (p == NULL) {
  601. /* No CSN following the rid, must be an init request. */
  602. *cookie_refresh = PR_TRUE;
  603. /* We need to keep the client rid though */
  604. sc->cookie_client_signature = slapi_ch_strdup(q);
  605. /* server sig and change info do not need to be set. */
  606. sc->cookie_server_signature = NULL;
  607. sc->cookie_change_info = 0;
  608. } else {
  609. /* Ensure that this really is a csn= */
  610. if (strncmp(p, ",csn=", 5) != 0) {
  611. /* Yeah nahhhhhhh */
  612. goto error_return;
  613. }
  614. /* We dont care about the remainder after the . */
  615. if (strlen(p) < 20) {
  616. /* Probably a corrupt CSN. We need at least 20 chars. */
  617. goto error_return;
  618. }
  619. /*
  620. * Replace the , with a '\0' This makes q -> p a str of the rid.
  621. * rid=123,csn=19700101001640.000000Z#000000#000#000000
  622. * ^ ^
  623. * q p
  624. * rid=123\0csn=19700101001640.000000Z#000000#000#000000
  625. */
  626. PR_ASSERT(p[0] == ',');
  627. p[0] = '\0';
  628. /*
  629. * Now terminate the ulong which is our change num so we can parse it.
  630. * rid=123\0csn=19700101001640.000000Z#000000#000#000000
  631. * ^ ^ ^
  632. * q p[0] p[19]
  633. * rid=123\0csn=19700101001640\0...
  634. */
  635. PR_ASSERT(p[19] == '.');
  636. p[19] = '\0';
  637. /*
  638. * And move the pointer up to the start of the int we need to parse.
  639. * rid=123\0csn=19700101001640\0...
  640. * ^ ^
  641. * q p +5 -->
  642. * rid=123\0csn=19700101001640\0...
  643. * ^ ^
  644. * q p
  645. */
  646. p = p + 5;
  647. PR_ASSERT(strlen(p) == 14);
  648. /* We are now ready to parse the csn and create a cookie! */
  649. sc->cookie_client_signature = slapi_ch_strdup(q);
  650. sc->cookie_server_signature = NULL;
  651. /* Get the change number from the string */
  652. sc->cookie_change_info = sync_olcsn2ulong(p);
  653. if (SYNC_INVALID_CHANGENUM == sc->cookie_change_info) {
  654. /* Sad trombone */
  655. goto error_return;
  656. }
  657. /* Done! 🎉 */
  658. }
  659. } else {
  660. /*
  661. * Format of the 389 cookie: server_signature#client_signature#change_info_number
  662. * If the cookie is malformed, NULL is returned.
  663. */
  664. p = strchr(q, '#');
  665. if (p) {
  666. *p = '\0';
  667. sc->cookie_server_signature = slapi_ch_strdup(q);
  668. q = p + 1;
  669. p = strchr(q, '#');
  670. if (p) {
  671. *p = '\0';
  672. sc->cookie_client_signature = slapi_ch_strdup(q);
  673. sc->cookie_change_info = sync_number2ulong(p + 1);
  674. if (SYNC_INVALID_CHANGENUM == sc->cookie_change_info) {
  675. goto error_return;
  676. }
  677. } else {
  678. goto error_return;
  679. }
  680. } else {
  681. goto error_return;
  682. }
  683. }
  684. return (sc);
  685. error_return:
  686. slapi_ch_free_string(&(sc->cookie_client_signature));
  687. slapi_ch_free_string(&(sc->cookie_server_signature));
  688. slapi_ch_free((void **)&sc);
  689. return NULL;
  690. }
  691. int
  692. sync_cookie_isvalid(Sync_Cookie *testcookie, Sync_Cookie *refcookie)
  693. {
  694. /* client and server info must match */
  695. if (testcookie == NULL || refcookie == NULL) {
  696. return 0;
  697. }
  698. if ((testcookie->openldap_compat != refcookie->openldap_compat ||
  699. strcmp(testcookie->cookie_client_signature, refcookie->cookie_client_signature) ||
  700. testcookie->cookie_change_info == -1 ||
  701. testcookie->cookie_change_info > refcookie->cookie_change_info)) {
  702. return 0;
  703. }
  704. if (refcookie->openldap_compat) {
  705. if (testcookie->cookie_server_signature != NULL ||
  706. refcookie->cookie_server_signature != NULL) {
  707. return 0;
  708. }
  709. } else {
  710. if (strcmp(testcookie->cookie_server_signature, refcookie->cookie_server_signature)) {
  711. return 0;
  712. }
  713. }
  714. /* could add an additional check if the requested state in client cookie is still
  715. * available. Accept any state request for now.
  716. */
  717. return 1;
  718. }
  719. void
  720. sync_cookie_free(Sync_Cookie **freecookie)
  721. {
  722. if (*freecookie) {
  723. slapi_ch_free((void **)&((*freecookie)->cookie_client_signature));
  724. slapi_ch_free((void **)&((*freecookie)->cookie_server_signature));
  725. slapi_ch_free((void **)freecookie);
  726. }
  727. }
  728. int
  729. sync_is_active_scope(const Slapi_DN *dn, Slapi_PBlock *pb)
  730. {
  731. int rc;
  732. char *origbase = NULL;
  733. Slapi_DN *base = NULL;
  734. int scope;
  735. slapi_pblock_get(pb, SLAPI_ORIGINAL_TARGET_DN, &origbase);
  736. slapi_pblock_get(pb, SLAPI_SEARCH_TARGET_SDN, &base);
  737. slapi_pblock_get(pb, SLAPI_SEARCH_SCOPE, &scope);
  738. if (NULL == base) {
  739. base = slapi_sdn_new_dn_byref(origbase);
  740. slapi_pblock_set(pb, SLAPI_SEARCH_TARGET_SDN, base);
  741. }
  742. if (slapi_sdn_scope_test(dn, base, scope)) {
  743. rc = 1;
  744. } else {
  745. rc = 0;
  746. }
  747. return (rc);
  748. }
  749. int
  750. sync_is_active(Slapi_Entry *e, Slapi_PBlock *pb)
  751. {
  752. if (pb == NULL) {
  753. /* not yet initialized */
  754. return (0);
  755. } else {
  756. /* check id entry is in scope of sync request */
  757. return (sync_is_active_scope(slapi_entry_get_sdn_const(e), pb));
  758. }
  759. }
  760. Slapi_PBlock *
  761. sync_pblock_copy(Slapi_PBlock *src)
  762. {
  763. Slapi_Operation *operation;
  764. Slapi_Operation *operation_new;
  765. Slapi_Connection *connection;
  766. Slapi_Backend *be = NULL;
  767. LDAPControl **ctrls = NULL;
  768. int *scope;
  769. int *deref;
  770. int *filter_normalized;
  771. char *fstr;
  772. char **attrs, **attrs_dup;
  773. char **reqattrs, **reqattrs_dup;
  774. int *attrsonly;
  775. int *isroot;
  776. int *sizelimit;
  777. int *timelimit;
  778. struct slapdplugin *pi;
  779. char *requestor_dn = NULL;
  780. ber_int_t msgid;
  781. ber_tag_t tag;
  782. slapi_pblock_get(src, SLAPI_OPERATION, &operation);
  783. slapi_pblock_get(src, SLAPI_CONNECTION, &connection);
  784. slapi_pblock_get(src, SLAPI_BACKEND, &be);
  785. slapi_pblock_get(src, SLAPI_SEARCH_SCOPE, &scope);
  786. slapi_pblock_get(src, SLAPI_SEARCH_DEREF, &deref);
  787. slapi_pblock_get(src, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED, &filter_normalized);
  788. slapi_pblock_get(src, SLAPI_SEARCH_STRFILTER, &fstr);
  789. slapi_pblock_get(src, SLAPI_SEARCH_ATTRS, &attrs);
  790. slapi_pblock_get(src, SLAPI_SEARCH_REQATTRS, &reqattrs);
  791. slapi_pblock_get(src, SLAPI_SEARCH_ATTRSONLY, &attrsonly);
  792. slapi_pblock_get(src, SLAPI_REQUESTOR_ISROOT, &isroot);
  793. slapi_pblock_get(src, SLAPI_REQUESTOR_DN, &requestor_dn);
  794. slapi_pblock_get(src, SLAPI_SEARCH_SIZELIMIT, &sizelimit);
  795. slapi_pblock_get(src, SLAPI_SEARCH_TIMELIMIT, &timelimit);
  796. slapi_pblock_get(src, SLAPI_REQCONTROLS, &ctrls);
  797. slapi_pblock_get(src, SLAPI_PLUGIN, &pi);
  798. Slapi_PBlock *dest = slapi_pblock_new();
  799. operation_new = slapi_operation_new(0);
  800. msgid = slapi_operation_get_msgid(operation);
  801. slapi_operation_set_msgid(operation_new, msgid);
  802. tag = slapi_operation_get_tag(operation);
  803. slapi_operation_set_tag(operation_new, tag);
  804. operation_new->o_extension = factory_create_extension(get_operation_object_type(), operation_new, connection);
  805. slapi_pblock_set(dest, SLAPI_OPERATION, operation_new);
  806. slapi_pblock_set(dest, SLAPI_CONNECTION, connection);
  807. slapi_pblock_set(dest, SLAPI_BACKEND, be);
  808. slapi_pblock_set(dest, SLAPI_SEARCH_SCOPE, &scope);
  809. slapi_pblock_set(dest, SLAPI_SEARCH_DEREF, &deref);
  810. slapi_pblock_set(dest, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED, &filter_normalized);
  811. slapi_pblock_set(dest, SLAPI_SEARCH_STRFILTER, slapi_ch_strdup(fstr));
  812. attrs_dup = slapi_ch_array_dup(attrs);
  813. reqattrs_dup = slapi_ch_array_dup(reqattrs);
  814. slapi_pblock_set(dest, SLAPI_SEARCH_ATTRS, attrs_dup);
  815. slapi_pblock_set(dest, SLAPI_SEARCH_REQATTRS, reqattrs_dup);
  816. slapi_pblock_set(dest, SLAPI_SEARCH_ATTRSONLY, &attrsonly);
  817. slapi_pblock_set(dest, SLAPI_REQUESTOR_ISROOT, &isroot);
  818. slapi_pblock_set(dest, SLAPI_REQUESTOR_DN, requestor_dn);
  819. slapi_pblock_set(dest, SLAPI_SEARCH_SIZELIMIT, &sizelimit);
  820. slapi_pblock_set(dest, SLAPI_SEARCH_TIMELIMIT, &timelimit);
  821. slapi_pblock_set(dest, SLAPI_REQCONTROLS, ctrls);
  822. slapi_pblock_set(dest, SLAPI_PLUGIN, pi);
  823. return dest;
  824. }
  825. int
  826. sync_number2int(char *chgnrstr)
  827. {
  828. char *end;
  829. int nr;
  830. nr = (int)strtoul(chgnrstr, &end, 10);
  831. if (*end == '\0') {
  832. return (nr);
  833. } else {
  834. return (-1);
  835. }
  836. }
  837. unsigned long
  838. sync_number2ulong(char *chgnrstr)
  839. {
  840. char *end;
  841. unsigned long nr;
  842. nr = strtoul(chgnrstr, &end, 10);
  843. if (*end == '\0') {
  844. return (nr);
  845. } else {
  846. return SYNC_INVALID_CHANGENUM;
  847. }
  848. }
  849. /*
  850. * Why is there a CSN offset?
  851. *
  852. * CSN offset is to bump our csn date to a future time so that
  853. * we always beat openldap in conflicts. I can only hope that
  854. * in 100 years this code is dead, buried, for no one to see
  855. * again. If you are reading this in 2100, William of 2020
  856. * says "I'm so very sorry".
  857. */
  858. static unsigned long
  859. sync_olcsn2ulong(char *csn) {
  860. struct tm pt = {0};
  861. char *ret = strptime(csn, "%Y%m%d%H%M%S", &pt);
  862. PR_ASSERT(ret);
  863. if (ret == NULL) {
  864. return SYNC_INVALID_CHANGENUM;
  865. }
  866. time_t pepoch = mktime(&pt);
  867. unsigned long px = (unsigned long)pepoch;
  868. PR_ASSERT(px >= CSN_OFFSET);
  869. if (px < CSN_OFFSET) {
  870. return SYNC_INVALID_CHANGENUM;
  871. }
  872. return px - CSN_OFFSET;
  873. }
  874. static void
  875. sync_ulong2olcsn(unsigned long chgnr, char *buf) {
  876. PR_ASSERT(buf);
  877. unsigned long x = chgnr + CSN_OFFSET;
  878. time_t epoch = x;
  879. struct tm t = {0};
  880. localtime_r(&epoch, &t);
  881. strftime(buf, 15, "%Y%m%d%H%M%S", &t);
  882. }