windows_connection.c 59 KB

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