1
0

windows_connection.c 62 KB

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