1
0

windows_connection.c 63 KB

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