windows_connection.c 59 KB

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