windows_connection.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071
  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_ERR, windows_repl_plugin_name,
  316. "windows_perform_operation - %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_ERR, windows_repl_plugin_name,
  399. "windows_perform_operation - %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. "windows_perform_operation - %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_ERR, windows_repl_plugin_name,
  430. "windows_perform_operation - %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_ERR, "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_ERR, windows_repl_plugin_name,
  787. "send_dirsync_search - %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_ERR, windows_repl_plugin_name, "windows_conn_get_search_result - "
  899. "Error in windows_conn_get_search_result, rc=%d\n", rc);
  900. break;
  901. case LDAP_RES_SEARCH_RESULT:
  902. {
  903. LDAPControl **returned_controls = NULL;
  904. int code = 0;
  905. /* Purify says this is a leak : */
  906. if (LDAP_SUCCESS != (rc = ldap_parse_result( conn->ld, res, &code, NULL, NULL, NULL, &returned_controls, 0 ))) {
  907. slapi_log_error(SLAPI_LOG_ERR, windows_repl_plugin_name, "windows_conn_get_search_result - "
  908. "Error reading search result in windows_conn_get_search_result, rc=%d:%s\n",
  909. rc, ldap_err2string(rc));
  910. }
  911. if (returned_controls)
  912. {
  913. windows_private_update_dirsync_control(conn->agmt, returned_controls);
  914. ldap_controls_free(returned_controls);
  915. }
  916. if (windows_private_dirsync_has_more(conn->agmt)) {
  917. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,"windows_conn_get_search_result - "
  918. "Received hasmore from dirsync\n");
  919. }
  920. }
  921. break;
  922. case LDAP_RES_SEARCH_ENTRY:
  923. {
  924. if (( dn = ldap_get_dn( conn->ld, res )) != NULL )
  925. {
  926. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,"windows_conn_get_search_result - "
  927. "Received entry from dirsync: %s\n", dn);
  928. lm = ldap_first_entry( conn->ld, res );
  929. /*
  930. * we don't have to retrieve all the members here.
  931. * here, we have to make sure to get the entry once.
  932. */
  933. e = windows_LDAPMessage2Entry(e, conn, lm, 0, NULL);
  934. ldap_memfree(dn);
  935. }
  936. }
  937. break;
  938. } /* switch */
  939. } /* if */
  940. if (res)
  941. {
  942. ldap_msgfree( res );
  943. res = NULL;
  944. }
  945. LDAPDebug(LDAP_DEBUG_TRACE, "<= windows_conn_get_search_result\n", 0, 0, 0 );
  946. return e;
  947. }
  948. /*
  949. * Send an LDAP extended operation.
  950. */
  951. ConnResult
  952. windows_conn_send_extended_operation(Repl_Connection *conn, const char *extop_oid,
  953. struct berval *payload, char **retoidp, struct berval **retdatap,
  954. LDAPControl **server_controls, LDAPControl ***returned_controls)
  955. {
  956. LDAPDebug(LDAP_DEBUG_TRACE, "=> windows_conn_send_extended_operation\n", 0, 0, 0 );
  957. LDAPDebug(LDAP_DEBUG_TRACE, "<= windows_conn_send_extended_operation\n", 0, 0, 0 );
  958. return windows_perform_operation(conn, CONN_EXTENDED_OPERATION, NULL /* dn */, NULL /* attrs */,
  959. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  960. server_controls, extop_oid, payload, retoidp, retdatap,
  961. returned_controls);
  962. }
  963. /*
  964. * Synchronously read an entry and return a specific attribute's values.
  965. * Returns CONN_OPERATION_SUCCESS if successful. Returns
  966. * CONN_OPERATION_FAILED if the operation was sent but an LDAP error
  967. * occurred (conn->last_ldap_error is set in this case), and
  968. * CONN_NOT_CONNECTED if no connection was active.
  969. *
  970. * The caller must free the returned_bvals.
  971. */
  972. ConnResult
  973. windows_conn_read_entry_attribute(Repl_Connection *conn, const char *dn,
  974. char *type, struct berval ***returned_bvals)
  975. {
  976. ConnResult return_value;
  977. int ldap_rc;
  978. LDAPControl *server_controls[2];
  979. LDAPMessage *res = NULL;
  980. char *attrs[2];
  981. LDAPDebug(LDAP_DEBUG_TRACE, "=> windows_conn_read_entry_attribute\n", 0, 0, 0 );
  982. PR_ASSERT(NULL != type);
  983. if (windows_conn_connected(conn))
  984. {
  985. server_controls[0] = &manageDSAITControl;
  986. server_controls[1] = NULL;
  987. attrs[0] = type;
  988. attrs[1] = NULL;
  989. ldap_rc = ldap_search_ext_s(conn->ld, dn, LDAP_SCOPE_BASE,
  990. "(objectclass=*)", attrs, 0 /* attrsonly */,
  991. server_controls, NULL /* client controls */,
  992. &conn->timeout, 0 /* sizelimit */, &res);
  993. if (LDAP_SUCCESS == ldap_rc)
  994. {
  995. LDAPMessage *entry = ldap_first_entry(conn->ld, res);
  996. if (NULL != entry)
  997. {
  998. *returned_bvals = ldap_get_values_len(conn->ld, entry, type);
  999. }
  1000. return_value = CONN_OPERATION_SUCCESS;
  1001. }
  1002. else if (IS_DISCONNECT_ERROR(ldap_rc))
  1003. {
  1004. windows_conn_disconnect(conn);
  1005. return_value = CONN_NOT_CONNECTED;
  1006. }
  1007. else
  1008. {
  1009. return_value = CONN_OPERATION_FAILED;
  1010. }
  1011. conn->last_ldap_error = ldap_rc;
  1012. if (NULL != res)
  1013. {
  1014. ldap_msgfree(res);
  1015. res = NULL;
  1016. }
  1017. }
  1018. else
  1019. {
  1020. return_value = CONN_NOT_CONNECTED;
  1021. }
  1022. LDAPDebug(LDAP_DEBUG_TRACE, "<= windows_conn_read_entry_attribute\n", 0, 0, 0 );
  1023. return return_value;
  1024. }
  1025. /*
  1026. * Return an pointer to a string describing the connection's status.
  1027. */
  1028. const char *
  1029. windows_conn_get_status(Repl_Connection *conn)
  1030. {
  1031. LDAPDebug(LDAP_DEBUG_TRACE, "=> windows_conn_get_status\n", 0, 0, 0 );
  1032. LDAPDebug(LDAP_DEBUG_TRACE, "<= windows_conn_get_status\n", 0, 0, 0 );
  1033. return conn->status;
  1034. }
  1035. /*
  1036. * Cancel any outstanding linger timer. Should be called when
  1037. * a replication session is beginning.
  1038. */
  1039. void
  1040. windows_conn_cancel_linger(Repl_Connection *conn)
  1041. {
  1042. LDAPDebug(LDAP_DEBUG_TRACE, "=> windows_conn_cancel_linger\n", 0, 0, 0 );
  1043. PR_ASSERT(NULL != conn);
  1044. PR_Lock(conn->lock);
  1045. if (conn->linger_active)
  1046. {
  1047. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1048. "windows_conn_cancel_linger - %s: Cancelling linger on the connection\n",
  1049. agmt_get_long_name(conn->agmt));
  1050. conn->linger_active = PR_FALSE;
  1051. if (slapi_eq_cancel(conn->linger_event) == 1)
  1052. {
  1053. conn->refcnt--;
  1054. }
  1055. conn->linger_event = NULL;
  1056. conn->status = STATUS_CONNECTED;
  1057. }
  1058. else
  1059. {
  1060. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1061. "windows_conn_cancel_linger - %s: No linger to cancel on the connection\n",
  1062. agmt_get_long_name(conn->agmt));
  1063. }
  1064. PR_Unlock(conn->lock);
  1065. LDAPDebug(LDAP_DEBUG_TRACE, "<= windows_conn_cancel_linger\n", 0, 0, 0 );
  1066. }
  1067. /*
  1068. * Called when our linger timeout timer expires. This means
  1069. * we should check to see if perhaps the connection's become
  1070. * active again, in which case we do nothing. Otherwise,
  1071. * we close the connection.
  1072. */
  1073. static void
  1074. linger_timeout(time_t event_time, void *arg)
  1075. {
  1076. PRBool delete_now;
  1077. Repl_Connection *conn = (Repl_Connection *)arg;
  1078. LDAPDebug(LDAP_DEBUG_TRACE, "=> linger_timeout\n", 0, 0, 0 );
  1079. PR_ASSERT(NULL != conn);
  1080. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1081. "linger_timeout - %s: Linger timeout has expired on the connection\n",
  1082. agmt_get_long_name(conn->agmt));
  1083. PR_Lock(conn->lock);
  1084. if (conn->linger_active)
  1085. {
  1086. conn->linger_active = PR_FALSE;
  1087. conn->linger_event = NULL;
  1088. close_connection_internal(conn);
  1089. }
  1090. delete_now = conn->delete_after_linger;
  1091. PR_Unlock(conn->lock);
  1092. if (delete_now)
  1093. {
  1094. windows_conn_delete_internal(conn);
  1095. }
  1096. LDAPDebug(LDAP_DEBUG_TRACE, "<= linger_timeout\n", 0, 0, 0 );
  1097. }
  1098. /*
  1099. * Indicate that a session is ending. The linger timer starts when
  1100. * this function is called.
  1101. */
  1102. void
  1103. windows_conn_start_linger(Repl_Connection *conn)
  1104. {
  1105. time_t now;
  1106. LDAPDebug(LDAP_DEBUG_TRACE, "=> windows_conn_start_linger\n", 0, 0, 0 );
  1107. PR_ASSERT(NULL != conn);
  1108. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1109. "windows_conn_start_linger - %s: Beginning linger on the connection\n",
  1110. agmt_get_long_name(conn->agmt));
  1111. if (!windows_conn_connected(conn))
  1112. {
  1113. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1114. "windows_conn_start_linger - %s: No linger on the closed conn\n",
  1115. agmt_get_long_name(conn->agmt));
  1116. return;
  1117. }
  1118. time(&now);
  1119. PR_Lock(conn->lock);
  1120. if (conn->linger_active)
  1121. {
  1122. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1123. "windows_conn_start_linger - %s: Linger already active on the connection\n",
  1124. agmt_get_long_name(conn->agmt));
  1125. }
  1126. else
  1127. {
  1128. conn->linger_active = PR_TRUE;
  1129. conn->linger_event = slapi_eq_once(linger_timeout, conn, now + conn->linger_time);
  1130. conn->status = STATUS_LINGERING;
  1131. }
  1132. PR_Unlock(conn->lock);
  1133. LDAPDebug(LDAP_DEBUG_TRACE, "<= windows_conn_start_linger\n", 0, 0, 0 );
  1134. }
  1135. /*
  1136. * If no connection is currently active, opens a connection and binds to
  1137. * the remote server. If a connection is open (e.g. lingering) then
  1138. * this is a no-op.
  1139. *
  1140. * Returns CONN_OPERATION_SUCCESS on success, or CONN_OPERATION_FAILED
  1141. * on failure. Sets conn->last_ldap_error and conn->last_operation;
  1142. */
  1143. ConnResult
  1144. windows_conn_connect(Repl_Connection *conn)
  1145. {
  1146. int optdata;
  1147. int secure = 0;
  1148. char* binddn = NULL;
  1149. struct berval *creds = NULL;
  1150. ConnResult return_value = CONN_OPERATION_SUCCESS;
  1151. int pw_ret = 1;
  1152. LDAPDebug(LDAP_DEBUG_TRACE, "=> windows_conn_connect\n", 0, 0, 0 );
  1153. /** Connection already open just return SUCCESS **/
  1154. if(conn->state == STATE_CONNECTED) {
  1155. goto done;
  1156. }
  1157. PR_Lock(conn->lock);
  1158. if (conn->flag_agmt_changed) {
  1159. /* So far we cannot change Hostname and Port */
  1160. /* slapi_ch_free((void **)&conn->hostname); */
  1161. /* conn->hostname = agmt_get_hostname(conn->agmt); */
  1162. /* conn->port = agmt_get_port(conn->agmt); */
  1163. slapi_ch_free((void **)&conn->binddn);
  1164. conn->binddn = agmt_get_binddn(conn->agmt);
  1165. conn->bindmethod = agmt_get_bindmethod(conn->agmt);
  1166. conn->transport_flags = agmt_get_transport_flags(conn->agmt);
  1167. conn->timeout.tv_sec = agmt_get_timeout(conn->agmt);
  1168. conn->flag_agmt_changed = 0;
  1169. slapi_ch_free((void **)&conn->plain);
  1170. }
  1171. PR_Unlock(conn->lock);
  1172. creds = agmt_get_credentials(conn->agmt);
  1173. if (conn->plain == NULL) {
  1174. char *plain = NULL;
  1175. /* kexcoff: for reversible encryption */
  1176. /* We need to test the return code of pw_rever_decode in order to decide
  1177. * if a free for plain will be needed (pw_ret == 0) or not (pw_ret != 0) */
  1178. pw_ret = pw_rever_decode(creds->bv_val, &plain, type_nsds5ReplicaCredentials);
  1179. /* Pb occured in decryption: stop now, binding will fail */
  1180. if ( pw_ret == -1 )
  1181. {
  1182. slapi_log_error(SLAPI_LOG_ERR, windows_repl_plugin_name, "windows_conn_connect - "
  1183. "%s: Decoding of the credentials failed.\n",
  1184. agmt_get_long_name(conn->agmt));
  1185. return_value = CONN_OPERATION_FAILED;
  1186. conn->last_ldap_error = LDAP_INVALID_CREDENTIALS;
  1187. conn->state = STATE_DISCONNECTED;
  1188. goto done;
  1189. } /* Else, does not mean that the plain is correct, only means the we had no internal
  1190. decoding pb */
  1191. conn->plain = slapi_ch_strdup (plain);
  1192. if (!pw_ret) slapi_ch_free((void**)&plain);
  1193. }
  1194. /* ugaston: if SSL has been selected in the replication agreement, SSL client
  1195. * initialisation should be done before ever trying to open any connection at all.
  1196. */
  1197. if (conn->transport_flags == TRANSPORT_FLAG_TLS) {
  1198. secure = SLAPI_LDAP_INIT_FLAG_startTLS;
  1199. } else if (conn->transport_flags == TRANSPORT_FLAG_SSL) {
  1200. secure = SLAPI_LDAP_INIT_FLAG_SSL;
  1201. }
  1202. if (secure > 0) {
  1203. if (!NSS_IsInitialized()) {
  1204. slapi_log_error(SLAPI_LOG_ERR, windows_repl_plugin_name, "windows_conn_connect - "
  1205. "%s: SSL Not Initialized, Replication over SSL FAILED\n",
  1206. agmt_get_long_name(conn->agmt));
  1207. return_value = CONN_SSL_NOT_ENABLED;
  1208. conn->last_ldap_error = LDAP_INAPPROPRIATE_AUTH;
  1209. conn->last_operation = CONN_INIT;
  1210. goto done;
  1211. }
  1212. }
  1213. if (return_value == CONN_OPERATION_SUCCESS) {
  1214. #if !defined(USE_OPENLDAP)
  1215. int io_timeout_ms;
  1216. #endif
  1217. /* Now we initialize the LDAP Structure and set options */
  1218. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1219. "windows_conn_connect - %s: Trying %s%s slapi_ldap_init_ext\n",
  1220. agmt_get_long_name(conn->agmt),
  1221. secure ? "secure" : "non-secure",
  1222. (secure == SLAPI_LDAP_INIT_FLAG_startTLS) ? " startTLS" : "");
  1223. conn->ld = slapi_ldap_init_ext(NULL, conn->hostname, conn->port, secure, 0, NULL);
  1224. if (NULL == conn->ld)
  1225. {
  1226. return_value = CONN_OPERATION_FAILED;
  1227. conn->state = STATE_DISCONNECTED;
  1228. conn->last_operation = CONN_INIT;
  1229. conn->last_ldap_error = LDAP_LOCAL_ERROR;
  1230. slapi_log_error(SLAPI_LOG_ERR, windows_repl_plugin_name,
  1231. "windows_conn_connect - %s: Failed to establish %s%sconnection to the consumer\n",
  1232. agmt_get_long_name(conn->agmt),
  1233. secure ? "secure " : "",
  1234. (secure == SLAPI_LDAP_INIT_FLAG_startTLS) ? "startTLS " : "");
  1235. goto done;
  1236. }
  1237. /* slapi_ch_strdup is OK with NULL strings */
  1238. binddn = slapi_ch_strdup(conn->binddn);
  1239. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1240. "%s: binddn = %s, passwd = %s\n",
  1241. agmt_get_long_name(conn->agmt),
  1242. binddn?binddn:"NULL", creds->bv_val?creds->bv_val:"NULL");
  1243. /* Set some options for the connection. */
  1244. optdata = LDAP_DEREF_NEVER; /* Don't dereference aliases */
  1245. ldap_set_option(conn->ld, LDAP_OPT_DEREF, &optdata);
  1246. optdata = LDAP_VERSION3; /* We need LDAP version 3 */
  1247. ldap_set_option(conn->ld, LDAP_OPT_PROTOCOL_VERSION, &optdata);
  1248. /* Don't chase any referrals (although we shouldn't get any) */
  1249. ldap_set_option(conn->ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
  1250. /* override the default timeout with the specified timeout */
  1251. #if defined(USE_OPENLDAP)
  1252. ldap_set_option(conn->ld, LDAP_OPT_NETWORK_TIMEOUT, &conn->timeout);
  1253. #else
  1254. io_timeout_ms = conn->timeout.tv_sec * 1000 + conn->timeout.tv_usec / 1000;
  1255. prldap_set_session_option(conn->ld, NULL, PRLDAP_OPT_IO_MAX_TIMEOUT,
  1256. io_timeout_ms);
  1257. #endif
  1258. /* We've got an ld. Now bind to the server. */
  1259. conn->last_operation = CONN_BIND;
  1260. }
  1261. if ( bind_and_check_pwp(conn, binddn, conn->plain) == CONN_OPERATION_FAILED )
  1262. {
  1263. conn->last_ldap_error = slapi_ldap_get_lderrno (conn->ld, NULL, NULL);
  1264. conn->state = STATE_DISCONNECTED;
  1265. return_value = CONN_OPERATION_FAILED;
  1266. }
  1267. else
  1268. {
  1269. conn->last_ldap_error = LDAP_SUCCESS;
  1270. conn->state = STATE_CONNECTED;
  1271. return_value = CONN_OPERATION_SUCCESS;
  1272. }
  1273. {
  1274. ConnResult supports = 0;
  1275. supports = windows_conn_replica_supports_dirsync(conn);
  1276. if (CONN_DOES_NOT_SUPPORT_DIRSYNC == supports)
  1277. {
  1278. /* We assume that a server that doesn't support dirsync is our NT4 LDAP service */
  1279. windows_private_set_isnt4(conn->agmt,1);
  1280. LDAPDebug(LDAP_DEBUG_REPL, "windows_conn_connect - detected NT4 peer\n", 0, 0, 0 );
  1281. } else
  1282. {
  1283. windows_private_set_isnt4(conn->agmt,0);
  1284. }
  1285. supports = windows_conn_replica_is_win2k3(conn);
  1286. if (CONN_IS_WIN2K3 == supports)
  1287. {
  1288. windows_private_set_iswin2k3(conn->agmt,1);
  1289. LDAPDebug(LDAP_DEBUG_REPL, "windows_conn_connect - detected Win2k3 or later peer\n", 0, 0, 0 );
  1290. } else
  1291. {
  1292. windows_private_set_iswin2k3(conn->agmt,0);
  1293. }
  1294. }
  1295. slapi_ch_free((void**)&binddn);
  1296. if(return_value == CONN_OPERATION_FAILED)
  1297. {
  1298. close_connection_internal(conn);
  1299. } else
  1300. {
  1301. conn->last_ldap_error = LDAP_SUCCESS;
  1302. conn->state = STATE_CONNECTED;
  1303. }
  1304. done:
  1305. if (creds) {
  1306. ber_bvfree(creds);
  1307. creds = NULL;
  1308. }
  1309. LDAPDebug(LDAP_DEBUG_TRACE, "<= windows_conn_connect\n", 0, 0, 0 );
  1310. return return_value;
  1311. }
  1312. static void
  1313. close_connection_internal(Repl_Connection *conn)
  1314. {
  1315. LDAPDebug(LDAP_DEBUG_TRACE, "=> close_connection_internal\n", 0, 0, 0 );
  1316. if (NULL != conn->ld)
  1317. {
  1318. /* Since we call slapi_ldap_init,
  1319. we must call slapi_ldap_unbind */
  1320. slapi_ldap_unbind(conn->ld);
  1321. }
  1322. conn->ld = NULL;
  1323. conn->state = STATE_DISCONNECTED;
  1324. conn->status = STATUS_DISCONNECTED;
  1325. conn->supports_ds50_repl = -1;
  1326. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1327. "close_connection_internal - %s: Disconnected from the consumer\n",
  1328. agmt_get_long_name(conn->agmt));
  1329. LDAPDebug(LDAP_DEBUG_TRACE, "<= close_connection_internal\n", 0, 0, 0 );
  1330. }
  1331. void
  1332. windows_conn_disconnect(Repl_Connection *conn)
  1333. {
  1334. LDAPDebug(LDAP_DEBUG_TRACE, "=> windows_conn_disconnect\n", 0, 0, 0 );
  1335. PR_ASSERT(NULL != conn);
  1336. PR_Lock(conn->lock);
  1337. close_connection_internal(conn);
  1338. PR_Unlock(conn->lock);
  1339. LDAPDebug(LDAP_DEBUG_TRACE, "<= windows_conn_disconnect\n", 0, 0, 0 );
  1340. }
  1341. /*
  1342. * Determine if the remote replica supports DS 5.0 replication.
  1343. * Return codes:
  1344. * CONN_SUPPORTS_DS5_REPL - the remote replica suport DS5 replication
  1345. * CONN_DOES_NOT_SUPPORT_DS5_REPL - the remote replica does not
  1346. * support DS5 replication.
  1347. * CONN_OPERATION_FAILED - it could not be determined if the remote
  1348. * replica supports DS5 replication.
  1349. * CONN_NOT_CONNECTED - no connection was active.
  1350. */
  1351. ConnResult
  1352. windows_conn_replica_supports_ds5_repl(Repl_Connection *conn)
  1353. {
  1354. ConnResult return_value;
  1355. int ldap_rc;
  1356. LDAPDebug(LDAP_DEBUG_TRACE, "=> windows_conn_replica_supports_ds5_repl\n", 0, 0, 0 );
  1357. if (windows_conn_connected(conn))
  1358. {
  1359. if (conn->supports_ds50_repl == -1) {
  1360. LDAPMessage *res = NULL;
  1361. LDAPMessage *entry = NULL;
  1362. char *attrs[] = {"supportedcontrol", "supportedextension", NULL};
  1363. conn->status = STATUS_SEARCHING;
  1364. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1365. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1366. NULL /* server controls */, NULL /* client controls */,
  1367. &conn->timeout, LDAP_NO_LIMIT, &res);
  1368. if (LDAP_SUCCESS == ldap_rc)
  1369. {
  1370. conn->supports_ds50_repl = 0;
  1371. entry = ldap_first_entry(conn->ld, res);
  1372. if (!attribute_string_value_present(conn->ld, entry, "supportedcontrol", REPL_NSDS50_UPDATE_INFO_CONTROL_OID))
  1373. {
  1374. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1375. }
  1376. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_START_NSDS50_REPLICATION_REQUEST_OID))
  1377. {
  1378. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1379. }
  1380. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_END_NSDS50_REPLICATION_REQUEST_OID))
  1381. {
  1382. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1383. }
  1384. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_ENTRY_REQUEST_OID))
  1385. {
  1386. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1387. }
  1388. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_RESPONSE_OID))
  1389. {
  1390. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1391. }
  1392. else
  1393. {
  1394. conn->supports_ds50_repl = 1;
  1395. return_value = CONN_SUPPORTS_DS5_REPL;
  1396. }
  1397. }
  1398. else
  1399. {
  1400. if (IS_DISCONNECT_ERROR(ldap_rc))
  1401. {
  1402. conn->last_ldap_error = ldap_rc; /* specific reason */
  1403. windows_conn_disconnect(conn);
  1404. return_value = CONN_NOT_CONNECTED;
  1405. }
  1406. else
  1407. {
  1408. return_value = CONN_OPERATION_FAILED;
  1409. }
  1410. }
  1411. if (NULL != res)
  1412. ldap_msgfree(res);
  1413. }
  1414. else {
  1415. return_value = conn->supports_ds50_repl ? CONN_SUPPORTS_DS5_REPL : CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1416. }
  1417. }
  1418. else
  1419. {
  1420. /* Not connected */
  1421. return_value = CONN_NOT_CONNECTED;
  1422. }
  1423. LDAPDebug(LDAP_DEBUG_TRACE, "<= windows_conn_replica_supports_ds5_repl\n", 0, 0, 0 );
  1424. return return_value;
  1425. }
  1426. ConnResult
  1427. windows_conn_replica_supports_dirsync(Repl_Connection *conn)
  1428. {
  1429. ConnResult return_value;
  1430. int ldap_rc;
  1431. LDAPDebug(LDAP_DEBUG_TRACE, "=> windows_conn_replica_supports_dirsync\n", 0, 0, 0 );
  1432. if (getenv("WINSYNC_USE_DS")) {
  1433. /* used to fake out dirsync to think it's talking to a real ad when in fact
  1434. it's just talking to another directory server */
  1435. conn->supports_dirsync = 1;
  1436. return CONN_SUPPORTS_DIRSYNC;
  1437. }
  1438. if (windows_conn_connected(conn))
  1439. {
  1440. if (conn->supports_dirsync == -1) {
  1441. LDAPMessage *res = NULL;
  1442. LDAPMessage *entry = NULL;
  1443. char *attrs[] = {"supportedcontrol", NULL};
  1444. conn->status = STATUS_SEARCHING;
  1445. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1446. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1447. NULL /* server controls */, NULL /* client controls */,
  1448. &conn->timeout, LDAP_NO_LIMIT, &res);
  1449. if (LDAP_SUCCESS == ldap_rc)
  1450. {
  1451. conn->supports_dirsync = 0;
  1452. entry = ldap_first_entry(conn->ld, res);
  1453. if (!attribute_string_value_present(conn->ld, entry, "supportedcontrol", REPL_DIRSYNC_CONTROL_OID))
  1454. {
  1455. return_value = CONN_DOES_NOT_SUPPORT_DIRSYNC;
  1456. }
  1457. else
  1458. {
  1459. conn->supports_dirsync =1;
  1460. return_value = CONN_SUPPORTS_DIRSYNC;
  1461. }
  1462. }
  1463. else
  1464. {
  1465. if (IS_DISCONNECT_ERROR(ldap_rc))
  1466. {
  1467. conn->last_ldap_error = ldap_rc; /* specific reason */
  1468. windows_conn_disconnect(conn);
  1469. return_value = CONN_NOT_CONNECTED;
  1470. }
  1471. else
  1472. {
  1473. return_value = CONN_OPERATION_FAILED;
  1474. }
  1475. }
  1476. if (NULL != res)
  1477. ldap_msgfree(res);
  1478. }
  1479. else {
  1480. return_value = conn->supports_dirsync ? CONN_SUPPORTS_DIRSYNC : CONN_DOES_NOT_SUPPORT_DIRSYNC;
  1481. }
  1482. }
  1483. else
  1484. {
  1485. /* Not connected */
  1486. return_value = CONN_NOT_CONNECTED;
  1487. }
  1488. LDAPDebug(LDAP_DEBUG_TRACE, "<= windows_conn_replica_supports_dirsync\n", 0, 0, 0 );
  1489. return return_value;
  1490. }
  1491. /* Checks if the AD server is running win2k3 (or later) */
  1492. ConnResult
  1493. windows_conn_replica_is_win2k3(Repl_Connection *conn)
  1494. {
  1495. ConnResult return_value;
  1496. int ldap_rc;
  1497. LDAPDebug(LDAP_DEBUG_TRACE, "=> windows_conn_replica_is_win2k3\n", 0, 0, 0 );
  1498. if (windows_conn_connected(conn))
  1499. {
  1500. if (conn->is_win2k3 == -1) {
  1501. LDAPMessage *res = NULL;
  1502. LDAPMessage *entry = NULL;
  1503. char *attrs[] = {"supportedCapabilities", NULL};
  1504. conn->status = STATUS_SEARCHING;
  1505. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1506. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1507. NULL /* server controls */, NULL /* client controls */,
  1508. &conn->timeout, LDAP_NO_LIMIT, &res);
  1509. if (LDAP_SUCCESS == ldap_rc)
  1510. {
  1511. conn->is_win2k3 = 0;
  1512. entry = ldap_first_entry(conn->ld, res);
  1513. if (!attribute_string_value_present(conn->ld, entry, "supportedCapabilities", REPL_WIN2K3_AD_OID))
  1514. {
  1515. return_value = CONN_NOT_WIN2K3;
  1516. }
  1517. else
  1518. {
  1519. conn->is_win2k3 =1;
  1520. return_value = CONN_IS_WIN2K3;
  1521. }
  1522. }
  1523. else
  1524. {
  1525. if (IS_DISCONNECT_ERROR(ldap_rc))
  1526. {
  1527. conn->last_ldap_error = ldap_rc; /* specific reason */
  1528. windows_conn_disconnect(conn);
  1529. return_value = CONN_NOT_CONNECTED;
  1530. }
  1531. else
  1532. {
  1533. return_value = CONN_OPERATION_FAILED;
  1534. }
  1535. }
  1536. if (NULL != res)
  1537. ldap_msgfree(res);
  1538. }
  1539. else {
  1540. return_value = conn->is_win2k3 ? CONN_IS_WIN2K3 : CONN_NOT_WIN2K3;
  1541. }
  1542. }
  1543. else
  1544. {
  1545. /* Not connected */
  1546. return_value = CONN_NOT_CONNECTED;
  1547. }
  1548. LDAPDebug(LDAP_DEBUG_TRACE, "<= windows_conn_replica_is_win2k3\n", 0, 0, 0 );
  1549. return return_value;
  1550. }
  1551. /*
  1552. * Return 1 if "value" is a value of attribute type "type" in entry "entry".
  1553. * Otherwise, return 0.
  1554. */
  1555. static int
  1556. attribute_string_value_present(LDAP *ld, LDAPMessage *entry, const char *type,
  1557. const char *value)
  1558. {
  1559. int return_value = 0;
  1560. ber_len_t vallen;
  1561. LDAPDebug(LDAP_DEBUG_TRACE, "=> attribute_string_value_present\n", 0, 0, 0 );
  1562. if (NULL != entry)
  1563. {
  1564. char *atype = NULL;
  1565. BerElement *ber = NULL;
  1566. vallen = strlen(value);
  1567. atype = ldap_first_attribute(ld, entry, &ber);
  1568. while (NULL != atype && 0 == return_value)
  1569. {
  1570. if (strcasecmp(atype, type) == 0)
  1571. {
  1572. struct berval **vals = ldap_get_values_len(ld, entry, atype);
  1573. int i;
  1574. for (i = 0; return_value == 0 && NULL != vals && NULL != vals[i]; i++)
  1575. {
  1576. if ((vallen == vals[i]->bv_len) && !strncmp(vals[i]->bv_val, value, vallen))
  1577. {
  1578. return_value = 1;
  1579. }
  1580. }
  1581. if (NULL != vals)
  1582. {
  1583. ldap_value_free_len(vals);
  1584. }
  1585. }
  1586. ldap_memfree(atype);
  1587. atype = ldap_next_attribute(ld, entry, ber);
  1588. }
  1589. if (NULL != ber)
  1590. ber_free(ber, 0);
  1591. /* The last atype has not been freed yet */
  1592. if (NULL != atype)
  1593. ldap_memfree(atype);
  1594. }
  1595. LDAPDebug(LDAP_DEBUG_TRACE, "<= attribute_string_value_present\n", 0, 0, 0 );
  1596. return return_value;
  1597. }
  1598. /*
  1599. * Read the remote server's schema entry, then read the local schema entry,
  1600. * and compare the nsschemacsn attribute. If the local csn is newer, or
  1601. * the remote csn is absent, push the schema down to the consumer.
  1602. * Return codes:
  1603. * CONN_SCHEMA_UPDATED if the schema was pushed successfully
  1604. * CONN_SCHEMA_NO_UPDATE_NEEDED if the schema was as new or newer than
  1605. * the local server's schema
  1606. * CONN_OPERATION_FAILED if an error occurred
  1607. * CONN_NOT_CONNECTED if no connection was active
  1608. * NOTE: Should only be called when a replication session has been
  1609. * established by sending a startReplication extended operation.
  1610. */
  1611. void
  1612. windows_conn_set_timeout(Repl_Connection *conn, long timeout)
  1613. {
  1614. LDAPDebug(LDAP_DEBUG_TRACE, "=> windows_conn_set_timeout\n", 0, 0, 0 );
  1615. PR_ASSERT(NULL != conn);
  1616. PR_ASSERT(timeout >= 0);
  1617. PR_Lock(conn->lock);
  1618. conn->timeout.tv_sec = timeout;
  1619. PR_Unlock(conn->lock);
  1620. LDAPDebug(LDAP_DEBUG_TRACE, "<= windows_conn_set_timeout\n", 0, 0, 0 );
  1621. }
  1622. void windows_conn_set_agmt_changed(Repl_Connection *conn)
  1623. {
  1624. LDAPDebug(LDAP_DEBUG_TRACE, "=> windows_conn_set_agmt_changed\n", 0, 0, 0 );
  1625. PR_ASSERT(NULL != conn);
  1626. PR_Lock(conn->lock);
  1627. if (NULL != conn->agmt)
  1628. conn->flag_agmt_changed = 1;
  1629. PR_Unlock(conn->lock);
  1630. LDAPDebug(LDAP_DEBUG_TRACE, "<= windows_conn_set_agmt_changed\n", 0, 0, 0 );
  1631. }
  1632. static const char *
  1633. bind_method_to_mech(int bindmethod)
  1634. {
  1635. switch (bindmethod) {
  1636. case BINDMETHOD_SSL_CLIENTAUTH:
  1637. return LDAP_SASL_EXTERNAL;
  1638. break;
  1639. case BINDMETHOD_SASL_GSSAPI:
  1640. return "GSSAPI";
  1641. break;
  1642. case BINDMETHOD_SASL_DIGEST_MD5:
  1643. return "DIGEST-MD5";
  1644. break;
  1645. default: /* anything else */
  1646. return LDAP_SASL_SIMPLE;
  1647. }
  1648. return LDAP_SASL_SIMPLE;
  1649. }
  1650. /*
  1651. * Check the result of an ldap_simple_bind operation to see we it
  1652. * contains the expiration controls
  1653. * return: -1 error, not bound
  1654. * 0, OK bind has succeeded
  1655. */
  1656. static int
  1657. bind_and_check_pwp(Repl_Connection *conn, char * binddn, char *password)
  1658. {
  1659. LDAPControl **ctrls = NULL;
  1660. LDAP *ld = conn->ld;
  1661. int rc;
  1662. const char *mech = bind_method_to_mech(conn->bindmethod);
  1663. LDAPDebug(LDAP_DEBUG_TRACE, "=> bind_and_check_pwp\n", 0, 0, 0 );
  1664. rc = slapi_ldap_bind(conn->ld, binddn, password, mech, NULL,
  1665. &ctrls, NULL, NULL);
  1666. if ( rc == LDAP_SUCCESS )
  1667. {
  1668. if (conn->last_ldap_error != rc)
  1669. {
  1670. conn->last_ldap_error = rc;
  1671. slapi_log_error(SLAPI_LOG_ERR, windows_repl_plugin_name,
  1672. "bind_and_check_pwp - %s: Replication bind with %s auth resumed\n",
  1673. agmt_get_long_name(conn->agmt),
  1674. mech ? mech : "SIMPLE");
  1675. }
  1676. if ( ctrls )
  1677. {
  1678. int i;
  1679. for( i = 0; ctrls[ i ] != NULL; ++i )
  1680. {
  1681. if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRED)) )
  1682. {
  1683. /* Bind is successfull but password has expired */
  1684. slapi_log_error(SLAPI_LOG_ERR, windows_repl_plugin_name,
  1685. "bind_and_check_pwp - %s: Successfully bound %s to consumer, "
  1686. "but password has expired on consumer.\n",
  1687. agmt_get_long_name(conn->agmt), binddn);
  1688. }
  1689. else if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRING)) )
  1690. {
  1691. /* The password is expiring in n seconds */
  1692. if ( (ctrls[ i ]->ldctl_value.bv_val != NULL) &&
  1693. (ctrls[ i ]->ldctl_value.bv_len > 0) )
  1694. {
  1695. int password_expiring = atoi( ctrls[ i ]->ldctl_value.bv_val );
  1696. slapi_log_error(SLAPI_LOG_ERR, windows_repl_plugin_name,
  1697. "bind_and_check_pwp - %s: Successfully bound %s to consumer, "
  1698. "but password is expiring on consumer in %d seconds.\n",
  1699. agmt_get_long_name(conn->agmt), binddn, password_expiring);
  1700. }
  1701. }
  1702. }
  1703. ldap_controls_free( ctrls );
  1704. }
  1705. LDAPDebug(LDAP_DEBUG_TRACE, "<= bind_and_check_pwp - CONN_OPERATION_SUCCESS\n", 0, 0, 0 );
  1706. return (CONN_OPERATION_SUCCESS);
  1707. }
  1708. else
  1709. {
  1710. ldap_controls_free( ctrls );
  1711. /* Do not report the same error over and over again
  1712. * unless replication level logging is enabled. */
  1713. if (conn->last_ldap_error != rc)
  1714. {
  1715. char *errmsg = NULL;
  1716. conn->last_ldap_error = rc;
  1717. /* errmsg is a pointer directly into the ld structure - do not free */
  1718. rc = slapi_ldap_get_lderrno( ld, NULL, &errmsg );
  1719. slapi_log_error(SLAPI_LOG_ERR, windows_repl_plugin_name,
  1720. "bind_and_check_pwp - %s: Replication bind with %s auth failed: LDAP error %d (%s) (%s)\n",
  1721. agmt_get_long_name(conn->agmt),
  1722. mech ? mech : "SIMPLE", rc,
  1723. ldap_err2string(rc), errmsg);
  1724. } else {
  1725. char *errmsg = NULL;
  1726. /* errmsg is a pointer directly into the ld structure - do not free */
  1727. rc = slapi_ldap_get_lderrno( ld, NULL, &errmsg );
  1728. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,
  1729. "bind_and_check_pwp - %s: Replication bind with %s auth failed: LDAP error %d (%s) (%s)\n",
  1730. agmt_get_long_name(conn->agmt),
  1731. mech ? mech : "SIMPLE", rc,
  1732. ldap_err2string(rc), errmsg);
  1733. }
  1734. LDAPDebug(LDAP_DEBUG_TRACE, "<= bind_and_check_pwp - CONN_OPERATION_FAILED\n", 0, 0, 0 );
  1735. return (CONN_OPERATION_FAILED);
  1736. }
  1737. }
  1738. /* Attempt to bind as a user to AD in order to see if we posess the
  1739. * most current password. Returns 0 if the bind was successful,
  1740. * non-zero otherwise. */
  1741. int
  1742. windows_check_user_password(Repl_Connection *conn, Slapi_DN *sdn, char *password)
  1743. {
  1744. const char *binddn = NULL;
  1745. LDAPMessage *res = NULL;
  1746. int rc = 0;
  1747. int msgid = 0;
  1748. int parse_rc = 0;
  1749. /* If we're already connected, this will just return success */
  1750. windows_conn_connect(conn);
  1751. /* Get binddn from sdn */
  1752. binddn = slapi_sdn_get_dn(sdn);
  1753. /* Attempt to do a bind on the existing connection
  1754. * using the dn and password that were passed in. */
  1755. msgid = do_simple_bind(conn, conn->ld, (char *) binddn, password);
  1756. rc = ldap_result(conn->ld, msgid, LDAP_MSG_ALL, NULL, &res);
  1757. if (0 > rc) { /* error */
  1758. rc = slapi_ldap_get_lderrno(conn->ld, NULL, NULL);
  1759. slapi_log_error(SLAPI_LOG_ERR, windows_repl_plugin_name,
  1760. "windows_check_user_password - Error reading bind response for id "
  1761. "[%s]: error %d (%s)\n",
  1762. binddn ? binddn : "(anon)",
  1763. rc, ldap_err2string(rc));
  1764. } else if (rc == 0) { /* timeout */
  1765. slapi_log_error(SLAPI_LOG_ERR, windows_repl_plugin_name,
  1766. "windows_check_user_password - Error: timeout reading "
  1767. "bind response for [%s]\n",
  1768. binddn ? binddn : "(anon)");
  1769. rc = -1;
  1770. } else {
  1771. parse_rc = ldap_parse_result( conn->ld, res, &rc, NULL, NULL, NULL, NULL, 1 /* Free res */);
  1772. if (parse_rc != LDAP_SUCCESS) {
  1773. slapi_log_error(SLAPI_LOG_ERR, windows_repl_plugin_name,
  1774. "windows_check_user_password - Error: unable to parse "
  1775. "bind result for [%s]: "
  1776. "error %d\n",
  1777. binddn ? binddn : "(anon)",
  1778. parse_rc);
  1779. rc = -1;
  1780. }
  1781. }
  1782. /* rebind as the DN specified in the sync agreement */
  1783. bind_and_check_pwp(conn, conn->binddn, conn->plain);
  1784. return rc;
  1785. }
  1786. static int
  1787. do_simple_bind (Repl_Connection *conn, LDAP *ld, char * binddn, char *password)
  1788. {
  1789. int msgid;
  1790. LDAPDebug(LDAP_DEBUG_TRACE, "=> do_simple_bind\n", 0, 0, 0 );
  1791. if( LDAP_SUCCESS != slapi_ldap_bind( ld, binddn, password, LDAP_SASL_SIMPLE, NULL, NULL, NULL, &msgid ) )
  1792. {
  1793. char *ldaperrtext = NULL;
  1794. int ldaperr;
  1795. int prerr = PR_GetError();
  1796. ldaperr = slapi_ldap_get_lderrno( ld, NULL, &ldaperrtext );
  1797. /* Do not report the same error over and over again */
  1798. if (conn->last_ldap_error != ldaperr)
  1799. {
  1800. conn->last_ldap_error = ldaperr;
  1801. slapi_log_error(SLAPI_LOG_ERR, windows_repl_plugin_name,
  1802. "do_simple_bind - %s: Simple bind failed, "
  1803. SLAPI_COMPONENT_NAME_LDAPSDK " error %d (%s) (%s), "
  1804. SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
  1805. agmt_get_long_name(conn->agmt),
  1806. ldaperr, ldap_err2string(ldaperr),
  1807. ldaperrtext ? ldaperrtext : "",
  1808. prerr, slapd_pr_strerror(prerr));
  1809. }
  1810. }
  1811. else if (conn->last_ldap_error != LDAP_SUCCESS)
  1812. {
  1813. conn->last_ldap_error = LDAP_SUCCESS;
  1814. slapi_log_error(SLAPI_LOG_INFO, windows_repl_plugin_name,
  1815. "do_simple_bind - %s: Simple bind resumed\n",
  1816. agmt_get_long_name(conn->agmt));
  1817. }
  1818. LDAPDebug(LDAP_DEBUG_TRACE, "<= do_simple_bind\n", 0, 0, 0 );
  1819. return msgid;
  1820. }
  1821. static Slapi_Eq_Context
  1822. repl5_start_debug_timeout(int *setlevel)
  1823. {
  1824. Slapi_Eq_Context eqctx = 0;
  1825. LDAPDebug(LDAP_DEBUG_TRACE, "=> repl5_start_debug_timeout\n", 0, 0, 0 );
  1826. if (s_debug_timeout && s_debug_level) {
  1827. time_t now = time(NULL);
  1828. eqctx = slapi_eq_once(repl5_debug_timeout_callback, setlevel,
  1829. s_debug_timeout + now);
  1830. }
  1831. LDAPDebug(LDAP_DEBUG_TRACE, "<= repl5_start_debug_timeout\n", 0, 0, 0 );
  1832. return eqctx;
  1833. }
  1834. static void
  1835. repl5_stop_debug_timeout(Slapi_Eq_Context eqctx, int *setlevel)
  1836. {
  1837. char buf[20];
  1838. LDAPDebug(LDAP_DEBUG_TRACE, "=> repl5_stop_debug_timeout\n", 0, 0, 0 );
  1839. if (eqctx && !*setlevel) {
  1840. (void)slapi_eq_cancel(eqctx);
  1841. }
  1842. if (s_debug_timeout && s_debug_level && *setlevel) {
  1843. /* No longer needed as we are including the one in slap.h */
  1844. sprintf(buf, "%d", 0);
  1845. config_set_errorlog_level("nsslapd-errorlog-level", buf, NULL, 1);
  1846. }
  1847. LDAPDebug(LDAP_DEBUG_TRACE, "<= repl5_stop_debug_timeout\n", 0, 0, 0 );
  1848. }
  1849. static void
  1850. repl5_debug_timeout_callback(time_t when, void *arg)
  1851. {
  1852. int *setlevel = (int *)arg;
  1853. /* No longer needed as we are including the one in slap.h */
  1854. char buf[20];
  1855. LDAPDebug(LDAP_DEBUG_TRACE, "=> repl5_debug_timeout_callback\n", 0, 0, 0 );
  1856. *setlevel = 1;
  1857. sprintf(buf, "%d", s_debug_level);
  1858. config_set_errorlog_level("nsslapd-errorlog-level", buf, NULL, 1);
  1859. slapi_log_error(SLAPI_LOG_INFO, windows_repl_plugin_name,
  1860. "repl5_debug_timeout_callback: Set debug level to %d at %ld\n",
  1861. s_debug_level, when);
  1862. LDAPDebug(LDAP_DEBUG_TRACE, "<= repl5_debug_timeout_callback\n", 0, 0, 0 );
  1863. }