windows_private.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* windows_private.c */
  42. #include "repl.h"
  43. #include "repl5.h"
  44. #include "slap.h"
  45. #include "slapi-plugin.h"
  46. #include "windowsrepl.h"
  47. struct windowsprivate {
  48. Slapi_DN *windows_subtree; /* DN of synchronized subtree (on the windows side) */
  49. Slapi_DN *directory_subtree; /* DN of synchronized subtree on directory side */
  50. /* this simplifies the mapping as it's simply
  51. from the former to the latter container, or
  52. vice versa */
  53. ber_int_t dirsync_flags;
  54. ber_int_t dirsync_maxattributecount;
  55. char *dirsync_cookie;
  56. int dirsync_cookie_len;
  57. PRBool dirsync_cookie_has_more;
  58. PRBool create_users_from_dirsync;
  59. PRBool create_groups_from_dirsync;
  60. char *windows_domain;
  61. int isnt4;
  62. int iswin2k3;
  63. /* This filter is used to determine if an entry belongs to this agreement. We put it here
  64. * so we only have to allocate each filter once instead of doing it every time we receive a change. */
  65. Slapi_Filter *directory_filter; /* Used for checking if local entries need to be sync'd to AD */
  66. Slapi_Filter *deleted_filter; /* Used for checking if an entry is an AD tombstone */
  67. };
  68. static int
  69. true_value_from_string(char *val)
  70. {
  71. if (strcasecmp (val, "on") == 0 || strcasecmp (val, "yes") == 0 ||
  72. strcasecmp (val, "true") == 0 || strcasecmp (val, "1") == 0)
  73. {
  74. return 1;
  75. } else
  76. {
  77. return 0;
  78. }
  79. }
  80. static int
  81. windows_parse_config_entry(Repl_Agmt *ra, const char *type, Slapi_Entry *e)
  82. {
  83. char *tmpstr = NULL;
  84. int retval = 0;
  85. if (type == NULL || slapi_attr_types_equivalent(type,type_nsds7WindowsReplicaArea))
  86. {
  87. tmpstr = slapi_entry_attr_get_charptr(e, type_nsds7WindowsReplicaArea);
  88. if (NULL != tmpstr)
  89. {
  90. windows_private_set_windows_subtree(ra, slapi_sdn_new_dn_passin(tmpstr) );
  91. }
  92. retval = 1;
  93. slapi_ch_free((void**)&tmpstr);
  94. }
  95. if (type == NULL || slapi_attr_types_equivalent(type,type_nsds7DirectoryReplicaArea))
  96. {
  97. tmpstr = slapi_entry_attr_get_charptr(e, type_nsds7DirectoryReplicaArea);
  98. if (NULL != tmpstr)
  99. {
  100. windows_private_set_directory_subtree(ra, slapi_sdn_new_dn_passin(tmpstr) );
  101. }
  102. retval = 1;
  103. slapi_ch_free((void**)&tmpstr);
  104. }
  105. if (type == NULL || slapi_attr_types_equivalent(type,type_nsds7CreateNewUsers))
  106. {
  107. tmpstr = slapi_entry_attr_get_charptr(e, type_nsds7CreateNewUsers);
  108. if (NULL != tmpstr && true_value_from_string(tmpstr))
  109. {
  110. windows_private_set_create_users(ra, PR_TRUE);
  111. }
  112. else
  113. {
  114. windows_private_set_create_users(ra, PR_FALSE);
  115. }
  116. retval = 1;
  117. slapi_ch_free((void**)&tmpstr);
  118. }
  119. if (type == NULL || slapi_attr_types_equivalent(type,type_nsds7CreateNewGroups))
  120. {
  121. tmpstr = slapi_entry_attr_get_charptr(e, type_nsds7CreateNewGroups);
  122. if (NULL != tmpstr && true_value_from_string(tmpstr))
  123. {
  124. windows_private_set_create_groups(ra, PR_TRUE);
  125. }
  126. else
  127. {
  128. windows_private_set_create_groups(ra, PR_FALSE);
  129. }
  130. retval = 1;
  131. slapi_ch_free((void**)&tmpstr);
  132. }
  133. if (type == NULL || slapi_attr_types_equivalent(type,type_nsds7WindowsDomain))
  134. {
  135. tmpstr = slapi_entry_attr_get_charptr(e, type_nsds7WindowsDomain);
  136. if (NULL != tmpstr)
  137. {
  138. windows_private_set_windows_domain(ra,tmpstr);
  139. }
  140. /* No need to free tmpstr because it was aliased by the call above */
  141. tmpstr = NULL;
  142. retval = 1;
  143. }
  144. return retval;
  145. }
  146. /* Returns non-zero if the modify was ok, zero if not */
  147. int
  148. windows_handle_modify_agreement(Repl_Agmt *ra, const char *type, Slapi_Entry *e)
  149. {
  150. /* Is this a Windows agreement ? */
  151. if (get_agmt_agreement_type(ra) == REPLICA_TYPE_WINDOWS)
  152. {
  153. return windows_parse_config_entry(ra,type,e);
  154. } else
  155. {
  156. return 0;
  157. }
  158. }
  159. void
  160. windows_init_agreement_from_entry(Repl_Agmt *ra, Slapi_Entry *e)
  161. {
  162. agmt_set_priv(ra,windows_private_new());
  163. windows_parse_config_entry(ra,NULL,e);
  164. }
  165. const char* windows_private_get_purl(const Repl_Agmt *ra)
  166. {
  167. const char* windows_purl;
  168. char *hostname;
  169. hostname = agmt_get_hostname(ra);
  170. windows_purl = slapi_ch_smprintf("ldap://%s:%d", hostname, agmt_get_port(ra));
  171. slapi_ch_free_string(&hostname);
  172. return windows_purl;
  173. }
  174. Dirsync_Private* windows_private_new()
  175. {
  176. Dirsync_Private *dp;
  177. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_new\n", 0, 0, 0 );
  178. dp = (Dirsync_Private *)slapi_ch_calloc(sizeof(Dirsync_Private),1);
  179. dp->dirsync_maxattributecount = -1;
  180. dp->directory_filter = NULL;
  181. dp->deleted_filter = NULL;
  182. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_new\n", 0, 0, 0 );
  183. return dp;
  184. }
  185. void windows_agreement_delete(Repl_Agmt *ra)
  186. {
  187. Dirsync_Private *dp = (Dirsync_Private *) agmt_get_priv(ra);
  188. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_delete\n", 0, 0, 0 );
  189. PR_ASSERT(dp != NULL);
  190. slapi_filter_free(dp->directory_filter, 1);
  191. slapi_filter_free(dp->deleted_filter, 1);
  192. slapi_ch_free((void **)dp);
  193. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_delete\n", 0, 0, 0 );
  194. }
  195. int windows_private_get_isnt4(const Repl_Agmt *ra)
  196. {
  197. Dirsync_Private *dp;
  198. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_get_isnt4\n", 0, 0, 0 );
  199. PR_ASSERT(ra);
  200. dp = (Dirsync_Private *) agmt_get_priv(ra);
  201. PR_ASSERT (dp);
  202. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_get_isnt4\n", 0, 0, 0 );
  203. return dp->isnt4;
  204. }
  205. void windows_private_set_isnt4(const Repl_Agmt *ra, int isit)
  206. {
  207. Dirsync_Private *dp;
  208. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_set_isnt4\n", 0, 0, 0 );
  209. PR_ASSERT(ra);
  210. dp = (Dirsync_Private *) agmt_get_priv(ra);
  211. PR_ASSERT (dp);
  212. dp->isnt4 = isit;
  213. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_set_isnt4\n", 0, 0, 0 );
  214. }
  215. int windows_private_get_iswin2k3(const Repl_Agmt *ra)
  216. {
  217. Dirsync_Private *dp;
  218. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_get_iswin2k3\n", 0, 0, 0 );
  219. PR_ASSERT(ra);
  220. dp = (Dirsync_Private *) agmt_get_priv(ra);
  221. PR_ASSERT (dp);
  222. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_get_iswin2k3\n", 0, 0, 0 );
  223. return dp->iswin2k3;
  224. }
  225. void windows_private_set_iswin2k3(const Repl_Agmt *ra, int isit)
  226. {
  227. Dirsync_Private *dp;
  228. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_set_iswin2k3\n", 0, 0, 0 );
  229. PR_ASSERT(ra);
  230. dp = (Dirsync_Private *) agmt_get_priv(ra);
  231. PR_ASSERT (dp);
  232. dp->iswin2k3 = isit;
  233. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_set_iswin2k3\n", 0, 0, 0 );
  234. }
  235. /* Returns a copy of the Slapi_Filter pointer. The caller should not free it */
  236. Slapi_Filter* windows_private_get_directory_filter(const Repl_Agmt *ra)
  237. {
  238. Dirsync_Private *dp;
  239. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_get_directory_filter\n", 0, 0, 0 );
  240. PR_ASSERT(ra);
  241. dp = (Dirsync_Private *) agmt_get_priv(ra);
  242. PR_ASSERT (dp);
  243. if (dp->directory_filter == NULL) {
  244. char *string_filter = slapi_ch_strdup("(&(|(objectclass=ntuser)(objectclass=ntgroup))(ntUserDomainId=*))");
  245. /* The filter gets freed in windows_agreement_delete() */
  246. dp->directory_filter = slapi_str2filter( string_filter );
  247. slapi_ch_free_string(&string_filter);
  248. }
  249. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_get_directory_filter\n", 0, 0, 0 );
  250. return dp->directory_filter;
  251. }
  252. /* Returns a copy of the Slapi_Filter pointer. The caller should not free it */
  253. Slapi_Filter* windows_private_get_deleted_filter(const Repl_Agmt *ra)
  254. {
  255. Dirsync_Private *dp;
  256. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_get_deleted_filter\n", 0, 0, 0 );
  257. PR_ASSERT(ra);
  258. dp = (Dirsync_Private *) agmt_get_priv(ra);
  259. PR_ASSERT (dp);
  260. if (dp->deleted_filter == NULL) {
  261. char *string_filter = slapi_ch_strdup("(isdeleted=*)");
  262. /* The filter gets freed in windows_agreement_delete() */
  263. dp->deleted_filter = slapi_str2filter( string_filter );
  264. slapi_ch_free_string(&string_filter);
  265. }
  266. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_get_deleted_filter\n", 0, 0, 0 );
  267. return dp->deleted_filter;
  268. }
  269. /* Returns a copy of the Slapi_DN pointer, no need to free it */
  270. const Slapi_DN* windows_private_get_windows_subtree (const Repl_Agmt *ra)
  271. {
  272. Dirsync_Private *dp;
  273. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_get_windows_subtree\n", 0, 0, 0 );
  274. PR_ASSERT(ra);
  275. dp = (Dirsync_Private *) agmt_get_priv(ra);
  276. PR_ASSERT (dp);
  277. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_get_windows_subtree\n", 0, 0, 0 );
  278. return dp->windows_subtree;
  279. }
  280. const char *
  281. windows_private_get_windows_domain(const Repl_Agmt *ra)
  282. {
  283. Dirsync_Private *dp;
  284. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_get_windows_domain\n", 0, 0, 0 );
  285. PR_ASSERT(ra);
  286. dp = (Dirsync_Private *) agmt_get_priv(ra);
  287. PR_ASSERT (dp);
  288. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_get_windows_domain\n", 0, 0, 0 );
  289. return dp->windows_domain;
  290. }
  291. static void
  292. windows_private_set_windows_domain(const Repl_Agmt *ra, char *domain)
  293. {
  294. Dirsync_Private *dp;
  295. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_set_windows_domain\n", 0, 0, 0 );
  296. PR_ASSERT(ra);
  297. dp = (Dirsync_Private *) agmt_get_priv(ra);
  298. PR_ASSERT (dp);
  299. dp->windows_domain = domain;
  300. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_set_windows_domain\n", 0, 0, 0 );
  301. }
  302. /* Returns a copy of the Slapi_DN pointer, no need to free it */
  303. const Slapi_DN* windows_private_get_directory_subtree (const Repl_Agmt *ra)
  304. {
  305. Dirsync_Private *dp;
  306. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_get_directory_replarea\n", 0, 0, 0 );
  307. PR_ASSERT(ra);
  308. dp = (Dirsync_Private *) agmt_get_priv(ra);
  309. PR_ASSERT (dp);
  310. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_get_directory_replarea\n", 0, 0, 0 );
  311. return dp->directory_subtree;
  312. }
  313. /* Takes a copy of the sdn passed in */
  314. void windows_private_set_windows_subtree (const Repl_Agmt *ra,const Slapi_DN* sdn )
  315. {
  316. Dirsync_Private *dp;
  317. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_set_windows_replarea\n", 0, 0, 0 );
  318. PR_ASSERT(ra);
  319. PR_ASSERT(sdn);
  320. dp = (Dirsync_Private *) agmt_get_priv(ra);
  321. PR_ASSERT (dp);
  322. dp->windows_subtree = slapi_sdn_dup(sdn);
  323. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_set_windows_replarea\n", 0, 0, 0 );
  324. }
  325. /* Takes a copy of the sdn passed in */
  326. void windows_private_set_directory_subtree (const Repl_Agmt *ra,const Slapi_DN* sdn )
  327. {
  328. Dirsync_Private *dp;
  329. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_set_directory_replarea\n", 0, 0, 0 );
  330. PR_ASSERT(ra);
  331. PR_ASSERT(sdn);
  332. dp = (Dirsync_Private *) agmt_get_priv(ra);
  333. PR_ASSERT (dp);
  334. dp->directory_subtree = slapi_sdn_dup(sdn);
  335. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_set_directory_replarea\n", 0, 0, 0 );
  336. }
  337. PRBool windows_private_create_users(const Repl_Agmt *ra)
  338. {
  339. Dirsync_Private *dp;
  340. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_create_users\n", 0, 0, 0 );
  341. PR_ASSERT(ra);
  342. dp = (Dirsync_Private *) agmt_get_priv(ra);
  343. PR_ASSERT (dp);
  344. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_create_users\n", 0, 0, 0 );
  345. return dp->create_users_from_dirsync;
  346. }
  347. void windows_private_set_create_users(const Repl_Agmt *ra, PRBool value)
  348. {
  349. Dirsync_Private *dp;
  350. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_set_create_users\n", 0, 0, 0 );
  351. PR_ASSERT(ra);
  352. dp = (Dirsync_Private *) agmt_get_priv(ra);
  353. PR_ASSERT (dp);
  354. dp->create_users_from_dirsync = value;
  355. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_set_create_users\n", 0, 0, 0 );
  356. }
  357. PRBool windows_private_create_groups(const Repl_Agmt *ra)
  358. {
  359. Dirsync_Private *dp;
  360. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_create_groups\n", 0, 0, 0 );
  361. PR_ASSERT(ra);
  362. dp = (Dirsync_Private *) agmt_get_priv(ra);
  363. PR_ASSERT (dp);
  364. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_create_groups\n", 0, 0, 0 );
  365. return dp->create_groups_from_dirsync;
  366. }
  367. void windows_private_set_create_groups(const Repl_Agmt *ra, PRBool value)
  368. {
  369. Dirsync_Private *dp;
  370. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_set_create_groups\n", 0, 0, 0 );
  371. PR_ASSERT(ra);
  372. dp = (Dirsync_Private *) agmt_get_priv(ra);
  373. PR_ASSERT (dp);
  374. dp->create_groups_from_dirsync = value;
  375. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_set_create_groups\n", 0, 0, 0 );
  376. }
  377. /*
  378. This function returns the current Dirsync_Private that's inside
  379. Repl_Agmt ra as a ldap control.
  380. */
  381. LDAPControl* windows_private_dirsync_control(const Repl_Agmt *ra)
  382. {
  383. LDAPControl *control = NULL;
  384. BerElement *ber;
  385. Dirsync_Private *dp;
  386. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_dirsync_control\n", 0, 0, 0 );
  387. PR_ASSERT(ra);
  388. dp = (Dirsync_Private *) agmt_get_priv(ra);
  389. PR_ASSERT (dp);
  390. ber = ber_alloc();
  391. ber_printf( ber, "{iio}", dp->dirsync_flags, dp->dirsync_maxattributecount, dp->dirsync_cookie, dp->dirsync_cookie_len );
  392. slapi_build_control( REPL_DIRSYNC_CONTROL_OID, ber, PR_TRUE, &control);
  393. ber_free(ber,1);
  394. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_dirsync_control\n", 0, 0, 0 );
  395. return control;
  396. }
  397. /*
  398. This function scans the array of controls and updates the Repl_Agmt's
  399. Dirsync_Private if the dirsync control is found.
  400. */
  401. void windows_private_update_dirsync_control(const Repl_Agmt *ra,LDAPControl **controls )
  402. {
  403. Dirsync_Private *dp;
  404. int foundDirsyncControl;
  405. int i;
  406. LDAPControl *dirsync;
  407. BerElement *ber;
  408. ber_int_t hasMoreData;
  409. ber_int_t maxAttributeCount;
  410. BerValue *serverCookie;
  411. int return_value = LDAP_SUCCESS;
  412. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_update_dirsync_control\n", 0, 0, 0 );
  413. PR_ASSERT(ra);
  414. dp = (Dirsync_Private *) agmt_get_priv(ra);
  415. PR_ASSERT (dp);
  416. if (NULL != controls )
  417. {
  418. foundDirsyncControl = 0;
  419. for ( i = 0; (( controls[i] != NULL ) && ( !foundDirsyncControl )); i++ ) {
  420. foundDirsyncControl = !strcmp( controls[i]->ldctl_oid, REPL_DIRSYNC_CONTROL_OID );
  421. }
  422. if ( !foundDirsyncControl )
  423. {
  424. return_value = LDAP_CONTROL_NOT_FOUND;
  425. goto choke;
  426. }
  427. else
  428. {
  429. dirsync = slapi_dup_control( controls[i-1]);
  430. }
  431. ber = ber_init( &dirsync->ldctl_value ) ;
  432. if (ber_scanf( ber, "{iiO}", &hasMoreData, &maxAttributeCount, &serverCookie) == LBER_ERROR)
  433. {
  434. return_value = LDAP_CONTROL_NOT_FOUND;
  435. goto choke;
  436. }
  437. slapi_ch_free_string(&dp->dirsync_cookie);
  438. dp->dirsync_cookie = ( char* ) slapi_ch_malloc(serverCookie->bv_len + 1);
  439. memcpy(dp->dirsync_cookie, serverCookie->bv_val, serverCookie->bv_len);
  440. dp->dirsync_cookie_len = (int) serverCookie->bv_len; /* XXX shouldn't cast? */
  441. /* dp->dirsync_maxattributecount = maxAttributeCount; We don't need to keep this */
  442. dp->dirsync_cookie_has_more = hasMoreData;
  443. choke:
  444. ber_bvfree(serverCookie);
  445. ber_free(ber,1);
  446. }
  447. else
  448. {
  449. return_value = LDAP_CONTROL_NOT_FOUND;
  450. }
  451. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_update_dirsync_control\n", 0, 0, 0 );
  452. /* return return_value; */
  453. }
  454. PRBool windows_private_dirsync_has_more(const Repl_Agmt *ra)
  455. {
  456. Dirsync_Private *dp;
  457. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_dirsync_has_more\n", 0, 0, 0 );
  458. PR_ASSERT(ra);
  459. dp = (Dirsync_Private *) agmt_get_priv(ra);
  460. PR_ASSERT (dp);
  461. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_dirsync_has_more\n", 0, 0, 0 );
  462. return dp->dirsync_cookie_has_more;
  463. }
  464. void windows_private_null_dirsync_cookie(const Repl_Agmt *ra)
  465. {
  466. Dirsync_Private *dp;
  467. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_null_dirsync_control\n", 0, 0, 0 );
  468. dp = (Dirsync_Private *) agmt_get_priv(ra);
  469. PR_ASSERT (dp);
  470. dp->dirsync_cookie_len = 0;
  471. slapi_ch_free_string(&dp->dirsync_cookie);
  472. dp->dirsync_cookie = NULL;
  473. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_null_dirsync_control\n", 0, 0, 0 );
  474. }
  475. static
  476. Slapi_Mods *windows_private_get_cookie_mod(Dirsync_Private *dp, int modtype)
  477. {
  478. Slapi_Mods *smods = NULL;
  479. smods = slapi_mods_new();
  480. slapi_mods_add( smods, modtype,
  481. "nsds7DirsyncCookie", dp->dirsync_cookie_len , dp->dirsync_cookie);
  482. return smods;
  483. }
  484. /* writes the current cookie into dse.ldif under the replication agreement entry
  485. returns: ldap result code of the operation. */
  486. int
  487. windows_private_save_dirsync_cookie(const Repl_Agmt *ra)
  488. {
  489. Dirsync_Private *dp = NULL;
  490. Slapi_PBlock *pb = NULL;
  491. const char* dn = NULL;
  492. Slapi_DN* sdn = NULL;
  493. int rc = 0;
  494. Slapi_Mods *mods = NULL;
  495. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_save_dirsync_cookie\n", 0, 0, 0 );
  496. PR_ASSERT(ra);
  497. dp = (Dirsync_Private *) agmt_get_priv(ra);
  498. PR_ASSERT (dp);
  499. pb = slapi_pblock_new ();
  500. mods = windows_private_get_cookie_mod(dp, LDAP_MOD_REPLACE);
  501. sdn = slapi_sdn_dup( agmt_get_dn_byref(ra) );
  502. dn = slapi_sdn_get_dn(sdn);
  503. slapi_modify_internal_set_pb (pb, dn, slapi_mods_get_ldapmods_byref(mods), NULL, NULL,
  504. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  505. slapi_modify_internal_pb (pb);
  506. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  507. if (rc == LDAP_NO_SUCH_ATTRIBUTE)
  508. { /* try again, but as an add instead */
  509. mods = windows_private_get_cookie_mod(dp, LDAP_MOD_ADD);
  510. slapi_modify_internal_set_pb (pb, dn, slapi_mods_get_ldapmods_byref(mods), NULL, NULL,
  511. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  512. slapi_modify_internal_pb (pb);
  513. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  514. }
  515. slapi_pblock_destroy (pb);
  516. slapi_mods_free(&mods);
  517. slapi_sdn_free(&sdn);
  518. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_save_dirsync_cookie\n", 0, 0, 0 );
  519. return rc;
  520. }
  521. /* reads the cookie in dse.ldif to the replication agreement entry
  522. returns: ldap result code of ldap operation, or
  523. LDAP_NO_SUCH_ATTRIBUTE. (this is the equilivent of a null cookie) */
  524. int windows_private_load_dirsync_cookie(const Repl_Agmt *ra)
  525. {
  526. Dirsync_Private *dp = NULL;
  527. Slapi_PBlock *pb = NULL;
  528. Slapi_DN* sdn = NULL;
  529. int rc = 0;
  530. Slapi_Entry *entry = NULL;
  531. Slapi_Attr *attr = NULL;
  532. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_load_dirsync_cookie\n", 0, 0, 0 );
  533. PR_ASSERT(ra);
  534. dp = (Dirsync_Private *) agmt_get_priv(ra);
  535. PR_ASSERT (dp);
  536. pb = slapi_pblock_new ();
  537. sdn = slapi_sdn_dup( agmt_get_dn_byref(ra) );
  538. rc = slapi_search_internal_get_entry(sdn, NULL, &entry,
  539. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION));
  540. if (rc == 0)
  541. {
  542. rc= slapi_entry_attr_find( entry, type_nsds7DirsyncCookie, &attr );
  543. if (attr)
  544. {
  545. struct berval **vals;
  546. rc = slapi_attr_get_bervals_copy(attr, &vals );
  547. if (vals)
  548. {
  549. dp->dirsync_cookie_len = (int) (vals[0])->bv_len;
  550. slapi_ch_free_string(&dp->dirsync_cookie);
  551. dp->dirsync_cookie = ( char* ) slapi_ch_malloc(dp->dirsync_cookie_len + 1);
  552. memcpy(dp->dirsync_cookie,(vals[0]->bv_val), (vals[0])->bv_len+1);
  553. }
  554. ber_bvecfree(vals);
  555. /* we do not free attr */
  556. }
  557. else
  558. {
  559. rc = LDAP_NO_SUCH_ATTRIBUTE;
  560. }
  561. }
  562. if (entry)
  563. {
  564. slapi_entry_free(entry);
  565. }
  566. slapi_sdn_free( &sdn);
  567. slapi_pblock_destroy (pb);
  568. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_load_dirsync_cookie\n", 0, 0, 0 );
  569. return rc;
  570. }