1
0

windows_connection.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108
  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. /* repl5_connection.c */
  42. /*
  43. The connection object manages a connection to a single replication
  44. consumer.
  45. XXXggood what to do on timeout? If we close connection, then we won't leave a
  46. replica locked. Seems like right thing to do.
  47. */
  48. #include "repl5.h"
  49. #include "windowsrepl.h"
  50. #if !defined(USE_OPENLDAP)
  51. #include "ldappr.h"
  52. #endif
  53. #include "slap.h"
  54. #include "nss.h"
  55. typedef struct repl_connection
  56. {
  57. char *hostname;
  58. int port;
  59. char *binddn;
  60. int bindmethod;
  61. int state;
  62. int last_operation;
  63. int last_ldap_error;
  64. const char *status;
  65. char *last_ldap_errmsg;
  66. PRUint32 transport_flags;
  67. LDAP *ld;
  68. int supports_ldapv3; /* 1 if does, 0 if doesn't, -1 if not determined */
  69. int supports_ds50_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  70. int supports_ds40_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  71. int supports_ds71_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  72. int supports_ds90_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  73. int linger_time; /* time in seconds to leave an idle connection open */
  74. int supports_dirsync; /* 1 if does, 0 if doesn't, -1 if not determined */
  75. PRBool linger_active;
  76. Slapi_Eq_Context *linger_event;
  77. PRBool delete_after_linger;
  78. int refcnt;
  79. const Repl_Agmt *agmt;
  80. PRLock *lock;
  81. struct timeval timeout;
  82. int flag_agmt_changed;
  83. char *plain;
  84. int is_win2k3; /* 1 if it is win2k3 or later, 0 if not, -1 if not determined */
  85. } repl_connection;
  86. /* #define DEFAULT_LINGER_TIME (5 * 60) */ /* 5 minutes */
  87. #define DEFAULT_LINGER_TIME (60)
  88. /* Controls we add on every outbound operation */
  89. static LDAPControl manageDSAITControl = {LDAP_CONTROL_MANAGEDSAIT, {0, ""}, '\0'};
  90. static int attribute_string_value_present(LDAP *ld, LDAPMessage *entry,
  91. const char *type, const char *value);
  92. static int bind_and_check_pwp(Repl_Connection *conn, char * binddn, char *password);
  93. static int do_simple_bind (Repl_Connection *conn, LDAP *ld, char * binddn, char *password);
  94. static int s_debug_timeout = 0;
  95. static int s_debug_level = 0;
  96. static Slapi_Eq_Context repl5_start_debug_timeout(int *setlevel);
  97. static void repl5_stop_debug_timeout(Slapi_Eq_Context eqctx, int *setlevel);
  98. static void repl5_debug_timeout_callback(time_t when, void *arg);
  99. #define STATE_CONNECTED 600
  100. #define STATE_DISCONNECTED 601
  101. #define STATUS_DISCONNECTED "disconnected"
  102. #define STATUS_CONNECTED "connected"
  103. #define STATUS_PROCESSING_ADD "processing add operation"
  104. #define STATUS_PROCESSING_DELETE "processing delete operation"
  105. #define STATUS_PROCESSING_MODIFY "processing modify operation"
  106. #define STATUS_PROCESSING_RENAME "processing rename operation"
  107. #define STATUS_PROCESSING_EXTENDED_OPERATION "processing extended operation"
  108. #define STATUS_LINGERING "lingering"
  109. #define STATUS_SHUTTING_DOWN "shutting down"
  110. #define STATUS_BINDING "connecting and binding"
  111. #define STATUS_SEARCHING "processing search operation"
  112. #define CONN_NO_OPERATION 0
  113. #define CONN_ADD 1
  114. #define CONN_DELETE 2
  115. #define CONN_MODIFY 3
  116. #define CONN_RENAME 4
  117. #define CONN_EXTENDED_OPERATION 5
  118. #define CONN_BIND 6
  119. #define CONN_INIT 7
  120. #define CONN_SEARCH 8
  121. /* These are errors returned from ldap operations which should cause us to disconnect and
  122. retry the connection later */
  123. #define IS_DISCONNECT_ERROR(rc) (rc == LDAP_SERVER_DOWN || rc == LDAP_CONNECT_ERROR || rc == LDAP_INVALID_CREDENTIALS || rc == LDAP_INAPPROPRIATE_AUTH || rc == LDAP_LOCAL_ERROR)
  124. /* Forward declarations */
  125. static void close_connection_internal(Repl_Connection *conn);
  126. /*
  127. * Create a new conenction object. Returns a pointer to the object, or
  128. * NULL if an error occurs.
  129. */
  130. Repl_Connection *
  131. windows_conn_new(Repl_Agmt *agmt)
  132. {
  133. Repl_Connection *rpc;
  134. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_new\n", 0, 0, 0 );
  135. rpc = (Repl_Connection *)slapi_ch_malloc(sizeof(repl_connection));
  136. if ((rpc->lock = PR_NewLock()) == NULL)
  137. {
  138. goto loser;
  139. }
  140. rpc->hostname = agmt_get_hostname(agmt);
  141. rpc->port = agmt_get_port(agmt);
  142. rpc->binddn = agmt_get_binddn(agmt);
  143. rpc->bindmethod = agmt_get_bindmethod(agmt);
  144. rpc->transport_flags = agmt_get_transport_flags(agmt);
  145. rpc->ld = NULL;
  146. rpc->state = STATE_DISCONNECTED;
  147. rpc->last_operation = CONN_NO_OPERATION;
  148. rpc->last_ldap_error = LDAP_SUCCESS;
  149. rpc->last_ldap_errmsg = NULL;
  150. rpc->supports_ldapv3 = -1;
  151. rpc->supports_ds40_repl = -1;
  152. rpc->supports_ds50_repl = -1;
  153. rpc->supports_dirsync = -1;
  154. rpc->is_win2k3 = -1;
  155. rpc->linger_active = PR_FALSE;
  156. rpc->delete_after_linger = PR_FALSE;
  157. rpc->linger_event = NULL;
  158. rpc->linger_time = DEFAULT_LINGER_TIME;
  159. rpc->status = STATUS_DISCONNECTED;
  160. rpc->agmt = agmt;
  161. rpc->refcnt = 1;
  162. rpc->timeout.tv_sec = agmt_get_timeout(agmt);
  163. rpc->timeout.tv_usec = 0;
  164. rpc->flag_agmt_changed = 0;
  165. rpc->plain = NULL;
  166. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_new\n", 0, 0, 0 );
  167. return rpc;
  168. loser:
  169. windows_conn_delete(rpc);
  170. slapi_ch_free((void**)&rpc);
  171. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_new - loser\n", 0, 0, 0 );
  172. return NULL;
  173. }
  174. /*
  175. * Return PR_TRUE if the connection is in the connected state
  176. */
  177. static PRBool
  178. windows_conn_connected(Repl_Connection *conn)
  179. {
  180. PRBool return_value;
  181. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_connected\n", 0, 0, 0 );
  182. PR_Lock(conn->lock);
  183. return_value = STATE_CONNECTED == conn->state;
  184. PR_Unlock(conn->lock);
  185. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_connected\n", 0, 0, 0 );
  186. return return_value;
  187. }
  188. /*
  189. * Destroy a connection object.
  190. */
  191. static void
  192. windows_conn_delete_internal(Repl_Connection *conn)
  193. {
  194. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_delete_internal\n", 0, 0, 0 );
  195. PR_ASSERT(NULL != conn);
  196. close_connection_internal(conn);
  197. /* slapi_ch_free accepts NULL pointer */
  198. slapi_ch_free((void **)&conn->hostname);
  199. slapi_ch_free((void **)&conn->binddn);
  200. slapi_ch_free((void **)&conn->plain);
  201. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_delete_internal\n", 0, 0, 0 );
  202. }
  203. /*
  204. * Destroy a connection. It is an error to use the connection object
  205. * after windows_conn_delete() has been called.
  206. */
  207. void
  208. windows_conn_delete(Repl_Connection *conn)
  209. {
  210. PRBool destroy_it = PR_FALSE;
  211. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_delete\n", 0, 0, 0 );
  212. PR_ASSERT(NULL != conn);
  213. PR_Lock(conn->lock);
  214. if (conn->linger_active)
  215. {
  216. if (slapi_eq_cancel(conn->linger_event) == 1)
  217. {
  218. /* Event was found and cancelled. Destroy the connection object. */
  219. PR_Unlock(conn->lock);
  220. destroy_it = PR_TRUE;
  221. }
  222. else
  223. {
  224. /*
  225. * The event wasn't found, but we think it's still active.
  226. * That means an event is in the process of being fired
  227. * off, so arrange for the event to destroy the object .
  228. */
  229. conn->delete_after_linger = PR_TRUE;
  230. PR_Unlock(conn->lock);
  231. }
  232. }
  233. if (destroy_it)
  234. {
  235. windows_conn_delete_internal(conn);
  236. }
  237. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_delete\n", 0, 0, 0 );
  238. }
  239. /*
  240. * Return the last operation type processed by the connection
  241. * object, and the LDAP error encountered.
  242. */
  243. void
  244. windows_conn_get_error(Repl_Connection *conn, int *operation, int *error)
  245. {
  246. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_get_error\n", 0, 0, 0 );
  247. PR_Lock(conn->lock);
  248. *operation = conn->last_operation;
  249. *error = conn->last_ldap_error;
  250. PR_Unlock(conn->lock);
  251. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_get_error\n", 0, 0, 0 );
  252. }
  253. void
  254. windows_conn_set_error(Repl_Connection *conn, int error)
  255. {
  256. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_set_error\n", 0, 0, 0 );
  257. PR_Lock(conn->lock);
  258. conn->last_ldap_error = error;
  259. PR_Unlock(conn->lock);
  260. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_error\n", 0, 0, 0 );
  261. }
  262. /*
  263. * Common code to send an LDAPv3 operation and collect the result.
  264. * Return values:
  265. * CONN_OPERATION_SUCCESS - the operation succeeded
  266. * CONN_OPERATION_FAILED - the operation was sent to the consumer
  267. * and failed. Use conn_get_error() to determine the LDAP error
  268. * code.
  269. * CONN_NOT_CONNECTED - no connection is active. The caller should
  270. * use conn_connect() to connect to the replica and bind, then should
  271. * reacquire the replica (if needed).
  272. * CONN_BUSY - the server is busy with previous requests, must wait for a while
  273. * before retrying
  274. */
  275. static ConnResult
  276. windows_perform_operation(Repl_Connection *conn, int optype, const char *dn,
  277. LDAPMod **attrs, const char *newrdn, const char *newparent,
  278. int deleteoldrdn, LDAPControl **server_controls,
  279. const char *extop_oid, struct berval *extop_payload, char **retoidp,
  280. struct berval **retdatap, LDAPControl ***returned_controls)
  281. {
  282. int rc = -1;
  283. ConnResult return_value;
  284. LDAPControl **loc_returned_controls;
  285. const char *op_string = NULL;
  286. const char *extra_op_string = NULL;
  287. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_perform_operation\n", 0, 0, 0 );
  288. if (windows_conn_connected(conn))
  289. {
  290. int msgid = -2; /* should match no messages */
  291. conn->last_operation = optype;
  292. switch (optype)
  293. {
  294. case CONN_ADD:
  295. conn->status = STATUS_PROCESSING_ADD;
  296. op_string = "add";
  297. rc = ldap_add_ext(conn->ld, dn, attrs, server_controls,
  298. NULL /* clientctls */, &msgid);
  299. break;
  300. case CONN_MODIFY:
  301. conn->status = STATUS_PROCESSING_MODIFY;
  302. op_string = "modify";
  303. rc = ldap_modify_ext(conn->ld, dn, attrs, server_controls,
  304. NULL /* clientctls */, &msgid);
  305. break;
  306. case CONN_DELETE:
  307. conn->status = STATUS_PROCESSING_DELETE;
  308. op_string = "delete";
  309. rc = ldap_delete_ext(conn->ld, dn, server_controls,
  310. NULL /* clientctls */, &msgid);
  311. break;
  312. case CONN_RENAME:
  313. conn->status = STATUS_PROCESSING_RENAME;
  314. op_string = "rename";
  315. rc = ldap_rename(conn->ld, dn, newrdn, newparent, deleteoldrdn,
  316. server_controls, NULL /* clientctls */, &msgid);
  317. break;
  318. case CONN_EXTENDED_OPERATION:
  319. conn->status = STATUS_PROCESSING_EXTENDED_OPERATION;
  320. op_string = "extended";
  321. extra_op_string = extop_oid;
  322. rc = ldap_extended_operation(conn->ld, extop_oid, extop_payload,
  323. server_controls, NULL /* clientctls */, &msgid);
  324. }
  325. if (LDAP_SUCCESS == rc)
  326. {
  327. LDAPMessage *res = NULL;
  328. int setlevel = 0;
  329. Slapi_Eq_Context eqctx = repl5_start_debug_timeout(&setlevel);
  330. rc = ldap_result(conn->ld, msgid, 1, &conn->timeout, &res);
  331. repl5_stop_debug_timeout(eqctx, &setlevel);
  332. if (0 == rc)
  333. {
  334. /* Timeout */
  335. rc = slapi_ldap_get_lderrno(conn->ld, NULL, NULL);
  336. conn->last_ldap_error = LDAP_TIMEOUT;
  337. return_value = CONN_TIMEOUT;
  338. }
  339. else if (-1 == rc)
  340. {
  341. /* Error */
  342. char *s = NULL;
  343. rc = slapi_ldap_get_lderrno(conn->ld, NULL, &s);
  344. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  345. "%s: Received error %d: %s for %s operation\n",
  346. agmt_get_long_name(conn->agmt),
  347. rc, s ? s : "NULL", op_string);
  348. conn->last_ldap_error = rc;
  349. /* some errors will require a disconnect and retry the connection
  350. later */
  351. if (IS_DISCONNECT_ERROR(rc))
  352. {
  353. windows_conn_disconnect(conn);
  354. return_value = CONN_NOT_CONNECTED;
  355. }
  356. else
  357. {
  358. conn->status = STATUS_CONNECTED;
  359. return_value = CONN_OPERATION_FAILED;
  360. }
  361. }
  362. else
  363. {
  364. int err;
  365. char *errmsg = NULL;
  366. char **referrals = NULL;
  367. char *matched = NULL;
  368. char *ptr;
  369. rc = ldap_parse_result(conn->ld, res, &err, &matched,
  370. &errmsg, &referrals, &loc_returned_controls,
  371. 0 /* Don't free the result */);
  372. if (IS_DISCONNECT_ERROR(rc))
  373. {
  374. conn->last_ldap_error = rc;
  375. windows_conn_disconnect(conn);
  376. return_value = CONN_NOT_CONNECTED;
  377. }
  378. else if (IS_DISCONNECT_ERROR(err))
  379. {
  380. conn->last_ldap_error = err;
  381. windows_conn_disconnect(conn);
  382. return_value = CONN_NOT_CONNECTED;
  383. }
  384. else if (err == LDAP_UNWILLING_TO_PERFORM && optype == CONN_MODIFY)
  385. {
  386. /* this permits password updates to fail gracefully */
  387. conn->last_ldap_error = LDAP_SUCCESS;
  388. return_value = CONN_OPERATION_SUCCESS;
  389. }
  390. else if (err == LDAP_ALREADY_EXISTS && optype == CONN_ADD)
  391. {
  392. /* Caller (windows_process_total_add) needs the ALREADY EXISTS info */
  393. conn->last_ldap_error = err;
  394. return_value = CONN_OPERATION_SUCCESS;
  395. }
  396. else if (err == LDAP_NO_SUCH_OBJECT && optype == CONN_DELETE)
  397. {
  398. conn->last_ldap_error = LDAP_SUCCESS;
  399. return_value = CONN_OPERATION_SUCCESS;
  400. }
  401. else /* regular operation, result returned */
  402. {
  403. if (NULL != returned_controls)
  404. {
  405. *returned_controls = loc_returned_controls;
  406. }
  407. if (LDAP_SUCCESS != rc)
  408. {
  409. conn->last_ldap_error = rc;
  410. }
  411. else
  412. {
  413. conn->last_ldap_error = err;
  414. }
  415. return_value = LDAP_SUCCESS == conn->last_ldap_error ? CONN_OPERATION_SUCCESS : CONN_OPERATION_FAILED;
  416. }
  417. /* remove extra newlines from AD error message */
  418. for (ptr = errmsg; ptr && *ptr; ++ptr) {
  419. if ((*ptr == '\n') || (*ptr == '\r')) {
  420. *ptr = ' ';
  421. }
  422. }
  423. /* handle special case of constraint violation - give admin
  424. * enough information to allow them to fix the problem
  425. * and retry - bug 170350 */
  426. if (conn->last_ldap_error == LDAP_CONSTRAINT_VIOLATION) {
  427. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  428. "%s: Received error [%s] when attempting to %s"
  429. " entry [%s]: Please correct the attribute specified "
  430. "in the error message. Refer to the Windows Active "
  431. "Directory docs for more information.\n",
  432. agmt_get_long_name(conn->agmt),
  433. errmsg, op_string, dn);
  434. } else {
  435. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  436. "%s: Received result code %d (%s) for %s operation %s%s\n",
  437. agmt_get_long_name(conn->agmt),
  438. conn->last_ldap_error, errmsg,
  439. op_string,
  440. extra_op_string == NULL ? "" : extra_op_string,
  441. extra_op_string == NULL ? "" : " ");
  442. }
  443. /*
  444. * XXXggood do I need to free matched, referrals,
  445. * anything else? Or can I pass NULL for the args
  446. * I'm not interested in?
  447. */
  448. /* Good question! Meanwhile, as RTM aproaches, let's free them... */
  449. slapi_ch_free((void **) &errmsg);
  450. slapi_ch_free((void **) &matched);
  451. charray_free(referrals);
  452. conn->status = STATUS_CONNECTED;
  453. }
  454. if (res) ldap_msgfree(res);
  455. }
  456. else
  457. {
  458. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  459. "%s: Failed to send %s operation: LDAP error %d (%s)\n",
  460. agmt_get_long_name(conn->agmt),
  461. op_string ? op_string : "NULL", rc, ldap_err2string(rc));
  462. conn->last_ldap_error = rc;
  463. if (IS_DISCONNECT_ERROR(rc))
  464. {
  465. windows_conn_disconnect(conn);
  466. return_value = CONN_NOT_CONNECTED;
  467. }
  468. else
  469. {
  470. conn->status = STATUS_CONNECTED;
  471. return_value = CONN_OPERATION_FAILED;
  472. }
  473. }
  474. }
  475. else
  476. {
  477. /* conn->last_ldap_error has been set to a more specific value
  478. * in windows_conn_connected()
  479. * conn->last_ldap_error = LDAP_SERVER_DOWN;
  480. */
  481. return_value = CONN_NOT_CONNECTED;
  482. }
  483. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_perform_operation\n", 0, 0, 0 );
  484. return return_value;
  485. }
  486. /* Copied from the chaining backend*/
  487. /*
  488. * exattrs: exceeded attribute list
  489. * If attr value pair exceeds MaxValRange, AD returns, e.g.,
  490. * <attr>;range=0-<maxValRange-1>: <value>
  491. * We need to repeat the search with "<attr>;range=1500-*"
  492. * until it returns
  493. * <attr>;range=<num>-*
  494. */
  495. static Slapi_Entry *
  496. windows_LDAPMessage2Entry(Slapi_Entry *e, Repl_Connection *conn,
  497. LDAPMessage * msg, int attrsonly, char ***exattrs)
  498. {
  499. Slapi_Entry *rawentry = NULL;
  500. char *a = NULL;
  501. BerElement * ber = NULL;
  502. LDAP *ld = conn->ld;
  503. int exattrlen = 0;
  504. int exattridx = 0;
  505. char **deletedattrs = NULL;
  506. char **dap;
  507. windows_private_set_raw_entry(conn->agmt, NULL); /* clear it first */
  508. if (msg == NULL) {
  509. return NULL;
  510. }
  511. /*
  512. * dn not allocated by slapi
  513. * attribute type and values ARE allocated
  514. */
  515. if (NULL == e) {
  516. e = slapi_entry_alloc();
  517. slapi_entry_set_dn( e, ldap_get_dn( ld, msg ) );
  518. }
  519. rawentry = slapi_entry_alloc();
  520. if ( rawentry == NULL ) {
  521. slapi_entry_free(e);
  522. return NULL;
  523. }
  524. slapi_entry_set_sdn( rawentry, slapi_entry_get_sdn(e) );
  525. for ( a = ldap_first_attribute( ld, msg, &ber ); a!=NULL; a=ldap_next_attribute( ld, msg, ber ) )
  526. {
  527. struct berval ** aVal = ldap_get_values_len( ld, msg, a);
  528. slapi_entry_add_values(rawentry, a, aVal);
  529. if (0 == strcasecmp(a,"dnsRecord") || 0 == strcasecmp(a,"dnsproperty") ||
  530. 0 == strcasecmp(a,"dscorepropagationdata"))
  531. {
  532. /* AD returns us entries with these attributes that we are not interested in,
  533. * but they break the entry attribute code (I think it is looking at null-terminated
  534. * string values, but the values are binary here). It appears that AD has some problems
  535. * with allowing duplicate values for system-only multi-valued attributes. So we skip
  536. * those attributes as a workaround.
  537. */
  538. ;
  539. } else
  540. {
  541. if (attrsonly)
  542. {
  543. slapi_entry_add_value(e, a, (Slapi_Value *)NULL);
  544. } else
  545. {
  546. #define SUBTYPERANGE "range="
  547. char *type_to_use = NULL;
  548. char *dupa = slapi_ch_strdup(a);
  549. char *newa = NULL; /* dup of 'a' with next range */
  550. char *p, *wp, *pp; /* work pointers */
  551. char *iter = NULL;
  552. int high = 0;
  553. int sizea = strlen(a) + 2;
  554. /* handling subtype(s) */
  555. ldap_utf8strtok_r(dupa, ";", &iter); /* primry type */
  556. p = ldap_utf8strtok_r(NULL, ";", &iter); /* subtype, if any */
  557. while (p) {
  558. if (0 == strncasecmp(p, SUBTYPERANGE, sizeof(SUBTYPERANGE) - 1)) {
  559. /* get rid of range */
  560. if (!newa) { /* first time for range= */
  561. /* Cannot use strdup,
  562. * since 'a' could be "<attr>;range=0-9";
  563. * then newa is <attr>;10-*; newa is 1 char longer than a. */
  564. newa = (char *)slapi_ch_malloc(sizea);
  565. PR_snprintf(newa, sizea, "%s", a);
  566. *(newa + (p - dupa) - 1) = '\0';
  567. }
  568. /* get the last count (high + 1) */
  569. /* range=low-high */
  570. pp = strchr(p, '-');
  571. if (*++pp == '*') {
  572. high = 0; /* high is *; done! */
  573. } else {
  574. high = strtol(pp, &p, 10);
  575. if (high > 0) {
  576. /* next low == high + 1 */
  577. high++;
  578. }
  579. }
  580. } else { /* subtype except "range=low-high" */
  581. if (newa) {
  582. int sizenewa = strlen(newa);
  583. /* range= appeared before, copy this subtype */
  584. wp = newa + sizenewa;
  585. /* append ;<subtype> */
  586. PR_snprintf(wp, sizea - sizenewa, ";%s", p);
  587. }
  588. }
  589. p = ldap_utf8strtok_r(NULL, ";", &iter);
  590. }
  591. slapi_ch_free_string(&dupa);
  592. /* Work around the fact that we alias street and streetaddress, while Microsoft do not */
  593. if (0 == strcasecmp(a, "streetaddress")) {
  594. type_to_use = FAKE_STREET_ATTR_NAME;
  595. } else if (newa) {
  596. type_to_use = newa;
  597. } else {
  598. type_to_use = a;
  599. }
  600. /* If the list of attribute values is null, we need to delete this attribute
  601. * from the local entry.
  602. */
  603. if (aVal == NULL) {
  604. /* Windows will send us an attribute with no values if it was deleted
  605. * on the AD side. Add this attribute to the deleted attributes list */
  606. /* Set it to the deleted attribute list only if the attribute does
  607. * not exist in the entry. For the multi-valued attribute (e.g.,
  608. * member), if there are multiple member attributes in an entry,
  609. * and one of them is deleted, this no value member is sent. But
  610. * if there are more member attributes in the entry, we should not
  611. * set member to the deleted attribute. */
  612. if (!charray_inlist(deletedattrs, type_to_use)) {
  613. charray_add(&deletedattrs, slapi_ch_strdup(type_to_use));
  614. }
  615. } else {
  616. slapi_entry_add_values(e, type_to_use, aVal);
  617. }
  618. /* if the addr for exattrs is given and next range retrieval is needed */
  619. if (exattrs && (high > 0)) {
  620. if (exattrlen == exattridx) {
  621. if (!*exattrs) {
  622. exattrlen = 4;
  623. exattridx = 0;
  624. *exattrs = (char **)slapi_ch_calloc(exattrlen, sizeof(char *));
  625. } else {
  626. *exattrs = (char **)slapi_ch_realloc((char *)*exattrs, exattrlen * 2 * sizeof(char *));
  627. memset(*exattrs + exattrlen, '\0', exattrlen * sizeof(char *));
  628. exattrlen *= 2;
  629. }
  630. PR_snprintf(newa + strlen(newa), strlen(a) + 2 - strlen(newa),
  631. ";%s%d-*", SUBTYPERANGE, high);
  632. (*exattrs)[exattridx++] = newa;
  633. }
  634. } else if (newa) {
  635. slapi_ch_free_string(&newa);
  636. }
  637. }
  638. }
  639. ldap_memfree(a);
  640. ldap_value_free_len(aVal);
  641. }
  642. if ( NULL != ber )
  643. {
  644. ber_free( ber, 0 );
  645. }
  646. /* Windows will send us an attribute with no values if it was deleted
  647. * on the AD side. Add this attribute to the deleted attributes list */
  648. /* Set to e_deleted_attrs only if there is no attribute of the type. */
  649. for (dap = deletedattrs; dap && *dap; dap++) {
  650. Slapi_Attr *attr = NULL;
  651. if (slapi_entry_attr_find(e, *dap, &attr)) { /* not found */
  652. attr = slapi_attr_new();
  653. slapi_attr_init(attr, *dap);
  654. entry_add_deleted_attribute_wsi(e, attr);
  655. }
  656. }
  657. charray_free(deletedattrs);
  658. windows_private_set_raw_entry(conn->agmt, rawentry); /* windows private now owns rawentry */
  659. return e;
  660. }
  661. /* Perform a simple search against Windows with no controls */
  662. ConnResult
  663. windows_search_entry(Repl_Connection *conn, char* searchbase, char *filter, Slapi_Entry **entry)
  664. {
  665. return windows_search_entry_ext(conn, searchbase, filter, entry, NULL, LDAP_SCOPE_SUBTREE);
  666. }
  667. /* Perform a simple search against Windows with optional controls */
  668. ConnResult
  669. windows_search_entry_ext(Repl_Connection *conn, char* searchbase, char *filter, Slapi_Entry **entry, LDAPControl **serverctrls, int scope)
  670. {
  671. ConnResult return_value = 0;
  672. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_search_entry\n", 0, 0, 0 );
  673. if (!entry) {
  674. LDAPDebug( LDAP_DEBUG_ANY, "windows_search_entry: NULL entry\n", 0, 0, 0 );
  675. return_value = CONN_LOCAL_ERROR; /* entry should never == NULL here */
  676. goto done;
  677. }
  678. *entry = NULL;
  679. if (windows_conn_connected(conn))
  680. {
  681. int ldap_rc = 0;
  682. LDAPMessage *res = NULL;
  683. char *searchbase_copy = slapi_ch_strdup(searchbase);
  684. char *filter_copy = slapi_ch_strdup(filter);
  685. char **attrs = NULL;
  686. char **exattrs = NULL;
  687. LDAPControl **serverctrls_copy = NULL;
  688. slapi_add_controls(&serverctrls_copy, serverctrls, 1 /* make a copy we can free */);
  689. LDAPDebug( LDAP_DEBUG_REPL, "Calling windows entry search request plugin\n", 0, 0, 0 );
  690. winsync_plugin_call_pre_ad_search_cb(conn->agmt, NULL, &searchbase_copy, &scope, &filter_copy,
  691. &attrs, &serverctrls_copy);
  692. next:
  693. ldap_rc = ldap_search_ext_s(conn->ld, searchbase_copy, scope,
  694. filter_copy, attrs, 0 /* attrsonly */,
  695. serverctrls_copy , NULL /* client controls */,
  696. &conn->timeout, 0 /* sizelimit */, &res);
  697. if ((LDAP_SUCCESS != ldap_rc) && !IS_DISCONNECT_ERROR(ldap_rc)) {
  698. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  699. "Could not retrieve entry from Windows using search "
  700. "base [%s] scope [%d] filter [%s]: error %d:%s\n",
  701. searchbase_copy, scope, filter_copy, ldap_rc,
  702. ldap_err2string(ldap_rc));
  703. }
  704. slapi_ch_array_free(attrs);
  705. attrs = NULL;
  706. if (LDAP_SUCCESS == ldap_rc)
  707. {
  708. LDAPMessage *message = ldap_first_entry(conn->ld, res);
  709. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  710. int nummessages = 0;
  711. int numentries = 0;
  712. int numreferences = 0;
  713. nummessages = ldap_count_messages(conn->ld, res);
  714. numentries = ldap_count_entries(conn->ld, res);
  715. numreferences = ldap_count_references(conn->ld, res);
  716. LDAPDebug( LDAP_DEBUG_REPL, "windows_search_entry: received %d messages, %d entries, %d references\n",
  717. nummessages, numentries, numreferences );
  718. }
  719. if (entry) {
  720. exattrs = NULL;
  721. *entry = windows_LDAPMessage2Entry(*entry, conn, message, 0, &exattrs);
  722. if (exattrs) {
  723. /* some attribute returned "<attr>;range=low-high" */
  724. attrs = exattrs;
  725. if (res) {
  726. ldap_msgfree(res);
  727. res = NULL;
  728. }
  729. goto next;
  730. }
  731. }
  732. /* See if there are any more entries : if so then that's an error
  733. * but we still need to get them to avoid gumming up the connection
  734. */
  735. /* have to check message first - cannot pass a NULL message */
  736. while (message && (NULL != ( message = ldap_next_entry(conn->ld,message)))) ;
  737. return_value = CONN_OPERATION_SUCCESS;
  738. }
  739. else if (IS_DISCONNECT_ERROR(ldap_rc))
  740. {
  741. windows_conn_disconnect(conn);
  742. return_value = CONN_NOT_CONNECTED;
  743. }
  744. else
  745. {
  746. return_value = CONN_OPERATION_FAILED;
  747. }
  748. slapi_ch_free_string(&searchbase_copy);
  749. slapi_ch_free_string(&filter_copy);
  750. ldap_controls_free(serverctrls_copy);
  751. serverctrls_copy = NULL;
  752. conn->last_ldap_error = ldap_rc;
  753. if (NULL != res)
  754. {
  755. ldap_msgfree(res);
  756. res = NULL;
  757. }
  758. }
  759. else
  760. {
  761. return_value = CONN_NOT_CONNECTED;
  762. }
  763. done:
  764. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_search_entry\n", 0, 0, 0 );
  765. return return_value;
  766. }
  767. ConnResult
  768. send_dirsync_search(Repl_Connection *conn)
  769. {
  770. ConnResult return_value;
  771. LDAPDebug( LDAP_DEBUG_TRACE, "=> send_dirsync_search\n", 0, 0, 0 );
  772. if (windows_conn_connected(conn))
  773. {
  774. const char *op_string = NULL;
  775. int rc;
  776. int scope = LDAP_SCOPE_SUBTREE;
  777. const char *userfilter = NULL;
  778. char *filter = NULL;
  779. char **attrs = NULL;
  780. LDAPControl **server_controls = NULL;
  781. int msgid;
  782. const char *old_dn = slapi_sdn_get_ndn( windows_private_get_windows_subtree(conn->agmt) );
  783. /* LDAP_SERVER_DIRSYNC_OID requires the search base Naming Context */
  784. char *dn = slapi_ch_strdup(strstr(old_dn, "dc="));
  785. char **exattrs = NULL;
  786. if (conn->supports_dirsync == 0)
  787. {
  788. /* unsupported */
  789. } else
  790. {
  791. slapi_add_control_ext(&server_controls,
  792. windows_private_dirsync_control(conn->agmt),
  793. 0 /* no copy - passin */);
  794. }
  795. conn->last_operation = CONN_SEARCH;
  796. conn->status = STATUS_SEARCHING;
  797. op_string = "search";
  798. LDAPDebug( LDAP_DEBUG_REPL, "Calling dirsync search request plugin\n", 0, 0, 0 );
  799. userfilter = windows_private_get_windows_userfilter(conn->agmt);
  800. if (userfilter) {
  801. filter = slapi_ch_strdup(userfilter);
  802. } else {
  803. filter = slapi_ch_strdup("(objectclass=*)");
  804. }
  805. winsync_plugin_call_dirsync_search_params_cb(conn->agmt, old_dn, &dn, &scope, &filter,
  806. &attrs, &server_controls);
  807. exattrs = windows_private_get_range_attrs(conn->agmt);
  808. charray_merge(&attrs, exattrs, 0 /* pass in */);
  809. slapi_ch_free((void **)&exattrs); /* strings are passed in */
  810. LDAPDebug( LDAP_DEBUG_REPL, "Sending dirsync search request\n", 0, 0, 0 );
  811. rc = ldap_search_ext( conn->ld, dn, scope, filter, attrs, PR_FALSE, server_controls,
  812. NULL /* ClientControls */, 0,0, &msgid);
  813. if (LDAP_SUCCESS == rc)
  814. {
  815. return_value = 0;
  816. }
  817. else
  818. {
  819. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  820. "%s: Failed to get %s operation: LDAP error %d (%s)\n",
  821. agmt_get_long_name(conn->agmt),
  822. op_string, rc, ldap_err2string(rc));
  823. conn->last_ldap_error = rc;
  824. if (IS_DISCONNECT_ERROR(rc))
  825. {
  826. windows_conn_disconnect(conn);
  827. return_value = CONN_NOT_CONNECTED;
  828. }
  829. else
  830. {
  831. conn->status = STATUS_CONNECTED;
  832. return_value = CONN_OPERATION_FAILED;
  833. }
  834. }
  835. /* cleanup */
  836. slapi_ch_free_string(&dn);
  837. slapi_ch_free_string(&filter);
  838. slapi_ch_array_free(attrs);
  839. attrs = NULL;
  840. ldap_controls_free(server_controls);
  841. server_controls = NULL;
  842. }
  843. else
  844. {
  845. /* conn->last_ldap_error has been set to a more specific value
  846. * in windows_conn_connected()
  847. * conn->last_ldap_error = LDAP_SERVER_DOWN;
  848. */
  849. return_value = CONN_NOT_CONNECTED;
  850. }
  851. LDAPDebug( LDAP_DEBUG_TRACE, "<= send_dirsync_search\n", 0, 0, 0 );
  852. return return_value;
  853. }
  854. /*
  855. * Send an LDAP add operation.
  856. */
  857. ConnResult
  858. windows_conn_send_add(Repl_Connection *conn, const char *dn, LDAPMod **attrs,
  859. LDAPControl **server_controls, LDAPControl ***returned_controls)
  860. {
  861. ConnResult res = 0;
  862. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_send_add\n", 0, 0, 0 );
  863. res = windows_perform_operation(conn, CONN_ADD, dn, attrs, NULL /* newrdn */,
  864. NULL /* newparent */, 0 /* deleteoldrdn */, server_controls,
  865. NULL /* extop OID */, NULL /* extop payload */, NULL /* retoidp */,
  866. NULL /* retdatap */, returned_controls);
  867. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_send_add\n", 0, 0, 0 );
  868. return res;
  869. }
  870. /*
  871. * Send an LDAP delete operation.
  872. */
  873. ConnResult
  874. windows_conn_send_delete(Repl_Connection *conn, const char *dn,
  875. LDAPControl **server_controls, LDAPControl ***returned_controls)
  876. {
  877. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_send_delete\n", 0, 0, 0 );
  878. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_send_delete\n", 0, 0, 0 );
  879. return windows_perform_operation(conn, CONN_DELETE, dn, NULL /* attrs */,
  880. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  881. server_controls, NULL /* extop OID */, NULL /* extop payload */,
  882. NULL /* retoidp */, NULL /* retdatap */, returned_controls);
  883. }
  884. /*
  885. * Send an LDAP modify operation.
  886. */
  887. ConnResult
  888. windows_conn_send_modify(Repl_Connection *conn, const char *dn, LDAPMod **mods,
  889. LDAPControl **server_controls, LDAPControl ***returned_controls)
  890. {
  891. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_send_modify\n", 0, 0, 0 );
  892. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_send_modify\n", 0, 0, 0 );
  893. return windows_perform_operation(conn, CONN_MODIFY, dn, mods, NULL /* newrdn */,
  894. NULL /* newparent */, 0 /* deleteoldrdn */, server_controls,
  895. NULL /* extop OID */, NULL /* extop payload */, NULL /* retoidp */,
  896. NULL /* retdatap */, returned_controls);
  897. }
  898. /*
  899. * Send an LDAP moddn operation.
  900. */
  901. ConnResult
  902. windows_conn_send_rename(Repl_Connection *conn, const char *dn,
  903. const char *newrdn, const char *newparent, int deleteoldrdn,
  904. LDAPControl **server_controls, LDAPControl ***returned_controls)
  905. {
  906. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_send_rename\n", 0, 0, 0 );
  907. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_send_rename\n", 0, 0, 0 );
  908. return windows_perform_operation(conn, CONN_RENAME, dn, NULL /* attrs */,
  909. newrdn, newparent, deleteoldrdn, server_controls,
  910. NULL /* extop OID */, NULL /* extop payload */, NULL /* retoidp */,
  911. NULL /* retdatap */, returned_controls);
  912. }
  913. /*
  914. * Send an LDAP search operation.
  915. */
  916. Slapi_Entry * windows_conn_get_search_result(Repl_Connection *conn)
  917. {
  918. int rc=0;
  919. LDAPMessage *res = NULL;
  920. Slapi_Entry *e = NULL;
  921. LDAPMessage *lm = NULL;
  922. char *dn = "";
  923. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_get_search_result\n", 0, 0, 0 );
  924. if (windows_conn_connected(conn))
  925. {
  926. rc = ldap_result( conn->ld, LDAP_RES_ANY, 0, &conn->timeout, &res );
  927. switch (rc) {
  928. case 0:
  929. case -1:
  930. case LDAP_RES_SEARCH_REFERENCE:
  931. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name, "error in windows_conn_get_search_result, rc=%d\n", rc);
  932. break;
  933. case LDAP_RES_SEARCH_RESULT:
  934. {
  935. LDAPControl **returned_controls = NULL;
  936. int code = 0;
  937. /* Purify says this is a leak : */
  938. if (LDAP_SUCCESS != (rc = ldap_parse_result( conn->ld, res, &code, NULL, NULL, NULL, &returned_controls, 0 ))) {
  939. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  940. "error reading search result in windows_conn_get_search_result, rc=%d:%s\n", rc, ldap_err2string(rc));
  941. }
  942. if (returned_controls)
  943. {
  944. windows_private_update_dirsync_control(conn->agmt, returned_controls);
  945. ldap_controls_free(returned_controls);
  946. }
  947. if (windows_private_dirsync_has_more(conn->agmt)) {
  948. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,"received hasmore from dirsync\n");
  949. }
  950. }
  951. break;
  952. case LDAP_RES_SEARCH_ENTRY:
  953. {
  954. if (( dn = ldap_get_dn( conn->ld, res )) != NULL )
  955. {
  956. char **exattrs = NULL;
  957. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,"received entry from dirsync: %s\n", dn);
  958. lm = ldap_first_entry( conn->ld, res );
  959. e = windows_private_get_curr_entry(conn->agmt); /* if range search, e != NULL */
  960. e = windows_LDAPMessage2Entry(e, conn, lm, 0, &exattrs);
  961. ldap_memfree(dn);
  962. if (exattrs) {
  963. /* some attribute returned "<attr>;range=low-high" */
  964. windows_private_set_curr_entry(conn->agmt, e);
  965. windows_private_set_range_attrs(conn->agmt, exattrs);
  966. } else {
  967. windows_private_set_curr_entry(conn->agmt, NULL);
  968. windows_private_set_range_attrs(conn->agmt, NULL);
  969. }
  970. }
  971. }
  972. break;
  973. } /* switch */
  974. } /* if */
  975. if (res)
  976. {
  977. ldap_msgfree( res );
  978. res = NULL;
  979. }
  980. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_get_search_result\n", 0, 0, 0 );
  981. return e;
  982. }
  983. /*
  984. * Send an LDAP extended operation.
  985. */
  986. ConnResult
  987. windows_conn_send_extended_operation(Repl_Connection *conn, const char *extop_oid,
  988. struct berval *payload, char **retoidp, struct berval **retdatap,
  989. LDAPControl **server_controls, LDAPControl ***returned_controls)
  990. {
  991. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_send_extended_operation\n", 0, 0, 0 );
  992. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_send_extended_operation\n", 0, 0, 0 );
  993. return windows_perform_operation(conn, CONN_EXTENDED_OPERATION, NULL /* dn */, NULL /* attrs */,
  994. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  995. server_controls, extop_oid, payload, retoidp, retdatap,
  996. returned_controls);
  997. }
  998. /*
  999. * Synchronously read an entry and return a specific attribute's values.
  1000. * Returns CONN_OPERATION_SUCCESS if successful. Returns
  1001. * CONN_OPERATION_FAILED if the operation was sent but an LDAP error
  1002. * occurred (conn->last_ldap_error is set in this case), and
  1003. * CONN_NOT_CONNECTED if no connection was active.
  1004. *
  1005. * The caller must free the returned_bvals.
  1006. */
  1007. ConnResult
  1008. windows_conn_read_entry_attribute(Repl_Connection *conn, const char *dn,
  1009. char *type, struct berval ***returned_bvals)
  1010. {
  1011. ConnResult return_value;
  1012. int ldap_rc;
  1013. LDAPControl *server_controls[2];
  1014. LDAPMessage *res = NULL;
  1015. char *attrs[2];
  1016. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_read_entry_attribute\n", 0, 0, 0 );
  1017. PR_ASSERT(NULL != type);
  1018. if (windows_conn_connected(conn))
  1019. {
  1020. server_controls[0] = &manageDSAITControl;
  1021. server_controls[1] = NULL;
  1022. attrs[0] = type;
  1023. attrs[1] = NULL;
  1024. ldap_rc = ldap_search_ext_s(conn->ld, dn, LDAP_SCOPE_BASE,
  1025. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1026. server_controls, NULL /* client controls */,
  1027. &conn->timeout, 0 /* sizelimit */, &res);
  1028. if (LDAP_SUCCESS == ldap_rc)
  1029. {
  1030. LDAPMessage *entry = ldap_first_entry(conn->ld, res);
  1031. if (NULL != entry)
  1032. {
  1033. *returned_bvals = ldap_get_values_len(conn->ld, entry, type);
  1034. }
  1035. return_value = CONN_OPERATION_SUCCESS;
  1036. }
  1037. else if (IS_DISCONNECT_ERROR(ldap_rc))
  1038. {
  1039. windows_conn_disconnect(conn);
  1040. return_value = CONN_NOT_CONNECTED;
  1041. }
  1042. else
  1043. {
  1044. return_value = CONN_OPERATION_FAILED;
  1045. }
  1046. conn->last_ldap_error = ldap_rc;
  1047. if (NULL != res)
  1048. {
  1049. ldap_msgfree(res);
  1050. res = NULL;
  1051. }
  1052. }
  1053. else
  1054. {
  1055. return_value = CONN_NOT_CONNECTED;
  1056. }
  1057. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_read_entry_attribute\n", 0, 0, 0 );
  1058. return return_value;
  1059. }
  1060. /*
  1061. * Return an pointer to a string describing the connection's status.
  1062. */
  1063. const char *
  1064. windows_conn_get_status(Repl_Connection *conn)
  1065. {
  1066. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_get_status\n", 0, 0, 0 );
  1067. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_get_status\n", 0, 0, 0 );
  1068. return conn->status;
  1069. }
  1070. /*
  1071. * Cancel any outstanding linger timer. Should be called when
  1072. * a replication session is beginning.
  1073. */
  1074. void
  1075. windows_conn_cancel_linger(Repl_Connection *conn)
  1076. {
  1077. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_cancel_linger\n", 0, 0, 0 );
  1078. PR_ASSERT(NULL != conn);
  1079. PR_Lock(conn->lock);
  1080. if (conn->linger_active)
  1081. {
  1082. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1083. "%s: Cancelling linger on the connection\n",
  1084. agmt_get_long_name(conn->agmt));
  1085. conn->linger_active = PR_FALSE;
  1086. if (slapi_eq_cancel(conn->linger_event) == 1)
  1087. {
  1088. conn->refcnt--;
  1089. }
  1090. conn->linger_event = NULL;
  1091. conn->status = STATUS_CONNECTED;
  1092. }
  1093. else
  1094. {
  1095. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1096. "%s: No linger to cancel on the connection\n",
  1097. agmt_get_long_name(conn->agmt));
  1098. }
  1099. PR_Unlock(conn->lock);
  1100. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_cancel_linger\n", 0, 0, 0 );
  1101. }
  1102. /*
  1103. * Called when our linger timeout timer expires. This means
  1104. * we should check to see if perhaps the connection's become
  1105. * active again, in which case we do nothing. Otherwise,
  1106. * we close the connection.
  1107. */
  1108. static void
  1109. linger_timeout(time_t event_time, void *arg)
  1110. {
  1111. PRBool delete_now;
  1112. Repl_Connection *conn = (Repl_Connection *)arg;
  1113. LDAPDebug( LDAP_DEBUG_TRACE, "=> linger_timeout\n", 0, 0, 0 );
  1114. PR_ASSERT(NULL != conn);
  1115. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1116. "%s: Linger timeout has expired on the connection\n",
  1117. agmt_get_long_name(conn->agmt));
  1118. PR_Lock(conn->lock);
  1119. if (conn->linger_active)
  1120. {
  1121. conn->linger_active = PR_FALSE;
  1122. conn->linger_event = NULL;
  1123. close_connection_internal(conn);
  1124. }
  1125. delete_now = conn->delete_after_linger;
  1126. PR_Unlock(conn->lock);
  1127. if (delete_now)
  1128. {
  1129. windows_conn_delete_internal(conn);
  1130. }
  1131. LDAPDebug( LDAP_DEBUG_TRACE, "<= linger_timeout\n", 0, 0, 0 );
  1132. }
  1133. /*
  1134. * Indicate that a session is ending. The linger timer starts when
  1135. * this function is called.
  1136. */
  1137. void
  1138. windows_conn_start_linger(Repl_Connection *conn)
  1139. {
  1140. time_t now;
  1141. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_start_linger\n", 0, 0, 0 );
  1142. PR_ASSERT(NULL != conn);
  1143. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1144. "%s: Beginning linger on the connection\n",
  1145. agmt_get_long_name(conn->agmt));
  1146. if (!windows_conn_connected(conn))
  1147. {
  1148. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1149. "%s: No linger on the closed conn\n",
  1150. agmt_get_long_name(conn->agmt));
  1151. return;
  1152. }
  1153. time(&now);
  1154. PR_Lock(conn->lock);
  1155. if (conn->linger_active)
  1156. {
  1157. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1158. "%s: Linger already active on the connection\n",
  1159. agmt_get_long_name(conn->agmt));
  1160. }
  1161. else
  1162. {
  1163. conn->linger_active = PR_TRUE;
  1164. conn->linger_event = slapi_eq_once(linger_timeout, conn, now + conn->linger_time);
  1165. conn->status = STATUS_LINGERING;
  1166. }
  1167. PR_Unlock(conn->lock);
  1168. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_start_linger\n", 0, 0, 0 );
  1169. }
  1170. /*
  1171. * If no connection is currently active, opens a connection and binds to
  1172. * the remote server. If a connection is open (e.g. lingering) then
  1173. * this is a no-op.
  1174. *
  1175. * Returns CONN_OPERATION_SUCCESS on success, or CONN_OPERATION_FAILED
  1176. * on failure. Sets conn->last_ldap_error and conn->last_operation;
  1177. */
  1178. ConnResult
  1179. windows_conn_connect(Repl_Connection *conn)
  1180. {
  1181. int optdata;
  1182. int secure = 0;
  1183. char* binddn = NULL;
  1184. struct berval *creds = NULL;
  1185. ConnResult return_value = CONN_OPERATION_SUCCESS;
  1186. int pw_ret = 1;
  1187. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_connect\n", 0, 0, 0 );
  1188. /** Connection already open just return SUCCESS **/
  1189. if(conn->state == STATE_CONNECTED) {
  1190. goto done;
  1191. }
  1192. PR_Lock(conn->lock);
  1193. if (conn->flag_agmt_changed) {
  1194. /* So far we cannot change Hostname and Port */
  1195. /* slapi_ch_free((void **)&conn->hostname); */
  1196. /* conn->hostname = agmt_get_hostname(conn->agmt); */
  1197. /* conn->port = agmt_get_port(conn->agmt); */
  1198. slapi_ch_free((void **)&conn->binddn);
  1199. conn->binddn = agmt_get_binddn(conn->agmt);
  1200. conn->bindmethod = agmt_get_bindmethod(conn->agmt);
  1201. conn->transport_flags = agmt_get_transport_flags(conn->agmt);
  1202. conn->timeout.tv_sec = agmt_get_timeout(conn->agmt);
  1203. conn->flag_agmt_changed = 0;
  1204. slapi_ch_free((void **)&conn->plain);
  1205. }
  1206. PR_Unlock(conn->lock);
  1207. creds = agmt_get_credentials(conn->agmt);
  1208. if (conn->plain == NULL) {
  1209. char *plain = NULL;
  1210. /* kexcoff: for reversible encryption */
  1211. /* We need to test the return code of pw_rever_decode in order to decide
  1212. * if a free for plain will be needed (pw_ret == 0) or not (pw_ret != 0) */
  1213. pw_ret = pw_rever_decode(creds->bv_val, &plain, type_nsds5ReplicaCredentials);
  1214. /* Pb occured in decryption: stop now, binding will fail */
  1215. if ( pw_ret == -1 )
  1216. {
  1217. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1218. "%s: Decoding of the credentials failed.\n",
  1219. agmt_get_long_name(conn->agmt));
  1220. return_value = CONN_OPERATION_FAILED;
  1221. conn->last_ldap_error = LDAP_INVALID_CREDENTIALS;
  1222. conn->state = STATE_DISCONNECTED;
  1223. goto done;
  1224. } /* Else, does not mean that the plain is correct, only means the we had no internal
  1225. decoding pb */
  1226. conn->plain = slapi_ch_strdup (plain);
  1227. if (!pw_ret) slapi_ch_free((void**)&plain);
  1228. }
  1229. /* ugaston: if SSL has been selected in the replication agreement, SSL client
  1230. * initialisation should be done before ever trying to open any connection at all.
  1231. */
  1232. if (conn->transport_flags == TRANSPORT_FLAG_TLS) {
  1233. secure = 2;
  1234. } else if (conn->transport_flags == TRANSPORT_FLAG_SSL) {
  1235. secure = 1;
  1236. }
  1237. if (secure > 0) {
  1238. if (!NSS_IsInitialized()) {
  1239. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1240. "%s: SSL Not Initialized, Replication over SSL FAILED\n",
  1241. agmt_get_long_name(conn->agmt));
  1242. return_value = CONN_SSL_NOT_ENABLED;
  1243. conn->last_ldap_error = LDAP_INAPPROPRIATE_AUTH;
  1244. conn->last_operation = CONN_INIT;
  1245. goto done;
  1246. }
  1247. }
  1248. if (return_value == CONN_OPERATION_SUCCESS) {
  1249. #if !defined(USE_OPENLDAP)
  1250. int io_timeout_ms;
  1251. #endif
  1252. /* Now we initialize the LDAP Structure and set options */
  1253. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1254. "%s: Trying %s%s slapi_ldap_init_ext\n",
  1255. agmt_get_long_name(conn->agmt),
  1256. secure ? "secure" : "non-secure",
  1257. (secure == 2) ? " startTLS" : "");
  1258. conn->ld = slapi_ldap_init_ext(NULL, conn->hostname, conn->port, secure, 0, NULL);
  1259. if (NULL == conn->ld)
  1260. {
  1261. return_value = CONN_OPERATION_FAILED;
  1262. conn->state = STATE_DISCONNECTED;
  1263. conn->last_operation = CONN_INIT;
  1264. conn->last_ldap_error = LDAP_LOCAL_ERROR;
  1265. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1266. "%s: Failed to establish %s%sconnection to the consumer\n",
  1267. agmt_get_long_name(conn->agmt),
  1268. secure ? "secure " : "",
  1269. (secure == 2) ? "startTLS " : "");
  1270. goto done;
  1271. }
  1272. /* slapi_ch_strdup is OK with NULL strings */
  1273. binddn = slapi_ch_strdup(conn->binddn);
  1274. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1275. "%s: binddn = %s, passwd = %s\n",
  1276. agmt_get_long_name(conn->agmt),
  1277. binddn?binddn:"NULL", creds->bv_val?creds->bv_val:"NULL");
  1278. /* Set some options for the connection. */
  1279. optdata = LDAP_DEREF_NEVER; /* Don't dereference aliases */
  1280. ldap_set_option(conn->ld, LDAP_OPT_DEREF, &optdata);
  1281. optdata = LDAP_VERSION3; /* We need LDAP version 3 */
  1282. ldap_set_option(conn->ld, LDAP_OPT_PROTOCOL_VERSION, &optdata);
  1283. /* Don't chase any referrals (although we shouldn't get any) */
  1284. ldap_set_option(conn->ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
  1285. /* override the default timeout with the specified timeout */
  1286. #if defined(USE_OPENLDAP)
  1287. ldap_set_option(conn->ld, LDAP_OPT_NETWORK_TIMEOUT, &conn->timeout);
  1288. #else
  1289. io_timeout_ms = conn->timeout.tv_sec * 1000 + conn->timeout.tv_usec / 1000;
  1290. prldap_set_session_option(conn->ld, NULL, PRLDAP_OPT_IO_MAX_TIMEOUT,
  1291. io_timeout_ms);
  1292. #endif
  1293. /* We've got an ld. Now bind to the server. */
  1294. conn->last_operation = CONN_BIND;
  1295. }
  1296. if ( bind_and_check_pwp(conn, binddn, conn->plain) == CONN_OPERATION_FAILED )
  1297. {
  1298. conn->last_ldap_error = slapi_ldap_get_lderrno (conn->ld, NULL, NULL);
  1299. conn->state = STATE_DISCONNECTED;
  1300. return_value = CONN_OPERATION_FAILED;
  1301. }
  1302. else
  1303. {
  1304. conn->last_ldap_error = LDAP_SUCCESS;
  1305. conn->state = STATE_CONNECTED;
  1306. return_value = CONN_OPERATION_SUCCESS;
  1307. }
  1308. {
  1309. ConnResult supports = 0;
  1310. supports = windows_conn_replica_supports_dirsync(conn);
  1311. if (CONN_DOES_NOT_SUPPORT_DIRSYNC == supports)
  1312. {
  1313. /* We assume that a server that doesn't support dirsync is our NT4 LDAP service */
  1314. windows_private_set_isnt4(conn->agmt,1);
  1315. LDAPDebug( LDAP_DEBUG_REPL, "windows_conn_connect : detected NT4 peer\n", 0, 0, 0 );
  1316. } else
  1317. {
  1318. windows_private_set_isnt4(conn->agmt,0);
  1319. }
  1320. supports = windows_conn_replica_is_win2k3(conn);
  1321. if (CONN_IS_WIN2K3 == supports)
  1322. {
  1323. windows_private_set_iswin2k3(conn->agmt,1);
  1324. LDAPDebug( LDAP_DEBUG_REPL, "windows_conn_connect : detected Win2k3 or later peer\n", 0, 0, 0 );
  1325. } else
  1326. {
  1327. windows_private_set_iswin2k3(conn->agmt,0);
  1328. }
  1329. }
  1330. slapi_ch_free((void**)&binddn);
  1331. if(return_value == CONN_OPERATION_FAILED)
  1332. {
  1333. close_connection_internal(conn);
  1334. } else
  1335. {
  1336. conn->last_ldap_error = LDAP_SUCCESS;
  1337. conn->state = STATE_CONNECTED;
  1338. }
  1339. done:
  1340. if (creds) {
  1341. ber_bvfree(creds);
  1342. creds = NULL;
  1343. }
  1344. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_connect\n", 0, 0, 0 );
  1345. return return_value;
  1346. }
  1347. static void
  1348. close_connection_internal(Repl_Connection *conn)
  1349. {
  1350. LDAPDebug( LDAP_DEBUG_TRACE, "=> close_connection_internal\n", 0, 0, 0 );
  1351. if (NULL != conn->ld)
  1352. {
  1353. /* Since we call slapi_ldap_init,
  1354. we must call slapi_ldap_unbind */
  1355. slapi_ldap_unbind(conn->ld);
  1356. }
  1357. conn->ld = NULL;
  1358. conn->state = STATE_DISCONNECTED;
  1359. conn->status = STATUS_DISCONNECTED;
  1360. conn->supports_ds50_repl = -1;
  1361. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1362. "%s: Disconnected from the consumer\n", agmt_get_long_name(conn->agmt));
  1363. LDAPDebug( LDAP_DEBUG_TRACE, "<= close_connection_internal\n", 0, 0, 0 );
  1364. }
  1365. void
  1366. windows_conn_disconnect(Repl_Connection *conn)
  1367. {
  1368. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_disconnect\n", 0, 0, 0 );
  1369. PR_ASSERT(NULL != conn);
  1370. PR_Lock(conn->lock);
  1371. close_connection_internal(conn);
  1372. PR_Unlock(conn->lock);
  1373. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_disconnect\n", 0, 0, 0 );
  1374. }
  1375. /*
  1376. * Determine if the remote replica supports DS 5.0 replication.
  1377. * Return codes:
  1378. * CONN_SUPPORTS_DS5_REPL - the remote replica suport DS5 replication
  1379. * CONN_DOES_NOT_SUPPORT_DS5_REPL - the remote replica does not
  1380. * support DS5 replication.
  1381. * CONN_OPERATION_FAILED - it could not be determined if the remote
  1382. * replica supports DS5 replication.
  1383. * CONN_NOT_CONNECTED - no connection was active.
  1384. */
  1385. ConnResult
  1386. windows_conn_replica_supports_ds5_repl(Repl_Connection *conn)
  1387. {
  1388. ConnResult return_value;
  1389. int ldap_rc;
  1390. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_replica_supports_ds5_repl\n", 0, 0, 0 );
  1391. if (windows_conn_connected(conn))
  1392. {
  1393. if (conn->supports_ds50_repl == -1) {
  1394. LDAPMessage *res = NULL;
  1395. LDAPMessage *entry = NULL;
  1396. char *attrs[] = {"supportedcontrol", "supportedextension", NULL};
  1397. conn->status = STATUS_SEARCHING;
  1398. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1399. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1400. NULL /* server controls */, NULL /* client controls */,
  1401. &conn->timeout, LDAP_NO_LIMIT, &res);
  1402. if (LDAP_SUCCESS == ldap_rc)
  1403. {
  1404. conn->supports_ds50_repl = 0;
  1405. entry = ldap_first_entry(conn->ld, res);
  1406. if (!attribute_string_value_present(conn->ld, entry, "supportedcontrol", REPL_NSDS50_UPDATE_INFO_CONTROL_OID))
  1407. {
  1408. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1409. }
  1410. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_START_NSDS50_REPLICATION_REQUEST_OID))
  1411. {
  1412. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1413. }
  1414. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_END_NSDS50_REPLICATION_REQUEST_OID))
  1415. {
  1416. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1417. }
  1418. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_ENTRY_REQUEST_OID))
  1419. {
  1420. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1421. }
  1422. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_RESPONSE_OID))
  1423. {
  1424. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1425. }
  1426. else
  1427. {
  1428. conn->supports_ds50_repl = 1;
  1429. return_value = CONN_SUPPORTS_DS5_REPL;
  1430. }
  1431. }
  1432. else
  1433. {
  1434. if (IS_DISCONNECT_ERROR(ldap_rc))
  1435. {
  1436. conn->last_ldap_error = ldap_rc; /* specific reason */
  1437. windows_conn_disconnect(conn);
  1438. return_value = CONN_NOT_CONNECTED;
  1439. }
  1440. else
  1441. {
  1442. return_value = CONN_OPERATION_FAILED;
  1443. }
  1444. }
  1445. if (NULL != res)
  1446. ldap_msgfree(res);
  1447. }
  1448. else {
  1449. return_value = conn->supports_ds50_repl ? CONN_SUPPORTS_DS5_REPL : CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1450. }
  1451. }
  1452. else
  1453. {
  1454. /* Not connected */
  1455. return_value = CONN_NOT_CONNECTED;
  1456. }
  1457. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_replica_supports_ds5_repl\n", 0, 0, 0 );
  1458. return return_value;
  1459. }
  1460. ConnResult
  1461. windows_conn_replica_supports_dirsync(Repl_Connection *conn)
  1462. {
  1463. ConnResult return_value;
  1464. int ldap_rc;
  1465. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_replica_supports_dirsync\n", 0, 0, 0 );
  1466. if (getenv("WINSYNC_USE_DS")) {
  1467. /* used to fake out dirsync to think it's talking to a real ad when in fact
  1468. it's just talking to another directory server */
  1469. conn->supports_dirsync = 1;
  1470. return CONN_SUPPORTS_DIRSYNC;
  1471. }
  1472. if (windows_conn_connected(conn))
  1473. {
  1474. if (conn->supports_dirsync == -1) {
  1475. LDAPMessage *res = NULL;
  1476. LDAPMessage *entry = NULL;
  1477. char *attrs[] = {"supportedcontrol", NULL};
  1478. conn->status = STATUS_SEARCHING;
  1479. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1480. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1481. NULL /* server controls */, NULL /* client controls */,
  1482. &conn->timeout, LDAP_NO_LIMIT, &res);
  1483. if (LDAP_SUCCESS == ldap_rc)
  1484. {
  1485. conn->supports_dirsync = 0;
  1486. entry = ldap_first_entry(conn->ld, res);
  1487. if (!attribute_string_value_present(conn->ld, entry, "supportedcontrol", REPL_DIRSYNC_CONTROL_OID))
  1488. {
  1489. return_value = CONN_DOES_NOT_SUPPORT_DIRSYNC;
  1490. }
  1491. else
  1492. {
  1493. conn->supports_dirsync =1;
  1494. return_value = CONN_SUPPORTS_DIRSYNC;
  1495. }
  1496. }
  1497. else
  1498. {
  1499. if (IS_DISCONNECT_ERROR(ldap_rc))
  1500. {
  1501. conn->last_ldap_error = ldap_rc; /* specific reason */
  1502. windows_conn_disconnect(conn);
  1503. return_value = CONN_NOT_CONNECTED;
  1504. }
  1505. else
  1506. {
  1507. return_value = CONN_OPERATION_FAILED;
  1508. }
  1509. }
  1510. if (NULL != res)
  1511. ldap_msgfree(res);
  1512. }
  1513. else {
  1514. return_value = conn->supports_dirsync ? CONN_SUPPORTS_DIRSYNC : CONN_DOES_NOT_SUPPORT_DIRSYNC;
  1515. }
  1516. }
  1517. else
  1518. {
  1519. /* Not connected */
  1520. return_value = CONN_NOT_CONNECTED;
  1521. }
  1522. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_replica_supports_dirsync\n", 0, 0, 0 );
  1523. return return_value;
  1524. }
  1525. /* Checks if the AD server is running win2k3 (or later) */
  1526. ConnResult
  1527. windows_conn_replica_is_win2k3(Repl_Connection *conn)
  1528. {
  1529. ConnResult return_value;
  1530. int ldap_rc;
  1531. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_replica_is_win2k3\n", 0, 0, 0 );
  1532. if (windows_conn_connected(conn))
  1533. {
  1534. if (conn->is_win2k3 == -1) {
  1535. LDAPMessage *res = NULL;
  1536. LDAPMessage *entry = NULL;
  1537. char *attrs[] = {"supportedCapabilities", NULL};
  1538. conn->status = STATUS_SEARCHING;
  1539. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1540. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1541. NULL /* server controls */, NULL /* client controls */,
  1542. &conn->timeout, LDAP_NO_LIMIT, &res);
  1543. if (LDAP_SUCCESS == ldap_rc)
  1544. {
  1545. conn->is_win2k3 = 0;
  1546. entry = ldap_first_entry(conn->ld, res);
  1547. if (!attribute_string_value_present(conn->ld, entry, "supportedCapabilities", REPL_WIN2K3_AD_OID))
  1548. {
  1549. return_value = CONN_NOT_WIN2K3;
  1550. }
  1551. else
  1552. {
  1553. conn->is_win2k3 =1;
  1554. return_value = CONN_IS_WIN2K3;
  1555. }
  1556. }
  1557. else
  1558. {
  1559. if (IS_DISCONNECT_ERROR(ldap_rc))
  1560. {
  1561. conn->last_ldap_error = ldap_rc; /* specific reason */
  1562. windows_conn_disconnect(conn);
  1563. return_value = CONN_NOT_CONNECTED;
  1564. }
  1565. else
  1566. {
  1567. return_value = CONN_OPERATION_FAILED;
  1568. }
  1569. }
  1570. if (NULL != res)
  1571. ldap_msgfree(res);
  1572. }
  1573. else {
  1574. return_value = conn->is_win2k3 ? CONN_IS_WIN2K3 : CONN_NOT_WIN2K3;
  1575. }
  1576. }
  1577. else
  1578. {
  1579. /* Not connected */
  1580. return_value = CONN_NOT_CONNECTED;
  1581. }
  1582. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_replica_is_win2k3\n", 0, 0, 0 );
  1583. return return_value;
  1584. }
  1585. /*
  1586. * Return 1 if "value" is a value of attribute type "type" in entry "entry".
  1587. * Otherwise, return 0.
  1588. */
  1589. static int
  1590. attribute_string_value_present(LDAP *ld, LDAPMessage *entry, const char *type,
  1591. const char *value)
  1592. {
  1593. int return_value = 0;
  1594. ber_len_t vallen;
  1595. LDAPDebug( LDAP_DEBUG_TRACE, "=> attribute_string_value_present\n", 0, 0, 0 );
  1596. if (NULL != entry)
  1597. {
  1598. char *atype = NULL;
  1599. BerElement *ber = NULL;
  1600. vallen = strlen(value);
  1601. atype = ldap_first_attribute(ld, entry, &ber);
  1602. while (NULL != atype && 0 == return_value)
  1603. {
  1604. if (strcasecmp(atype, type) == 0)
  1605. {
  1606. struct berval **vals = ldap_get_values_len(ld, entry, atype);
  1607. int i;
  1608. for (i = 0; return_value == 0 && NULL != vals && NULL != vals[i]; i++)
  1609. {
  1610. if ((vallen == vals[i]->bv_len) && !strncmp(vals[i]->bv_val, value, vallen))
  1611. {
  1612. return_value = 1;
  1613. }
  1614. }
  1615. if (NULL != vals)
  1616. {
  1617. ldap_value_free_len(vals);
  1618. }
  1619. }
  1620. ldap_memfree(atype);
  1621. atype = ldap_next_attribute(ld, entry, ber);
  1622. }
  1623. if (NULL != ber)
  1624. ber_free(ber, 0);
  1625. /* The last atype has not been freed yet */
  1626. if (NULL != atype)
  1627. ldap_memfree(atype);
  1628. }
  1629. LDAPDebug( LDAP_DEBUG_TRACE, "<= attribute_string_value_present\n", 0, 0, 0 );
  1630. return return_value;
  1631. }
  1632. /*
  1633. * Read the remote server's schema entry, then read the local schema entry,
  1634. * and compare the nsschemacsn attribute. If the local csn is newer, or
  1635. * the remote csn is absent, push the schema down to the consumer.
  1636. * Return codes:
  1637. * CONN_SCHEMA_UPDATED if the schema was pushed successfully
  1638. * CONN_SCHEMA_NO_UPDATE_NEEDED if the schema was as new or newer than
  1639. * the local server's schema
  1640. * CONN_OPERATION_FAILED if an error occurred
  1641. * CONN_NOT_CONNECTED if no connection was active
  1642. * NOTE: Should only be called when a replication session has been
  1643. * established by sending a startReplication extended operation.
  1644. */
  1645. void
  1646. windows_conn_set_timeout(Repl_Connection *conn, long timeout)
  1647. {
  1648. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_set_timeout\n", 0, 0, 0 );
  1649. PR_ASSERT(NULL != conn);
  1650. PR_ASSERT(timeout >= 0);
  1651. PR_Lock(conn->lock);
  1652. conn->timeout.tv_sec = timeout;
  1653. PR_Unlock(conn->lock);
  1654. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_timeout\n", 0, 0, 0 );
  1655. }
  1656. void windows_conn_set_agmt_changed(Repl_Connection *conn)
  1657. {
  1658. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_set_agmt_changed\n", 0, 0, 0 );
  1659. PR_ASSERT(NULL != conn);
  1660. PR_Lock(conn->lock);
  1661. if (NULL != conn->agmt)
  1662. conn->flag_agmt_changed = 1;
  1663. PR_Unlock(conn->lock);
  1664. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_agmt_changed\n", 0, 0, 0 );
  1665. }
  1666. static const char *
  1667. bind_method_to_mech(int bindmethod)
  1668. {
  1669. switch (bindmethod) {
  1670. case BINDMETHOD_SSL_CLIENTAUTH:
  1671. return LDAP_SASL_EXTERNAL;
  1672. break;
  1673. case BINDMETHOD_SASL_GSSAPI:
  1674. return "GSSAPI";
  1675. break;
  1676. case BINDMETHOD_SASL_DIGEST_MD5:
  1677. return "DIGEST-MD5";
  1678. break;
  1679. default: /* anything else */
  1680. return LDAP_SASL_SIMPLE;
  1681. }
  1682. return LDAP_SASL_SIMPLE;
  1683. }
  1684. /*
  1685. * Check the result of an ldap_simple_bind operation to see we it
  1686. * contains the expiration controls
  1687. * return: -1 error, not bound
  1688. * 0, OK bind has succeeded
  1689. */
  1690. static int
  1691. bind_and_check_pwp(Repl_Connection *conn, char * binddn, char *password)
  1692. {
  1693. LDAPControl **ctrls = NULL;
  1694. LDAP *ld = conn->ld;
  1695. int rc;
  1696. const char *mech = bind_method_to_mech(conn->bindmethod);
  1697. LDAPDebug( LDAP_DEBUG_TRACE, "=> bind_and_check_pwp\n", 0, 0, 0 );
  1698. rc = slapi_ldap_bind(conn->ld, binddn, password, mech, NULL,
  1699. &ctrls, NULL, NULL);
  1700. if ( rc == LDAP_SUCCESS )
  1701. {
  1702. if (conn->last_ldap_error != rc)
  1703. {
  1704. conn->last_ldap_error = rc;
  1705. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1706. "%s: Replication bind with %s auth resumed\n",
  1707. agmt_get_long_name(conn->agmt),
  1708. mech ? mech : "SIMPLE");
  1709. }
  1710. if ( ctrls )
  1711. {
  1712. int i;
  1713. for( i = 0; ctrls[ i ] != NULL; ++i )
  1714. {
  1715. if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRED)) )
  1716. {
  1717. /* Bind is successfull but password has expired */
  1718. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1719. "%s: Successfully bound %s to consumer, "
  1720. "but password has expired on consumer.\n",
  1721. agmt_get_long_name(conn->agmt), binddn);
  1722. }
  1723. else if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRING)) )
  1724. {
  1725. /* The password is expiring in n seconds */
  1726. if ( (ctrls[ i ]->ldctl_value.bv_val != NULL) &&
  1727. (ctrls[ i ]->ldctl_value.bv_len > 0) )
  1728. {
  1729. int password_expiring = atoi( ctrls[ i ]->ldctl_value.bv_val );
  1730. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1731. "%s: Successfully bound %s to consumer, "
  1732. "but password is expiring on consumer in %d seconds.\n",
  1733. agmt_get_long_name(conn->agmt), binddn, password_expiring);
  1734. }
  1735. }
  1736. }
  1737. ldap_controls_free( ctrls );
  1738. }
  1739. LDAPDebug( LDAP_DEBUG_TRACE, "<= bind_and_check_pwp - CONN_OPERATION_SUCCESS\n", 0, 0, 0 );
  1740. return (CONN_OPERATION_SUCCESS);
  1741. }
  1742. else
  1743. {
  1744. ldap_controls_free( ctrls );
  1745. /* Do not report the same error over and over again
  1746. * unless replication level logging is enabled. */
  1747. if (conn->last_ldap_error != rc)
  1748. {
  1749. char *errmsg = NULL;
  1750. conn->last_ldap_error = rc;
  1751. /* errmsg is a pointer directly into the ld structure - do not free */
  1752. rc = slapi_ldap_get_lderrno( ld, NULL, &errmsg );
  1753. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1754. "%s: Replication bind with %s auth failed: LDAP error %d (%s) (%s)\n",
  1755. agmt_get_long_name(conn->agmt),
  1756. mech ? mech : "SIMPLE", rc,
  1757. ldap_err2string(rc), errmsg);
  1758. } else {
  1759. char *errmsg = NULL;
  1760. /* errmsg is a pointer directly into the ld structure - do not free */
  1761. rc = slapi_ldap_get_lderrno( ld, NULL, &errmsg );
  1762. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1763. "%s: Replication bind with %s auth failed: LDAP error %d (%s) (%s)\n",
  1764. agmt_get_long_name(conn->agmt),
  1765. mech ? mech : "SIMPLE", rc,
  1766. ldap_err2string(rc), errmsg);
  1767. }
  1768. LDAPDebug( LDAP_DEBUG_TRACE, "<= bind_and_check_pwp - CONN_OPERATION_FAILED\n", 0, 0, 0 );
  1769. return (CONN_OPERATION_FAILED);
  1770. }
  1771. }
  1772. /* Attempt to bind as a user to AD in order to see if we posess the
  1773. * most current password. Returns 0 if the bind was successful,
  1774. * non-zero otherwise. */
  1775. int
  1776. windows_check_user_password(Repl_Connection *conn, Slapi_DN *sdn, char *password)
  1777. {
  1778. const char *binddn = NULL;
  1779. LDAPMessage *res = NULL;
  1780. int rc = 0;
  1781. int msgid = 0;
  1782. int parse_rc = 0;
  1783. /* If we're already connected, this will just return success */
  1784. windows_conn_connect(conn);
  1785. /* Get binddn from sdn */
  1786. binddn = slapi_sdn_get_dn(sdn);
  1787. /* Attempt to do a bind on the existing connection
  1788. * using the dn and password that were passed in. */
  1789. msgid = do_simple_bind(conn, conn->ld, (char *) binddn, password);
  1790. rc = ldap_result(conn->ld, msgid, LDAP_MSG_ALL, NULL, &res);
  1791. if (0 > rc) { /* error */
  1792. rc = slapi_ldap_get_lderrno(conn->ld, NULL, NULL);
  1793. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1794. "Error reading bind response for id "
  1795. "[%s]: error %d (%s)\n",
  1796. binddn ? binddn : "(anon)",
  1797. rc, ldap_err2string(rc));
  1798. } else if (rc == 0) { /* timeout */
  1799. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1800. "Error: timeout reading "
  1801. "bind response for [%s]\n",
  1802. binddn ? binddn : "(anon)");
  1803. rc = -1;
  1804. } else {
  1805. parse_rc = ldap_parse_result( conn->ld, res, &rc, NULL, NULL, NULL, NULL, 1 /* Free res */);
  1806. if (parse_rc != LDAP_SUCCESS) {
  1807. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1808. "Error: unable to parse "
  1809. "bind result for [%s]: "
  1810. "error %d\n",
  1811. binddn ? binddn : "(anon)",
  1812. parse_rc);
  1813. rc = -1;
  1814. }
  1815. }
  1816. /* rebind as the DN specified in the sync agreement */
  1817. bind_and_check_pwp(conn, conn->binddn, conn->plain);
  1818. return rc;
  1819. }
  1820. static int
  1821. do_simple_bind (Repl_Connection *conn, LDAP *ld, char * binddn, char *password)
  1822. {
  1823. int msgid;
  1824. LDAPDebug( LDAP_DEBUG_TRACE, "=> do_simple_bind\n", 0, 0, 0 );
  1825. if( LDAP_SUCCESS != slapi_ldap_bind( ld, binddn, password, LDAP_SASL_SIMPLE, NULL, NULL, NULL, &msgid ) )
  1826. {
  1827. char *ldaperrtext = NULL;
  1828. int ldaperr;
  1829. int prerr = PR_GetError();
  1830. ldaperr = slapi_ldap_get_lderrno( ld, NULL, &ldaperrtext );
  1831. /* Do not report the same error over and over again */
  1832. if (conn->last_ldap_error != ldaperr)
  1833. {
  1834. conn->last_ldap_error = ldaperr;
  1835. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1836. "%s: Simple bind failed, "
  1837. SLAPI_COMPONENT_NAME_LDAPSDK " error %d (%s) (%s), "
  1838. SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
  1839. agmt_get_long_name(conn->agmt),
  1840. ldaperr, ldap_err2string(ldaperr),
  1841. ldaperrtext ? ldaperrtext : "",
  1842. prerr, slapd_pr_strerror(prerr));
  1843. }
  1844. }
  1845. else if (conn->last_ldap_error != LDAP_SUCCESS)
  1846. {
  1847. conn->last_ldap_error = LDAP_SUCCESS;
  1848. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1849. "%s: Simple bind resumed\n",
  1850. agmt_get_long_name(conn->agmt));
  1851. }
  1852. LDAPDebug( LDAP_DEBUG_TRACE, "<= do_simple_bind\n", 0, 0, 0 );
  1853. return msgid;
  1854. }
  1855. static Slapi_Eq_Context
  1856. repl5_start_debug_timeout(int *setlevel)
  1857. {
  1858. Slapi_Eq_Context eqctx = 0;
  1859. LDAPDebug( LDAP_DEBUG_TRACE, "=> repl5_start_debug_timeout\n", 0, 0, 0 );
  1860. if (s_debug_timeout && s_debug_level) {
  1861. time_t now = time(NULL);
  1862. eqctx = slapi_eq_once(repl5_debug_timeout_callback, setlevel,
  1863. s_debug_timeout + now);
  1864. }
  1865. LDAPDebug( LDAP_DEBUG_TRACE, "<= repl5_start_debug_timeout\n", 0, 0, 0 );
  1866. return eqctx;
  1867. }
  1868. static void
  1869. repl5_stop_debug_timeout(Slapi_Eq_Context eqctx, int *setlevel)
  1870. {
  1871. char buf[20];
  1872. char msg[SLAPI_DSE_RETURNTEXT_SIZE];
  1873. LDAPDebug( LDAP_DEBUG_TRACE, "=> repl5_stop_debug_timeout\n", 0, 0, 0 );
  1874. if (eqctx && !*setlevel) {
  1875. (void)slapi_eq_cancel(eqctx);
  1876. }
  1877. if (s_debug_timeout && s_debug_level && *setlevel) {
  1878. /* No longer needed as we are including the one in slap.h */
  1879. sprintf(buf, "%d", 0);
  1880. config_set_errorlog_level("nsslapd-errorlog-level", buf, msg, 1);
  1881. }
  1882. LDAPDebug( LDAP_DEBUG_TRACE, "<= repl5_stop_debug_timeout\n", 0, 0, 0 );
  1883. }
  1884. static void
  1885. repl5_debug_timeout_callback(time_t when, void *arg)
  1886. {
  1887. int *setlevel = (int *)arg;
  1888. /* No longer needed as we are including the one in slap.h */
  1889. char buf[20];
  1890. char msg[SLAPI_DSE_RETURNTEXT_SIZE];
  1891. LDAPDebug( LDAP_DEBUG_TRACE, "=> repl5_debug_timeout_callback\n", 0, 0, 0 );
  1892. *setlevel = 1;
  1893. sprintf(buf, "%d", s_debug_level);
  1894. config_set_errorlog_level("nsslapd-errorlog-level", buf, msg, 1);
  1895. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  1896. "repl5_debug_timeout_callback: set debug level to %d at %ld\n",
  1897. s_debug_level, when);
  1898. LDAPDebug( LDAP_DEBUG_TRACE, "<= repl5_debug_timeout_callback\n", 0, 0, 0 );
  1899. }