windows_connection.c 65 KB

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