windows_connection.c 58 KB

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