windows_connection.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760
  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. /* repl5_connection.c */
  39. /*
  40. The connection object manages a connection to a single replication
  41. consumer.
  42. XXXggood what to do on timeout? If we close connection, then we won't leave a
  43. replica locked. Seems like right thing to do.
  44. */
  45. #include "repl5.h"
  46. #include "windowsrepl.h"
  47. #include "ldappr.h"
  48. #include "slap.h"
  49. typedef struct repl_connection
  50. {
  51. char *hostname;
  52. int port;
  53. char *binddn;
  54. int bindmethod;
  55. int state;
  56. int last_operation;
  57. int last_ldap_error;
  58. const char *status;
  59. char *last_ldap_errmsg;
  60. PRUint32 transport_flags;
  61. LDAP *ld;
  62. int supports_ldapv3; /* 1 if does, 0 if doesn't, -1 if not determined */
  63. int supports_ds50_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  64. int supports_ds40_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  65. int linger_time; /* time in seconds to leave an idle connection open */
  66. int supports_dirsync; /* 1 if does, 0 if doesn't, -1 if not determined */
  67. PRBool linger_active;
  68. Slapi_Eq_Context *linger_event;
  69. PRBool delete_after_linger;
  70. int refcnt;
  71. const Repl_Agmt *agmt;
  72. PRLock *lock;
  73. struct timeval timeout;
  74. int flag_agmt_changed;
  75. char *plain;
  76. } repl_connection;
  77. /* #define DEFAULT_LINGER_TIME (5 * 60) */ /* 5 minutes */
  78. #define DEFAULT_LINGER_TIME (60)
  79. /* Controls we add on every outbound operation */
  80. static LDAPControl manageDSAITControl = {LDAP_CONTROL_MANAGEDSAIT, {0, ""}, '\0'};
  81. static int attribute_string_value_present(LDAP *ld, LDAPMessage *entry,
  82. const char *type, const char *value);
  83. static int bind_and_check_pwp(Repl_Connection *conn, char * binddn, char *password);
  84. static int do_simple_bind (Repl_Connection *conn, LDAP *ld, char * binddn, char *password);
  85. static int s_debug_timeout = 0;
  86. static int s_debug_level = 0;
  87. static Slapi_Eq_Context repl5_start_debug_timeout(int *setlevel);
  88. static void repl5_stop_debug_timeout(Slapi_Eq_Context eqctx, int *setlevel);
  89. static void repl5_debug_timeout_callback(time_t when, void *arg);
  90. #ifndef DSE_RETURNTEXT_SIZE
  91. #define SLAPI_DSE_RETURNTEXT_SIZE 512
  92. #endif
  93. #define STATE_CONNECTED 600
  94. #define STATE_DISCONNECTED 601
  95. #define STATUS_DISCONNECTED "disconnected"
  96. #define STATUS_CONNECTED "connected"
  97. #define STATUS_PROCESSING_ADD "processing add operation"
  98. #define STATUS_PROCESSING_DELETE "processing delete operation"
  99. #define STATUS_PROCESSING_MODIFY "processing modify operation"
  100. #define STATUS_PROCESSING_RENAME "processing rename operation"
  101. #define STATUS_PROCESSING_EXTENDED_OPERATION "processing extended operation"
  102. #define STATUS_LINGERING "lingering"
  103. #define STATUS_SHUTTING_DOWN "shutting down"
  104. #define STATUS_BINDING "connecting and binding"
  105. #define STATUS_SEARCHING "processing search operation"
  106. #define CONN_NO_OPERATION 0
  107. #define CONN_ADD 1
  108. #define CONN_DELETE 2
  109. #define CONN_MODIFY 3
  110. #define CONN_RENAME 4
  111. #define CONN_EXTENDED_OPERATION 5
  112. #define CONN_BIND 6
  113. #define CONN_INIT 7
  114. #define CONN_SEARCH 8
  115. /* These are errors returned from ldap operations which should cause us to disconnect and
  116. retry the connection later */
  117. #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)
  118. /* Forward declarations */
  119. static void close_connection_internal(Repl_Connection *conn);
  120. /*
  121. * Create a new conenction object. Returns a pointer to the object, or
  122. * NULL if an error occurs.
  123. */
  124. Repl_Connection *
  125. windows_conn_new(Repl_Agmt *agmt)
  126. {
  127. Repl_Connection *rpc;
  128. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_new\n", 0, 0, 0 );
  129. rpc = (Repl_Connection *)slapi_ch_malloc(sizeof(repl_connection));
  130. if ((rpc->lock = PR_NewLock()) == NULL)
  131. {
  132. goto loser;
  133. }
  134. rpc->hostname = agmt_get_hostname(agmt);
  135. rpc->port = agmt_get_port(agmt);
  136. rpc->binddn = agmt_get_binddn(agmt);
  137. rpc->bindmethod = agmt_get_bindmethod(agmt);
  138. rpc->transport_flags = agmt_get_transport_flags(agmt);
  139. rpc->ld = NULL;
  140. rpc->state = STATE_DISCONNECTED;
  141. rpc->last_operation = CONN_NO_OPERATION;
  142. rpc->last_ldap_error = LDAP_SUCCESS;
  143. rpc->last_ldap_errmsg = NULL;
  144. rpc->supports_ldapv3 = -1;
  145. rpc->supports_ds40_repl = -1;
  146. rpc->supports_ds50_repl = -1;
  147. rpc->supports_dirsync = -1;
  148. rpc->linger_active = PR_FALSE;
  149. rpc->delete_after_linger = PR_FALSE;
  150. rpc->linger_event = NULL;
  151. rpc->linger_time = DEFAULT_LINGER_TIME;
  152. rpc->status = STATUS_DISCONNECTED;
  153. rpc->agmt = agmt;
  154. rpc->refcnt = 1;
  155. rpc->timeout.tv_sec = agmt_get_timeout(agmt);
  156. rpc->timeout.tv_usec = 0;
  157. rpc->flag_agmt_changed = 0;
  158. rpc->plain = NULL;
  159. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_new\n", 0, 0, 0 );
  160. return rpc;
  161. loser:
  162. windows_conn_delete(rpc);
  163. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_new - loser\n", 0, 0, 0 );
  164. return NULL;
  165. }
  166. /*
  167. * Return PR_TRUE if the connection is in the connected state
  168. */
  169. static PRBool
  170. windows_conn_connected(Repl_Connection *conn)
  171. {
  172. PRBool return_value;
  173. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_connected\n", 0, 0, 0 );
  174. PR_Lock(conn->lock);
  175. return_value = STATE_CONNECTED == conn->state;
  176. PR_Unlock(conn->lock);
  177. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_connected\n", 0, 0, 0 );
  178. return return_value;
  179. }
  180. /*
  181. * Destroy a connection object.
  182. */
  183. static void
  184. windows_conn_delete_internal(Repl_Connection *conn)
  185. {
  186. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_delete_internal\n", 0, 0, 0 );
  187. PR_ASSERT(NULL != conn);
  188. close_connection_internal(conn);
  189. /* slapi_ch_free accepts NULL pointer */
  190. slapi_ch_free((void **)&conn->hostname);
  191. slapi_ch_free((void **)&conn->binddn);
  192. slapi_ch_free((void **)&conn->plain);
  193. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_delete_internal\n", 0, 0, 0 );
  194. }
  195. /*
  196. * Destroy a connection. It is an error to use the connection object
  197. * after windows_conn_delete() has been called.
  198. */
  199. void
  200. windows_conn_delete(Repl_Connection *conn)
  201. {
  202. PRBool destroy_it = PR_FALSE;
  203. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_delete\n", 0, 0, 0 );
  204. PR_ASSERT(NULL != conn);
  205. PR_Lock(conn->lock);
  206. if (conn->linger_active)
  207. {
  208. if (slapi_eq_cancel(conn->linger_event) == 1)
  209. {
  210. /* Event was found and cancelled. Destroy the connection object. */
  211. PR_Unlock(conn->lock);
  212. destroy_it = PR_TRUE;
  213. }
  214. else
  215. {
  216. /*
  217. * The event wasn't found, but we think it's still active.
  218. * That means an event is in the process of being fired
  219. * off, so arrange for the event to destroy the object .
  220. */
  221. conn->delete_after_linger = PR_TRUE;
  222. PR_Unlock(conn->lock);
  223. }
  224. }
  225. if (destroy_it)
  226. {
  227. windows_conn_delete_internal(conn);
  228. }
  229. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_delete\n", 0, 0, 0 );
  230. }
  231. /*
  232. * Return the last operation type processed by the connection
  233. * object, and the LDAP error encountered.
  234. */
  235. void
  236. windows_conn_get_error(Repl_Connection *conn, int *operation, int *error)
  237. {
  238. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_get_error\n", 0, 0, 0 );
  239. PR_Lock(conn->lock);
  240. *operation = conn->last_operation;
  241. *error = conn->last_ldap_error;
  242. PR_Unlock(conn->lock);
  243. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_get_error\n", 0, 0, 0 );
  244. }
  245. /*
  246. * Common code to send an LDAPv3 operation and collect the result.
  247. * Return values:
  248. * CONN_OPERATION_SUCCESS - the operation succeeded
  249. * CONN_OPERATION_FAILED - the operation was sent to the consumer
  250. * and failed. Use conn_get_error() to determine the LDAP error
  251. * code.
  252. * CONN_NOT_CONNECTED - no connection is active. The caller should
  253. * use conn_connect() to connect to the replica and bind, then should
  254. * reacquire the replica (if needed).
  255. * CONN_BUSY - the server is busy with previous requests, must wait for a while
  256. * before retrying
  257. */
  258. static ConnResult
  259. windows_perform_operation(Repl_Connection *conn, int optype, const char *dn,
  260. LDAPMod **attrs, const char *newrdn, const char *newparent,
  261. int deleteoldrdn, LDAPControl *update_control,
  262. const char *extop_oid, struct berval *extop_payload, char **retoidp,
  263. struct berval **retdatap, LDAPControl ***returned_controls)
  264. {
  265. int rc;
  266. ConnResult return_value;
  267. LDAPControl *server_controls[1];
  268. LDAPControl **loc_returned_controls;
  269. const char *op_string = NULL;
  270. const char *extra_op_string = NULL;
  271. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_perform_operation\n", 0, 0, 0 );
  272. server_controls[0] = NULL;
  273. if (windows_conn_connected(conn))
  274. {
  275. int msgid;
  276. conn->last_operation = optype;
  277. switch (optype)
  278. {
  279. case CONN_ADD:
  280. conn->status = STATUS_PROCESSING_ADD;
  281. op_string = "add";
  282. rc = ldap_add_ext(conn->ld, dn, attrs, server_controls,
  283. NULL /* clientctls */, &msgid);
  284. break;
  285. case CONN_MODIFY:
  286. conn->status = STATUS_PROCESSING_MODIFY;
  287. op_string = "modify";
  288. rc = ldap_modify_ext(conn->ld, dn, attrs, server_controls,
  289. NULL /* clientctls */, &msgid);
  290. break;
  291. case CONN_DELETE:
  292. conn->status = STATUS_PROCESSING_DELETE;
  293. op_string = "delete";
  294. rc = ldap_delete_ext(conn->ld, dn, server_controls,
  295. NULL /* clientctls */, &msgid);
  296. break;
  297. case CONN_RENAME:
  298. conn->status = STATUS_PROCESSING_RENAME;
  299. op_string = "rename";
  300. rc = ldap_rename(conn->ld, dn, newrdn, newparent, deleteoldrdn,
  301. server_controls, NULL /* clientctls */, &msgid);
  302. break;
  303. case CONN_EXTENDED_OPERATION:
  304. conn->status = STATUS_PROCESSING_EXTENDED_OPERATION;
  305. op_string = "extended";
  306. extra_op_string = extop_oid;
  307. rc = ldap_extended_operation(conn->ld, extop_oid, extop_payload,
  308. server_controls, NULL /* clientctls */, &msgid);
  309. }
  310. if (LDAP_SUCCESS == rc)
  311. {
  312. LDAPMessage *res = NULL;
  313. int setlevel = 0;
  314. Slapi_Eq_Context eqctx = repl5_start_debug_timeout(&setlevel);
  315. rc = ldap_result(conn->ld, msgid, 1, &conn->timeout, &res);
  316. repl5_stop_debug_timeout(eqctx, &setlevel);
  317. if (0 == rc)
  318. {
  319. /* Timeout */
  320. rc = ldap_get_lderrno(conn->ld, NULL, NULL);
  321. conn->last_ldap_error = LDAP_TIMEOUT;
  322. return_value = CONN_TIMEOUT;
  323. }
  324. else if (-1 == rc)
  325. {
  326. /* Error */
  327. char *s = NULL;
  328. rc = ldap_get_lderrno(conn->ld, NULL, &s);
  329. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  330. "%s: Received error %d: %s for %s operation\n",
  331. agmt_get_long_name(conn->agmt),
  332. rc, s ? s : "NULL",
  333. op_string ? op_string : "NULL");
  334. conn->last_ldap_error = rc;
  335. /* some errors will require a disconnect and retry the connection
  336. later */
  337. if (IS_DISCONNECT_ERROR(rc))
  338. {
  339. windows_conn_disconnect(conn);
  340. return_value = CONN_NOT_CONNECTED;
  341. }
  342. else
  343. {
  344. conn->status = STATUS_CONNECTED;
  345. return_value = CONN_OPERATION_FAILED;
  346. }
  347. }
  348. else
  349. {
  350. int err;
  351. char *errmsg = NULL;
  352. char **referrals = NULL;
  353. char *matched = NULL;
  354. rc = ldap_parse_result(conn->ld, res, &err, &matched,
  355. &errmsg, &referrals, &loc_returned_controls,
  356. 0 /* Don't free the result */);
  357. if (IS_DISCONNECT_ERROR(rc))
  358. {
  359. conn->last_ldap_error = rc;
  360. windows_conn_disconnect(conn);
  361. return_value = CONN_NOT_CONNECTED;
  362. }
  363. else if (IS_DISCONNECT_ERROR(err))
  364. {
  365. conn->last_ldap_error = err;
  366. windows_conn_disconnect(conn);
  367. return_value = CONN_NOT_CONNECTED;
  368. }
  369. else if (err == LDAP_UNWILLING_TO_PERFORM && optype == CONN_MODIFY)
  370. {
  371. /* this permits password updates to fail gracefully */
  372. conn->last_ldap_error = LDAP_SUCCESS;
  373. return_value = CONN_OPERATION_SUCCESS;
  374. }
  375. else if (err == LDAP_ALREADY_EXISTS && optype == CONN_ADD)
  376. {
  377. conn->last_ldap_error = LDAP_SUCCESS;
  378. return_value = CONN_OPERATION_SUCCESS;
  379. }
  380. else if (err == LDAP_NO_SUCH_OBJECT && optype == CONN_DELETE)
  381. {
  382. conn->last_ldap_error = LDAP_SUCCESS;
  383. return_value = CONN_OPERATION_SUCCESS;
  384. }
  385. else /* regular operation, result returned */
  386. {
  387. if (NULL != returned_controls)
  388. {
  389. *returned_controls = loc_returned_controls;
  390. }
  391. if (LDAP_SUCCESS != rc)
  392. {
  393. conn->last_ldap_error = rc;
  394. }
  395. else
  396. {
  397. conn->last_ldap_error = err;
  398. }
  399. return_value = LDAP_SUCCESS == conn->last_ldap_error ? CONN_OPERATION_SUCCESS : CONN_OPERATION_FAILED;
  400. }
  401. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  402. "%s: Received result code %d for %s operation %s%s\n",
  403. agmt_get_long_name(conn->agmt),
  404. conn->last_ldap_error,
  405. op_string == NULL ? "" : op_string,
  406. extra_op_string == NULL ? "" : extra_op_string,
  407. extra_op_string == NULL ? "" : " ");
  408. /*
  409. * XXXggood do I need to free matched, referrals,
  410. * anything else? Or can I pass NULL for the args
  411. * I'm not interested in?
  412. */
  413. /* Good question! Meanwhile, as RTM aproaches, let's free them... */
  414. slapi_ch_free((void **) &errmsg);
  415. slapi_ch_free((void **) &matched);
  416. charray_free(referrals);
  417. conn->status = STATUS_CONNECTED;
  418. }
  419. if (res) ldap_msgfree(res);
  420. }
  421. else
  422. {
  423. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  424. "%s: Failed to send %s operation: LDAP error %d (%s)\n",
  425. agmt_get_long_name(conn->agmt),
  426. op_string ? op_string : "NULL", rc, ldap_err2string(rc));
  427. conn->last_ldap_error = rc;
  428. if (IS_DISCONNECT_ERROR(rc))
  429. {
  430. windows_conn_disconnect(conn);
  431. return_value = CONN_NOT_CONNECTED;
  432. }
  433. else
  434. {
  435. conn->status = STATUS_CONNECTED;
  436. return_value = CONN_OPERATION_FAILED;
  437. }
  438. }
  439. }
  440. else
  441. {
  442. /* conn->last_ldap_error has been set to a more specific value
  443. * in windows_conn_connected()
  444. * conn->last_ldap_error = LDAP_SERVER_DOWN;
  445. */
  446. return_value = CONN_NOT_CONNECTED;
  447. }
  448. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_perform_operation\n", 0, 0, 0 );
  449. return return_value;
  450. }
  451. /* Copied from the chaining backend*/
  452. static Slapi_Entry *
  453. windows_LDAPMessage2Entry(LDAP * ld, LDAPMessage * msg, int attrsonly) {
  454. Slapi_Entry * e = slapi_entry_alloc();
  455. char * a=NULL;
  456. BerElement * ber=NULL;
  457. if ( e == NULL ) return NULL;
  458. if (msg == NULL) {
  459. slapi_entry_free(e);
  460. return NULL;
  461. }
  462. /*
  463. * dn not allocated by slapi
  464. * attribute type and values ARE allocated
  465. */
  466. slapi_entry_set_dn( e, ldap_get_dn( ld, msg ) );
  467. for ( a = ldap_first_attribute( ld, msg, &ber ); a!=NULL;
  468. a=ldap_next_attribute( ld, msg, ber ) ) {
  469. if(attrsonly) {
  470. slapi_entry_add_value(e, a, (Slapi_Value *)NULL);
  471. ldap_memfree(a);
  472. } else {
  473. struct berval ** aVal = ldap_get_values_len( ld, msg, a);
  474. slapi_entry_add_values( e, a, aVal);
  475. ldap_memfree(a);
  476. ldap_value_free_len(aVal);
  477. }
  478. }
  479. if ( NULL != ber )
  480. ldap_ber_free( ber, 0 );
  481. return e;
  482. }
  483. ConnResult
  484. windows_search_entry(Repl_Connection *conn, char* searchbase, char *filter, Slapi_Entry **entry)
  485. {
  486. ConnResult return_value = 0;
  487. int ldap_rc = 0;
  488. LDAPMessage *res = NULL;
  489. int not_unique = 0;
  490. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_search_entry\n", 0, 0, 0 );
  491. *entry = NULL;
  492. if (windows_conn_connected(conn))
  493. {
  494. ldap_rc = ldap_search_ext_s(conn->ld, searchbase, LDAP_SCOPE_SUBTREE,
  495. filter, NULL, 0 /* attrsonly */,
  496. NULL , NULL /* client controls */,
  497. &conn->timeout, 0 /* sizelimit */, &res);
  498. if (LDAP_SUCCESS == ldap_rc)
  499. {
  500. LDAPMessage *message = ldap_first_entry(conn->ld, res);
  501. LDAPMessage *next_entry = NULL;
  502. if (NULL != entry)
  503. {
  504. *entry = windows_LDAPMessage2Entry(conn->ld,message,0);
  505. }
  506. /* See if there are any more entries : if so then that's an error
  507. * but we still need to get them to avoid gumming up the connection
  508. */
  509. while (NULL != ( next_entry = ldap_next_entry(conn->ld,res)))
  510. {
  511. not_unique = 1;
  512. }
  513. return_value = CONN_OPERATION_SUCCESS;
  514. }
  515. else if (IS_DISCONNECT_ERROR(ldap_rc))
  516. {
  517. windows_conn_disconnect(conn);
  518. return_value = CONN_NOT_CONNECTED;
  519. }
  520. else
  521. {
  522. return_value = CONN_OPERATION_FAILED;
  523. }
  524. conn->last_ldap_error = ldap_rc;
  525. if (NULL != res)
  526. {
  527. ldap_msgfree(res);
  528. res = NULL;
  529. }
  530. }
  531. else
  532. {
  533. return_value = CONN_NOT_CONNECTED;
  534. }
  535. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_search_entry\n", 0, 0, 0 );
  536. return return_value;
  537. }
  538. ConnResult
  539. send_dirsync_search(Repl_Connection *conn)
  540. {
  541. int rc;
  542. ConnResult return_value;
  543. LDAPControl *server_controls[2];
  544. int msgid;
  545. const char *op_string = NULL;
  546. const char* old_dn = NULL;
  547. char* dn = NULL;
  548. int i=0;
  549. int num_comp=0;
  550. LDAPDebug( LDAP_DEBUG_TRACE, "=> send_dirsync_search\n", 0, 0, 0 );
  551. /* need to strip the dn down to dc= */
  552. old_dn = slapi_sdn_get_ndn( windows_private_get_windows_subtree(conn->agmt) );
  553. dn = strstr(old_dn, "dc=");
  554. if (windows_conn_connected(conn))
  555. {
  556. if (conn->supports_dirsync == 0)
  557. {
  558. server_controls[0] = NULL; /* unsupported */
  559. } else
  560. {
  561. /* DBDB: I'm pretty sure that the control is leaked from here */
  562. /* Purify agrees */
  563. server_controls[0] = windows_private_dirsync_control(conn->agmt); /* yes, or don't know */
  564. }
  565. server_controls[1] = NULL;
  566. conn->last_operation = CONN_SEARCH;
  567. conn->status = STATUS_SEARCHING;
  568. op_string = "search";
  569. LDAPDebug( LDAP_DEBUG_REPL, "Sending dirsync search request\n", 0, 0, 0 );
  570. rc = ldap_search_ext( conn->ld, dn, LDAP_SCOPE_SUBTREE, "(objectclass=*)", /* filter */
  571. NULL /*attrs */, PR_FALSE, server_controls, NULL, /* ClientControls */
  572. 0,0, &msgid);
  573. if (LDAP_SUCCESS == rc)
  574. {
  575. return_value = 0;
  576. }
  577. else
  578. {
  579. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  580. "%s: Failed to get %s operation: LDAP error %d (%s)\n",
  581. agmt_get_long_name(conn->agmt),
  582. op_string ? op_string : "NULL", rc, ldap_err2string(rc));
  583. conn->last_ldap_error = rc;
  584. if (IS_DISCONNECT_ERROR(rc))
  585. {
  586. windows_conn_disconnect(conn);
  587. return_value = CONN_NOT_CONNECTED;
  588. }
  589. else
  590. {
  591. conn->status = STATUS_CONNECTED;
  592. return_value = CONN_OPERATION_FAILED;
  593. }
  594. }
  595. if (server_controls[0])
  596. {
  597. ldap_control_free(server_controls[0]);
  598. }
  599. }
  600. else
  601. {
  602. /* conn->last_ldap_error has been set to a more specific value
  603. * in windows_conn_connected()
  604. * conn->last_ldap_error = LDAP_SERVER_DOWN;
  605. */
  606. return_value = CONN_NOT_CONNECTED;
  607. }
  608. LDAPDebug( LDAP_DEBUG_TRACE, "<= send_dirsync_search\n", 0, 0, 0 );
  609. return return_value;
  610. }
  611. /*
  612. * Send an LDAP add operation.
  613. */
  614. ConnResult
  615. windows_conn_send_add(Repl_Connection *conn, const char *dn, LDAPMod **attrs,
  616. LDAPControl *update_control, LDAPControl ***returned_controls)
  617. {
  618. ConnResult res = 0;
  619. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_send_add\n", 0, 0, 0 );
  620. res = windows_perform_operation(conn, CONN_ADD, dn, attrs, NULL /* newrdn */,
  621. NULL /* newparent */, 0 /* deleteoldrdn */, update_control,
  622. NULL /* extop OID */, NULL /* extop payload */, NULL /* retoidp */,
  623. NULL /* retdatap */, returned_controls);
  624. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_send_add\n", 0, 0, 0 );
  625. return res;
  626. }
  627. /*
  628. * Send an LDAP delete operation.
  629. */
  630. ConnResult
  631. windows_conn_send_delete(Repl_Connection *conn, const char *dn,
  632. LDAPControl *update_control, LDAPControl ***returned_controls)
  633. {
  634. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_send_delete\n", 0, 0, 0 );
  635. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_send_delete\n", 0, 0, 0 );
  636. return windows_perform_operation(conn, CONN_DELETE, dn, NULL /* attrs */,
  637. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  638. update_control, NULL /* extop OID */, NULL /* extop payload */,
  639. NULL /* retoidp */, NULL /* retdatap */, returned_controls);
  640. }
  641. /*
  642. * Send an LDAP modify operation.
  643. */
  644. ConnResult
  645. windows_conn_send_modify(Repl_Connection *conn, const char *dn, LDAPMod **mods,
  646. LDAPControl *update_control, LDAPControl ***returned_controls)
  647. {
  648. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_send_modify\n", 0, 0, 0 );
  649. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_send_modify\n", 0, 0, 0 );
  650. return windows_perform_operation(conn, CONN_MODIFY, dn, mods, NULL /* newrdn */,
  651. NULL /* newparent */, 0 /* deleteoldrdn */, update_control,
  652. NULL /* extop OID */, NULL /* extop payload */, NULL /* retoidp */,
  653. NULL /* retdatap */, returned_controls);
  654. }
  655. /*
  656. * Send an LDAP moddn operation.
  657. */
  658. ConnResult
  659. windows_conn_send_rename(Repl_Connection *conn, const char *dn,
  660. const char *newrdn, const char *newparent, int deleteoldrdn,
  661. LDAPControl *update_control, LDAPControl ***returned_controls)
  662. {
  663. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_send_rename\n", 0, 0, 0 );
  664. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_send_rename\n", 0, 0, 0 );
  665. return windows_perform_operation(conn, CONN_RENAME, dn, NULL /* attrs */,
  666. newrdn, newparent, deleteoldrdn, update_control,
  667. NULL /* extop OID */, NULL /* extop payload */, NULL /* retoidp */,
  668. NULL /* retdatap */, returned_controls);
  669. }
  670. /*
  671. * Send an LDAP search operation.
  672. */
  673. Slapi_Entry * windows_conn_get_search_result(Repl_Connection *conn)
  674. {
  675. int rc=0;
  676. int matches=0;
  677. LDAPMessage *res = NULL;
  678. Slapi_Entry *e = NULL;
  679. LDAPMessage *lm = NULL;
  680. char *a = "";
  681. char *dn = "";
  682. BerElement *ber = NULL;
  683. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_get_search_result\n", 0, 0, 0 );
  684. if (windows_conn_connected(conn))
  685. {
  686. rc = ldap_result( conn->ld, LDAP_RES_ANY, 0, &conn->timeout, &res );
  687. switch (rc) {
  688. case 0:
  689. case -1:
  690. case LDAP_RES_SEARCH_REFERENCE:
  691. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name, "error in windows_conn_get_search_result, rc=%d\n", rc);
  692. break;
  693. case LDAP_RES_SEARCH_RESULT:
  694. {
  695. LDAPControl **returned_controls = NULL;
  696. int code = 0;
  697. int parse_rc = 0;
  698. /* Purify says this is a leak : */
  699. parse_rc = ldap_parse_result( conn->ld, res, &code, NULL, NULL, NULL, &returned_controls, 0 );
  700. if (returned_controls)
  701. {
  702. windows_private_update_dirsync_control(conn->agmt, returned_controls);
  703. ldap_controls_free(returned_controls);
  704. }
  705. if (windows_private_dirsync_has_more(conn->agmt)) {
  706. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,"received hasmore from dirsync\n", 0);
  707. }
  708. }
  709. break;
  710. case LDAP_RES_SEARCH_ENTRY:
  711. {
  712. if (( dn = ldap_get_dn( conn->ld, res )) != NULL )
  713. {
  714. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,"received entry from dirsync: %s\n", dn);
  715. lm = ldap_first_entry( conn->ld, res );
  716. e = windows_LDAPMessage2Entry(conn->ld,lm,0);
  717. ldap_memfree(dn);
  718. }
  719. }
  720. break;
  721. } /* switch */
  722. } /* if */
  723. if (res)
  724. {
  725. ldap_msgfree( res );
  726. res = NULL;
  727. }
  728. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_get_search_result\n", 0, 0, 0 );
  729. return e;
  730. }
  731. /*
  732. * Send an LDAP extended operation.
  733. */
  734. ConnResult
  735. windows_conn_send_extended_operation(Repl_Connection *conn, const char *extop_oid,
  736. struct berval *payload, char **retoidp, struct berval **retdatap,
  737. LDAPControl *update_control, LDAPControl ***returned_controls)
  738. {
  739. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_send_extended_operation\n", 0, 0, 0 );
  740. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_send_extended_operation\n", 0, 0, 0 );
  741. return windows_perform_operation(conn, CONN_EXTENDED_OPERATION, NULL /* dn */, NULL /* attrs */,
  742. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  743. update_control, extop_oid, payload, retoidp, retdatap,
  744. returned_controls);
  745. }
  746. /*
  747. * Synchronously read an entry and return a specific attribute's values.
  748. * Returns CONN_OPERATION_SUCCESS if successful. Returns
  749. * CONN_OPERATION_FAILED if the operation was sent but an LDAP error
  750. * occurred (conn->last_ldap_error is set in this case), and
  751. * CONN_NOT_CONNECTED if no connection was active.
  752. *
  753. * The caller must free the returned_bvals.
  754. */
  755. ConnResult
  756. windows_conn_read_entry_attribute(Repl_Connection *conn, const char *dn,
  757. char *type, struct berval ***returned_bvals)
  758. {
  759. ConnResult return_value;
  760. int ldap_rc;
  761. LDAPControl *server_controls[2];
  762. LDAPMessage *res = NULL;
  763. char *attrs[2];
  764. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_read_entry_attribute\n", 0, 0, 0 );
  765. PR_ASSERT(NULL != type);
  766. if (windows_conn_connected(conn))
  767. {
  768. server_controls[0] = &manageDSAITControl;
  769. server_controls[1] = NULL;
  770. attrs[0] = type;
  771. attrs[1] = NULL;
  772. ldap_rc = ldap_search_ext_s(conn->ld, dn, LDAP_SCOPE_BASE,
  773. "(objectclass=*)", attrs, 0 /* attrsonly */,
  774. server_controls, NULL /* client controls */,
  775. &conn->timeout, 0 /* sizelimit */, &res);
  776. if (LDAP_SUCCESS == ldap_rc)
  777. {
  778. LDAPMessage *entry = ldap_first_entry(conn->ld, res);
  779. if (NULL != entry)
  780. {
  781. *returned_bvals = ldap_get_values_len(conn->ld, entry, type);
  782. }
  783. return_value = CONN_OPERATION_SUCCESS;
  784. }
  785. else if (IS_DISCONNECT_ERROR(ldap_rc))
  786. {
  787. windows_conn_disconnect(conn);
  788. return_value = CONN_NOT_CONNECTED;
  789. }
  790. else
  791. {
  792. return_value = CONN_OPERATION_FAILED;
  793. }
  794. conn->last_ldap_error = ldap_rc;
  795. if (NULL != res)
  796. {
  797. ldap_msgfree(res);
  798. res = NULL;
  799. }
  800. }
  801. else
  802. {
  803. return_value = CONN_NOT_CONNECTED;
  804. }
  805. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_read_entry_attribute\n", 0, 0, 0 );
  806. return return_value;
  807. }
  808. /*
  809. * Return an pointer to a string describing the connection's status.
  810. */
  811. const char *
  812. windows_conn_get_status(Repl_Connection *conn)
  813. {
  814. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_get_status\n", 0, 0, 0 );
  815. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_get_status\n", 0, 0, 0 );
  816. return conn->status;
  817. }
  818. /*
  819. * Cancel any outstanding linger timer. Should be called when
  820. * a replication session is beginning.
  821. */
  822. void
  823. windows_conn_cancel_linger(Repl_Connection *conn)
  824. {
  825. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_cancel_linger\n", 0, 0, 0 );
  826. PR_ASSERT(NULL != conn);
  827. PR_Lock(conn->lock);
  828. if (conn->linger_active)
  829. {
  830. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  831. "%s: Cancelling linger on the connection\n",
  832. agmt_get_long_name(conn->agmt));
  833. conn->linger_active = PR_FALSE;
  834. if (slapi_eq_cancel(conn->linger_event) == 1)
  835. {
  836. conn->refcnt--;
  837. }
  838. conn->linger_event = NULL;
  839. conn->status = STATUS_CONNECTED;
  840. }
  841. else
  842. {
  843. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  844. "%s: No linger to cancel on the connection\n",
  845. agmt_get_long_name(conn->agmt));
  846. }
  847. PR_Unlock(conn->lock);
  848. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_cancel_linger\n", 0, 0, 0 );
  849. }
  850. /*
  851. * Called when our linger timeout timer expires. This means
  852. * we should check to see if perhaps the connection's become
  853. * active again, in which case we do nothing. Otherwise,
  854. * we close the connection.
  855. */
  856. static void
  857. linger_timeout(time_t event_time, void *arg)
  858. {
  859. PRBool delete_now;
  860. Repl_Connection *conn = (Repl_Connection *)arg;
  861. LDAPDebug( LDAP_DEBUG_TRACE, "=> linger_timeout\n", 0, 0, 0 );
  862. PR_ASSERT(NULL != conn);
  863. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  864. "%s: Linger timeout has expired on the connection\n",
  865. agmt_get_long_name(conn->agmt));
  866. PR_Lock(conn->lock);
  867. if (conn->linger_active)
  868. {
  869. conn->linger_active = PR_FALSE;
  870. conn->linger_event = NULL;
  871. close_connection_internal(conn);
  872. }
  873. delete_now = conn->delete_after_linger;
  874. PR_Unlock(conn->lock);
  875. if (delete_now)
  876. {
  877. windows_conn_delete_internal(conn);
  878. }
  879. LDAPDebug( LDAP_DEBUG_TRACE, "<= linger_timeout\n", 0, 0, 0 );
  880. }
  881. /*
  882. * Indicate that a session is ending. The linger timer starts when
  883. * this function is called.
  884. */
  885. void
  886. windows_conn_start_linger(Repl_Connection *conn)
  887. {
  888. time_t now;
  889. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_start_linger\n", 0, 0, 0 );
  890. PR_ASSERT(NULL != conn);
  891. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  892. "%s: Beginning linger on the connection\n",
  893. agmt_get_long_name(conn->agmt));
  894. if (!windows_conn_connected(conn))
  895. {
  896. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  897. "%s: No linger on the closed conn\n",
  898. agmt_get_long_name(conn->agmt));
  899. return;
  900. }
  901. time(&now);
  902. PR_Lock(conn->lock);
  903. if (conn->linger_active)
  904. {
  905. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  906. "%s: Linger already active on the connection\n",
  907. agmt_get_long_name(conn->agmt));
  908. }
  909. else
  910. {
  911. conn->linger_active = PR_TRUE;
  912. conn->linger_event = slapi_eq_once(linger_timeout, conn, now + conn->linger_time);
  913. conn->status = STATUS_LINGERING;
  914. }
  915. PR_Unlock(conn->lock);
  916. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_start_linger\n", 0, 0, 0 );
  917. }
  918. /*
  919. * If no connection is currently active, opens a connection and binds to
  920. * the remote server. If a connection is open (e.g. lingering) then
  921. * this is a no-op.
  922. *
  923. * Returns CONN_OPERATION_SUCCESS on success, or CONN_OPERATION_FAILED
  924. * on failure. Sets conn->last_ldap_error and conn->last_operation;
  925. */
  926. ConnResult
  927. windows_conn_connect(Repl_Connection *conn)
  928. {
  929. int ldap_rc;
  930. int optdata;
  931. int secure = 0;
  932. char* binddn = NULL;
  933. struct berval *creds;
  934. ConnResult return_value = CONN_OPERATION_SUCCESS;
  935. int pw_ret = 1;
  936. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_connect\n", 0, 0, 0 );
  937. /** Connection already open just return SUCCESS **/
  938. if(conn->state == STATE_CONNECTED) {
  939. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_connect\n", 0, 0, 0 );
  940. return return_value;
  941. }
  942. PR_Lock(conn->lock);
  943. if (conn->flag_agmt_changed) {
  944. /* So far we cannot change Hostname and Port */
  945. /* slapi_ch_free((void **)&conn->hostname); */
  946. /* conn->hostname = agmt_get_hostname(conn->agmt); */
  947. /* conn->port = agmt_get_port(conn->agmt); */
  948. slapi_ch_free((void **)&conn->binddn);
  949. conn->binddn = agmt_get_binddn(conn->agmt);
  950. conn->bindmethod = agmt_get_bindmethod(conn->agmt);
  951. conn->transport_flags = agmt_get_transport_flags(conn->agmt);
  952. conn->timeout.tv_sec = agmt_get_timeout(conn->agmt);
  953. conn->flag_agmt_changed = 0;
  954. slapi_ch_free((void **)&conn->plain);
  955. }
  956. PR_Unlock(conn->lock);
  957. creds = agmt_get_credentials(conn->agmt);
  958. if (conn->plain == NULL) {
  959. char *plain = NULL;
  960. /* kexcoff: for reversible encryption */
  961. /* We need to test the return code of pw_rever_decode in order to decide
  962. * if a free for plain will be needed (pw_ret == 0) or not (pw_ret != 0) */
  963. pw_ret = pw_rever_decode(creds->bv_val, &plain, type_nsds5ReplicaCredentials);
  964. /* Pb occured in decryption: stop now, binding will fail */
  965. if ( pw_ret == -1 )
  966. {
  967. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  968. "%s: Decoding of the credentials failed.\n",
  969. agmt_get_long_name(conn->agmt));
  970. return_value = CONN_OPERATION_FAILED;
  971. conn->last_ldap_error = LDAP_INVALID_CREDENTIALS;
  972. conn->state = STATE_DISCONNECTED;
  973. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_connect\n", 0, 0, 0 );
  974. return (return_value);
  975. } /* Else, does not mean that the plain is correct, only means the we had no internal
  976. decoding pb */
  977. conn->plain = slapi_ch_strdup (plain);
  978. if (!pw_ret) slapi_ch_free((void**)&plain);
  979. }
  980. /* ugaston: if SSL has been selected in the replication agreement, SSL client
  981. * initialisation should be done before ever trying to open any connection at all.
  982. */
  983. if (conn->transport_flags == TRANSPORT_FLAG_TLS)
  984. {
  985. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  986. "%s: Replication secured by StartTLS not currently supported\n",
  987. agmt_get_long_name(conn->agmt));
  988. return_value = CONN_OPERATION_FAILED;
  989. conn->last_ldap_error = LDAP_STRONG_AUTH_NOT_SUPPORTED;
  990. conn->state = STATE_DISCONNECTED;
  991. } else if(conn->transport_flags == TRANSPORT_FLAG_SSL)
  992. {
  993. /** Make sure the SSL Library has been initialized before anything else **/
  994. if(slapd_security_library_is_initialized() != 1)
  995. {
  996. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  997. "%s: SSL Not Initialized, Replication over SSL FAILED\n",
  998. agmt_get_long_name(conn->agmt));
  999. conn->last_ldap_error = LDAP_INAPPROPRIATE_AUTH;
  1000. conn->last_operation = CONN_INIT;
  1001. ber_bvfree(creds);
  1002. creds = NULL;
  1003. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_connect\n", 0, 0, 0 );
  1004. return CONN_SSL_NOT_ENABLED;
  1005. } else
  1006. {
  1007. secure = 1;
  1008. }
  1009. }
  1010. if (return_value == CONN_OPERATION_SUCCESS) {
  1011. int io_timeout_ms;
  1012. /* Now we initialize the LDAP Structure and set options */
  1013. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1014. "%s: Trying %s slapi_ldap_init\n",
  1015. agmt_get_long_name(conn->agmt),
  1016. secure ? "secure" : "non-secure");
  1017. conn->ld = slapi_ldap_init(conn->hostname, conn->port, secure, 0);
  1018. if (NULL == conn->ld)
  1019. {
  1020. return_value = CONN_OPERATION_FAILED;
  1021. conn->state = STATE_DISCONNECTED;
  1022. conn->last_operation = CONN_INIT;
  1023. conn->last_ldap_error = LDAP_LOCAL_ERROR;
  1024. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1025. "%s: Failed to establish %sconnection to the consumer\n",
  1026. agmt_get_long_name(conn->agmt),
  1027. secure ? "secure " : "");
  1028. ber_bvfree(creds);
  1029. creds = NULL;
  1030. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_connect\n", 0, 0, 0 );
  1031. return return_value;
  1032. }
  1033. /* slapi_ch_strdup is OK with NULL strings */
  1034. binddn = slapi_ch_strdup(conn->binddn);
  1035. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1036. "%s: binddn = %s, passwd = %s\n",
  1037. agmt_get_long_name(conn->agmt),
  1038. binddn?binddn:"NULL", creds->bv_val?creds->bv_val:"NULL");
  1039. /* Set some options for the connection. */
  1040. optdata = LDAP_DEREF_NEVER; /* Don't dereference aliases */
  1041. ldap_set_option(conn->ld, LDAP_OPT_DEREF, &optdata);
  1042. optdata = LDAP_VERSION3; /* We need LDAP version 3 */
  1043. ldap_set_option(conn->ld, LDAP_OPT_PROTOCOL_VERSION, &optdata);
  1044. /* Don't chase any referrals (although we shouldn't get any) */
  1045. ldap_set_option(conn->ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
  1046. /* override the default timeout with the specified timeout */
  1047. io_timeout_ms = conn->timeout.tv_sec * 1000 + conn->timeout.tv_usec / 1000;
  1048. prldap_set_session_option(conn->ld, NULL, PRLDAP_OPT_IO_MAX_TIMEOUT,
  1049. io_timeout_ms);
  1050. /* We've got an ld. Now bind to the server. */
  1051. conn->last_operation = CONN_BIND;
  1052. }
  1053. if ( bind_and_check_pwp(conn, binddn, conn->plain) == CONN_OPERATION_FAILED )
  1054. {
  1055. conn->last_ldap_error = ldap_get_lderrno (conn->ld, NULL, NULL);
  1056. conn->state = STATE_DISCONNECTED;
  1057. return_value = CONN_OPERATION_FAILED;
  1058. }
  1059. else
  1060. {
  1061. conn->last_ldap_error = ldap_rc = LDAP_SUCCESS;
  1062. conn->state = STATE_CONNECTED;
  1063. return_value = CONN_OPERATION_SUCCESS;
  1064. }
  1065. {
  1066. ConnResult supports = 0;
  1067. supports = windows_conn_replica_supports_dirsync(conn);
  1068. if (CONN_DOES_NOT_SUPPORT_DIRSYNC == supports)
  1069. {
  1070. /* We assume that a server that doesn't support dirsync is our NT4 LDAP service */
  1071. windows_private_set_isnt4(conn->agmt,1);
  1072. LDAPDebug( LDAP_DEBUG_REPL, "windows_conn_connect : detected NT4 peer\n", 0, 0, 0 );
  1073. } else
  1074. {
  1075. windows_private_set_isnt4(conn->agmt,0);
  1076. }
  1077. }
  1078. ber_bvfree(creds);
  1079. creds = NULL;
  1080. slapi_ch_free((void**)&binddn);
  1081. if(return_value == CONN_OPERATION_FAILED)
  1082. {
  1083. close_connection_internal(conn);
  1084. } else
  1085. {
  1086. conn->last_ldap_error = ldap_rc = LDAP_SUCCESS;
  1087. conn->state = STATE_CONNECTED;
  1088. }
  1089. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_connect\n", 0, 0, 0 );
  1090. return return_value;
  1091. }
  1092. static void
  1093. close_connection_internal(Repl_Connection *conn)
  1094. {
  1095. LDAPDebug( LDAP_DEBUG_TRACE, "=> close_connection_internal\n", 0, 0, 0 );
  1096. if (NULL != conn->ld)
  1097. {
  1098. /* Since we call slapi_ldap_init,
  1099. we must call slapi_ldap_unbind */
  1100. slapi_ldap_unbind(conn->ld);
  1101. }
  1102. conn->ld = NULL;
  1103. conn->state = STATE_DISCONNECTED;
  1104. conn->status = STATUS_DISCONNECTED;
  1105. conn->supports_ds50_repl = -1;
  1106. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1107. "%s: Disconnected from the consumer\n", agmt_get_long_name(conn->agmt));
  1108. LDAPDebug( LDAP_DEBUG_TRACE, "<= close_connection_internal\n", 0, 0, 0 );
  1109. }
  1110. void
  1111. windows_conn_disconnect(Repl_Connection *conn)
  1112. {
  1113. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_disconnect\n", 0, 0, 0 );
  1114. PR_ASSERT(NULL != conn);
  1115. PR_Lock(conn->lock);
  1116. close_connection_internal(conn);
  1117. PR_Unlock(conn->lock);
  1118. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_disconnect\n", 0, 0, 0 );
  1119. }
  1120. /*
  1121. * Determine if the remote replica supports DS 5.0 replication.
  1122. * Return codes:
  1123. * CONN_SUPPORTS_DS5_REPL - the remote replica suport DS5 replication
  1124. * CONN_DOES_NOT_SUPPORT_DS5_REPL - the remote replica does not
  1125. * support DS5 replication.
  1126. * CONN_OPERATION_FAILED - it could not be determined if the remote
  1127. * replica supports DS5 replication.
  1128. * CONN_NOT_CONNECTED - no connection was active.
  1129. */
  1130. ConnResult
  1131. windows_conn_replica_supports_ds5_repl(Repl_Connection *conn)
  1132. {
  1133. ConnResult return_value;
  1134. int ldap_rc;
  1135. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_replica_supports_ds5_repl\n", 0, 0, 0 );
  1136. if (windows_conn_connected(conn))
  1137. {
  1138. if (conn->supports_ds50_repl == -1) {
  1139. LDAPMessage *res = NULL;
  1140. LDAPMessage *entry = NULL;
  1141. char *attrs[] = {"supportedcontrol", "supportedextension", NULL};
  1142. conn->status = STATUS_SEARCHING;
  1143. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1144. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1145. NULL /* server controls */, NULL /* client controls */,
  1146. &conn->timeout, LDAP_NO_LIMIT, &res);
  1147. if (LDAP_SUCCESS == ldap_rc)
  1148. {
  1149. conn->supports_ds50_repl = 0;
  1150. entry = ldap_first_entry(conn->ld, res);
  1151. if (!attribute_string_value_present(conn->ld, entry, "supportedcontrol", REPL_NSDS50_UPDATE_INFO_CONTROL_OID))
  1152. {
  1153. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1154. }
  1155. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_START_NSDS50_REPLICATION_REQUEST_OID))
  1156. {
  1157. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1158. }
  1159. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_END_NSDS50_REPLICATION_REQUEST_OID))
  1160. {
  1161. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1162. }
  1163. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_ENTRY_REQUEST_OID))
  1164. {
  1165. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1166. }
  1167. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_RESPONSE_OID))
  1168. {
  1169. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1170. }
  1171. else
  1172. {
  1173. conn->supports_ds50_repl = 1;
  1174. return_value = CONN_SUPPORTS_DS5_REPL;
  1175. }
  1176. }
  1177. else
  1178. {
  1179. if (IS_DISCONNECT_ERROR(ldap_rc))
  1180. {
  1181. conn->last_ldap_error = ldap_rc; /* specific reason */
  1182. windows_conn_disconnect(conn);
  1183. return_value = CONN_NOT_CONNECTED;
  1184. }
  1185. else
  1186. {
  1187. return_value = CONN_OPERATION_FAILED;
  1188. }
  1189. }
  1190. if (NULL != res)
  1191. ldap_msgfree(res);
  1192. }
  1193. else {
  1194. return_value = conn->supports_ds50_repl ? CONN_SUPPORTS_DS5_REPL : CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1195. }
  1196. }
  1197. else
  1198. {
  1199. /* Not connected */
  1200. return_value = CONN_NOT_CONNECTED;
  1201. }
  1202. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_replica_supports_ds5_repl\n", 0, 0, 0 );
  1203. return return_value;
  1204. }
  1205. ConnResult
  1206. windows_conn_replica_supports_dirsync(Repl_Connection *conn)
  1207. {
  1208. ConnResult return_value;
  1209. int ldap_rc;
  1210. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_replica_supports_dirsync\n", 0, 0, 0 );
  1211. if (windows_conn_connected(conn))
  1212. {
  1213. if (conn->supports_dirsync == -1) {
  1214. LDAPMessage *res = NULL;
  1215. LDAPMessage *entry = NULL;
  1216. char *attrs[] = {"supportedcontrol", NULL};
  1217. conn->status = STATUS_SEARCHING;
  1218. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1219. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1220. NULL /* server controls */, NULL /* client controls */,
  1221. &conn->timeout, LDAP_NO_LIMIT, &res);
  1222. if (LDAP_SUCCESS == ldap_rc)
  1223. {
  1224. conn->supports_dirsync = 0;
  1225. entry = ldap_first_entry(conn->ld, res);
  1226. if (!attribute_string_value_present(conn->ld, entry, "supportedcontrol", REPL_DIRSYNC_CONTROL_OID))
  1227. {
  1228. return_value = CONN_DOES_NOT_SUPPORT_DIRSYNC;
  1229. }
  1230. else
  1231. {
  1232. conn->supports_dirsync =1;
  1233. return_value = CONN_SUPPORTS_DIRSYNC;
  1234. }
  1235. }
  1236. else
  1237. {
  1238. if (IS_DISCONNECT_ERROR(ldap_rc))
  1239. {
  1240. conn->last_ldap_error = ldap_rc; /* specific reason */
  1241. windows_conn_disconnect(conn);
  1242. return_value = CONN_NOT_CONNECTED;
  1243. }
  1244. else
  1245. {
  1246. return_value = CONN_OPERATION_FAILED;
  1247. }
  1248. }
  1249. if (NULL != res)
  1250. ldap_msgfree(res);
  1251. }
  1252. else {
  1253. return_value = conn->supports_dirsync ? CONN_SUPPORTS_DIRSYNC : CONN_DOES_NOT_SUPPORT_DIRSYNC;
  1254. }
  1255. }
  1256. else
  1257. {
  1258. /* Not connected */
  1259. return_value = CONN_NOT_CONNECTED;
  1260. }
  1261. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_replica_supports_dirsync\n", 0, 0, 0 );
  1262. return return_value;
  1263. }
  1264. /*
  1265. * Return 1 if "value" is a value of attribute type "type" in entry "entry".
  1266. * Otherwise, return 0.
  1267. */
  1268. static int
  1269. attribute_string_value_present(LDAP *ld, LDAPMessage *entry, const char *type,
  1270. const char *value)
  1271. {
  1272. int return_value = 0;
  1273. LDAPDebug( LDAP_DEBUG_TRACE, "=> attribute_string_value_present\n", 0, 0, 0 );
  1274. if (NULL != entry)
  1275. {
  1276. char *atype = NULL;
  1277. BerElement *ber = NULL;
  1278. atype = ldap_first_attribute(ld, entry, &ber);
  1279. while (NULL != atype && 0 == return_value)
  1280. {
  1281. if (strcasecmp(atype, type) == 0)
  1282. {
  1283. char **strvals = ldap_get_values(ld, entry, atype);
  1284. int i;
  1285. for (i = 0; return_value == 0 && NULL != strvals && NULL != strvals[i]; i++)
  1286. {
  1287. if (strcmp(strvals[i], value) == 0)
  1288. {
  1289. return_value = 1;
  1290. }
  1291. }
  1292. if (NULL != strvals)
  1293. {
  1294. ldap_value_free(strvals);
  1295. }
  1296. }
  1297. ldap_memfree(atype);
  1298. atype = ldap_next_attribute(ld, entry, ber);
  1299. }
  1300. if (NULL != ber)
  1301. ldap_ber_free(ber, 0);
  1302. /* The last atype has not been freed yet */
  1303. if (NULL != atype)
  1304. ldap_memfree(atype);
  1305. }
  1306. LDAPDebug( LDAP_DEBUG_TRACE, "<= attribute_string_value_present\n", 0, 0, 0 );
  1307. return return_value;
  1308. }
  1309. /*
  1310. * Read the remote server's schema entry, then read the local schema entry,
  1311. * and compare the nsschemacsn attribute. If the local csn is newer, or
  1312. * the remote csn is absent, push the schema down to the consumer.
  1313. * Return codes:
  1314. * CONN_SCHEMA_UPDATED if the schema was pushed successfully
  1315. * CONN_SCHEMA_NO_UPDATE_NEEDED if the schema was as new or newer than
  1316. * the local server's schema
  1317. * CONN_OPERATION_FAILED if an error occurred
  1318. * CONN_NOT_CONNECTED if no connection was active
  1319. * NOTE: Should only be called when a replication session has been
  1320. * established by sending a startReplication extended operation.
  1321. */
  1322. void
  1323. windows_conn_set_timeout(Repl_Connection *conn, long timeout)
  1324. {
  1325. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_set_timeout\n", 0, 0, 0 );
  1326. PR_ASSERT(NULL != conn);
  1327. PR_ASSERT(timeout >= 0);
  1328. PR_Lock(conn->lock);
  1329. conn->timeout.tv_sec = timeout;
  1330. PR_Unlock(conn->lock);
  1331. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_timeout\n", 0, 0, 0 );
  1332. }
  1333. void windows_conn_set_agmt_changed(Repl_Connection *conn)
  1334. {
  1335. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_set_agmt_changed\n", 0, 0, 0 );
  1336. PR_ASSERT(NULL != conn);
  1337. PR_Lock(conn->lock);
  1338. if (NULL != conn->agmt)
  1339. conn->flag_agmt_changed = 1;
  1340. PR_Unlock(conn->lock);
  1341. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_agmt_changed\n", 0, 0, 0 );
  1342. }
  1343. /*
  1344. * Check the result of an ldap_simple_bind operation to see we it
  1345. * contains the expiration controls
  1346. * return: -1 error, not bound
  1347. * 0, OK bind has succeeded
  1348. */
  1349. static int
  1350. bind_and_check_pwp(Repl_Connection *conn, char * binddn, char *password)
  1351. {
  1352. LDAPControl **ctrls = NULL;
  1353. LDAPMessage *res = NULL;
  1354. char *errmsg = NULL;
  1355. LDAP *ld = conn->ld;
  1356. int msgid;
  1357. int *msgidAdr = &msgid;
  1358. int rc;
  1359. char * optype; /* ldap_simple_bind or slapd_SSL_client_bind */
  1360. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_set_agmt_changed\n", 0, 0, 0 );
  1361. if ( conn->transport_flags == TRANSPORT_FLAG_SSL )
  1362. {
  1363. char *auth;
  1364. optype = "ldap_sasl_bind";
  1365. if ( conn->bindmethod == BINDMETHOD_SSL_CLIENTAUTH )
  1366. {
  1367. rc = slapd_sasl_ext_client_bind(conn->ld, &msgidAdr);
  1368. auth = "SSL client authentication";
  1369. if ( rc == LDAP_SUCCESS )
  1370. {
  1371. if (conn->last_ldap_error != rc)
  1372. {
  1373. conn->last_ldap_error = rc;
  1374. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1375. "%s: Replication bind with %s resumed\n",
  1376. agmt_get_long_name(conn->agmt), auth);
  1377. }
  1378. }
  1379. else
  1380. {
  1381. /* Do not report the same error over and over again */
  1382. if (conn->last_ldap_error != rc)
  1383. {
  1384. conn->last_ldap_error = rc;
  1385. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1386. "%s: Replication bind with %s failed: LDAP error %d (%s)\n",
  1387. agmt_get_long_name(conn->agmt), auth, rc,
  1388. ldap_err2string(rc));
  1389. }
  1390. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_agmt_changed - CONN_OPERATION_FAILED\n", 0, 0, 0 );
  1391. return (CONN_OPERATION_FAILED);
  1392. }
  1393. }
  1394. else
  1395. {
  1396. if( ( msgid = do_simple_bind( conn, ld, binddn, password ) ) == -1 )
  1397. {
  1398. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_agmt_changed - CONN_OPERATION_FAILED\n", 0, 0, 0 );
  1399. return (CONN_OPERATION_FAILED);
  1400. }
  1401. }
  1402. }
  1403. else
  1404. {
  1405. optype = "ldap_simple_bind";
  1406. if( ( msgid = do_simple_bind( conn, ld, binddn, password ) ) == -1 )
  1407. {
  1408. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_agmt_changed - CONN_OPERATION_FAILED\n", 0, 0, 0 );
  1409. return (CONN_OPERATION_FAILED);
  1410. }
  1411. }
  1412. /* Wait for the result */
  1413. if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &res ) == -1 )
  1414. {
  1415. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1416. "%s: Received error from consumer for %s operation\n",
  1417. agmt_get_long_name(conn->agmt), optype);
  1418. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_agmt_changed - CONN_OPERATION_FAILED\n", 0, 0, 0 );
  1419. return (CONN_OPERATION_FAILED);
  1420. }
  1421. /* Don't check ldap_result against 0 because, no timeout is specified */
  1422. /* Free res as we won't use it any longer */
  1423. if ( ldap_parse_result( ld, res, &rc, NULL, NULL, NULL, &ctrls, 1 /* Free res */)
  1424. != LDAP_SUCCESS )
  1425. {
  1426. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1427. "%s: Received error from consumer for %s operation\n",
  1428. agmt_get_long_name(conn->agmt), optype);
  1429. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_agmt_changed - CONN_OPERATION_FAILED\n", 0, 0, 0 );
  1430. return (CONN_OPERATION_FAILED);
  1431. }
  1432. if ( rc == LDAP_SUCCESS )
  1433. {
  1434. if ( ctrls )
  1435. {
  1436. int i;
  1437. for( i = 0; ctrls[ i ] != NULL; ++i )
  1438. {
  1439. if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRED)) )
  1440. {
  1441. /* Bind is successfull but password has expired */
  1442. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1443. "%s: Succesfully bound %s to consumer, "
  1444. "but password has expired on consumer.\n",
  1445. agmt_get_long_name(conn->agmt), binddn);
  1446. }
  1447. else if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRING)) )
  1448. {
  1449. /* The password is expiring in n seconds */
  1450. if ( (ctrls[ i ]->ldctl_value.bv_val != NULL) &&
  1451. (ctrls[ i ]->ldctl_value.bv_len > 0) )
  1452. {
  1453. int password_expiring = atoi( ctrls[ i ]->ldctl_value.bv_val );
  1454. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1455. "%s: Succesfully bound %s to consumer, "
  1456. "but password is expiring on consumer in %d seconds.\n",
  1457. agmt_get_long_name(conn->agmt), binddn, password_expiring);
  1458. }
  1459. }
  1460. }
  1461. ldap_controls_free( ctrls );
  1462. }
  1463. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_agmt_changed - CONN_OPERATION_SUCCESS\n", 0, 0, 0 );
  1464. return (CONN_OPERATION_SUCCESS);
  1465. }
  1466. else
  1467. {
  1468. /* errmsg is a pointer directly into the ld structure - do not free */
  1469. rc = ldap_get_lderrno( ld, NULL, &errmsg );
  1470. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1471. "%s: Replication bind to %s on consumer failed: %d (%s)\n",
  1472. agmt_get_long_name(conn->agmt), binddn, rc, errmsg);
  1473. conn->last_ldap_error = rc; /* specific error */
  1474. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_agmt_changed - CONN_OPERATION_FAILED\n", 0, 0, 0 );
  1475. return (CONN_OPERATION_FAILED);
  1476. }
  1477. }
  1478. static int
  1479. do_simple_bind (Repl_Connection *conn, LDAP *ld, char * binddn, char *password)
  1480. {
  1481. int msgid;
  1482. LDAPDebug( LDAP_DEBUG_TRACE, "=> do_simple_bind\n", 0, 0, 0 );
  1483. if( ( msgid = ldap_simple_bind( ld, binddn, password ) ) == -1 )
  1484. {
  1485. char *ldaperrtext = NULL;
  1486. int ldaperr;
  1487. int prerr = PR_GetError();
  1488. ldaperr = ldap_get_lderrno( ld, NULL, &ldaperrtext );
  1489. /* Do not report the same error over and over again */
  1490. if (conn->last_ldap_error != ldaperr)
  1491. {
  1492. conn->last_ldap_error = ldaperr;
  1493. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1494. "%s: Simple bind failed, "
  1495. SLAPI_COMPONENT_NAME_LDAPSDK " error %d (%s), "
  1496. SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
  1497. agmt_get_long_name(conn->agmt),
  1498. ldaperr, ldaperrtext ? ldaperrtext : ldap_err2string(ldaperr),
  1499. prerr, slapd_pr_strerror(prerr));
  1500. }
  1501. }
  1502. else if (conn->last_ldap_error != LDAP_SUCCESS)
  1503. {
  1504. conn->last_ldap_error = LDAP_SUCCESS;
  1505. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1506. "%s: Simple bind resumed\n",
  1507. agmt_get_long_name(conn->agmt));
  1508. }
  1509. LDAPDebug( LDAP_DEBUG_TRACE, "<= do_simple_bind\n", 0, 0, 0 );
  1510. return msgid;
  1511. }
  1512. static time_t
  1513. PRTime2time_t (PRTime tm)
  1514. {
  1515. PRInt64 rt;
  1516. LDAPDebug( LDAP_DEBUG_TRACE, "=> PRTime2time_t\n", 0, 0, 0 );
  1517. PR_ASSERT (tm);
  1518. LL_DIV(rt, tm, PR_USEC_PER_SEC);
  1519. LDAPDebug( LDAP_DEBUG_TRACE, "<= PRTime2time_t\n", 0, 0, 0 );
  1520. return (time_t)rt;
  1521. }
  1522. static Slapi_Eq_Context
  1523. repl5_start_debug_timeout(int *setlevel)
  1524. {
  1525. Slapi_Eq_Context eqctx = 0;
  1526. LDAPDebug( LDAP_DEBUG_TRACE, "=> repl5_start_debug_timeout\n", 0, 0, 0 );
  1527. if (s_debug_timeout && s_debug_level) {
  1528. time_t now = time(NULL);
  1529. eqctx = slapi_eq_once(repl5_debug_timeout_callback, setlevel,
  1530. s_debug_timeout + now);
  1531. }
  1532. LDAPDebug( LDAP_DEBUG_TRACE, "<= repl5_start_debug_timeout\n", 0, 0, 0 );
  1533. return eqctx;
  1534. }
  1535. static void
  1536. repl5_stop_debug_timeout(Slapi_Eq_Context eqctx, int *setlevel)
  1537. {
  1538. char buf[20];
  1539. char msg[SLAPI_DSE_RETURNTEXT_SIZE];
  1540. LDAPDebug( LDAP_DEBUG_TRACE, "=> repl5_stop_debug_timeout\n", 0, 0, 0 );
  1541. if (eqctx && !*setlevel) {
  1542. int found = slapi_eq_cancel(eqctx);
  1543. }
  1544. if (s_debug_timeout && s_debug_level && *setlevel) {
  1545. /* No longer needed as we are including the one in slap.h */
  1546. sprintf(buf, "%d", 0);
  1547. config_set_errorlog_level("nsslapd-errorlog-level", buf, msg, 1);
  1548. }
  1549. LDAPDebug( LDAP_DEBUG_TRACE, "<= repl5_stop_debug_timeout\n", 0, 0, 0 );
  1550. }
  1551. static void
  1552. repl5_debug_timeout_callback(time_t when, void *arg)
  1553. {
  1554. int *setlevel = (int *)arg;
  1555. /* No longer needed as we are including the one in slap.h */
  1556. char buf[20];
  1557. char msg[SLAPI_DSE_RETURNTEXT_SIZE];
  1558. LDAPDebug( LDAP_DEBUG_TRACE, "=> repl5_debug_timeout_callback\n", 0, 0, 0 );
  1559. *setlevel = 1;
  1560. sprintf(buf, "%d", s_debug_level);
  1561. config_set_errorlog_level("nsslapd-errorlog-level", buf, msg, 1);
  1562. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1563. "repl5_debug_timeout_callback: set debug level to %d at %d\n",
  1564. s_debug_level, when);
  1565. LDAPDebug( LDAP_DEBUG_TRACE, "<= repl5_debug_timeout_callback\n", 0, 0, 0 );
  1566. }