windows_connection.c 59 KB

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