windows_connection.c 75 KB

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