windows_connection.c 58 KB

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