windows_connection.c 59 KB

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