windows_connection.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984
  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, LDAP_SCOPE_SUBTREE);
  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, int scope)
  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. char *filter_copy = slapi_ch_strdup(filter);
  589. char **attrs = NULL;
  590. LDAPControl **serverctrls_copy = NULL;
  591. slapi_add_controls(&serverctrls_copy, serverctrls, 1 /* make a copy we can free */);
  592. LDAPDebug( LDAP_DEBUG_REPL, "Calling windows entry search request plugin\n", 0, 0, 0 );
  593. winsync_plugin_call_pre_ad_search_cb(conn->agmt, NULL, &searchbase_copy, &scope, &filter_copy,
  594. &attrs, &serverctrls_copy);
  595. ldap_rc = ldap_search_ext_s(conn->ld, searchbase_copy, scope,
  596. filter_copy, attrs, 0 /* attrsonly */,
  597. serverctrls_copy , NULL /* client controls */,
  598. &conn->timeout, 0 /* sizelimit */, &res);
  599. if ((LDAP_SUCCESS != ldap_rc) && !IS_DISCONNECT_ERROR(ldap_rc)) {
  600. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  601. "Could not retrieve entry from Windows using search "
  602. "base [%s] scope [%d] filter [%s]: error %d:%s\n",
  603. searchbase_copy, scope, filter_copy, ldap_rc,
  604. ldap_err2string(ldap_rc));
  605. }
  606. slapi_ch_free_string(&searchbase_copy);
  607. slapi_ch_free_string(&filter_copy);
  608. slapi_ch_array_free(attrs);
  609. attrs = NULL;
  610. ldap_controls_free(serverctrls_copy);
  611. serverctrls_copy = NULL;
  612. /* clear it here in case the search fails and
  613. we are left with a bogus old entry */
  614. windows_private_set_raw_entry(conn->agmt, NULL);
  615. if (LDAP_SUCCESS == ldap_rc)
  616. {
  617. LDAPMessage *message = ldap_first_entry(conn->ld, res);
  618. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  619. int nummessages = 0;
  620. int numentries = 0;
  621. int numreferences = 0;
  622. nummessages = ldap_count_messages(conn->ld, res);
  623. numentries = ldap_count_entries(conn->ld, res);
  624. numreferences = ldap_count_references(conn->ld, res);
  625. LDAPDebug( LDAP_DEBUG_REPL, "windows_search_entry: received %d messages, %d entries, %d references\n",
  626. nummessages, numentries, numreferences );
  627. }
  628. if (NULL != entry)
  629. {
  630. *entry = windows_LDAPMessage2Entry(conn,message,0);
  631. }
  632. /* See if there are any more entries : if so then that's an error
  633. * but we still need to get them to avoid gumming up the connection
  634. */
  635. /* have to check message first - cannot pass a NULL message */
  636. while (message && (NULL != ( message = ldap_next_entry(conn->ld,message)))) ;
  637. return_value = CONN_OPERATION_SUCCESS;
  638. }
  639. else if (IS_DISCONNECT_ERROR(ldap_rc))
  640. {
  641. windows_conn_disconnect(conn);
  642. return_value = CONN_NOT_CONNECTED;
  643. }
  644. else
  645. {
  646. return_value = CONN_OPERATION_FAILED;
  647. }
  648. conn->last_ldap_error = ldap_rc;
  649. if (NULL != res)
  650. {
  651. ldap_msgfree(res);
  652. res = NULL;
  653. }
  654. }
  655. else
  656. {
  657. return_value = CONN_NOT_CONNECTED;
  658. }
  659. done:
  660. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_search_entry\n", 0, 0, 0 );
  661. return return_value;
  662. }
  663. ConnResult
  664. send_dirsync_search(Repl_Connection *conn)
  665. {
  666. ConnResult return_value;
  667. LDAPDebug( LDAP_DEBUG_TRACE, "=> send_dirsync_search\n", 0, 0, 0 );
  668. if (windows_conn_connected(conn))
  669. {
  670. const char *op_string = NULL;
  671. int rc;
  672. int scope = LDAP_SCOPE_SUBTREE;
  673. char *filter = slapi_ch_strdup("(objectclass=*)");
  674. char **attrs = NULL;
  675. LDAPControl **server_controls = NULL;
  676. int msgid;
  677. /* need to strip the dn down to dc= */
  678. const char *old_dn = slapi_sdn_get_ndn( windows_private_get_windows_subtree(conn->agmt) );
  679. char *dn = slapi_ch_strdup(strstr(old_dn, "dc="));
  680. if (conn->supports_dirsync == 0)
  681. {
  682. /* unsupported */
  683. } else
  684. {
  685. slapi_add_control_ext(&server_controls,
  686. windows_private_dirsync_control(conn->agmt),
  687. 0 /* no copy - passin */);
  688. }
  689. conn->last_operation = CONN_SEARCH;
  690. conn->status = STATUS_SEARCHING;
  691. op_string = "search";
  692. LDAPDebug( LDAP_DEBUG_REPL, "Calling dirsync search request plugin\n", 0, 0, 0 );
  693. winsync_plugin_call_dirsync_search_params_cb(conn->agmt, old_dn, &dn, &scope, &filter,
  694. &attrs, &server_controls);
  695. LDAPDebug( LDAP_DEBUG_REPL, "Sending dirsync search request\n", 0, 0, 0 );
  696. rc = ldap_search_ext( conn->ld, dn, scope, filter, attrs, PR_FALSE, server_controls,
  697. NULL /* ClientControls */, 0,0, &msgid);
  698. if (LDAP_SUCCESS == rc)
  699. {
  700. return_value = 0;
  701. }
  702. else
  703. {
  704. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  705. "%s: Failed to get %s operation: LDAP error %d (%s)\n",
  706. agmt_get_long_name(conn->agmt),
  707. op_string, rc, ldap_err2string(rc));
  708. conn->last_ldap_error = rc;
  709. if (IS_DISCONNECT_ERROR(rc))
  710. {
  711. windows_conn_disconnect(conn);
  712. return_value = CONN_NOT_CONNECTED;
  713. }
  714. else
  715. {
  716. conn->status = STATUS_CONNECTED;
  717. return_value = CONN_OPERATION_FAILED;
  718. }
  719. }
  720. /* cleanup */
  721. slapi_ch_free_string(&dn);
  722. slapi_ch_free_string(&filter);
  723. slapi_ch_array_free(attrs);
  724. attrs = NULL;
  725. ldap_controls_free(server_controls);
  726. server_controls = NULL;
  727. }
  728. else
  729. {
  730. /* conn->last_ldap_error has been set to a more specific value
  731. * in windows_conn_connected()
  732. * conn->last_ldap_error = LDAP_SERVER_DOWN;
  733. */
  734. return_value = CONN_NOT_CONNECTED;
  735. }
  736. LDAPDebug( LDAP_DEBUG_TRACE, "<= send_dirsync_search\n", 0, 0, 0 );
  737. return return_value;
  738. }
  739. /*
  740. * Send an LDAP add operation.
  741. */
  742. ConnResult
  743. windows_conn_send_add(Repl_Connection *conn, const char *dn, LDAPMod **attrs,
  744. LDAPControl **server_controls, LDAPControl ***returned_controls)
  745. {
  746. ConnResult res = 0;
  747. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_send_add\n", 0, 0, 0 );
  748. res = windows_perform_operation(conn, CONN_ADD, dn, attrs, NULL /* newrdn */,
  749. NULL /* newparent */, 0 /* deleteoldrdn */, server_controls,
  750. NULL /* extop OID */, NULL /* extop payload */, NULL /* retoidp */,
  751. NULL /* retdatap */, returned_controls);
  752. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_send_add\n", 0, 0, 0 );
  753. return res;
  754. }
  755. /*
  756. * Send an LDAP delete operation.
  757. */
  758. ConnResult
  759. windows_conn_send_delete(Repl_Connection *conn, const char *dn,
  760. LDAPControl **server_controls, LDAPControl ***returned_controls)
  761. {
  762. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_send_delete\n", 0, 0, 0 );
  763. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_send_delete\n", 0, 0, 0 );
  764. return windows_perform_operation(conn, CONN_DELETE, dn, NULL /* attrs */,
  765. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  766. server_controls, NULL /* extop OID */, NULL /* extop payload */,
  767. NULL /* retoidp */, NULL /* retdatap */, returned_controls);
  768. }
  769. /*
  770. * Send an LDAP modify operation.
  771. */
  772. ConnResult
  773. windows_conn_send_modify(Repl_Connection *conn, const char *dn, LDAPMod **mods,
  774. LDAPControl **server_controls, LDAPControl ***returned_controls)
  775. {
  776. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_send_modify\n", 0, 0, 0 );
  777. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_send_modify\n", 0, 0, 0 );
  778. return windows_perform_operation(conn, CONN_MODIFY, dn, mods, NULL /* newrdn */,
  779. NULL /* newparent */, 0 /* deleteoldrdn */, server_controls,
  780. NULL /* extop OID */, NULL /* extop payload */, NULL /* retoidp */,
  781. NULL /* retdatap */, returned_controls);
  782. }
  783. /*
  784. * Send an LDAP moddn operation.
  785. */
  786. ConnResult
  787. windows_conn_send_rename(Repl_Connection *conn, const char *dn,
  788. const char *newrdn, const char *newparent, int deleteoldrdn,
  789. LDAPControl **server_controls, LDAPControl ***returned_controls)
  790. {
  791. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_send_rename\n", 0, 0, 0 );
  792. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_send_rename\n", 0, 0, 0 );
  793. return windows_perform_operation(conn, CONN_RENAME, dn, NULL /* attrs */,
  794. newrdn, newparent, deleteoldrdn, server_controls,
  795. NULL /* extop OID */, NULL /* extop payload */, NULL /* retoidp */,
  796. NULL /* retdatap */, returned_controls);
  797. }
  798. /*
  799. * Send an LDAP search operation.
  800. */
  801. Slapi_Entry * windows_conn_get_search_result(Repl_Connection *conn)
  802. {
  803. int rc=0;
  804. LDAPMessage *res = NULL;
  805. Slapi_Entry *e = NULL;
  806. LDAPMessage *lm = NULL;
  807. char *dn = "";
  808. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_get_search_result\n", 0, 0, 0 );
  809. if (windows_conn_connected(conn))
  810. {
  811. rc = ldap_result( conn->ld, LDAP_RES_ANY, 0, &conn->timeout, &res );
  812. switch (rc) {
  813. case 0:
  814. case -1:
  815. case LDAP_RES_SEARCH_REFERENCE:
  816. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name, "error in windows_conn_get_search_result, rc=%d\n", rc);
  817. break;
  818. case LDAP_RES_SEARCH_RESULT:
  819. {
  820. LDAPControl **returned_controls = NULL;
  821. int code = 0;
  822. /* Purify says this is a leak : */
  823. if (LDAP_SUCCESS != (rc = ldap_parse_result( conn->ld, res, &code, NULL, NULL, NULL, &returned_controls, 0 ))) {
  824. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name,
  825. "error reading search result in windows_conn_get_search_result, rc=%d:%s\n", rc, ldap_err2string(rc));
  826. }
  827. if (returned_controls)
  828. {
  829. windows_private_update_dirsync_control(conn->agmt, returned_controls);
  830. ldap_controls_free(returned_controls);
  831. }
  832. if (windows_private_dirsync_has_more(conn->agmt)) {
  833. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,"received hasmore from dirsync\n");
  834. }
  835. }
  836. break;
  837. case LDAP_RES_SEARCH_ENTRY:
  838. {
  839. if (( dn = ldap_get_dn( conn->ld, res )) != NULL )
  840. {
  841. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name,"received entry from dirsync: %s\n", dn);
  842. lm = ldap_first_entry( conn->ld, res );
  843. e = windows_LDAPMessage2Entry(conn,lm,0);
  844. ldap_memfree(dn);
  845. }
  846. }
  847. break;
  848. } /* switch */
  849. } /* if */
  850. if (res)
  851. {
  852. ldap_msgfree( res );
  853. res = NULL;
  854. }
  855. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_get_search_result\n", 0, 0, 0 );
  856. return e;
  857. }
  858. /*
  859. * Send an LDAP extended operation.
  860. */
  861. ConnResult
  862. windows_conn_send_extended_operation(Repl_Connection *conn, const char *extop_oid,
  863. struct berval *payload, char **retoidp, struct berval **retdatap,
  864. LDAPControl **server_controls, LDAPControl ***returned_controls)
  865. {
  866. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_send_extended_operation\n", 0, 0, 0 );
  867. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_send_extended_operation\n", 0, 0, 0 );
  868. return windows_perform_operation(conn, CONN_EXTENDED_OPERATION, NULL /* dn */, NULL /* attrs */,
  869. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  870. server_controls, extop_oid, payload, retoidp, retdatap,
  871. returned_controls);
  872. }
  873. /*
  874. * Synchronously read an entry and return a specific attribute's values.
  875. * Returns CONN_OPERATION_SUCCESS if successful. Returns
  876. * CONN_OPERATION_FAILED if the operation was sent but an LDAP error
  877. * occurred (conn->last_ldap_error is set in this case), and
  878. * CONN_NOT_CONNECTED if no connection was active.
  879. *
  880. * The caller must free the returned_bvals.
  881. */
  882. ConnResult
  883. windows_conn_read_entry_attribute(Repl_Connection *conn, const char *dn,
  884. char *type, struct berval ***returned_bvals)
  885. {
  886. ConnResult return_value;
  887. int ldap_rc;
  888. LDAPControl *server_controls[2];
  889. LDAPMessage *res = NULL;
  890. char *attrs[2];
  891. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_read_entry_attribute\n", 0, 0, 0 );
  892. PR_ASSERT(NULL != type);
  893. if (windows_conn_connected(conn))
  894. {
  895. server_controls[0] = &manageDSAITControl;
  896. server_controls[1] = NULL;
  897. attrs[0] = type;
  898. attrs[1] = NULL;
  899. ldap_rc = ldap_search_ext_s(conn->ld, dn, LDAP_SCOPE_BASE,
  900. "(objectclass=*)", attrs, 0 /* attrsonly */,
  901. server_controls, NULL /* client controls */,
  902. &conn->timeout, 0 /* sizelimit */, &res);
  903. if (LDAP_SUCCESS == ldap_rc)
  904. {
  905. LDAPMessage *entry = ldap_first_entry(conn->ld, res);
  906. if (NULL != entry)
  907. {
  908. *returned_bvals = ldap_get_values_len(conn->ld, entry, type);
  909. }
  910. return_value = CONN_OPERATION_SUCCESS;
  911. }
  912. else if (IS_DISCONNECT_ERROR(ldap_rc))
  913. {
  914. windows_conn_disconnect(conn);
  915. return_value = CONN_NOT_CONNECTED;
  916. }
  917. else
  918. {
  919. return_value = CONN_OPERATION_FAILED;
  920. }
  921. conn->last_ldap_error = ldap_rc;
  922. if (NULL != res)
  923. {
  924. ldap_msgfree(res);
  925. res = NULL;
  926. }
  927. }
  928. else
  929. {
  930. return_value = CONN_NOT_CONNECTED;
  931. }
  932. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_read_entry_attribute\n", 0, 0, 0 );
  933. return return_value;
  934. }
  935. /*
  936. * Return an pointer to a string describing the connection's status.
  937. */
  938. const char *
  939. windows_conn_get_status(Repl_Connection *conn)
  940. {
  941. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_get_status\n", 0, 0, 0 );
  942. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_get_status\n", 0, 0, 0 );
  943. return conn->status;
  944. }
  945. /*
  946. * Cancel any outstanding linger timer. Should be called when
  947. * a replication session is beginning.
  948. */
  949. void
  950. windows_conn_cancel_linger(Repl_Connection *conn)
  951. {
  952. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_cancel_linger\n", 0, 0, 0 );
  953. PR_ASSERT(NULL != conn);
  954. PR_Lock(conn->lock);
  955. if (conn->linger_active)
  956. {
  957. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  958. "%s: Cancelling linger on the connection\n",
  959. agmt_get_long_name(conn->agmt));
  960. conn->linger_active = PR_FALSE;
  961. if (slapi_eq_cancel(conn->linger_event) == 1)
  962. {
  963. conn->refcnt--;
  964. }
  965. conn->linger_event = NULL;
  966. conn->status = STATUS_CONNECTED;
  967. }
  968. else
  969. {
  970. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  971. "%s: No linger to cancel on the connection\n",
  972. agmt_get_long_name(conn->agmt));
  973. }
  974. PR_Unlock(conn->lock);
  975. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_cancel_linger\n", 0, 0, 0 );
  976. }
  977. /*
  978. * Called when our linger timeout timer expires. This means
  979. * we should check to see if perhaps the connection's become
  980. * active again, in which case we do nothing. Otherwise,
  981. * we close the connection.
  982. */
  983. static void
  984. linger_timeout(time_t event_time, void *arg)
  985. {
  986. PRBool delete_now;
  987. Repl_Connection *conn = (Repl_Connection *)arg;
  988. LDAPDebug( LDAP_DEBUG_TRACE, "=> linger_timeout\n", 0, 0, 0 );
  989. PR_ASSERT(NULL != conn);
  990. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  991. "%s: Linger timeout has expired on the connection\n",
  992. agmt_get_long_name(conn->agmt));
  993. PR_Lock(conn->lock);
  994. if (conn->linger_active)
  995. {
  996. conn->linger_active = PR_FALSE;
  997. conn->linger_event = NULL;
  998. close_connection_internal(conn);
  999. }
  1000. delete_now = conn->delete_after_linger;
  1001. PR_Unlock(conn->lock);
  1002. if (delete_now)
  1003. {
  1004. windows_conn_delete_internal(conn);
  1005. }
  1006. LDAPDebug( LDAP_DEBUG_TRACE, "<= linger_timeout\n", 0, 0, 0 );
  1007. }
  1008. /*
  1009. * Indicate that a session is ending. The linger timer starts when
  1010. * this function is called.
  1011. */
  1012. void
  1013. windows_conn_start_linger(Repl_Connection *conn)
  1014. {
  1015. time_t now;
  1016. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_start_linger\n", 0, 0, 0 );
  1017. PR_ASSERT(NULL != conn);
  1018. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1019. "%s: Beginning linger on the connection\n",
  1020. agmt_get_long_name(conn->agmt));
  1021. if (!windows_conn_connected(conn))
  1022. {
  1023. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1024. "%s: No linger on the closed conn\n",
  1025. agmt_get_long_name(conn->agmt));
  1026. return;
  1027. }
  1028. time(&now);
  1029. PR_Lock(conn->lock);
  1030. if (conn->linger_active)
  1031. {
  1032. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1033. "%s: Linger already active on the connection\n",
  1034. agmt_get_long_name(conn->agmt));
  1035. }
  1036. else
  1037. {
  1038. conn->linger_active = PR_TRUE;
  1039. conn->linger_event = slapi_eq_once(linger_timeout, conn, now + conn->linger_time);
  1040. conn->status = STATUS_LINGERING;
  1041. }
  1042. PR_Unlock(conn->lock);
  1043. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_start_linger\n", 0, 0, 0 );
  1044. }
  1045. /*
  1046. * If no connection is currently active, opens a connection and binds to
  1047. * the remote server. If a connection is open (e.g. lingering) then
  1048. * this is a no-op.
  1049. *
  1050. * Returns CONN_OPERATION_SUCCESS on success, or CONN_OPERATION_FAILED
  1051. * on failure. Sets conn->last_ldap_error and conn->last_operation;
  1052. */
  1053. ConnResult
  1054. windows_conn_connect(Repl_Connection *conn)
  1055. {
  1056. int optdata;
  1057. int secure = 0;
  1058. char* binddn = NULL;
  1059. struct berval *creds = NULL;
  1060. ConnResult return_value = CONN_OPERATION_SUCCESS;
  1061. int pw_ret = 1;
  1062. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_connect\n", 0, 0, 0 );
  1063. /** Connection already open just return SUCCESS **/
  1064. if(conn->state == STATE_CONNECTED) {
  1065. goto done;
  1066. }
  1067. PR_Lock(conn->lock);
  1068. if (conn->flag_agmt_changed) {
  1069. /* So far we cannot change Hostname and Port */
  1070. /* slapi_ch_free((void **)&conn->hostname); */
  1071. /* conn->hostname = agmt_get_hostname(conn->agmt); */
  1072. /* conn->port = agmt_get_port(conn->agmt); */
  1073. slapi_ch_free((void **)&conn->binddn);
  1074. conn->binddn = agmt_get_binddn(conn->agmt);
  1075. conn->bindmethod = agmt_get_bindmethod(conn->agmt);
  1076. conn->transport_flags = agmt_get_transport_flags(conn->agmt);
  1077. conn->timeout.tv_sec = agmt_get_timeout(conn->agmt);
  1078. conn->flag_agmt_changed = 0;
  1079. slapi_ch_free((void **)&conn->plain);
  1080. }
  1081. PR_Unlock(conn->lock);
  1082. creds = agmt_get_credentials(conn->agmt);
  1083. if (conn->plain == NULL) {
  1084. char *plain = NULL;
  1085. /* kexcoff: for reversible encryption */
  1086. /* We need to test the return code of pw_rever_decode in order to decide
  1087. * if a free for plain will be needed (pw_ret == 0) or not (pw_ret != 0) */
  1088. pw_ret = pw_rever_decode(creds->bv_val, &plain, type_nsds5ReplicaCredentials);
  1089. /* Pb occured in decryption: stop now, binding will fail */
  1090. if ( pw_ret == -1 )
  1091. {
  1092. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1093. "%s: Decoding of the credentials failed.\n",
  1094. agmt_get_long_name(conn->agmt));
  1095. return_value = CONN_OPERATION_FAILED;
  1096. conn->last_ldap_error = LDAP_INVALID_CREDENTIALS;
  1097. conn->state = STATE_DISCONNECTED;
  1098. goto done;
  1099. } /* Else, does not mean that the plain is correct, only means the we had no internal
  1100. decoding pb */
  1101. conn->plain = slapi_ch_strdup (plain);
  1102. if (!pw_ret) slapi_ch_free((void**)&plain);
  1103. }
  1104. /* ugaston: if SSL has been selected in the replication agreement, SSL client
  1105. * initialisation should be done before ever trying to open any connection at all.
  1106. */
  1107. if (conn->transport_flags == TRANSPORT_FLAG_TLS) {
  1108. secure = 2;
  1109. } else if (conn->transport_flags == TRANSPORT_FLAG_SSL) {
  1110. secure = 1;
  1111. }
  1112. if (secure > 0) {
  1113. if (!NSS_IsInitialized()) {
  1114. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1115. "%s: SSL Not Initialized, Replication over SSL FAILED\n",
  1116. agmt_get_long_name(conn->agmt));
  1117. return_value = CONN_SSL_NOT_ENABLED;
  1118. conn->last_ldap_error = LDAP_INAPPROPRIATE_AUTH;
  1119. conn->last_operation = CONN_INIT;
  1120. goto done;
  1121. }
  1122. }
  1123. if (return_value == CONN_OPERATION_SUCCESS) {
  1124. #if !defined(USE_OPENLDAP)
  1125. int io_timeout_ms;
  1126. #endif
  1127. /* Now we initialize the LDAP Structure and set options */
  1128. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1129. "%s: Trying %s%s slapi_ldap_init_ext\n",
  1130. agmt_get_long_name(conn->agmt),
  1131. secure ? "secure" : "non-secure",
  1132. (secure == 2) ? " startTLS" : "");
  1133. conn->ld = slapi_ldap_init_ext(NULL, conn->hostname, conn->port, secure, 0, NULL);
  1134. if (NULL == conn->ld)
  1135. {
  1136. return_value = CONN_OPERATION_FAILED;
  1137. conn->state = STATE_DISCONNECTED;
  1138. conn->last_operation = CONN_INIT;
  1139. conn->last_ldap_error = LDAP_LOCAL_ERROR;
  1140. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1141. "%s: Failed to establish %s%sconnection to the consumer\n",
  1142. agmt_get_long_name(conn->agmt),
  1143. secure ? "secure " : "",
  1144. (secure == 2) ? "startTLS " : "");
  1145. goto done;
  1146. }
  1147. /* slapi_ch_strdup is OK with NULL strings */
  1148. binddn = slapi_ch_strdup(conn->binddn);
  1149. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1150. "%s: binddn = %s, passwd = %s\n",
  1151. agmt_get_long_name(conn->agmt),
  1152. binddn?binddn:"NULL", creds->bv_val?creds->bv_val:"NULL");
  1153. /* Set some options for the connection. */
  1154. optdata = LDAP_DEREF_NEVER; /* Don't dereference aliases */
  1155. ldap_set_option(conn->ld, LDAP_OPT_DEREF, &optdata);
  1156. optdata = LDAP_VERSION3; /* We need LDAP version 3 */
  1157. ldap_set_option(conn->ld, LDAP_OPT_PROTOCOL_VERSION, &optdata);
  1158. /* Don't chase any referrals (although we shouldn't get any) */
  1159. ldap_set_option(conn->ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
  1160. /* override the default timeout with the specified timeout */
  1161. #if defined(USE_OPENLDAP)
  1162. ldap_set_option(conn->ld, LDAP_OPT_NETWORK_TIMEOUT, &conn->timeout);
  1163. #else
  1164. io_timeout_ms = conn->timeout.tv_sec * 1000 + conn->timeout.tv_usec / 1000;
  1165. prldap_set_session_option(conn->ld, NULL, PRLDAP_OPT_IO_MAX_TIMEOUT,
  1166. io_timeout_ms);
  1167. #endif
  1168. /* We've got an ld. Now bind to the server. */
  1169. conn->last_operation = CONN_BIND;
  1170. }
  1171. if ( bind_and_check_pwp(conn, binddn, conn->plain) == CONN_OPERATION_FAILED )
  1172. {
  1173. conn->last_ldap_error = slapi_ldap_get_lderrno (conn->ld, NULL, NULL);
  1174. conn->state = STATE_DISCONNECTED;
  1175. return_value = CONN_OPERATION_FAILED;
  1176. }
  1177. else
  1178. {
  1179. conn->last_ldap_error = LDAP_SUCCESS;
  1180. conn->state = STATE_CONNECTED;
  1181. return_value = CONN_OPERATION_SUCCESS;
  1182. }
  1183. {
  1184. ConnResult supports = 0;
  1185. supports = windows_conn_replica_supports_dirsync(conn);
  1186. if (CONN_DOES_NOT_SUPPORT_DIRSYNC == supports)
  1187. {
  1188. /* We assume that a server that doesn't support dirsync is our NT4 LDAP service */
  1189. windows_private_set_isnt4(conn->agmt,1);
  1190. LDAPDebug( LDAP_DEBUG_REPL, "windows_conn_connect : detected NT4 peer\n", 0, 0, 0 );
  1191. } else
  1192. {
  1193. windows_private_set_isnt4(conn->agmt,0);
  1194. }
  1195. supports = windows_conn_replica_is_win2k3(conn);
  1196. if (CONN_IS_WIN2K3 == supports)
  1197. {
  1198. windows_private_set_iswin2k3(conn->agmt,1);
  1199. LDAPDebug( LDAP_DEBUG_REPL, "windows_conn_connect : detected Win2k3 peer\n", 0, 0, 0 );
  1200. } else
  1201. {
  1202. windows_private_set_iswin2k3(conn->agmt,0);
  1203. }
  1204. }
  1205. slapi_ch_free((void**)&binddn);
  1206. if(return_value == CONN_OPERATION_FAILED)
  1207. {
  1208. close_connection_internal(conn);
  1209. } else
  1210. {
  1211. conn->last_ldap_error = LDAP_SUCCESS;
  1212. conn->state = STATE_CONNECTED;
  1213. }
  1214. done:
  1215. if (creds) {
  1216. ber_bvfree(creds);
  1217. creds = NULL;
  1218. }
  1219. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_connect\n", 0, 0, 0 );
  1220. return return_value;
  1221. }
  1222. static void
  1223. close_connection_internal(Repl_Connection *conn)
  1224. {
  1225. LDAPDebug( LDAP_DEBUG_TRACE, "=> close_connection_internal\n", 0, 0, 0 );
  1226. if (NULL != conn->ld)
  1227. {
  1228. /* Since we call slapi_ldap_init,
  1229. we must call slapi_ldap_unbind */
  1230. slapi_ldap_unbind(conn->ld);
  1231. }
  1232. conn->ld = NULL;
  1233. conn->state = STATE_DISCONNECTED;
  1234. conn->status = STATUS_DISCONNECTED;
  1235. conn->supports_ds50_repl = -1;
  1236. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1237. "%s: Disconnected from the consumer\n", agmt_get_long_name(conn->agmt));
  1238. LDAPDebug( LDAP_DEBUG_TRACE, "<= close_connection_internal\n", 0, 0, 0 );
  1239. }
  1240. void
  1241. windows_conn_disconnect(Repl_Connection *conn)
  1242. {
  1243. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_disconnect\n", 0, 0, 0 );
  1244. PR_ASSERT(NULL != conn);
  1245. PR_Lock(conn->lock);
  1246. close_connection_internal(conn);
  1247. PR_Unlock(conn->lock);
  1248. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_disconnect\n", 0, 0, 0 );
  1249. }
  1250. /*
  1251. * Determine if the remote replica supports DS 5.0 replication.
  1252. * Return codes:
  1253. * CONN_SUPPORTS_DS5_REPL - the remote replica suport DS5 replication
  1254. * CONN_DOES_NOT_SUPPORT_DS5_REPL - the remote replica does not
  1255. * support DS5 replication.
  1256. * CONN_OPERATION_FAILED - it could not be determined if the remote
  1257. * replica supports DS5 replication.
  1258. * CONN_NOT_CONNECTED - no connection was active.
  1259. */
  1260. ConnResult
  1261. windows_conn_replica_supports_ds5_repl(Repl_Connection *conn)
  1262. {
  1263. ConnResult return_value;
  1264. int ldap_rc;
  1265. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_replica_supports_ds5_repl\n", 0, 0, 0 );
  1266. if (windows_conn_connected(conn))
  1267. {
  1268. if (conn->supports_ds50_repl == -1) {
  1269. LDAPMessage *res = NULL;
  1270. LDAPMessage *entry = NULL;
  1271. char *attrs[] = {"supportedcontrol", "supportedextension", NULL};
  1272. conn->status = STATUS_SEARCHING;
  1273. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1274. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1275. NULL /* server controls */, NULL /* client controls */,
  1276. &conn->timeout, LDAP_NO_LIMIT, &res);
  1277. if (LDAP_SUCCESS == ldap_rc)
  1278. {
  1279. conn->supports_ds50_repl = 0;
  1280. entry = ldap_first_entry(conn->ld, res);
  1281. if (!attribute_string_value_present(conn->ld, entry, "supportedcontrol", REPL_NSDS50_UPDATE_INFO_CONTROL_OID))
  1282. {
  1283. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1284. }
  1285. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_START_NSDS50_REPLICATION_REQUEST_OID))
  1286. {
  1287. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1288. }
  1289. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_END_NSDS50_REPLICATION_REQUEST_OID))
  1290. {
  1291. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1292. }
  1293. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_ENTRY_REQUEST_OID))
  1294. {
  1295. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1296. }
  1297. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_RESPONSE_OID))
  1298. {
  1299. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1300. }
  1301. else
  1302. {
  1303. conn->supports_ds50_repl = 1;
  1304. return_value = CONN_SUPPORTS_DS5_REPL;
  1305. }
  1306. }
  1307. else
  1308. {
  1309. if (IS_DISCONNECT_ERROR(ldap_rc))
  1310. {
  1311. conn->last_ldap_error = ldap_rc; /* specific reason */
  1312. windows_conn_disconnect(conn);
  1313. return_value = CONN_NOT_CONNECTED;
  1314. }
  1315. else
  1316. {
  1317. return_value = CONN_OPERATION_FAILED;
  1318. }
  1319. }
  1320. if (NULL != res)
  1321. ldap_msgfree(res);
  1322. }
  1323. else {
  1324. return_value = conn->supports_ds50_repl ? CONN_SUPPORTS_DS5_REPL : CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1325. }
  1326. }
  1327. else
  1328. {
  1329. /* Not connected */
  1330. return_value = CONN_NOT_CONNECTED;
  1331. }
  1332. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_replica_supports_ds5_repl\n", 0, 0, 0 );
  1333. return return_value;
  1334. }
  1335. ConnResult
  1336. windows_conn_replica_supports_dirsync(Repl_Connection *conn)
  1337. {
  1338. ConnResult return_value;
  1339. int ldap_rc;
  1340. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_replica_supports_dirsync\n", 0, 0, 0 );
  1341. if (getenv("WINSYNC_USE_DS")) {
  1342. /* used to fake out dirsync to think it's talking to a real ad when in fact
  1343. it's just talking to another directory server */
  1344. conn->supports_dirsync = 1;
  1345. return CONN_SUPPORTS_DIRSYNC;
  1346. }
  1347. if (windows_conn_connected(conn))
  1348. {
  1349. if (conn->supports_dirsync == -1) {
  1350. LDAPMessage *res = NULL;
  1351. LDAPMessage *entry = NULL;
  1352. char *attrs[] = {"supportedcontrol", NULL};
  1353. conn->status = STATUS_SEARCHING;
  1354. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1355. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1356. NULL /* server controls */, NULL /* client controls */,
  1357. &conn->timeout, LDAP_NO_LIMIT, &res);
  1358. if (LDAP_SUCCESS == ldap_rc)
  1359. {
  1360. conn->supports_dirsync = 0;
  1361. entry = ldap_first_entry(conn->ld, res);
  1362. if (!attribute_string_value_present(conn->ld, entry, "supportedcontrol", REPL_DIRSYNC_CONTROL_OID))
  1363. {
  1364. return_value = CONN_DOES_NOT_SUPPORT_DIRSYNC;
  1365. }
  1366. else
  1367. {
  1368. conn->supports_dirsync =1;
  1369. return_value = CONN_SUPPORTS_DIRSYNC;
  1370. }
  1371. }
  1372. else
  1373. {
  1374. if (IS_DISCONNECT_ERROR(ldap_rc))
  1375. {
  1376. conn->last_ldap_error = ldap_rc; /* specific reason */
  1377. windows_conn_disconnect(conn);
  1378. return_value = CONN_NOT_CONNECTED;
  1379. }
  1380. else
  1381. {
  1382. return_value = CONN_OPERATION_FAILED;
  1383. }
  1384. }
  1385. if (NULL != res)
  1386. ldap_msgfree(res);
  1387. }
  1388. else {
  1389. return_value = conn->supports_dirsync ? CONN_SUPPORTS_DIRSYNC : CONN_DOES_NOT_SUPPORT_DIRSYNC;
  1390. }
  1391. }
  1392. else
  1393. {
  1394. /* Not connected */
  1395. return_value = CONN_NOT_CONNECTED;
  1396. }
  1397. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_replica_supports_dirsync\n", 0, 0, 0 );
  1398. return return_value;
  1399. }
  1400. /* Checks if the AD server is running win2k3 (or later) */
  1401. ConnResult
  1402. windows_conn_replica_is_win2k3(Repl_Connection *conn)
  1403. {
  1404. ConnResult return_value;
  1405. int ldap_rc;
  1406. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_replica_is_win2k3\n", 0, 0, 0 );
  1407. if (windows_conn_connected(conn))
  1408. {
  1409. if (conn->is_win2k3 == -1) {
  1410. LDAPMessage *res = NULL;
  1411. LDAPMessage *entry = NULL;
  1412. char *attrs[] = {"supportedCapabilities", NULL};
  1413. conn->status = STATUS_SEARCHING;
  1414. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1415. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1416. NULL /* server controls */, NULL /* client controls */,
  1417. &conn->timeout, LDAP_NO_LIMIT, &res);
  1418. if (LDAP_SUCCESS == ldap_rc)
  1419. {
  1420. conn->is_win2k3 = 0;
  1421. entry = ldap_first_entry(conn->ld, res);
  1422. if (!attribute_string_value_present(conn->ld, entry, "supportedCapabilities", REPL_WIN2K3_AD_OID))
  1423. {
  1424. return_value = CONN_NOT_WIN2K3;
  1425. }
  1426. else
  1427. {
  1428. conn->is_win2k3 =1;
  1429. return_value = CONN_IS_WIN2K3;
  1430. }
  1431. }
  1432. else
  1433. {
  1434. if (IS_DISCONNECT_ERROR(ldap_rc))
  1435. {
  1436. conn->last_ldap_error = ldap_rc; /* specific reason */
  1437. windows_conn_disconnect(conn);
  1438. return_value = CONN_NOT_CONNECTED;
  1439. }
  1440. else
  1441. {
  1442. return_value = CONN_OPERATION_FAILED;
  1443. }
  1444. }
  1445. if (NULL != res)
  1446. ldap_msgfree(res);
  1447. }
  1448. else {
  1449. return_value = conn->is_win2k3 ? CONN_IS_WIN2K3 : CONN_NOT_WIN2K3;
  1450. }
  1451. }
  1452. else
  1453. {
  1454. /* Not connected */
  1455. return_value = CONN_NOT_CONNECTED;
  1456. }
  1457. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_replica_is_win2k3\n", 0, 0, 0 );
  1458. return return_value;
  1459. }
  1460. /*
  1461. * Return 1 if "value" is a value of attribute type "type" in entry "entry".
  1462. * Otherwise, return 0.
  1463. */
  1464. static int
  1465. attribute_string_value_present(LDAP *ld, LDAPMessage *entry, const char *type,
  1466. const char *value)
  1467. {
  1468. int return_value = 0;
  1469. ber_len_t vallen;
  1470. LDAPDebug( LDAP_DEBUG_TRACE, "=> attribute_string_value_present\n", 0, 0, 0 );
  1471. if (NULL != entry)
  1472. {
  1473. char *atype = NULL;
  1474. BerElement *ber = NULL;
  1475. vallen = strlen(value);
  1476. atype = ldap_first_attribute(ld, entry, &ber);
  1477. while (NULL != atype && 0 == return_value)
  1478. {
  1479. if (strcasecmp(atype, type) == 0)
  1480. {
  1481. struct berval **vals = ldap_get_values_len(ld, entry, atype);
  1482. int i;
  1483. for (i = 0; return_value == 0 && NULL != vals && NULL != vals[i]; i++)
  1484. {
  1485. if ((vallen == vals[i]->bv_len) && !strncmp(vals[i]->bv_val, value, vallen))
  1486. {
  1487. return_value = 1;
  1488. }
  1489. }
  1490. if (NULL != vals)
  1491. {
  1492. ldap_value_free_len(vals);
  1493. }
  1494. }
  1495. ldap_memfree(atype);
  1496. atype = ldap_next_attribute(ld, entry, ber);
  1497. }
  1498. if (NULL != ber)
  1499. ber_free(ber, 0);
  1500. /* The last atype has not been freed yet */
  1501. if (NULL != atype)
  1502. ldap_memfree(atype);
  1503. }
  1504. LDAPDebug( LDAP_DEBUG_TRACE, "<= attribute_string_value_present\n", 0, 0, 0 );
  1505. return return_value;
  1506. }
  1507. /*
  1508. * Read the remote server's schema entry, then read the local schema entry,
  1509. * and compare the nsschemacsn attribute. If the local csn is newer, or
  1510. * the remote csn is absent, push the schema down to the consumer.
  1511. * Return codes:
  1512. * CONN_SCHEMA_UPDATED if the schema was pushed successfully
  1513. * CONN_SCHEMA_NO_UPDATE_NEEDED if the schema was as new or newer than
  1514. * the local server's schema
  1515. * CONN_OPERATION_FAILED if an error occurred
  1516. * CONN_NOT_CONNECTED if no connection was active
  1517. * NOTE: Should only be called when a replication session has been
  1518. * established by sending a startReplication extended operation.
  1519. */
  1520. void
  1521. windows_conn_set_timeout(Repl_Connection *conn, long timeout)
  1522. {
  1523. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_set_timeout\n", 0, 0, 0 );
  1524. PR_ASSERT(NULL != conn);
  1525. PR_ASSERT(timeout >= 0);
  1526. PR_Lock(conn->lock);
  1527. conn->timeout.tv_sec = timeout;
  1528. PR_Unlock(conn->lock);
  1529. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_timeout\n", 0, 0, 0 );
  1530. }
  1531. void windows_conn_set_agmt_changed(Repl_Connection *conn)
  1532. {
  1533. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_conn_set_agmt_changed\n", 0, 0, 0 );
  1534. PR_ASSERT(NULL != conn);
  1535. PR_Lock(conn->lock);
  1536. if (NULL != conn->agmt)
  1537. conn->flag_agmt_changed = 1;
  1538. PR_Unlock(conn->lock);
  1539. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_conn_set_agmt_changed\n", 0, 0, 0 );
  1540. }
  1541. static const char *
  1542. bind_method_to_mech(int bindmethod)
  1543. {
  1544. switch (bindmethod) {
  1545. case BINDMETHOD_SSL_CLIENTAUTH:
  1546. return LDAP_SASL_EXTERNAL;
  1547. break;
  1548. case BINDMETHOD_SASL_GSSAPI:
  1549. return "GSSAPI";
  1550. break;
  1551. case BINDMETHOD_SASL_DIGEST_MD5:
  1552. return "DIGEST-MD5";
  1553. break;
  1554. default: /* anything else */
  1555. return LDAP_SASL_SIMPLE;
  1556. }
  1557. return LDAP_SASL_SIMPLE;
  1558. }
  1559. /*
  1560. * Check the result of an ldap_simple_bind operation to see we it
  1561. * contains the expiration controls
  1562. * return: -1 error, not bound
  1563. * 0, OK bind has succeeded
  1564. */
  1565. static int
  1566. bind_and_check_pwp(Repl_Connection *conn, char * binddn, char *password)
  1567. {
  1568. LDAPControl **ctrls = NULL;
  1569. LDAP *ld = conn->ld;
  1570. int rc;
  1571. const char *mech = bind_method_to_mech(conn->bindmethod);
  1572. LDAPDebug( LDAP_DEBUG_TRACE, "=> bind_and_check_pwp\n", 0, 0, 0 );
  1573. rc = slapi_ldap_bind(conn->ld, binddn, password, mech, NULL,
  1574. &ctrls, NULL, NULL);
  1575. if ( rc == LDAP_SUCCESS )
  1576. {
  1577. if (conn->last_ldap_error != rc)
  1578. {
  1579. conn->last_ldap_error = rc;
  1580. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1581. "%s: Replication bind with %s auth resumed\n",
  1582. agmt_get_long_name(conn->agmt),
  1583. mech ? mech : "SIMPLE");
  1584. }
  1585. if ( ctrls )
  1586. {
  1587. int i;
  1588. for( i = 0; ctrls[ i ] != NULL; ++i )
  1589. {
  1590. if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRED)) )
  1591. {
  1592. /* Bind is successfull but password has expired */
  1593. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1594. "%s: Successfully bound %s to consumer, "
  1595. "but password has expired on consumer.\n",
  1596. agmt_get_long_name(conn->agmt), binddn);
  1597. }
  1598. else if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRING)) )
  1599. {
  1600. /* The password is expiring in n seconds */
  1601. if ( (ctrls[ i ]->ldctl_value.bv_val != NULL) &&
  1602. (ctrls[ i ]->ldctl_value.bv_len > 0) )
  1603. {
  1604. int password_expiring = atoi( ctrls[ i ]->ldctl_value.bv_val );
  1605. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1606. "%s: Successfully bound %s to consumer, "
  1607. "but password is expiring on consumer in %d seconds.\n",
  1608. agmt_get_long_name(conn->agmt), binddn, password_expiring);
  1609. }
  1610. }
  1611. }
  1612. ldap_controls_free( ctrls );
  1613. }
  1614. LDAPDebug( LDAP_DEBUG_TRACE, "<= bind_and_check_pwp - CONN_OPERATION_SUCCESS\n", 0, 0, 0 );
  1615. return (CONN_OPERATION_SUCCESS);
  1616. }
  1617. else
  1618. {
  1619. ldap_controls_free( ctrls );
  1620. /* Do not report the same error over and over again
  1621. * unless replication level logging is enabled. */
  1622. if (conn->last_ldap_error != rc)
  1623. {
  1624. char *errmsg = NULL;
  1625. conn->last_ldap_error = rc;
  1626. /* errmsg is a pointer directly into the ld structure - do not free */
  1627. rc = slapi_ldap_get_lderrno( ld, NULL, &errmsg );
  1628. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1629. "%s: Replication bind with %s auth failed: LDAP error %d (%s) (%s)\n",
  1630. agmt_get_long_name(conn->agmt),
  1631. mech ? mech : "SIMPLE", rc,
  1632. ldap_err2string(rc), errmsg);
  1633. } else {
  1634. char *errmsg = NULL;
  1635. /* errmsg is a pointer directly into the ld structure - do not free */
  1636. rc = slapi_ldap_get_lderrno( ld, NULL, &errmsg );
  1637. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1638. "%s: Replication bind with %s auth failed: LDAP error %d (%s) (%s)\n",
  1639. agmt_get_long_name(conn->agmt),
  1640. mech ? mech : "SIMPLE", rc,
  1641. ldap_err2string(rc), errmsg);
  1642. }
  1643. LDAPDebug( LDAP_DEBUG_TRACE, "<= bind_and_check_pwp - CONN_OPERATION_FAILED\n", 0, 0, 0 );
  1644. return (CONN_OPERATION_FAILED);
  1645. }
  1646. }
  1647. /* Attempt to bind as a user to AD in order to see if we posess the
  1648. * most current password. Returns 0 if the bind was successful,
  1649. * non-zero otherwise. */
  1650. int
  1651. windows_check_user_password(Repl_Connection *conn, Slapi_DN *sdn, char *password)
  1652. {
  1653. const char *binddn = NULL;
  1654. LDAPMessage *res = NULL;
  1655. int rc = 0;
  1656. int msgid = 0;
  1657. int parse_rc = 0;
  1658. /* If we're already connected, this will just return success */
  1659. windows_conn_connect(conn);
  1660. /* Get binddn from sdn */
  1661. binddn = slapi_sdn_get_dn(sdn);
  1662. /* Attempt to do a bind on the existing connection
  1663. * using the dn and password that were passed in. */
  1664. msgid = do_simple_bind(conn, conn->ld, (char *) binddn, password);
  1665. rc = ldap_result(conn->ld, msgid, LDAP_MSG_ALL, NULL, &res);
  1666. if (0 > rc) { /* error */
  1667. rc = slapi_ldap_get_lderrno(conn->ld, NULL, NULL);
  1668. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1669. "Error reading bind response for id "
  1670. "[%s]: error %d (%s)\n",
  1671. binddn ? binddn : "(anon)",
  1672. rc, ldap_err2string(rc));
  1673. } else if (rc == 0) { /* timeout */
  1674. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1675. "Error: timeout reading "
  1676. "bind response for [%s]\n",
  1677. binddn ? binddn : "(anon)");
  1678. rc = -1;
  1679. } else {
  1680. parse_rc = ldap_parse_result( conn->ld, res, &rc, NULL, NULL, NULL, NULL, 1 /* Free res */);
  1681. if (parse_rc != LDAP_SUCCESS) {
  1682. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1683. "Error: unable to parse "
  1684. "bind result for [%s]: "
  1685. "error %d\n",
  1686. binddn ? binddn : "(anon)",
  1687. parse_rc);
  1688. rc = -1;
  1689. }
  1690. }
  1691. /* rebind as the DN specified in the sync agreement */
  1692. bind_and_check_pwp(conn, conn->binddn, conn->plain);
  1693. return rc;
  1694. }
  1695. static int
  1696. do_simple_bind (Repl_Connection *conn, LDAP *ld, char * binddn, char *password)
  1697. {
  1698. int msgid;
  1699. LDAPDebug( LDAP_DEBUG_TRACE, "=> do_simple_bind\n", 0, 0, 0 );
  1700. if( LDAP_SUCCESS != slapi_ldap_bind( ld, binddn, password, LDAP_SASL_SIMPLE, NULL, NULL, NULL, &msgid ) )
  1701. {
  1702. char *ldaperrtext = NULL;
  1703. int ldaperr;
  1704. int prerr = PR_GetError();
  1705. ldaperr = slapi_ldap_get_lderrno( ld, NULL, &ldaperrtext );
  1706. /* Do not report the same error over and over again */
  1707. if (conn->last_ldap_error != ldaperr)
  1708. {
  1709. conn->last_ldap_error = ldaperr;
  1710. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1711. "%s: Simple bind failed, "
  1712. SLAPI_COMPONENT_NAME_LDAPSDK " error %d (%s) (%s), "
  1713. SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
  1714. agmt_get_long_name(conn->agmt),
  1715. ldaperr, ldap_err2string(ldaperr),
  1716. ldaperrtext ? ldaperrtext : "",
  1717. prerr, slapd_pr_strerror(prerr));
  1718. }
  1719. }
  1720. else if (conn->last_ldap_error != LDAP_SUCCESS)
  1721. {
  1722. conn->last_ldap_error = LDAP_SUCCESS;
  1723. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1724. "%s: Simple bind resumed\n",
  1725. agmt_get_long_name(conn->agmt));
  1726. }
  1727. LDAPDebug( LDAP_DEBUG_TRACE, "<= do_simple_bind\n", 0, 0, 0 );
  1728. return msgid;
  1729. }
  1730. static Slapi_Eq_Context
  1731. repl5_start_debug_timeout(int *setlevel)
  1732. {
  1733. Slapi_Eq_Context eqctx = 0;
  1734. LDAPDebug( LDAP_DEBUG_TRACE, "=> repl5_start_debug_timeout\n", 0, 0, 0 );
  1735. if (s_debug_timeout && s_debug_level) {
  1736. time_t now = time(NULL);
  1737. eqctx = slapi_eq_once(repl5_debug_timeout_callback, setlevel,
  1738. s_debug_timeout + now);
  1739. }
  1740. LDAPDebug( LDAP_DEBUG_TRACE, "<= repl5_start_debug_timeout\n", 0, 0, 0 );
  1741. return eqctx;
  1742. }
  1743. static void
  1744. repl5_stop_debug_timeout(Slapi_Eq_Context eqctx, int *setlevel)
  1745. {
  1746. char buf[20];
  1747. char msg[SLAPI_DSE_RETURNTEXT_SIZE];
  1748. LDAPDebug( LDAP_DEBUG_TRACE, "=> repl5_stop_debug_timeout\n", 0, 0, 0 );
  1749. if (eqctx && !*setlevel) {
  1750. (void)slapi_eq_cancel(eqctx);
  1751. }
  1752. if (s_debug_timeout && s_debug_level && *setlevel) {
  1753. /* No longer needed as we are including the one in slap.h */
  1754. sprintf(buf, "%d", 0);
  1755. config_set_errorlog_level("nsslapd-errorlog-level", buf, msg, 1);
  1756. }
  1757. LDAPDebug( LDAP_DEBUG_TRACE, "<= repl5_stop_debug_timeout\n", 0, 0, 0 );
  1758. }
  1759. static void
  1760. repl5_debug_timeout_callback(time_t when, void *arg)
  1761. {
  1762. int *setlevel = (int *)arg;
  1763. /* No longer needed as we are including the one in slap.h */
  1764. char buf[20];
  1765. char msg[SLAPI_DSE_RETURNTEXT_SIZE];
  1766. LDAPDebug( LDAP_DEBUG_TRACE, "=> repl5_debug_timeout_callback\n", 0, 0, 0 );
  1767. *setlevel = 1;
  1768. sprintf(buf, "%d", s_debug_level);
  1769. config_set_errorlog_level("nsslapd-errorlog-level", buf, msg, 1);
  1770. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1771. "repl5_debug_timeout_callback: set debug level to %d at %ld\n",
  1772. s_debug_level, when);
  1773. LDAPDebug( LDAP_DEBUG_TRACE, "<= repl5_debug_timeout_callback\n", 0, 0, 0 );
  1774. }