windows_private.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. /* windows_private.c */
  39. #include "repl.h"
  40. #include "repl5.h"
  41. #include "slap.h"
  42. #include "slapi-plugin.h"
  43. #include "windowsrepl.h"
  44. struct windowsprivate {
  45. Slapi_DN *windows_subtree; /* DN of synchronized subtree (on the windows side) */
  46. Slapi_DN *directory_subtree; /* DN of synchronized subtree on directory side */
  47. /* this simplifies the mapping as it's simply
  48. from the former to the latter container, or
  49. vice versa */
  50. int dirsync_flags;
  51. int dirsync_maxattributecount;
  52. char *dirsync_cookie;
  53. int dirsync_cookie_len;
  54. PRBool dirsync_cookie_has_more;
  55. PRBool create_users_from_dirsync;
  56. PRBool create_groups_from_dirsync;
  57. char *windows_domain;
  58. int isnt4;
  59. };
  60. void
  61. windows_init_agreement_from_entry(Repl_Agmt *ra, Slapi_Entry *e)
  62. {
  63. char *tmpstr = NULL;
  64. agmt_set_priv(ra,windows_private_new());
  65. /* DN of entry at root of replicated area */
  66. tmpstr = slapi_entry_attr_get_charptr(e, type_nsds7WindowsReplicaArea);
  67. if (NULL != tmpstr)
  68. {
  69. windows_private_set_windows_subtree(ra, slapi_sdn_new_dn_passin(tmpstr) );
  70. }
  71. tmpstr = slapi_entry_attr_get_charptr(e, type_nsds7DirectoryReplicaArea);
  72. if (NULL != tmpstr)
  73. {
  74. windows_private_set_directory_subtree(ra, slapi_sdn_new_dn_passin(tmpstr) );
  75. }
  76. tmpstr = slapi_entry_attr_get_charptr(e, type_nsds7CreateNewUsers);
  77. if (NULL != tmpstr)
  78. {
  79. windows_private_set_create_users(ra, PR_TRUE);
  80. slapi_ch_free((void**)&tmpstr);
  81. }
  82. else
  83. {
  84. windows_private_set_create_users(ra, PR_FALSE);
  85. }
  86. tmpstr = slapi_entry_attr_get_charptr(e, type_nsds7WindowsDomain);
  87. if (NULL != tmpstr)
  88. {
  89. windows_private_set_windows_domain(ra,tmpstr);
  90. }
  91. }
  92. const char* windows_private_get_purl(const Repl_Agmt *ra)
  93. {
  94. const char* windows_purl;
  95. char *hostname;
  96. hostname = agmt_get_hostname(ra);
  97. windows_purl = slapi_ch_smprintf("ldap://%s:%d", hostname, agmt_get_port(ra));
  98. slapi_ch_free_string(&hostname);
  99. return windows_purl;
  100. }
  101. Dirsync_Private* windows_private_new()
  102. {
  103. Dirsync_Private *dp;
  104. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_new\n", 0, 0, 0 );
  105. dp = (Dirsync_Private *)slapi_ch_calloc(sizeof(Dirsync_Private),1);
  106. dp->dirsync_maxattributecount = -1;
  107. dp->create_users_from_dirsync = PR_TRUE;
  108. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_new\n", 0, 0, 0 );
  109. return dp;
  110. }
  111. void windows_agreement_delete(Repl_Agmt *ra)
  112. {
  113. Dirsync_Private *dp = (Dirsync_Private *) agmt_get_priv(ra);
  114. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_delete\n", 0, 0, 0 );
  115. PR_ASSERT(dp != NULL);
  116. /* DBDB: need to free payoad here */
  117. slapi_ch_free((void **)dp);
  118. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_delete\n", 0, 0, 0 );
  119. }
  120. int windows_private_get_isnt4(const Repl_Agmt *ra)
  121. {
  122. Dirsync_Private *dp;
  123. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_get_isnt4\n", 0, 0, 0 );
  124. PR_ASSERT(ra);
  125. dp = (Dirsync_Private *) agmt_get_priv(ra);
  126. PR_ASSERT (dp);
  127. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_get_isnt4\n", 0, 0, 0 );
  128. return dp->isnt4;
  129. }
  130. void windows_private_set_isnt4(const Repl_Agmt *ra, int isit)
  131. {
  132. Dirsync_Private *dp;
  133. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_set_isnt4\n", 0, 0, 0 );
  134. PR_ASSERT(ra);
  135. dp = (Dirsync_Private *) agmt_get_priv(ra);
  136. PR_ASSERT (dp);
  137. dp->isnt4 = isit;
  138. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_set_isnt4\n", 0, 0, 0 );
  139. }
  140. /* Returns a copy of the Slapi_DN pointer, no need to free it */
  141. const Slapi_DN* windows_private_get_windows_subtree (const Repl_Agmt *ra)
  142. {
  143. Dirsync_Private *dp;
  144. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_get_windows_subtree\n", 0, 0, 0 );
  145. PR_ASSERT(ra);
  146. dp = (Dirsync_Private *) agmt_get_priv(ra);
  147. PR_ASSERT (dp);
  148. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_get_windows_subtree\n", 0, 0, 0 );
  149. return dp->windows_subtree;
  150. }
  151. const char *
  152. windows_private_get_windows_domain(const Repl_Agmt *ra)
  153. {
  154. Dirsync_Private *dp;
  155. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_get_windows_domain\n", 0, 0, 0 );
  156. PR_ASSERT(ra);
  157. dp = (Dirsync_Private *) agmt_get_priv(ra);
  158. PR_ASSERT (dp);
  159. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_get_windows_domain\n", 0, 0, 0 );
  160. return dp->windows_domain;
  161. }
  162. static void
  163. windows_private_set_windows_domain(const Repl_Agmt *ra, char *domain)
  164. {
  165. Dirsync_Private *dp;
  166. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_set_windows_domain\n", 0, 0, 0 );
  167. PR_ASSERT(ra);
  168. dp = (Dirsync_Private *) agmt_get_priv(ra);
  169. PR_ASSERT (dp);
  170. dp->windows_domain = domain;
  171. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_set_windows_domain\n", 0, 0, 0 );
  172. }
  173. /* Returns a copy of the Slapi_DN pointer, no need to free it */
  174. const Slapi_DN* windows_private_get_directory_subtree (const Repl_Agmt *ra)
  175. {
  176. Dirsync_Private *dp;
  177. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_get_directory_replarea\n", 0, 0, 0 );
  178. PR_ASSERT(ra);
  179. dp = (Dirsync_Private *) agmt_get_priv(ra);
  180. PR_ASSERT (dp);
  181. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_get_directory_replarea\n", 0, 0, 0 );
  182. return dp->directory_subtree;
  183. }
  184. /* Takes a copy of the sdn passed in */
  185. void windows_private_set_windows_subtree (const Repl_Agmt *ra,const Slapi_DN* sdn )
  186. {
  187. Dirsync_Private *dp;
  188. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_set_windows_replarea\n", 0, 0, 0 );
  189. PR_ASSERT(ra);
  190. PR_ASSERT(sdn);
  191. dp = (Dirsync_Private *) agmt_get_priv(ra);
  192. PR_ASSERT (dp);
  193. dp->windows_subtree = slapi_sdn_dup(sdn);
  194. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_set_windows_replarea\n", 0, 0, 0 );
  195. }
  196. /* Takes a copy of the sdn passed in */
  197. void windows_private_set_directory_subtree (const Repl_Agmt *ra,const Slapi_DN* sdn )
  198. {
  199. Dirsync_Private *dp;
  200. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_set_directory_replarea\n", 0, 0, 0 );
  201. PR_ASSERT(ra);
  202. PR_ASSERT(sdn);
  203. dp = (Dirsync_Private *) agmt_get_priv(ra);
  204. PR_ASSERT (dp);
  205. dp->directory_subtree = slapi_sdn_dup(sdn);
  206. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_set_directory_replarea\n", 0, 0, 0 );
  207. }
  208. PRBool windows_private_create_users(const Repl_Agmt *ra)
  209. {
  210. Dirsync_Private *dp;
  211. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_create_users\n", 0, 0, 0 );
  212. PR_ASSERT(ra);
  213. dp = (Dirsync_Private *) agmt_get_priv(ra);
  214. PR_ASSERT (dp);
  215. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_create_users\n", 0, 0, 0 );
  216. return dp->create_users_from_dirsync;
  217. }
  218. void windows_private_set_create_users(const Repl_Agmt *ra, PRBool value)
  219. {
  220. Dirsync_Private *dp;
  221. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_set_create_users\n", 0, 0, 0 );
  222. PR_ASSERT(ra);
  223. dp = (Dirsync_Private *) agmt_get_priv(ra);
  224. PR_ASSERT (dp);
  225. dp->create_users_from_dirsync = value;
  226. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_set_create_users\n", 0, 0, 0 );
  227. }
  228. /*
  229. This function returns the current Dirsync_Private that's inside
  230. Repl_Agmt ra as a ldap control.
  231. */
  232. LDAPControl* windows_private_dirsync_control(const Repl_Agmt *ra)
  233. {
  234. LDAPControl *control = NULL;
  235. LDAPControl **lc = &control ;
  236. BerElement *ber;
  237. Dirsync_Private *dp;
  238. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_dirsync_control\n", 0, 0, 0 );
  239. PR_ASSERT(ra);
  240. dp = (Dirsync_Private *) agmt_get_priv(ra);
  241. PR_ASSERT (dp);
  242. ber = ber_alloc();
  243. ber_printf( ber, "{iio}", dp->dirsync_flags, dp->dirsync_maxattributecount, dp->dirsync_cookie, dp->dirsync_cookie_len );
  244. slapi_build_control( REPL_DIRSYNC_CONTROL_OID, ber, PR_TRUE, &control);
  245. ber_free(ber,1);
  246. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_dirsync_control\n", 0, 0, 0 );
  247. return control;
  248. }
  249. /*
  250. This function scans the array of controls and updates the Repl_Agmt's
  251. Dirsync_Private if the dirsync control is found.
  252. */
  253. void windows_private_update_dirsync_control(const Repl_Agmt *ra,LDAPControl **controls )
  254. {
  255. Dirsync_Private *dp;
  256. int foundDirsyncControl;
  257. int i;
  258. LDAPControl *dirsync;
  259. BerElement *ber;
  260. int hasMoreData;
  261. int maxAttributeCount;
  262. BerValue *serverCookie;
  263. int return_value = LDAP_SUCCESS;
  264. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_update_dirsync_control\n", 0, 0, 0 );
  265. PR_ASSERT(ra);
  266. dp = (Dirsync_Private *) agmt_get_priv(ra);
  267. PR_ASSERT (dp);
  268. if (NULL != controls )
  269. {
  270. foundDirsyncControl = 0;
  271. for ( i = 0; (( controls[i] != NULL ) && ( !foundDirsyncControl )); i++ ) {
  272. foundDirsyncControl = !strcmp( controls[i]->ldctl_oid, REPL_DIRSYNC_CONTROL_OID );
  273. }
  274. if ( !foundDirsyncControl )
  275. {
  276. return_value = LDAP_CONTROL_NOT_FOUND;
  277. goto choke;
  278. }
  279. else
  280. {
  281. dirsync = slapi_dup_control( controls[i-1]);
  282. }
  283. ber = ber_init( &dirsync->ldctl_value ) ;
  284. if (ber_scanf( ber, "{iiO}", &hasMoreData, &maxAttributeCount, &serverCookie) == LBER_ERROR)
  285. {
  286. return_value = LDAP_CONTROL_NOT_FOUND;
  287. goto choke;
  288. }
  289. slapi_ch_free(&dp->dirsync_cookie);
  290. dp->dirsync_cookie = ( char* ) slapi_ch_malloc(serverCookie->bv_len + 1);
  291. memcpy(dp->dirsync_cookie, serverCookie->bv_val, serverCookie->bv_len);
  292. dp->dirsync_cookie_len = (int) serverCookie->bv_len; /* XXX shouldn't cast? */
  293. /* dp->dirsync_maxattributecount = maxAttributeCount; We don't need to keep this */
  294. dp->dirsync_cookie_has_more = hasMoreData;
  295. choke:
  296. ber_bvfree(serverCookie);
  297. ber_free(ber,1);
  298. }
  299. else
  300. {
  301. return_value = LDAP_CONTROL_NOT_FOUND;
  302. }
  303. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_update_dirsync_control\n", 0, 0, 0 );
  304. /* return return_value; */
  305. }
  306. PRBool windows_private_dirsync_has_more(const Repl_Agmt *ra)
  307. {
  308. Dirsync_Private *dp;
  309. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_dirsync_has_more\n", 0, 0, 0 );
  310. PR_ASSERT(ra);
  311. dp = (Dirsync_Private *) agmt_get_priv(ra);
  312. PR_ASSERT (dp);
  313. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_dirsync_has_more\n", 0, 0, 0 );
  314. return dp->dirsync_cookie_has_more;
  315. }
  316. void windows_private_null_dirsync_cookie(const Repl_Agmt *ra)
  317. {
  318. Dirsync_Private *dp;
  319. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_null_dirsync_control\n", 0, 0, 0 );
  320. dp = (Dirsync_Private *) agmt_get_priv(ra);
  321. PR_ASSERT (dp);
  322. dp->dirsync_cookie_len = 0;
  323. slapi_ch_free(&dp->dirsync_cookie);
  324. dp->dirsync_cookie = NULL;
  325. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_null_dirsync_control\n", 0, 0, 0 );
  326. }
  327. static
  328. Slapi_Mods *windows_private_get_cookie_mod(Dirsync_Private *dp, int modtype)
  329. {
  330. Slapi_Mods *smods = NULL;
  331. smods = slapi_mods_new();
  332. slapi_mods_add( smods, modtype,
  333. "nsds7DirsyncCookie", dp->dirsync_cookie_len , dp->dirsync_cookie);
  334. return smods;
  335. }
  336. /* writes the current cookie into dse.ldif under the replication agreement entry
  337. returns: ldap result code of the operation. */
  338. int
  339. windows_private_save_dirsync_cookie(const Repl_Agmt *ra)
  340. {
  341. Dirsync_Private *dp = NULL;
  342. Slapi_PBlock *pb = NULL;
  343. const char* dn = NULL;
  344. Slapi_DN* sdn = NULL;
  345. int rc = 0;
  346. Slapi_Mods *mods = NULL;
  347. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_save_dirsync_cookie\n", 0, 0, 0 );
  348. PR_ASSERT(ra);
  349. dp = (Dirsync_Private *) agmt_get_priv(ra);
  350. PR_ASSERT (dp);
  351. pb = slapi_pblock_new ();
  352. mods = windows_private_get_cookie_mod(dp, LDAP_MOD_REPLACE);
  353. sdn = slapi_sdn_dup( agmt_get_dn_byref(ra) );
  354. dn = slapi_sdn_get_dn(sdn);
  355. slapi_modify_internal_set_pb (pb, dn, slapi_mods_get_ldapmods_byref(mods), NULL, NULL,
  356. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  357. slapi_modify_internal_pb (pb);
  358. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  359. if (rc == LDAP_NO_SUCH_ATTRIBUTE)
  360. { /* try again, but as an add instead */
  361. mods = windows_private_get_cookie_mod(dp, LDAP_MOD_ADD);
  362. slapi_modify_internal_set_pb (pb, dn, slapi_mods_get_ldapmods_byref(mods), NULL, NULL,
  363. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  364. slapi_modify_internal_pb (pb);
  365. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  366. }
  367. slapi_pblock_destroy (pb);
  368. slapi_mods_free(&mods);
  369. slapi_sdn_free(&sdn);
  370. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_save_dirsync_cookie\n", 0, 0, 0 );
  371. return rc;
  372. }
  373. /* reads the cookie in dse.ldif to the replication agreement entry
  374. returns: ldap result code of ldap operation, or
  375. LDAP_NO_SUCH_ATTRIBUTE. (this is the equilivent of a null cookie) */
  376. int windows_private_load_dirsync_cookie(const Repl_Agmt *ra)
  377. {
  378. Dirsync_Private *dp = NULL;
  379. Slapi_PBlock *pb = NULL;
  380. Slapi_DN* sdn = NULL;
  381. int rc = 0;
  382. Slapi_Entry *entry = NULL;
  383. char* cookie = NULL;
  384. Slapi_Attr *attr = NULL;
  385. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_private_load_dirsync_cookie\n", 0, 0, 0 );
  386. PR_ASSERT(ra);
  387. dp = (Dirsync_Private *) agmt_get_priv(ra);
  388. PR_ASSERT (dp);
  389. pb = slapi_pblock_new ();
  390. sdn = slapi_sdn_dup( agmt_get_dn_byref(ra) );
  391. rc = slapi_search_internal_get_entry(sdn, NULL, &entry,
  392. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION));
  393. if (rc == 0)
  394. {
  395. rc= slapi_entry_attr_find( entry, type_nsds7DirsyncCookie, &attr );
  396. if (attr)
  397. {
  398. struct berval **vals;
  399. rc = slapi_attr_get_bervals_copy(attr, &vals );
  400. if (vals)
  401. {
  402. dp->dirsync_cookie_len = (int) (vals[0])->bv_len;
  403. slapi_ch_free(&dp->dirsync_cookie);
  404. dp->dirsync_cookie = ( char* ) slapi_ch_malloc(dp->dirsync_cookie_len + 1);
  405. memcpy(dp->dirsync_cookie,(vals[0]->bv_val), (vals[0])->bv_len+1);
  406. }
  407. ber_bvecfree(vals);
  408. /* we do not free attr */
  409. }
  410. else
  411. {
  412. rc = LDAP_NO_SUCH_ATTRIBUTE;
  413. }
  414. }
  415. if (entry)
  416. {
  417. slapi_entry_free(entry);
  418. }
  419. slapi_sdn_free( &sdn);
  420. slapi_pblock_destroy (pb);
  421. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_private_load_dirsync_cookie\n", 0, 0, 0 );
  422. return rc;
  423. }