1
0

windows_connection.c 60 KB

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