windows_connection.c 58 KB

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