windows_connection.c 54 KB

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