windows_connection.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818
  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. char *ptr;
  355. rc = ldap_parse_result(conn->ld, res, &err, &matched,
  356. &errmsg, &referrals, &loc_returned_controls,
  357. 0 /* Don't free the result */);
  358. if (IS_DISCONNECT_ERROR(rc))
  359. {
  360. conn->last_ldap_error = rc;
  361. windows_conn_disconnect(conn);
  362. return_value = CONN_NOT_CONNECTED;
  363. }
  364. else if (IS_DISCONNECT_ERROR(err))
  365. {
  366. conn->last_ldap_error = err;
  367. windows_conn_disconnect(conn);
  368. return_value = CONN_NOT_CONNECTED;
  369. }
  370. else if (err == LDAP_UNWILLING_TO_PERFORM && optype == CONN_MODIFY)
  371. {
  372. /* this permits password updates to fail gracefully */
  373. conn->last_ldap_error = LDAP_SUCCESS;
  374. return_value = CONN_OPERATION_SUCCESS;
  375. }
  376. else if (err == LDAP_ALREADY_EXISTS && optype == CONN_ADD)
  377. {
  378. conn->last_ldap_error = LDAP_SUCCESS;
  379. return_value = CONN_OPERATION_SUCCESS;
  380. }
  381. else if (err == LDAP_NO_SUCH_OBJECT && optype == CONN_DELETE)
  382. {
  383. conn->last_ldap_error = LDAP_SUCCESS;
  384. return_value = CONN_OPERATION_SUCCESS;
  385. }
  386. else /* regular operation, result returned */
  387. {
  388. if (NULL != returned_controls)
  389. {
  390. *returned_controls = loc_returned_controls;
  391. }
  392. if (LDAP_SUCCESS != rc)
  393. {
  394. conn->last_ldap_error = rc;
  395. }
  396. else
  397. {
  398. conn->last_ldap_error = err;
  399. }
  400. return_value = LDAP_SUCCESS == conn->last_ldap_error ? CONN_OPERATION_SUCCESS : CONN_OPERATION_FAILED;
  401. }
  402. /* remove extra newlines from AD error message */
  403. for (ptr = errmsg; ptr && *ptr; ++ptr) {
  404. if ((*ptr == '\n') || (*ptr == '\r')) {
  405. *ptr = ' ';
  406. }
  407. }
  408. /* handle special case of constraint violation - give admin
  409. enough information to allow them to fix the problem
  410. and retry - bug 170350 */
  411. if (conn->last_ldap_error == LDAP_CONSTRAINT_VIOLATION) {
  412. char ebuf[BUFSIZ];
  413. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  414. "%s: Received error [%s] when attempting to %s"
  415. " entry [%s]: Please correct the attribute specified "
  416. "in the error message. Refer to the Windows Active "
  417. "Directory docs for more information.\n",
  418. agmt_get_long_name(conn->agmt),
  419. errmsg, op_string == NULL ? "" : op_string,
  420. escape_string(dn, ebuf));
  421. } else {
  422. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  423. "%s: Received result code %d (%s) for %s operation %s%s\n",
  424. agmt_get_long_name(conn->agmt),
  425. conn->last_ldap_error, errmsg,
  426. op_string == NULL ? "" : op_string,
  427. extra_op_string == NULL ? "" : extra_op_string,
  428. extra_op_string == NULL ? "" : " ");
  429. }
  430. /*
  431. * XXXggood do I need to free matched, referrals,
  432. * anything else? Or can I pass NULL for the args
  433. * I'm not interested in?
  434. */
  435. /* Good question! Meanwhile, as RTM aproaches, let's free them... */
  436. slapi_ch_free((void **) &errmsg);
  437. slapi_ch_free((void **) &matched);
  438. charray_free(referrals);
  439. conn->status = STATUS_CONNECTED;
  440. }
  441. if (res) ldap_msgfree(res);
  442. }
  443. else
  444. {
  445. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  446. "%s: Failed to send %s operation: LDAP error %d (%s)\n",
  447. agmt_get_long_name(conn->agmt),
  448. op_string ? op_string : "NULL", rc, ldap_err2string(rc));
  449. conn->last_ldap_error = rc;
  450. if (IS_DISCONNECT_ERROR(rc))
  451. {
  452. windows_conn_disconnect(conn);
  453. return_value = CONN_NOT_CONNECTED;
  454. }
  455. else
  456. {
  457. conn->status = STATUS_CONNECTED;
  458. return_value = CONN_OPERATION_FAILED;
  459. }
  460. }
  461. }
  462. else
  463. {
  464. /* conn->last_ldap_error has been set to a more specific value
  465. * in windows_conn_connected()
  466. * conn->last_ldap_error = LDAP_SERVER_DOWN;
  467. */
  468. return_value = CONN_NOT_CONNECTED;
  469. }
  470. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_perform_operation\n", 0, 0, 0 );
  471. return return_value;
  472. }
  473. /* Copied from the chaining backend*/
  474. static Slapi_Entry *
  475. windows_LDAPMessage2Entry(LDAP * ld, LDAPMessage * msg, int attrsonly) {
  476. Slapi_Entry *e = slapi_entry_alloc();
  477. char *a = NULL;
  478. BerElement * ber = NULL;
  479. if ( e == NULL ) return NULL;
  480. if (msg == NULL) {
  481. slapi_entry_free(e);
  482. return NULL;
  483. }
  484. /*
  485. * dn not allocated by slapi
  486. * attribute type and values ARE allocated
  487. */
  488. slapi_entry_set_dn( e, ldap_get_dn( ld, msg ) );
  489. for ( a = ldap_first_attribute( ld, msg, &ber ); a!=NULL; a=ldap_next_attribute( ld, msg, ber ) )
  490. {
  491. if (0 == strcasecmp(a,"dnsRecord") || 0 == strcasecmp(a,"dnsproperty") ||
  492. 0 == strcasecmp(a,"dscorepropagationdata"))
  493. {
  494. /* AD returns us entries with these attributes that we are not interested in,
  495. * but they break the entry attribute code (I think it is looking at null-terminated
  496. * string values, but the values are binary here). It appears that AD has some problems
  497. * with allowing duplicate values for system-only multi-valued attributes. So we skip
  498. * those attributes as a workaround.
  499. */
  500. ;
  501. } else
  502. {
  503. if (attrsonly)
  504. {
  505. slapi_entry_add_value(e, a, (Slapi_Value *)NULL);
  506. ldap_memfree(a);
  507. } else
  508. {
  509. struct berval ** aVal = ldap_get_values_len( ld, msg, a);
  510. char *type_to_use = NULL;
  511. /* Work around the fact that we alias street and streetaddress, while Microsoft do not */
  512. if (0 == strcasecmp(a,"streetaddress"))
  513. {
  514. type_to_use = FAKE_STREET_ATTR_NAME;
  515. } else
  516. {
  517. type_to_use = a;
  518. }
  519. slapi_entry_add_values( e, type_to_use, aVal);
  520. ldap_memfree(a);
  521. ldap_value_free_len(aVal);
  522. }
  523. }
  524. }
  525. if ( NULL != ber )
  526. {
  527. ldap_ber_free( ber, 0 );
  528. }
  529. return e;
  530. }
  531. ConnResult
  532. windows_search_entry(Repl_Connection *conn, char* searchbase, char *filter, Slapi_Entry **entry)
  533. {
  534. ConnResult return_value = 0;
  535. int ldap_rc = 0;
  536. LDAPMessage *res = NULL;
  537. int not_unique = 0;
  538. int nummessages = 0;
  539. int numentries = 0;
  540. int numreferences = 0;
  541. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_search_entry\n", 0, 0, 0 );
  542. *entry = NULL;
  543. if (windows_conn_connected(conn))
  544. {
  545. ldap_rc = ldap_search_ext_s(conn->ld, searchbase, LDAP_SCOPE_SUBTREE,
  546. filter, NULL, 0 /* attrsonly */,
  547. NULL , NULL /* client controls */,
  548. &conn->timeout, 0 /* sizelimit */, &res);
  549. if (LDAP_SUCCESS == ldap_rc)
  550. {
  551. LDAPMessage *message = ldap_first_entry(conn->ld, res);
  552. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  553. nummessages = ldap_count_messages(conn->ld, res);
  554. numentries = ldap_count_entries(conn->ld, res);
  555. numreferences = ldap_count_references(conn->ld, res);
  556. LDAPDebug( LDAP_DEBUG_REPL, "windows_search_entry: recieved %d messages, %d entries, %d references\n",
  557. nummessages, numentries, numreferences );
  558. }
  559. if (NULL != entry)
  560. {
  561. *entry = windows_LDAPMessage2Entry(conn->ld,message,0);
  562. }
  563. /* See if there are any more entries : if so then that's an error
  564. * but we still need to get them to avoid gumming up the connection
  565. */
  566. while (NULL != ( message = ldap_next_entry(conn->ld,message)))
  567. {
  568. not_unique = 1;
  569. }
  570. return_value = CONN_OPERATION_SUCCESS;
  571. }
  572. else if (IS_DISCONNECT_ERROR(ldap_rc))
  573. {
  574. windows_conn_disconnect(conn);
  575. return_value = CONN_NOT_CONNECTED;
  576. }
  577. else
  578. {
  579. return_value = CONN_OPERATION_FAILED;
  580. }
  581. conn->last_ldap_error = ldap_rc;
  582. if (NULL != res)
  583. {
  584. ldap_msgfree(res);
  585. res = NULL;
  586. }
  587. }
  588. else
  589. {
  590. return_value = CONN_NOT_CONNECTED;
  591. }
  592. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_search_entry\n", 0, 0, 0 );
  593. return return_value;
  594. }
  595. ConnResult
  596. send_dirsync_search(Repl_Connection *conn)
  597. {
  598. int rc;
  599. ConnResult return_value;
  600. LDAPControl *server_controls[2];
  601. int msgid;
  602. const char *op_string = NULL;
  603. const char* old_dn = NULL;
  604. char* dn = NULL;
  605. int i=0;
  606. int num_comp=0;
  607. LDAPDebug( LDAP_DEBUG_TRACE, "=> send_dirsync_search\n", 0, 0, 0 );
  608. /* need to strip the dn down to dc= */
  609. old_dn = slapi_sdn_get_ndn( windows_private_get_windows_subtree(conn->agmt) );
  610. dn = strstr(old_dn, "dc=");
  611. if (windows_conn_connected(conn))
  612. {
  613. if (conn->supports_dirsync == 0)
  614. {
  615. server_controls[0] = NULL; /* unsupported */
  616. } else
  617. {
  618. /* DBDB: I'm pretty sure that the control is leaked from here */
  619. /* Purify agrees */
  620. server_controls[0] = windows_private_dirsync_control(conn->agmt); /* yes, or don't know */
  621. }
  622. server_controls[1] = NULL;
  623. conn->last_operation = CONN_SEARCH;
  624. conn->status = STATUS_SEARCHING;
  625. op_string = "search";
  626. LDAPDebug( LDAP_DEBUG_REPL, "Sending dirsync search request\n", 0, 0, 0 );
  627. rc = ldap_search_ext( conn->ld, dn, LDAP_SCOPE_SUBTREE, "(objectclass=*)", /* filter */
  628. NULL /*attrs */, PR_FALSE, server_controls, NULL, /* ClientControls */
  629. 0,0, &msgid);
  630. if (LDAP_SUCCESS == rc)
  631. {
  632. return_value = 0;
  633. }
  634. else
  635. {
  636. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  637. "%s: Failed to get %s operation: LDAP error %d (%s)\n",
  638. agmt_get_long_name(conn->agmt),
  639. op_string ? op_string : "NULL", rc, ldap_err2string(rc));
  640. conn->last_ldap_error = rc;
  641. if (IS_DISCONNECT_ERROR(rc))
  642. {
  643. windows_conn_disconnect(conn);
  644. return_value = CONN_NOT_CONNECTED;
  645. }
  646. else
  647. {
  648. conn->status = STATUS_CONNECTED;
  649. return_value = CONN_OPERATION_FAILED;
  650. }
  651. }
  652. if (server_controls[0])
  653. {
  654. ldap_control_free(server_controls[0]);
  655. }
  656. }
  657. else
  658. {
  659. /* conn->last_ldap_error has been set to a more specific value
  660. * in windows_conn_connected()
  661. * conn->last_ldap_error = LDAP_SERVER_DOWN;
  662. */
  663. return_value = CONN_NOT_CONNECTED;
  664. }
  665. LDAPDebug( LDAP_DEBUG_TRACE, "<= send_dirsync_search\n", 0, 0, 0 );
  666. return return_value;
  667. }
  668. /*
  669. * Send an LDAP add operation.
  670. */
  671. ConnResult
  672. windows_conn_send_add(Repl_Connection *conn, const char *dn, LDAPMod **attrs,
  673. LDAPControl *update_control, LDAPControl ***returned_controls)
  674. {
  675. ConnResult res = 0;
  676. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_send_add\n", 0, 0, 0 );
  677. res = windows_perform_operation(conn, CONN_ADD, dn, attrs, NULL /* newrdn */,
  678. NULL /* newparent */, 0 /* deleteoldrdn */, update_control,
  679. NULL /* extop OID */, NULL /* extop payload */, NULL /* retoidp */,
  680. NULL /* retdatap */, returned_controls);
  681. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_send_add\n", 0, 0, 0 );
  682. return res;
  683. }
  684. /*
  685. * Send an LDAP delete operation.
  686. */
  687. ConnResult
  688. windows_conn_send_delete(Repl_Connection *conn, const char *dn,
  689. LDAPControl *update_control, LDAPControl ***returned_controls)
  690. {
  691. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_send_delete\n", 0, 0, 0 );
  692. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_send_delete\n", 0, 0, 0 );
  693. return windows_perform_operation(conn, CONN_DELETE, dn, NULL /* attrs */,
  694. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  695. update_control, NULL /* extop OID */, NULL /* extop payload */,
  696. NULL /* retoidp */, NULL /* retdatap */, returned_controls);
  697. }
  698. /*
  699. * Send an LDAP modify operation.
  700. */
  701. ConnResult
  702. windows_conn_send_modify(Repl_Connection *conn, const char *dn, LDAPMod **mods,
  703. LDAPControl *update_control, LDAPControl ***returned_controls)
  704. {
  705. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_send_modify\n", 0, 0, 0 );
  706. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_send_modify\n", 0, 0, 0 );
  707. return windows_perform_operation(conn, CONN_MODIFY, dn, mods, NULL /* newrdn */,
  708. NULL /* newparent */, 0 /* deleteoldrdn */, update_control,
  709. NULL /* extop OID */, NULL /* extop payload */, NULL /* retoidp */,
  710. NULL /* retdatap */, returned_controls);
  711. }
  712. /*
  713. * Send an LDAP moddn operation.
  714. */
  715. ConnResult
  716. windows_conn_send_rename(Repl_Connection *conn, const char *dn,
  717. const char *newrdn, const char *newparent, int deleteoldrdn,
  718. LDAPControl *update_control, LDAPControl ***returned_controls)
  719. {
  720. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_send_rename\n", 0, 0, 0 );
  721. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_send_rename\n", 0, 0, 0 );
  722. return windows_perform_operation(conn, CONN_RENAME, dn, NULL /* attrs */,
  723. newrdn, newparent, deleteoldrdn, update_control,
  724. NULL /* extop OID */, NULL /* extop payload */, NULL /* retoidp */,
  725. NULL /* retdatap */, returned_controls);
  726. }
  727. /*
  728. * Send an LDAP search operation.
  729. */
  730. Slapi_Entry * windows_conn_get_search_result(Repl_Connection *conn)
  731. {
  732. int rc=0;
  733. int matches=0;
  734. LDAPMessage *res = NULL;
  735. Slapi_Entry *e = NULL;
  736. LDAPMessage *lm = NULL;
  737. char *a = "";
  738. char *dn = "";
  739. BerElement *ber = NULL;
  740. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_get_search_result\n", 0, 0, 0 );
  741. if (windows_conn_connected(conn))
  742. {
  743. rc = ldap_result( conn->ld, LDAP_RES_ANY, 0, &conn->timeout, &res );
  744. switch (rc) {
  745. case 0:
  746. case -1:
  747. case LDAP_RES_SEARCH_REFERENCE:
  748. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name, "error in windows_conn_get_search_result, rc=%d\n", rc);
  749. break;
  750. case LDAP_RES_SEARCH_RESULT:
  751. {
  752. LDAPControl **returned_controls = NULL;
  753. int code = 0;
  754. int parse_rc = 0;
  755. /* Purify says this is a leak : */
  756. parse_rc = ldap_parse_result( conn->ld, res, &code, NULL, NULL, NULL, &returned_controls, 0 );
  757. if (returned_controls)
  758. {
  759. windows_private_update_dirsync_control(conn->agmt, returned_controls);
  760. ldap_controls_free(returned_controls);
  761. }
  762. if (windows_private_dirsync_has_more(conn->agmt)) {
  763. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,"received hasmore from dirsync\n", 0);
  764. }
  765. }
  766. break;
  767. case LDAP_RES_SEARCH_ENTRY:
  768. {
  769. if (( dn = ldap_get_dn( conn->ld, res )) != NULL )
  770. {
  771. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,"received entry from dirsync: %s\n", dn);
  772. lm = ldap_first_entry( conn->ld, res );
  773. e = windows_LDAPMessage2Entry(conn->ld,lm,0);
  774. ldap_memfree(dn);
  775. }
  776. }
  777. break;
  778. } /* switch */
  779. } /* if */
  780. if (res)
  781. {
  782. ldap_msgfree( res );
  783. res = NULL;
  784. }
  785. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_get_search_result\n", 0, 0, 0 );
  786. return e;
  787. }
  788. /*
  789. * Send an LDAP extended operation.
  790. */
  791. ConnResult
  792. windows_conn_send_extended_operation(Repl_Connection *conn, const char *extop_oid,
  793. struct berval *payload, char **retoidp, struct berval **retdatap,
  794. LDAPControl *update_control, LDAPControl ***returned_controls)
  795. {
  796. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_send_extended_operation\n", 0, 0, 0 );
  797. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_send_extended_operation\n", 0, 0, 0 );
  798. return windows_perform_operation(conn, CONN_EXTENDED_OPERATION, NULL /* dn */, NULL /* attrs */,
  799. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  800. update_control, extop_oid, payload, retoidp, retdatap,
  801. returned_controls);
  802. }
  803. /*
  804. * Synchronously read an entry and return a specific attribute's values.
  805. * Returns CONN_OPERATION_SUCCESS if successful. Returns
  806. * CONN_OPERATION_FAILED if the operation was sent but an LDAP error
  807. * occurred (conn->last_ldap_error is set in this case), and
  808. * CONN_NOT_CONNECTED if no connection was active.
  809. *
  810. * The caller must free the returned_bvals.
  811. */
  812. ConnResult
  813. windows_conn_read_entry_attribute(Repl_Connection *conn, const char *dn,
  814. char *type, struct berval ***returned_bvals)
  815. {
  816. ConnResult return_value;
  817. int ldap_rc;
  818. LDAPControl *server_controls[2];
  819. LDAPMessage *res = NULL;
  820. char *attrs[2];
  821. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_read_entry_attribute\n", 0, 0, 0 );
  822. PR_ASSERT(NULL != type);
  823. if (windows_conn_connected(conn))
  824. {
  825. server_controls[0] = &manageDSAITControl;
  826. server_controls[1] = NULL;
  827. attrs[0] = type;
  828. attrs[1] = NULL;
  829. ldap_rc = ldap_search_ext_s(conn->ld, dn, LDAP_SCOPE_BASE,
  830. "(objectclass=*)", attrs, 0 /* attrsonly */,
  831. server_controls, NULL /* client controls */,
  832. &conn->timeout, 0 /* sizelimit */, &res);
  833. if (LDAP_SUCCESS == ldap_rc)
  834. {
  835. LDAPMessage *entry = ldap_first_entry(conn->ld, res);
  836. if (NULL != entry)
  837. {
  838. *returned_bvals = ldap_get_values_len(conn->ld, entry, type);
  839. }
  840. return_value = CONN_OPERATION_SUCCESS;
  841. }
  842. else if (IS_DISCONNECT_ERROR(ldap_rc))
  843. {
  844. windows_conn_disconnect(conn);
  845. return_value = CONN_NOT_CONNECTED;
  846. }
  847. else
  848. {
  849. return_value = CONN_OPERATION_FAILED;
  850. }
  851. conn->last_ldap_error = ldap_rc;
  852. if (NULL != res)
  853. {
  854. ldap_msgfree(res);
  855. res = NULL;
  856. }
  857. }
  858. else
  859. {
  860. return_value = CONN_NOT_CONNECTED;
  861. }
  862. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_read_entry_attribute\n", 0, 0, 0 );
  863. return return_value;
  864. }
  865. /*
  866. * Return an pointer to a string describing the connection's status.
  867. */
  868. const char *
  869. windows_conn_get_status(Repl_Connection *conn)
  870. {
  871. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_get_status\n", 0, 0, 0 );
  872. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_get_status\n", 0, 0, 0 );
  873. return conn->status;
  874. }
  875. /*
  876. * Cancel any outstanding linger timer. Should be called when
  877. * a replication session is beginning.
  878. */
  879. void
  880. windows_conn_cancel_linger(Repl_Connection *conn)
  881. {
  882. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_cancel_linger\n", 0, 0, 0 );
  883. PR_ASSERT(NULL != conn);
  884. PR_Lock(conn->lock);
  885. if (conn->linger_active)
  886. {
  887. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  888. "%s: Cancelling linger on the connection\n",
  889. agmt_get_long_name(conn->agmt));
  890. conn->linger_active = PR_FALSE;
  891. if (slapi_eq_cancel(conn->linger_event) == 1)
  892. {
  893. conn->refcnt--;
  894. }
  895. conn->linger_event = NULL;
  896. conn->status = STATUS_CONNECTED;
  897. }
  898. else
  899. {
  900. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  901. "%s: No linger to cancel on the connection\n",
  902. agmt_get_long_name(conn->agmt));
  903. }
  904. PR_Unlock(conn->lock);
  905. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_cancel_linger\n", 0, 0, 0 );
  906. }
  907. /*
  908. * Called when our linger timeout timer expires. This means
  909. * we should check to see if perhaps the connection's become
  910. * active again, in which case we do nothing. Otherwise,
  911. * we close the connection.
  912. */
  913. static void
  914. linger_timeout(time_t event_time, void *arg)
  915. {
  916. PRBool delete_now;
  917. Repl_Connection *conn = (Repl_Connection *)arg;
  918. LDAPDebug( LDAP_DEBUG_TRACE, "=> linger_timeout\n", 0, 0, 0 );
  919. PR_ASSERT(NULL != conn);
  920. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  921. "%s: Linger timeout has expired on the connection\n",
  922. agmt_get_long_name(conn->agmt));
  923. PR_Lock(conn->lock);
  924. if (conn->linger_active)
  925. {
  926. conn->linger_active = PR_FALSE;
  927. conn->linger_event = NULL;
  928. close_connection_internal(conn);
  929. }
  930. delete_now = conn->delete_after_linger;
  931. PR_Unlock(conn->lock);
  932. if (delete_now)
  933. {
  934. windows_conn_delete_internal(conn);
  935. }
  936. LDAPDebug( LDAP_DEBUG_TRACE, "<= linger_timeout\n", 0, 0, 0 );
  937. }
  938. /*
  939. * Indicate that a session is ending. The linger timer starts when
  940. * this function is called.
  941. */
  942. void
  943. windows_conn_start_linger(Repl_Connection *conn)
  944. {
  945. time_t now;
  946. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_start_linger\n", 0, 0, 0 );
  947. PR_ASSERT(NULL != conn);
  948. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  949. "%s: Beginning linger on the connection\n",
  950. agmt_get_long_name(conn->agmt));
  951. if (!windows_conn_connected(conn))
  952. {
  953. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  954. "%s: No linger on the closed conn\n",
  955. agmt_get_long_name(conn->agmt));
  956. return;
  957. }
  958. time(&now);
  959. PR_Lock(conn->lock);
  960. if (conn->linger_active)
  961. {
  962. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  963. "%s: Linger already active on the connection\n",
  964. agmt_get_long_name(conn->agmt));
  965. }
  966. else
  967. {
  968. conn->linger_active = PR_TRUE;
  969. conn->linger_event = slapi_eq_once(linger_timeout, conn, now + conn->linger_time);
  970. conn->status = STATUS_LINGERING;
  971. }
  972. PR_Unlock(conn->lock);
  973. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_start_linger\n", 0, 0, 0 );
  974. }
  975. /*
  976. * If no connection is currently active, opens a connection and binds to
  977. * the remote server. If a connection is open (e.g. lingering) then
  978. * this is a no-op.
  979. *
  980. * Returns CONN_OPERATION_SUCCESS on success, or CONN_OPERATION_FAILED
  981. * on failure. Sets conn->last_ldap_error and conn->last_operation;
  982. */
  983. ConnResult
  984. windows_conn_connect(Repl_Connection *conn)
  985. {
  986. int ldap_rc;
  987. int optdata;
  988. int secure = 0;
  989. char* binddn = NULL;
  990. struct berval *creds;
  991. ConnResult return_value = CONN_OPERATION_SUCCESS;
  992. int pw_ret = 1;
  993. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_connect\n", 0, 0, 0 );
  994. /** Connection already open just return SUCCESS **/
  995. if(conn->state == STATE_CONNECTED) {
  996. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_connect\n", 0, 0, 0 );
  997. return return_value;
  998. }
  999. PR_Lock(conn->lock);
  1000. if (conn->flag_agmt_changed) {
  1001. /* So far we cannot change Hostname and Port */
  1002. /* slapi_ch_free((void **)&conn->hostname); */
  1003. /* conn->hostname = agmt_get_hostname(conn->agmt); */
  1004. /* conn->port = agmt_get_port(conn->agmt); */
  1005. slapi_ch_free((void **)&conn->binddn);
  1006. conn->binddn = agmt_get_binddn(conn->agmt);
  1007. conn->bindmethod = agmt_get_bindmethod(conn->agmt);
  1008. conn->transport_flags = agmt_get_transport_flags(conn->agmt);
  1009. conn->timeout.tv_sec = agmt_get_timeout(conn->agmt);
  1010. conn->flag_agmt_changed = 0;
  1011. slapi_ch_free((void **)&conn->plain);
  1012. }
  1013. PR_Unlock(conn->lock);
  1014. creds = agmt_get_credentials(conn->agmt);
  1015. if (conn->plain == NULL) {
  1016. char *plain = NULL;
  1017. /* kexcoff: for reversible encryption */
  1018. /* We need to test the return code of pw_rever_decode in order to decide
  1019. * if a free for plain will be needed (pw_ret == 0) or not (pw_ret != 0) */
  1020. pw_ret = pw_rever_decode(creds->bv_val, &plain, type_nsds5ReplicaCredentials);
  1021. /* Pb occured in decryption: stop now, binding will fail */
  1022. if ( pw_ret == -1 )
  1023. {
  1024. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1025. "%s: Decoding of the credentials failed.\n",
  1026. agmt_get_long_name(conn->agmt));
  1027. return_value = CONN_OPERATION_FAILED;
  1028. conn->last_ldap_error = LDAP_INVALID_CREDENTIALS;
  1029. conn->state = STATE_DISCONNECTED;
  1030. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_connect\n", 0, 0, 0 );
  1031. return (return_value);
  1032. } /* Else, does not mean that the plain is correct, only means the we had no internal
  1033. decoding pb */
  1034. conn->plain = slapi_ch_strdup (plain);
  1035. if (!pw_ret) slapi_ch_free((void**)&plain);
  1036. }
  1037. /* ugaston: if SSL has been selected in the replication agreement, SSL client
  1038. * initialisation should be done before ever trying to open any connection at all.
  1039. */
  1040. if (conn->transport_flags == TRANSPORT_FLAG_TLS)
  1041. {
  1042. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1043. "%s: Replication secured by StartTLS not currently supported\n",
  1044. agmt_get_long_name(conn->agmt));
  1045. return_value = CONN_OPERATION_FAILED;
  1046. conn->last_ldap_error = LDAP_STRONG_AUTH_NOT_SUPPORTED;
  1047. conn->state = STATE_DISCONNECTED;
  1048. } else if(conn->transport_flags == TRANSPORT_FLAG_SSL)
  1049. {
  1050. /** Make sure the SSL Library has been initialized before anything else **/
  1051. if(slapd_security_library_is_initialized() != 1)
  1052. {
  1053. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1054. "%s: SSL Not Initialized, Replication over SSL FAILED\n",
  1055. agmt_get_long_name(conn->agmt));
  1056. conn->last_ldap_error = LDAP_INAPPROPRIATE_AUTH;
  1057. conn->last_operation = CONN_INIT;
  1058. ber_bvfree(creds);
  1059. creds = NULL;
  1060. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_connect\n", 0, 0, 0 );
  1061. return CONN_SSL_NOT_ENABLED;
  1062. } else
  1063. {
  1064. secure = 1;
  1065. }
  1066. }
  1067. if (return_value == CONN_OPERATION_SUCCESS) {
  1068. int io_timeout_ms;
  1069. /* Now we initialize the LDAP Structure and set options */
  1070. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1071. "%s: Trying %s slapi_ldap_init\n",
  1072. agmt_get_long_name(conn->agmt),
  1073. secure ? "secure" : "non-secure");
  1074. conn->ld = slapi_ldap_init(conn->hostname, conn->port, secure, 0);
  1075. if (NULL == conn->ld)
  1076. {
  1077. return_value = CONN_OPERATION_FAILED;
  1078. conn->state = STATE_DISCONNECTED;
  1079. conn->last_operation = CONN_INIT;
  1080. conn->last_ldap_error = LDAP_LOCAL_ERROR;
  1081. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1082. "%s: Failed to establish %sconnection to the consumer\n",
  1083. agmt_get_long_name(conn->agmt),
  1084. secure ? "secure " : "");
  1085. ber_bvfree(creds);
  1086. creds = NULL;
  1087. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_connect\n", 0, 0, 0 );
  1088. return return_value;
  1089. }
  1090. /* slapi_ch_strdup is OK with NULL strings */
  1091. binddn = slapi_ch_strdup(conn->binddn);
  1092. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1093. "%s: binddn = %s, passwd = %s\n",
  1094. agmt_get_long_name(conn->agmt),
  1095. binddn?binddn:"NULL", creds->bv_val?creds->bv_val:"NULL");
  1096. /* Set some options for the connection. */
  1097. optdata = LDAP_DEREF_NEVER; /* Don't dereference aliases */
  1098. ldap_set_option(conn->ld, LDAP_OPT_DEREF, &optdata);
  1099. optdata = LDAP_VERSION3; /* We need LDAP version 3 */
  1100. ldap_set_option(conn->ld, LDAP_OPT_PROTOCOL_VERSION, &optdata);
  1101. /* Don't chase any referrals (although we shouldn't get any) */
  1102. ldap_set_option(conn->ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
  1103. /* override the default timeout with the specified timeout */
  1104. io_timeout_ms = conn->timeout.tv_sec * 1000 + conn->timeout.tv_usec / 1000;
  1105. prldap_set_session_option(conn->ld, NULL, PRLDAP_OPT_IO_MAX_TIMEOUT,
  1106. io_timeout_ms);
  1107. /* We've got an ld. Now bind to the server. */
  1108. conn->last_operation = CONN_BIND;
  1109. }
  1110. if ( bind_and_check_pwp(conn, binddn, conn->plain) == CONN_OPERATION_FAILED )
  1111. {
  1112. conn->last_ldap_error = ldap_get_lderrno (conn->ld, NULL, NULL);
  1113. conn->state = STATE_DISCONNECTED;
  1114. return_value = CONN_OPERATION_FAILED;
  1115. }
  1116. else
  1117. {
  1118. conn->last_ldap_error = ldap_rc = LDAP_SUCCESS;
  1119. conn->state = STATE_CONNECTED;
  1120. return_value = CONN_OPERATION_SUCCESS;
  1121. }
  1122. {
  1123. ConnResult supports = 0;
  1124. supports = windows_conn_replica_supports_dirsync(conn);
  1125. if (CONN_DOES_NOT_SUPPORT_DIRSYNC == supports)
  1126. {
  1127. /* We assume that a server that doesn't support dirsync is our NT4 LDAP service */
  1128. windows_private_set_isnt4(conn->agmt,1);
  1129. LDAPDebug( LDAP_DEBUG_REPL, "windows_conn_connect : detected NT4 peer\n", 0, 0, 0 );
  1130. } else
  1131. {
  1132. windows_private_set_isnt4(conn->agmt,0);
  1133. }
  1134. }
  1135. ber_bvfree(creds);
  1136. creds = NULL;
  1137. slapi_ch_free((void**)&binddn);
  1138. if(return_value == CONN_OPERATION_FAILED)
  1139. {
  1140. close_connection_internal(conn);
  1141. } else
  1142. {
  1143. conn->last_ldap_error = ldap_rc = LDAP_SUCCESS;
  1144. conn->state = STATE_CONNECTED;
  1145. }
  1146. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_connect\n", 0, 0, 0 );
  1147. return return_value;
  1148. }
  1149. static void
  1150. close_connection_internal(Repl_Connection *conn)
  1151. {
  1152. LDAPDebug( LDAP_DEBUG_TRACE, "=> close_connection_internal\n", 0, 0, 0 );
  1153. if (NULL != conn->ld)
  1154. {
  1155. /* Since we call slapi_ldap_init,
  1156. we must call slapi_ldap_unbind */
  1157. slapi_ldap_unbind(conn->ld);
  1158. }
  1159. conn->ld = NULL;
  1160. conn->state = STATE_DISCONNECTED;
  1161. conn->status = STATUS_DISCONNECTED;
  1162. conn->supports_ds50_repl = -1;
  1163. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1164. "%s: Disconnected from the consumer\n", agmt_get_long_name(conn->agmt));
  1165. LDAPDebug( LDAP_DEBUG_TRACE, "<= close_connection_internal\n", 0, 0, 0 );
  1166. }
  1167. void
  1168. windows_conn_disconnect(Repl_Connection *conn)
  1169. {
  1170. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_disconnect\n", 0, 0, 0 );
  1171. PR_ASSERT(NULL != conn);
  1172. PR_Lock(conn->lock);
  1173. close_connection_internal(conn);
  1174. PR_Unlock(conn->lock);
  1175. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_disconnect\n", 0, 0, 0 );
  1176. }
  1177. /*
  1178. * Determine if the remote replica supports DS 5.0 replication.
  1179. * Return codes:
  1180. * CONN_SUPPORTS_DS5_REPL - the remote replica suport DS5 replication
  1181. * CONN_DOES_NOT_SUPPORT_DS5_REPL - the remote replica does not
  1182. * support DS5 replication.
  1183. * CONN_OPERATION_FAILED - it could not be determined if the remote
  1184. * replica supports DS5 replication.
  1185. * CONN_NOT_CONNECTED - no connection was active.
  1186. */
  1187. ConnResult
  1188. windows_conn_replica_supports_ds5_repl(Repl_Connection *conn)
  1189. {
  1190. ConnResult return_value;
  1191. int ldap_rc;
  1192. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_replica_supports_ds5_repl\n", 0, 0, 0 );
  1193. if (windows_conn_connected(conn))
  1194. {
  1195. if (conn->supports_ds50_repl == -1) {
  1196. LDAPMessage *res = NULL;
  1197. LDAPMessage *entry = NULL;
  1198. char *attrs[] = {"supportedcontrol", "supportedextension", NULL};
  1199. conn->status = STATUS_SEARCHING;
  1200. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1201. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1202. NULL /* server controls */, NULL /* client controls */,
  1203. &conn->timeout, LDAP_NO_LIMIT, &res);
  1204. if (LDAP_SUCCESS == ldap_rc)
  1205. {
  1206. conn->supports_ds50_repl = 0;
  1207. entry = ldap_first_entry(conn->ld, res);
  1208. if (!attribute_string_value_present(conn->ld, entry, "supportedcontrol", REPL_NSDS50_UPDATE_INFO_CONTROL_OID))
  1209. {
  1210. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1211. }
  1212. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_START_NSDS50_REPLICATION_REQUEST_OID))
  1213. {
  1214. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1215. }
  1216. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_END_NSDS50_REPLICATION_REQUEST_OID))
  1217. {
  1218. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1219. }
  1220. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_ENTRY_REQUEST_OID))
  1221. {
  1222. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1223. }
  1224. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_RESPONSE_OID))
  1225. {
  1226. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1227. }
  1228. else
  1229. {
  1230. conn->supports_ds50_repl = 1;
  1231. return_value = CONN_SUPPORTS_DS5_REPL;
  1232. }
  1233. }
  1234. else
  1235. {
  1236. if (IS_DISCONNECT_ERROR(ldap_rc))
  1237. {
  1238. conn->last_ldap_error = ldap_rc; /* specific reason */
  1239. windows_conn_disconnect(conn);
  1240. return_value = CONN_NOT_CONNECTED;
  1241. }
  1242. else
  1243. {
  1244. return_value = CONN_OPERATION_FAILED;
  1245. }
  1246. }
  1247. if (NULL != res)
  1248. ldap_msgfree(res);
  1249. }
  1250. else {
  1251. return_value = conn->supports_ds50_repl ? CONN_SUPPORTS_DS5_REPL : CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1252. }
  1253. }
  1254. else
  1255. {
  1256. /* Not connected */
  1257. return_value = CONN_NOT_CONNECTED;
  1258. }
  1259. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_replica_supports_ds5_repl\n", 0, 0, 0 );
  1260. return return_value;
  1261. }
  1262. ConnResult
  1263. windows_conn_replica_supports_dirsync(Repl_Connection *conn)
  1264. {
  1265. ConnResult return_value;
  1266. int ldap_rc;
  1267. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_replica_supports_dirsync\n", 0, 0, 0 );
  1268. if (windows_conn_connected(conn))
  1269. {
  1270. if (conn->supports_dirsync == -1) {
  1271. LDAPMessage *res = NULL;
  1272. LDAPMessage *entry = NULL;
  1273. char *attrs[] = {"supportedcontrol", NULL};
  1274. conn->status = STATUS_SEARCHING;
  1275. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1276. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1277. NULL /* server controls */, NULL /* client controls */,
  1278. &conn->timeout, LDAP_NO_LIMIT, &res);
  1279. if (LDAP_SUCCESS == ldap_rc)
  1280. {
  1281. conn->supports_dirsync = 0;
  1282. entry = ldap_first_entry(conn->ld, res);
  1283. if (!attribute_string_value_present(conn->ld, entry, "supportedcontrol", REPL_DIRSYNC_CONTROL_OID))
  1284. {
  1285. return_value = CONN_DOES_NOT_SUPPORT_DIRSYNC;
  1286. }
  1287. else
  1288. {
  1289. conn->supports_dirsync =1;
  1290. return_value = CONN_SUPPORTS_DIRSYNC;
  1291. }
  1292. }
  1293. else
  1294. {
  1295. if (IS_DISCONNECT_ERROR(ldap_rc))
  1296. {
  1297. conn->last_ldap_error = ldap_rc; /* specific reason */
  1298. windows_conn_disconnect(conn);
  1299. return_value = CONN_NOT_CONNECTED;
  1300. }
  1301. else
  1302. {
  1303. return_value = CONN_OPERATION_FAILED;
  1304. }
  1305. }
  1306. if (NULL != res)
  1307. ldap_msgfree(res);
  1308. }
  1309. else {
  1310. return_value = conn->supports_dirsync ? CONN_SUPPORTS_DIRSYNC : CONN_DOES_NOT_SUPPORT_DIRSYNC;
  1311. }
  1312. }
  1313. else
  1314. {
  1315. /* Not connected */
  1316. return_value = CONN_NOT_CONNECTED;
  1317. }
  1318. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_replica_supports_dirsync\n", 0, 0, 0 );
  1319. return return_value;
  1320. }
  1321. /*
  1322. * Return 1 if "value" is a value of attribute type "type" in entry "entry".
  1323. * Otherwise, return 0.
  1324. */
  1325. static int
  1326. attribute_string_value_present(LDAP *ld, LDAPMessage *entry, const char *type,
  1327. const char *value)
  1328. {
  1329. int return_value = 0;
  1330. LDAPDebug( LDAP_DEBUG_TRACE, "=> attribute_string_value_present\n", 0, 0, 0 );
  1331. if (NULL != entry)
  1332. {
  1333. char *atype = NULL;
  1334. BerElement *ber = NULL;
  1335. atype = ldap_first_attribute(ld, entry, &ber);
  1336. while (NULL != atype && 0 == return_value)
  1337. {
  1338. if (strcasecmp(atype, type) == 0)
  1339. {
  1340. char **strvals = ldap_get_values(ld, entry, atype);
  1341. int i;
  1342. for (i = 0; return_value == 0 && NULL != strvals && NULL != strvals[i]; i++)
  1343. {
  1344. if (strcmp(strvals[i], value) == 0)
  1345. {
  1346. return_value = 1;
  1347. }
  1348. }
  1349. if (NULL != strvals)
  1350. {
  1351. ldap_value_free(strvals);
  1352. }
  1353. }
  1354. ldap_memfree(atype);
  1355. atype = ldap_next_attribute(ld, entry, ber);
  1356. }
  1357. if (NULL != ber)
  1358. ldap_ber_free(ber, 0);
  1359. /* The last atype has not been freed yet */
  1360. if (NULL != atype)
  1361. ldap_memfree(atype);
  1362. }
  1363. LDAPDebug( LDAP_DEBUG_TRACE, "<= attribute_string_value_present\n", 0, 0, 0 );
  1364. return return_value;
  1365. }
  1366. /*
  1367. * Read the remote server's schema entry, then read the local schema entry,
  1368. * and compare the nsschemacsn attribute. If the local csn is newer, or
  1369. * the remote csn is absent, push the schema down to the consumer.
  1370. * Return codes:
  1371. * CONN_SCHEMA_UPDATED if the schema was pushed successfully
  1372. * CONN_SCHEMA_NO_UPDATE_NEEDED if the schema was as new or newer than
  1373. * the local server's schema
  1374. * CONN_OPERATION_FAILED if an error occurred
  1375. * CONN_NOT_CONNECTED if no connection was active
  1376. * NOTE: Should only be called when a replication session has been
  1377. * established by sending a startReplication extended operation.
  1378. */
  1379. void
  1380. windows_conn_set_timeout(Repl_Connection *conn, long timeout)
  1381. {
  1382. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_set_timeout\n", 0, 0, 0 );
  1383. PR_ASSERT(NULL != conn);
  1384. PR_ASSERT(timeout >= 0);
  1385. PR_Lock(conn->lock);
  1386. conn->timeout.tv_sec = timeout;
  1387. PR_Unlock(conn->lock);
  1388. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_timeout\n", 0, 0, 0 );
  1389. }
  1390. void windows_conn_set_agmt_changed(Repl_Connection *conn)
  1391. {
  1392. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_set_agmt_changed\n", 0, 0, 0 );
  1393. PR_ASSERT(NULL != conn);
  1394. PR_Lock(conn->lock);
  1395. if (NULL != conn->agmt)
  1396. conn->flag_agmt_changed = 1;
  1397. PR_Unlock(conn->lock);
  1398. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_agmt_changed\n", 0, 0, 0 );
  1399. }
  1400. /*
  1401. * Check the result of an ldap_simple_bind operation to see we it
  1402. * contains the expiration controls
  1403. * return: -1 error, not bound
  1404. * 0, OK bind has succeeded
  1405. */
  1406. static int
  1407. bind_and_check_pwp(Repl_Connection *conn, char * binddn, char *password)
  1408. {
  1409. LDAPControl **ctrls = NULL;
  1410. LDAPMessage *res = NULL;
  1411. char *errmsg = NULL;
  1412. LDAP *ld = conn->ld;
  1413. int msgid;
  1414. int *msgidAdr = &msgid;
  1415. int rc;
  1416. char * optype; /* ldap_simple_bind or slapd_SSL_client_bind */
  1417. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_set_agmt_changed\n", 0, 0, 0 );
  1418. if ( conn->transport_flags == TRANSPORT_FLAG_SSL )
  1419. {
  1420. char *auth;
  1421. optype = "ldap_sasl_bind";
  1422. if ( conn->bindmethod == BINDMETHOD_SSL_CLIENTAUTH )
  1423. {
  1424. rc = slapd_sasl_ext_client_bind(conn->ld, &msgidAdr);
  1425. auth = "SSL client authentication";
  1426. if ( rc == LDAP_SUCCESS )
  1427. {
  1428. if (conn->last_ldap_error != rc)
  1429. {
  1430. conn->last_ldap_error = rc;
  1431. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1432. "%s: Replication bind with %s resumed\n",
  1433. agmt_get_long_name(conn->agmt), auth);
  1434. }
  1435. }
  1436. else
  1437. {
  1438. /* Do not report the same error over and over again */
  1439. if (conn->last_ldap_error != rc)
  1440. {
  1441. conn->last_ldap_error = rc;
  1442. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1443. "%s: Replication bind with %s failed: LDAP error %d (%s)\n",
  1444. agmt_get_long_name(conn->agmt), auth, rc,
  1445. ldap_err2string(rc));
  1446. }
  1447. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_agmt_changed - CONN_OPERATION_FAILED\n", 0, 0, 0 );
  1448. return (CONN_OPERATION_FAILED);
  1449. }
  1450. }
  1451. else
  1452. {
  1453. if( ( msgid = do_simple_bind( conn, ld, binddn, password ) ) == -1 )
  1454. {
  1455. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_agmt_changed - CONN_OPERATION_FAILED\n", 0, 0, 0 );
  1456. return (CONN_OPERATION_FAILED);
  1457. }
  1458. }
  1459. }
  1460. else
  1461. {
  1462. optype = "ldap_simple_bind";
  1463. if( ( msgid = do_simple_bind( conn, ld, binddn, password ) ) == -1 )
  1464. {
  1465. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_agmt_changed - CONN_OPERATION_FAILED\n", 0, 0, 0 );
  1466. return (CONN_OPERATION_FAILED);
  1467. }
  1468. }
  1469. /* Wait for the result */
  1470. if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &res ) == -1 )
  1471. {
  1472. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1473. "%s: Received error from consumer for %s operation\n",
  1474. agmt_get_long_name(conn->agmt), optype);
  1475. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_agmt_changed - CONN_OPERATION_FAILED\n", 0, 0, 0 );
  1476. return (CONN_OPERATION_FAILED);
  1477. }
  1478. /* Don't check ldap_result against 0 because, no timeout is specified */
  1479. /* Free res as we won't use it any longer */
  1480. if ( ldap_parse_result( ld, res, &rc, NULL, NULL, NULL, &ctrls, 1 /* Free res */)
  1481. != LDAP_SUCCESS )
  1482. {
  1483. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1484. "%s: Received error from consumer for %s operation\n",
  1485. agmt_get_long_name(conn->agmt), optype);
  1486. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_agmt_changed - CONN_OPERATION_FAILED\n", 0, 0, 0 );
  1487. return (CONN_OPERATION_FAILED);
  1488. }
  1489. if ( rc == LDAP_SUCCESS )
  1490. {
  1491. if ( ctrls )
  1492. {
  1493. int i;
  1494. for( i = 0; ctrls[ i ] != NULL; ++i )
  1495. {
  1496. if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRED)) )
  1497. {
  1498. /* Bind is successfull but password has expired */
  1499. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1500. "%s: Succesfully bound %s to consumer, "
  1501. "but password has expired on consumer.\n",
  1502. agmt_get_long_name(conn->agmt), binddn);
  1503. }
  1504. else if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRING)) )
  1505. {
  1506. /* The password is expiring in n seconds */
  1507. if ( (ctrls[ i ]->ldctl_value.bv_val != NULL) &&
  1508. (ctrls[ i ]->ldctl_value.bv_len > 0) )
  1509. {
  1510. int password_expiring = atoi( ctrls[ i ]->ldctl_value.bv_val );
  1511. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1512. "%s: Succesfully bound %s to consumer, "
  1513. "but password is expiring on consumer in %d seconds.\n",
  1514. agmt_get_long_name(conn->agmt), binddn, password_expiring);
  1515. }
  1516. }
  1517. }
  1518. ldap_controls_free( ctrls );
  1519. }
  1520. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_agmt_changed - CONN_OPERATION_SUCCESS\n", 0, 0, 0 );
  1521. return (CONN_OPERATION_SUCCESS);
  1522. }
  1523. else
  1524. {
  1525. /* errmsg is a pointer directly into the ld structure - do not free */
  1526. rc = ldap_get_lderrno( ld, NULL, &errmsg );
  1527. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1528. "%s: Replication bind to %s on consumer failed: %d (%s)\n",
  1529. agmt_get_long_name(conn->agmt), binddn, rc, errmsg);
  1530. conn->last_ldap_error = rc; /* specific error */
  1531. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_agmt_changed - CONN_OPERATION_FAILED\n", 0, 0, 0 );
  1532. return (CONN_OPERATION_FAILED);
  1533. }
  1534. }
  1535. static int
  1536. do_simple_bind (Repl_Connection *conn, LDAP *ld, char * binddn, char *password)
  1537. {
  1538. int msgid;
  1539. LDAPDebug( LDAP_DEBUG_TRACE, "=> do_simple_bind\n", 0, 0, 0 );
  1540. if( ( msgid = ldap_simple_bind( ld, binddn, password ) ) == -1 )
  1541. {
  1542. char *ldaperrtext = NULL;
  1543. int ldaperr;
  1544. int prerr = PR_GetError();
  1545. ldaperr = ldap_get_lderrno( ld, NULL, &ldaperrtext );
  1546. /* Do not report the same error over and over again */
  1547. if (conn->last_ldap_error != ldaperr)
  1548. {
  1549. conn->last_ldap_error = ldaperr;
  1550. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1551. "%s: Simple bind failed, "
  1552. SLAPI_COMPONENT_NAME_LDAPSDK " error %d (%s), "
  1553. SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
  1554. agmt_get_long_name(conn->agmt),
  1555. ldaperr, ldaperrtext ? ldaperrtext : ldap_err2string(ldaperr),
  1556. prerr, slapd_pr_strerror(prerr));
  1557. }
  1558. }
  1559. else if (conn->last_ldap_error != LDAP_SUCCESS)
  1560. {
  1561. conn->last_ldap_error = LDAP_SUCCESS;
  1562. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1563. "%s: Simple bind resumed\n",
  1564. agmt_get_long_name(conn->agmt));
  1565. }
  1566. LDAPDebug( LDAP_DEBUG_TRACE, "<= do_simple_bind\n", 0, 0, 0 );
  1567. return msgid;
  1568. }
  1569. static time_t
  1570. PRTime2time_t (PRTime tm)
  1571. {
  1572. PRInt64 rt;
  1573. LDAPDebug( LDAP_DEBUG_TRACE, "=> PRTime2time_t\n", 0, 0, 0 );
  1574. PR_ASSERT (tm);
  1575. LL_DIV(rt, tm, PR_USEC_PER_SEC);
  1576. LDAPDebug( LDAP_DEBUG_TRACE, "<= PRTime2time_t\n", 0, 0, 0 );
  1577. return (time_t)rt;
  1578. }
  1579. static Slapi_Eq_Context
  1580. repl5_start_debug_timeout(int *setlevel)
  1581. {
  1582. Slapi_Eq_Context eqctx = 0;
  1583. LDAPDebug( LDAP_DEBUG_TRACE, "=> repl5_start_debug_timeout\n", 0, 0, 0 );
  1584. if (s_debug_timeout && s_debug_level) {
  1585. time_t now = time(NULL);
  1586. eqctx = slapi_eq_once(repl5_debug_timeout_callback, setlevel,
  1587. s_debug_timeout + now);
  1588. }
  1589. LDAPDebug( LDAP_DEBUG_TRACE, "<= repl5_start_debug_timeout\n", 0, 0, 0 );
  1590. return eqctx;
  1591. }
  1592. static void
  1593. repl5_stop_debug_timeout(Slapi_Eq_Context eqctx, int *setlevel)
  1594. {
  1595. char buf[20];
  1596. char msg[SLAPI_DSE_RETURNTEXT_SIZE];
  1597. LDAPDebug( LDAP_DEBUG_TRACE, "=> repl5_stop_debug_timeout\n", 0, 0, 0 );
  1598. if (eqctx && !*setlevel) {
  1599. int found = slapi_eq_cancel(eqctx);
  1600. }
  1601. if (s_debug_timeout && s_debug_level && *setlevel) {
  1602. /* No longer needed as we are including the one in slap.h */
  1603. sprintf(buf, "%d", 0);
  1604. config_set_errorlog_level("nsslapd-errorlog-level", buf, msg, 1);
  1605. }
  1606. LDAPDebug( LDAP_DEBUG_TRACE, "<= repl5_stop_debug_timeout\n", 0, 0, 0 );
  1607. }
  1608. static void
  1609. repl5_debug_timeout_callback(time_t when, void *arg)
  1610. {
  1611. int *setlevel = (int *)arg;
  1612. /* No longer needed as we are including the one in slap.h */
  1613. char buf[20];
  1614. char msg[SLAPI_DSE_RETURNTEXT_SIZE];
  1615. LDAPDebug( LDAP_DEBUG_TRACE, "=> repl5_debug_timeout_callback\n", 0, 0, 0 );
  1616. *setlevel = 1;
  1617. sprintf(buf, "%d", s_debug_level);
  1618. config_set_errorlog_level("nsslapd-errorlog-level", buf, msg, 1);
  1619. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1620. "repl5_debug_timeout_callback: set debug level to %d at %d\n",
  1621. s_debug_level, when);
  1622. LDAPDebug( LDAP_DEBUG_TRACE, "<= repl5_debug_timeout_callback\n", 0, 0, 0 );
  1623. }