windows_connection.c 52 KB

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