windows_connection.c 58 KB

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