repl5_connection.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008
  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 "slapi-private.h"
  50. #if defined(USE_OPENLDAP)
  51. #include "ldap.h"
  52. #else
  53. #include "ldappr.h"
  54. #include "ldap-extension.h"
  55. #endif
  56. #include "nspr.h"
  57. #include "private/pprio.h"
  58. #include "nss.h"
  59. typedef struct repl_connection
  60. {
  61. char *hostname;
  62. int port;
  63. char *binddn;
  64. int bindmethod;
  65. int state;
  66. int last_operation;
  67. int last_ldap_error;
  68. const char *status;
  69. char *last_ldap_errmsg;
  70. PRUint32 transport_flags;
  71. LDAP *ld;
  72. int supports_ldapv3; /* 1 if does, 0 if doesn't, -1 if not determined */
  73. int supports_ds50_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  74. int supports_ds40_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  75. int supports_ds71_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  76. int supports_ds90_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  77. int linger_time; /* time in seconds to leave an idle connection open */
  78. PRBool linger_active;
  79. Slapi_Eq_Context *linger_event;
  80. PRBool delete_after_linger;
  81. int refcnt;
  82. const Repl_Agmt *agmt;
  83. PRLock *lock;
  84. struct timeval timeout;
  85. int flag_agmt_changed;
  86. char *plain;
  87. } repl_connection;
  88. /* #define DEFAULT_LINGER_TIME (5 * 60) */ /* 5 minutes */
  89. #define DEFAULT_LINGER_TIME (60)
  90. /*** from proto-slap.h ***/
  91. int schema_objectclasses_superset_check(struct berval **remote_schema, char *type);
  92. int schema_attributetypes_superset_check(struct berval **remote_schema, char *type);
  93. /* Controls we add on every outbound operation */
  94. static LDAPControl manageDSAITControl = {LDAP_CONTROL_MANAGEDSAIT, {0, ""}, '\0'};
  95. static int attribute_string_value_present(LDAP *ld, LDAPMessage *entry,
  96. const char *type, const char *value);
  97. static int bind_and_check_pwp(Repl_Connection *conn, char * binddn, char *password);
  98. static int s_debug_timeout = 0;
  99. static int s_debug_level = 0;
  100. static Slapi_Eq_Context repl5_start_debug_timeout(int *setlevel);
  101. static void repl5_stop_debug_timeout(Slapi_Eq_Context eqctx, int *setlevel);
  102. static void repl5_debug_timeout_callback(time_t when, void *arg);
  103. #define STATE_CONNECTED 600
  104. #define STATE_DISCONNECTED 601
  105. #define STATUS_DISCONNECTED "disconnected"
  106. #define STATUS_CONNECTED "connected"
  107. #define STATUS_PROCESSING_ADD "processing add operation"
  108. #define STATUS_PROCESSING_DELETE "processing delete operation"
  109. #define STATUS_PROCESSING_MODIFY "processing modify operation"
  110. #define STATUS_PROCESSING_RENAME "processing rename operation"
  111. #define STATUS_PROCESSING_EXTENDED_OPERATION "processing extended operation"
  112. #define STATUS_LINGERING "lingering"
  113. #define STATUS_SHUTTING_DOWN "shutting down"
  114. #define STATUS_BINDING "connecting and binding"
  115. #define STATUS_SEARCHING "processing search operation"
  116. #define CONN_NO_OPERATION 0
  117. #define CONN_ADD 1
  118. #define CONN_DELETE 2
  119. #define CONN_MODIFY 3
  120. #define CONN_RENAME 4
  121. #define CONN_EXTENDED_OPERATION 5
  122. #define CONN_BIND 6
  123. #define CONN_INIT 7
  124. /* These are errors returned from ldap operations which should cause us to disconnect and
  125. retry the connection later */
  126. #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)
  127. /* Forward declarations */
  128. static void close_connection_internal(Repl_Connection *conn);
  129. /*
  130. * Create a new connection object. Returns a pointer to the object, or
  131. * NULL if an error occurs.
  132. */
  133. Repl_Connection *
  134. conn_new(Repl_Agmt *agmt)
  135. {
  136. Repl_Connection *rpc;
  137. rpc = (Repl_Connection *)slapi_ch_malloc(sizeof(repl_connection));
  138. if ((rpc->lock = PR_NewLock()) == NULL)
  139. {
  140. goto loser;
  141. }
  142. rpc->hostname = agmt_get_hostname(agmt);
  143. rpc->port = agmt_get_port(agmt);
  144. rpc->binddn = agmt_get_binddn(agmt);
  145. rpc->bindmethod = agmt_get_bindmethod(agmt);
  146. rpc->transport_flags = agmt_get_transport_flags(agmt);
  147. rpc->ld = NULL;
  148. rpc->state = STATE_DISCONNECTED;
  149. rpc->last_operation = CONN_NO_OPERATION;
  150. rpc->last_ldap_error = LDAP_SUCCESS;
  151. rpc->last_ldap_errmsg = NULL;
  152. rpc->supports_ldapv3 = -1;
  153. rpc->supports_ds40_repl = -1;
  154. rpc->supports_ds50_repl = -1;
  155. rpc->supports_ds71_repl = -1;
  156. rpc->supports_ds90_repl = -1;
  157. rpc->linger_active = PR_FALSE;
  158. rpc->delete_after_linger = PR_FALSE;
  159. rpc->linger_event = NULL;
  160. rpc->linger_time = DEFAULT_LINGER_TIME;
  161. rpc->status = STATUS_DISCONNECTED;
  162. rpc->agmt = agmt;
  163. rpc->refcnt = 1;
  164. rpc->timeout.tv_sec = agmt_get_timeout(agmt);
  165. rpc->timeout.tv_usec = 0;
  166. rpc->flag_agmt_changed = 0;
  167. rpc->plain = NULL;
  168. return rpc;
  169. loser:
  170. conn_delete(rpc);
  171. slapi_ch_free((void**)&rpc);
  172. return NULL;
  173. }
  174. /*
  175. * Return PR_TRUE if the connection is in the connected state
  176. */
  177. static PRBool
  178. conn_connected(Repl_Connection *conn)
  179. {
  180. PRBool return_value;
  181. PR_Lock(conn->lock);
  182. return_value = STATE_CONNECTED == conn->state;
  183. PR_Unlock(conn->lock);
  184. return return_value;
  185. }
  186. /*
  187. * Destroy a connection object.
  188. */
  189. static void
  190. conn_delete_internal(Repl_Connection *conn)
  191. {
  192. PR_ASSERT(NULL != conn);
  193. close_connection_internal(conn);
  194. /* slapi_ch_free accepts NULL pointer */
  195. slapi_ch_free((void **)&conn->hostname);
  196. slapi_ch_free((void **)&conn->binddn);
  197. slapi_ch_free((void **)&conn->plain);
  198. }
  199. /*
  200. * Used by CLEANALLRUV - free it all!
  201. */
  202. void
  203. conn_delete_internal_ext(Repl_Connection *conn)
  204. {
  205. conn_delete_internal(conn);
  206. PR_DestroyLock(conn->lock);
  207. slapi_ch_free((void **)&conn);
  208. }
  209. /*
  210. * Destroy a connection. It is an error to use the connection object
  211. * after conn_delete() has been called.
  212. */
  213. void
  214. conn_delete(Repl_Connection *conn)
  215. {
  216. PRBool destroy_it = PR_FALSE;
  217. PR_ASSERT(NULL != conn);
  218. PR_Lock(conn->lock);
  219. if (conn->linger_active)
  220. {
  221. if (slapi_eq_cancel(conn->linger_event) == 1)
  222. {
  223. /* Event was found and cancelled. Destroy the connection object. */
  224. PR_Unlock(conn->lock);
  225. destroy_it = PR_TRUE;
  226. }
  227. else
  228. {
  229. /*
  230. * The event wasn't found, but we think it's still active.
  231. * That means an event is in the process of being fired
  232. * off, so arrange for the event to destroy the object .
  233. */
  234. conn->delete_after_linger = PR_TRUE;
  235. PR_Unlock(conn->lock);
  236. }
  237. }
  238. if (destroy_it)
  239. {
  240. conn_delete_internal(conn);
  241. }
  242. }
  243. /*
  244. * Return the last operation type processed by the connection
  245. * object, and the LDAP error encountered.
  246. */
  247. void
  248. conn_get_error(Repl_Connection *conn, int *operation, int *error)
  249. {
  250. PR_Lock(conn->lock);
  251. *operation = conn->last_operation;
  252. *error = conn->last_ldap_error;
  253. PR_Unlock(conn->lock);
  254. }
  255. /*
  256. * Return the last operation type processed by the connection
  257. * object, and the LDAP error encountered.
  258. * Beware that the error string will only be in scope and valid
  259. * before the next operation result has been read from the connection
  260. * (so don't alias the pointer).
  261. */
  262. void
  263. conn_get_error_ex(Repl_Connection *conn, int *operation, int *error, char **error_string)
  264. {
  265. PR_Lock(conn->lock);
  266. *operation = conn->last_operation;
  267. *error = conn->last_ldap_error;
  268. *error_string = conn->last_ldap_errmsg;
  269. PR_Unlock(conn->lock);
  270. }
  271. /* Returns the result (asyncronously) from an opertation and also returns that operations message ID */
  272. /* The _ex version handles a bunch of parameters (retoidp et al) that were present in the original
  273. * sync operation functions, but were never actually used) */
  274. ConnResult
  275. conn_read_result_ex(Repl_Connection *conn, char **retoidp, struct berval **retdatap, LDAPControl ***returned_controls, int send_msgid, int *resp_msgid, int block)
  276. {
  277. LDAPMessage *res = NULL;
  278. int setlevel = 0;
  279. int rc = 0;
  280. int return_value = 0;
  281. LDAPControl **loc_returned_controls = NULL;
  282. struct timeval local_timeout = {0};
  283. time_t time_now = 0;
  284. time_t start_time = time( NULL );
  285. int backoff_time = 1;
  286. Slapi_Eq_Context eqctx = repl5_start_debug_timeout(&setlevel);
  287. /* Here, we want to not block inside ldap_result().
  288. * Reason is that blocking there will deadlock with a
  289. * concurrent sender. We send concurrently, and hence
  290. * blocking is not good : deadlock results.
  291. * So, instead, we call ldap_result() with a zero timeout.
  292. * This makes it do a non-blocking poll and return to us
  293. * if there's no data to read.
  294. * We can then handle our timeout here by sleeping and re-trying.
  295. * In order that we do pickup results reasonably quickly,
  296. * we implement a backoff algorithm for the sleep: if we
  297. * keep getting results quickly then we won't spend much time sleeping.
  298. */
  299. while (!slapi_is_shutting_down())
  300. {
  301. /* we have to make sure the update sending thread does not
  302. attempt to call conn_disconnect while we are reading
  303. results - so lock the conn while we get the results */
  304. PR_Lock(conn->lock);
  305. if ((STATE_CONNECTED != conn->state) || !conn->ld) {
  306. rc = -1;
  307. return_value = CONN_NOT_CONNECTED;
  308. PR_Unlock(conn->lock);
  309. break;
  310. }
  311. rc = ldap_result(conn->ld, send_msgid, 1, &local_timeout, &res);
  312. PR_Unlock(conn->lock);
  313. if (0 != rc)
  314. {
  315. /* Something other than a timeout happened */
  316. break;
  317. }
  318. if (block)
  319. {
  320. /* Did the connection's timeout expire ? */
  321. time_now = time( NULL );
  322. if (conn->timeout.tv_sec <= ( time_now - start_time ))
  323. {
  324. /* We timed out */
  325. rc = 0;
  326. break;
  327. }
  328. /* Otherwise we backoff */
  329. DS_Sleep(PR_MillisecondsToInterval(backoff_time));
  330. if (backoff_time < 1000)
  331. {
  332. backoff_time <<= 1;
  333. }
  334. } else
  335. {
  336. rc = 0;
  337. break;
  338. }
  339. }
  340. repl5_stop_debug_timeout(eqctx, &setlevel);
  341. PR_Lock(conn->lock);
  342. /* we have to check again since the connection may have
  343. been closed in the meantime
  344. acquire the lock for the rest of the function
  345. to protect against another attempt to close
  346. the conn while we are using it
  347. */
  348. if ((STATE_CONNECTED != conn->state) || !conn->ld) {
  349. rc = -1;
  350. return_value = CONN_NOT_CONNECTED;
  351. }
  352. if (0 == rc)
  353. {
  354. /* Timeout */
  355. rc = slapi_ldap_get_lderrno(conn->ld, NULL, NULL);
  356. conn->last_ldap_error = LDAP_TIMEOUT;
  357. return_value = CONN_TIMEOUT;
  358. }
  359. else if ((-1 == rc) && (CONN_NOT_CONNECTED == return_value))
  360. {
  361. /* must not access conn->ld if disconnected in another thread */
  362. /* the other thread that actually did the conn_disconnect() */
  363. /* will set the status and error info */
  364. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  365. "%s: Connection disconnected by another thread\n",
  366. agmt_get_long_name(conn->agmt));
  367. }
  368. else if (-1 == rc)
  369. {
  370. /* Error */
  371. char *s = NULL;
  372. rc = slapi_ldap_get_lderrno(conn->ld, NULL, &s);
  373. conn->last_ldap_errmsg = s;
  374. conn->last_ldap_error = rc;
  375. /* some errors will require a disconnect and retry the connection
  376. later */
  377. if (IS_DISCONNECT_ERROR(rc))
  378. {
  379. close_connection_internal(conn); /* we already have the lock */
  380. return_value = CONN_NOT_CONNECTED;
  381. }
  382. else
  383. {
  384. conn->status = STATUS_CONNECTED;
  385. return_value = CONN_OPERATION_FAILED;
  386. }
  387. }
  388. else
  389. {
  390. int err;
  391. char *errmsg = NULL;
  392. char **referrals = NULL;
  393. char *matched = NULL;
  394. if (resp_msgid)
  395. {
  396. *resp_msgid = ldap_msgid(res);
  397. }
  398. rc = ldap_parse_result(conn->ld, res, &err, &matched,
  399. &errmsg, &referrals, &loc_returned_controls,
  400. 0 /* Don't free the result */);
  401. if (IS_DISCONNECT_ERROR(rc))
  402. {
  403. conn->last_ldap_error = rc;
  404. close_connection_internal(conn); /* we already have the lock */
  405. return_value = CONN_NOT_CONNECTED;
  406. }
  407. else if (IS_DISCONNECT_ERROR(err))
  408. {
  409. conn->last_ldap_error = err;
  410. close_connection_internal(conn); /* we already have the lock */
  411. return_value = CONN_NOT_CONNECTED;
  412. }
  413. /* Got a result */
  414. if ((rc == LDAP_SUCCESS) && (err == LDAP_BUSY))
  415. return_value = CONN_BUSY;
  416. else if (retoidp)
  417. {
  418. if (!((rc == LDAP_SUCCESS) && (err == LDAP_BUSY)))
  419. {
  420. if (rc == LDAP_SUCCESS) {
  421. rc = ldap_parse_extended_result(conn->ld, res, retoidp,
  422. retdatap, 0 /* Don't Free it */);
  423. }
  424. conn->last_ldap_error = rc;
  425. return_value = (LDAP_SUCCESS == conn->last_ldap_error ?
  426. CONN_OPERATION_SUCCESS : CONN_OPERATION_FAILED);
  427. }
  428. }
  429. else /* regular operation, result returned */
  430. {
  431. if (NULL != returned_controls)
  432. {
  433. *returned_controls = loc_returned_controls;
  434. }
  435. if (LDAP_SUCCESS != rc)
  436. {
  437. conn->last_ldap_error = rc;
  438. }
  439. else
  440. {
  441. conn->last_ldap_error = err;
  442. }
  443. return_value = LDAP_SUCCESS == conn->last_ldap_error ? CONN_OPERATION_SUCCESS : CONN_OPERATION_FAILED;
  444. }
  445. /*
  446. * XXXggood do I need to free matched, referrals,
  447. * anything else? Or can I pass NULL for the args
  448. * I'm not interested in?
  449. */
  450. /* Good question! Meanwhile, as RTM aproaches, let's free them... */
  451. slapi_ch_free((void **) &errmsg);
  452. slapi_ch_free((void **) &matched);
  453. charray_free(referrals);
  454. conn->status = STATUS_CONNECTED;
  455. }
  456. if (res) ldap_msgfree(res);
  457. PR_Unlock(conn->lock); /* release the conn lock */
  458. return return_value;
  459. }
  460. ConnResult
  461. conn_read_result(Repl_Connection *conn, int *message_id)
  462. {
  463. return conn_read_result_ex(conn,NULL,NULL,NULL,LDAP_RES_ANY,message_id,1);
  464. }
  465. /* Because the SDK isn't really thread-safe (it can deadlock between
  466. * a thread sending an operation and a thread trying to retrieve a response
  467. * on the same connection), we need to _first_ verify that the connection
  468. * is writable. If it isn't, we can deadlock if we proceed any further...
  469. */
  470. #if defined(USE_OPENLDAP)
  471. /* openldap has LBER_SB_OPT_DATA_READY but that doesn't really
  472. work for our purposes - so we grab the openldap fd from the
  473. ber sockbuf layer, import it into a PR Poll FD, then
  474. do the poll
  475. */
  476. static ConnResult
  477. see_if_write_available(Repl_Connection *conn, PRIntervalTime timeout)
  478. {
  479. PRFileDesc *pollfd = NULL;
  480. PRPollDesc polldesc;
  481. ber_socket_t fd = 0;
  482. int rc;
  483. /* get the sockbuf */
  484. if ((ldap_get_option(conn->ld, LDAP_OPT_DESC, &fd) != LDAP_OPT_SUCCESS) || (fd <= 0)) {
  485. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  486. "%s: invalid connection insee_if_write_available \n",
  487. agmt_get_long_name(conn->agmt));
  488. conn->last_ldap_error = LDAP_PARAM_ERROR;
  489. return CONN_OPERATION_FAILED;
  490. }
  491. /* wrap the sockbuf fd with a NSPR FD created especially
  492. for use with polling, and only with polling */
  493. pollfd = PR_CreateSocketPollFd(fd);
  494. polldesc.fd = pollfd;
  495. polldesc.in_flags = PR_POLL_WRITE|PR_POLL_EXCEPT;
  496. polldesc.out_flags = 0;
  497. /* do the poll */
  498. rc = PR_Poll(&polldesc, 1, timeout);
  499. /* unwrap the socket */
  500. PR_DestroySocketPollFd(pollfd);
  501. /* check */
  502. if (rc == 0) { /* timeout */
  503. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  504. "%s: poll timed out - poll interval [%d]\n",
  505. agmt_get_long_name(conn->agmt),
  506. timeout);
  507. return CONN_TIMEOUT;
  508. } else if ((rc < 0) || (!(polldesc.out_flags&PR_POLL_WRITE))) { /* error */
  509. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  510. "%s: error during poll attempt [%d:%s]\n",
  511. agmt_get_long_name(conn->agmt),
  512. PR_GetError(), slapd_pr_strerror(PR_GetError()));
  513. conn->last_ldap_error = LDAP_PARAM_ERROR;
  514. return CONN_OPERATION_FAILED;
  515. }
  516. return CONN_OPERATION_SUCCESS;
  517. }
  518. #else /* ! USE_OPENLDAP */
  519. /* Since we're poking around with ldap c sdk internals, we have to
  520. be careful since the PR layer stores different session and socket
  521. info than the NSS SSL layer than the SASL layer - and they all
  522. use different poll functions too
  523. */
  524. static ConnResult
  525. see_if_write_available(Repl_Connection *conn, PRIntervalTime timeout)
  526. {
  527. LDAP_X_PollFD pollstr;
  528. int nfds = 1;
  529. struct ldap_x_ext_io_fns iofns;
  530. int rc = LDAP_SUCCESS;
  531. LDAP_X_EXTIOF_POLL_CALLBACK *ldap_poll;
  532. struct lextiof_session_private *private;
  533. /* get the poll function to use */
  534. memset(&iofns, 0, sizeof(iofns));
  535. iofns.lextiof_size = LDAP_X_EXTIO_FNS_SIZE;
  536. if (ldap_get_option(conn->ld, LDAP_X_OPT_EXTIO_FN_PTRS, &iofns) < 0) {
  537. rc = slapi_ldap_get_lderrno(conn->ld, NULL, NULL);
  538. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  539. "%s: Failed call to ldap_get_option to get extiofns in "
  540. "see_if_write_available: LDAP error %d (%s)\n",
  541. agmt_get_long_name(conn->agmt),
  542. rc, ldap_err2string(rc));
  543. conn->last_ldap_error = rc;
  544. return CONN_OPERATION_FAILED;
  545. }
  546. ldap_poll = iofns.lextiof_poll;
  547. /* set up the poll structure */
  548. if (ldap_get_option(conn->ld, LDAP_OPT_DESC, &pollstr.lpoll_fd) < 0) {
  549. rc = slapi_ldap_get_lderrno(conn->ld, NULL, NULL);
  550. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  551. "%s: Failed call to ldap_get_option for poll_fd in "
  552. "see_if_write_available: LDAP error %d (%s)\n",
  553. agmt_get_long_name(conn->agmt),
  554. rc, ldap_err2string(rc));
  555. conn->last_ldap_error = rc;
  556. return CONN_OPERATION_FAILED;
  557. }
  558. if (ldap_get_option(conn->ld, LDAP_X_OPT_SOCKETARG,
  559. &pollstr.lpoll_socketarg) < 0) {
  560. rc = slapi_ldap_get_lderrno(conn->ld, NULL, NULL);
  561. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  562. "%s: Failed call to ldap_get_option for socketarg in "
  563. "see_if_write_available: LDAP error %d (%s)\n",
  564. agmt_get_long_name(conn->agmt),
  565. rc, ldap_err2string(rc));
  566. conn->last_ldap_error = rc;
  567. return CONN_OPERATION_FAILED;
  568. }
  569. pollstr.lpoll_events = LDAP_X_POLLOUT;
  570. pollstr.lpoll_revents = 0;
  571. private = iofns.lextiof_session_arg;
  572. if (0 == (*ldap_poll)(&pollstr, nfds, timeout, private)) {
  573. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  574. "%s: poll timed out - poll interval [%d]\n",
  575. agmt_get_long_name(conn->agmt),
  576. timeout);
  577. return CONN_TIMEOUT;
  578. }
  579. return CONN_OPERATION_SUCCESS;
  580. }
  581. #endif /* ! USE_OPENLDAP */
  582. /*
  583. * Common code to send an LDAPv3 operation and collect the result.
  584. * Return values:
  585. * CONN_OPERATION_SUCCESS - the operation succeeded
  586. * CONN_OPERATION_FAILED - the operation was sent to the consumer
  587. * and failed. Use conn_get_error() to determine the LDAP error
  588. * code.
  589. * CONN_NOT_CONNECTED - no connection is active. The caller should
  590. * use conn_connect() to connect to the replica and bind, then should
  591. * reacquire the replica (if needed).
  592. * CONN_BUSY - the server is busy with previous requests, must wait for a while
  593. * before retrying
  594. * DBDB: also returns the operation's message ID, if it was successfully sent, now that
  595. * we're reading results async.
  596. */
  597. static ConnResult
  598. perform_operation(Repl_Connection *conn, int optype, const char *dn,
  599. LDAPMod **attrs, const char *newrdn, const char *newparent,
  600. int deleteoldrdn, LDAPControl *update_control,
  601. const char *extop_oid, struct berval *extop_payload, int *message_id)
  602. {
  603. int rc = -1;
  604. ConnResult return_value = CONN_OPERATION_FAILED;
  605. LDAPControl *server_controls[3];
  606. /* LDAPControl **loc_returned_controls; */
  607. const char *op_string = NULL;
  608. int msgid = 0;
  609. server_controls[0] = &manageDSAITControl;
  610. server_controls[1] = update_control;
  611. server_controls[2] = NULL;
  612. /* lock the conn to prevent the result reader thread
  613. from closing the connection out from under us */
  614. PR_Lock(conn->lock);
  615. if (STATE_CONNECTED == conn->state)
  616. {
  617. int setlevel = 0;
  618. Slapi_Eq_Context eqctx = repl5_start_debug_timeout(&setlevel);
  619. return_value = see_if_write_available(
  620. conn, PR_SecondsToInterval(conn->timeout.tv_sec));
  621. if (return_value != CONN_OPERATION_SUCCESS) {
  622. PR_Unlock(conn->lock);
  623. return return_value;
  624. }
  625. conn->last_operation = optype;
  626. switch (optype)
  627. {
  628. case CONN_ADD:
  629. conn->status = STATUS_PROCESSING_ADD;
  630. op_string = "add";
  631. rc = ldap_add_ext(conn->ld, dn, attrs, server_controls,
  632. NULL /* clientctls */, &msgid);
  633. break;
  634. case CONN_MODIFY:
  635. conn->status = STATUS_PROCESSING_MODIFY;
  636. op_string = "modify";
  637. rc = ldap_modify_ext(conn->ld, dn, attrs, server_controls,
  638. NULL /* clientctls */, &msgid);
  639. break;
  640. case CONN_DELETE:
  641. conn->status = STATUS_PROCESSING_DELETE;
  642. op_string = "delete";
  643. rc = ldap_delete_ext(conn->ld, dn, server_controls,
  644. NULL /* clientctls */, &msgid);
  645. break;
  646. case CONN_RENAME:
  647. conn->status = STATUS_PROCESSING_RENAME;
  648. op_string = "rename";
  649. rc = ldap_rename(conn->ld, dn, newrdn, newparent, deleteoldrdn,
  650. server_controls, NULL /* clientctls */, &msgid);
  651. break;
  652. case CONN_EXTENDED_OPERATION:
  653. conn->status = STATUS_PROCESSING_EXTENDED_OPERATION;
  654. op_string = "extended";
  655. rc = ldap_extended_operation(conn->ld, extop_oid, extop_payload,
  656. server_controls, NULL /* clientctls */, &msgid);
  657. }
  658. repl5_stop_debug_timeout(eqctx, &setlevel);
  659. if (LDAP_SUCCESS == rc)
  660. {
  661. /* DBDB: The code that used to be here has been moved for async operation
  662. * Results are now picked up in another thread. All we need to do here is
  663. * queue the operation details in the outstanding operation list.
  664. */
  665. return_value = CONN_OPERATION_SUCCESS;
  666. }
  667. else
  668. {
  669. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  670. "%s: Failed to send %s operation: LDAP error %d (%s)\n",
  671. agmt_get_long_name(conn->agmt),
  672. op_string ? op_string : "NULL", rc, ldap_err2string(rc));
  673. conn->last_ldap_error = rc;
  674. if (IS_DISCONNECT_ERROR(rc))
  675. {
  676. close_connection_internal(conn); /* already have the lock */
  677. return_value = CONN_NOT_CONNECTED;
  678. }
  679. else
  680. {
  681. conn->status = STATUS_CONNECTED;
  682. return_value = CONN_OPERATION_FAILED;
  683. }
  684. }
  685. }
  686. else
  687. {
  688. /* conn->last_ldap_error has been set to a more specific value
  689. * in the thread that did the disconnection
  690. * conn->last_ldap_error = LDAP_SERVER_DOWN;
  691. */
  692. return_value = CONN_NOT_CONNECTED;
  693. }
  694. PR_Unlock(conn->lock); /* release the lock */
  695. if (message_id)
  696. {
  697. *message_id = msgid;
  698. }
  699. return return_value;
  700. }
  701. /*
  702. * Send an LDAP add operation.
  703. */
  704. ConnResult
  705. conn_send_add(Repl_Connection *conn, const char *dn, LDAPMod **attrs,
  706. LDAPControl *update_control, int *message_id)
  707. {
  708. return perform_operation(conn, CONN_ADD, dn, attrs, NULL /* newrdn */,
  709. NULL /* newparent */, 0 /* deleteoldrdn */, update_control,
  710. NULL /* extop OID */, NULL /* extop payload */, message_id);
  711. }
  712. /*
  713. * Send an LDAP delete operation.
  714. */
  715. ConnResult
  716. conn_send_delete(Repl_Connection *conn, const char *dn,
  717. LDAPControl *update_control, int *message_id)
  718. {
  719. return perform_operation(conn, CONN_DELETE, dn, NULL /* attrs */,
  720. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  721. update_control, NULL /* extop OID */, NULL /* extop payload */, message_id);
  722. }
  723. /*
  724. * Send an LDAP modify operation.
  725. */
  726. ConnResult
  727. conn_send_modify(Repl_Connection *conn, const char *dn, LDAPMod **mods,
  728. LDAPControl *update_control, int *message_id)
  729. {
  730. return perform_operation(conn, CONN_MODIFY, dn, mods, NULL /* newrdn */,
  731. NULL /* newparent */, 0 /* deleteoldrdn */, update_control,
  732. NULL /* extop OID */, NULL /* extop payload */, message_id);
  733. }
  734. /*
  735. * Send an LDAP moddn operation.
  736. */
  737. ConnResult
  738. conn_send_rename(Repl_Connection *conn, const char *dn,
  739. const char *newrdn, const char *newparent, int deleteoldrdn,
  740. LDAPControl *update_control, int *message_id)
  741. {
  742. return perform_operation(conn, CONN_RENAME, dn, NULL /* attrs */,
  743. newrdn, newparent, deleteoldrdn, update_control,
  744. NULL /* extop OID */, NULL /* extop payload */, message_id);
  745. }
  746. /*
  747. * Send an LDAP extended operation.
  748. */
  749. ConnResult
  750. conn_send_extended_operation(Repl_Connection *conn, const char *extop_oid,
  751. struct berval *payload,
  752. LDAPControl *update_control, int *message_id)
  753. {
  754. return perform_operation(conn, CONN_EXTENDED_OPERATION, NULL /* dn */, NULL /* attrs */,
  755. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  756. update_control, extop_oid, payload, message_id);
  757. }
  758. /*
  759. * Synchronously read an entry and return a specific attribute's values.
  760. * Returns CONN_OPERATION_SUCCESS if successful. Returns
  761. * CONN_OPERATION_FAILED if the operation was sent but an LDAP error
  762. * occurred (conn->last_ldap_error is set in this case), and
  763. * CONN_NOT_CONNECTED if no connection was active.
  764. *
  765. * The caller must free the returned_bvals.
  766. */
  767. ConnResult
  768. conn_read_entry_attribute(Repl_Connection *conn, const char *dn,
  769. char *type, struct berval ***returned_bvals)
  770. {
  771. ConnResult return_value;
  772. int ldap_rc;
  773. LDAPControl *server_controls[2];
  774. LDAPMessage *res = NULL;
  775. char *attrs[2];
  776. PR_ASSERT(NULL != type);
  777. if (conn_connected(conn))
  778. {
  779. server_controls[0] = &manageDSAITControl;
  780. server_controls[1] = NULL;
  781. attrs[0] = type;
  782. attrs[1] = NULL;
  783. ldap_rc = ldap_search_ext_s(conn->ld, dn, LDAP_SCOPE_BASE,
  784. "(objectclass=*)", attrs, 0 /* attrsonly */,
  785. server_controls, NULL /* client controls */,
  786. &conn->timeout, 0 /* sizelimit */, &res);
  787. if (LDAP_SUCCESS == ldap_rc)
  788. {
  789. LDAPMessage *entry = ldap_first_entry(conn->ld, res);
  790. if (NULL != entry)
  791. {
  792. *returned_bvals = ldap_get_values_len(conn->ld, entry, type);
  793. }
  794. return_value = CONN_OPERATION_SUCCESS;
  795. }
  796. else if (IS_DISCONNECT_ERROR(ldap_rc))
  797. {
  798. conn_disconnect(conn);
  799. return_value = CONN_NOT_CONNECTED;
  800. }
  801. else
  802. {
  803. return_value = CONN_OPERATION_FAILED;
  804. }
  805. conn->last_ldap_error = ldap_rc;
  806. if (NULL != res)
  807. {
  808. ldap_msgfree(res);
  809. res = NULL;
  810. }
  811. }
  812. else
  813. {
  814. return_value = CONN_NOT_CONNECTED;
  815. }
  816. return return_value;
  817. }
  818. /*
  819. * Return an pointer to a string describing the connection's status.
  820. */
  821. const char *
  822. conn_get_status(Repl_Connection *conn)
  823. {
  824. return conn->status;
  825. }
  826. /*
  827. * Cancel any outstanding linger timer. Should be called when
  828. * a replication session is beginning.
  829. */
  830. void
  831. conn_cancel_linger(Repl_Connection *conn)
  832. {
  833. PR_ASSERT(NULL != conn);
  834. PR_Lock(conn->lock);
  835. if (conn->linger_active)
  836. {
  837. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  838. "%s: Cancelling linger on the connection\n",
  839. agmt_get_long_name(conn->agmt));
  840. conn->linger_active = PR_FALSE;
  841. if (slapi_eq_cancel(conn->linger_event) == 1)
  842. {
  843. conn->refcnt--;
  844. }
  845. conn->linger_event = NULL;
  846. conn->status = STATUS_CONNECTED;
  847. }
  848. else
  849. {
  850. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  851. "%s: No linger to cancel on the connection\n",
  852. agmt_get_long_name(conn->agmt));
  853. }
  854. PR_Unlock(conn->lock);
  855. }
  856. /*
  857. * Called when our linger timeout timer expires. This means
  858. * we should check to see if perhaps the connection's become
  859. * active again, in which case we do nothing. Otherwise,
  860. * we close the connection.
  861. */
  862. static void
  863. linger_timeout(time_t event_time, void *arg)
  864. {
  865. PRBool delete_now;
  866. Repl_Connection *conn = (Repl_Connection *)arg;
  867. PR_ASSERT(NULL != conn);
  868. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  869. "%s: Linger timeout has expired on the connection\n",
  870. agmt_get_long_name(conn->agmt));
  871. PR_Lock(conn->lock);
  872. if (conn->linger_active)
  873. {
  874. conn->linger_active = PR_FALSE;
  875. conn->linger_event = NULL;
  876. close_connection_internal(conn);
  877. }
  878. delete_now = conn->delete_after_linger;
  879. PR_Unlock(conn->lock);
  880. if (delete_now)
  881. {
  882. conn_delete_internal(conn);
  883. }
  884. }
  885. /*
  886. * Indicate that a session is ending. The linger timer starts when
  887. * this function is called.
  888. */
  889. void
  890. conn_start_linger(Repl_Connection *conn)
  891. {
  892. time_t now;
  893. PR_ASSERT(NULL != conn);
  894. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  895. "%s: Beginning linger on the connection\n",
  896. agmt_get_long_name(conn->agmt));
  897. if (!conn_connected(conn))
  898. {
  899. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  900. "%s: No linger on the closed conn\n",
  901. agmt_get_long_name(conn->agmt));
  902. return;
  903. }
  904. time(&now);
  905. PR_Lock(conn->lock);
  906. if (conn->linger_active)
  907. {
  908. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  909. "%s: Linger already active on the connection\n",
  910. agmt_get_long_name(conn->agmt));
  911. }
  912. else
  913. {
  914. conn->linger_active = PR_TRUE;
  915. conn->linger_event = slapi_eq_once(linger_timeout, conn, now + conn->linger_time);
  916. conn->status = STATUS_LINGERING;
  917. }
  918. PR_Unlock(conn->lock);
  919. }
  920. /*
  921. * If no connection is currently active, opens a connection and binds to
  922. * the remote server. If a connection is open (e.g. lingering) then
  923. * this is a no-op.
  924. *
  925. * Returns CONN_OPERATION_SUCCESS on success, or CONN_OPERATION_FAILED
  926. * on failure. Sets conn->last_ldap_error and conn->last_operation;
  927. */
  928. ConnResult
  929. conn_connect(Repl_Connection *conn)
  930. {
  931. int optdata;
  932. int secure = 0;
  933. char* binddn = NULL;
  934. struct berval *creds = NULL;
  935. ConnResult return_value = CONN_OPERATION_SUCCESS;
  936. int pw_ret = 1;
  937. /** Connection already open just return SUCCESS **/
  938. if(conn->state == STATE_CONNECTED) goto done;
  939. PR_Lock(conn->lock);
  940. if (conn->flag_agmt_changed) {
  941. /* So far we cannot change Hostname and Port */
  942. /* slapi_ch_free((void **)&conn->hostname); */
  943. /* conn->hostname = agmt_get_hostname(conn->agmt); */
  944. /* conn->port = agmt_get_port(conn->agmt); */
  945. slapi_ch_free((void **)&conn->binddn);
  946. conn->binddn = agmt_get_binddn(conn->agmt);
  947. conn->bindmethod = agmt_get_bindmethod(conn->agmt);
  948. conn->transport_flags = agmt_get_transport_flags(conn->agmt);
  949. conn->timeout.tv_sec = agmt_get_timeout(conn->agmt);
  950. conn->flag_agmt_changed = 0;
  951. conn->port = agmt_get_port(conn->agmt); /* port could be updated */
  952. slapi_ch_free((void **)&conn->plain);
  953. }
  954. PR_Unlock(conn->lock);
  955. creds = agmt_get_credentials(conn->agmt);
  956. if (conn->plain == NULL) {
  957. char *plain = NULL;
  958. /* kexcoff: for reversible encryption */
  959. /* We need to test the return code of pw_rever_decode in order to decide
  960. * if a free for plain will be needed (pw_ret == 0) or not (pw_ret != 0) */
  961. pw_ret = pw_rever_decode(creds->bv_val, &plain, type_nsds5ReplicaCredentials);
  962. /* Pb occured in decryption: stop now, binding will fail */
  963. if ( pw_ret == -1 )
  964. {
  965. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  966. "%s: Decoding of the credentials failed.\n",
  967. agmt_get_long_name(conn->agmt));
  968. return_value = CONN_OPERATION_FAILED;
  969. conn->last_ldap_error = LDAP_INVALID_CREDENTIALS;
  970. conn->state = STATE_DISCONNECTED;
  971. goto done;
  972. } /* Else, does not mean that the plain is correct, only means the we had no internal
  973. decoding pb */
  974. conn->plain = slapi_ch_strdup (plain);
  975. if (!pw_ret) slapi_ch_free((void**)&plain);
  976. }
  977. /* ugaston: if SSL has been selected in the replication agreement, SSL client
  978. * initialisation should be done before ever trying to open any connection at all.
  979. */
  980. if (conn->transport_flags == TRANSPORT_FLAG_TLS) {
  981. secure = 2;
  982. } else if (conn->transport_flags == TRANSPORT_FLAG_SSL) {
  983. secure = 1;
  984. }
  985. if (secure > 0) {
  986. if (!NSS_IsInitialized()) {
  987. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  988. "%s: SSL Not Initialized, Replication over SSL FAILED\n",
  989. agmt_get_long_name(conn->agmt));
  990. conn->last_ldap_error = LDAP_INAPPROPRIATE_AUTH;
  991. conn->last_operation = CONN_INIT;
  992. return_value = CONN_SSL_NOT_ENABLED;
  993. goto done;
  994. }
  995. }
  996. if (return_value == CONN_OPERATION_SUCCESS) {
  997. #if !defined(USE_OPENLDAP)
  998. int io_timeout_ms;
  999. #endif
  1000. /* Now we initialize the LDAP Structure and set options */
  1001. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1002. "%s: Trying %s%s slapi_ldap_init_ext\n",
  1003. agmt_get_long_name(conn->agmt),
  1004. secure ? "secure" : "non-secure",
  1005. (secure == 2) ? " startTLS" : "");
  1006. /* shared = 1 because we will read results from a second thread */
  1007. if (conn->ld) {
  1008. /* Since we call slapi_ldap_init, we must call slapi_ldap_unbind */
  1009. /* ldap_unbind internally calls ldap_ld_free */
  1010. slapi_ldap_unbind(conn->ld);
  1011. }
  1012. conn->ld = slapi_ldap_init_ext(NULL, conn->hostname, conn->port, secure, 1, NULL);
  1013. if (NULL == conn->ld)
  1014. {
  1015. return_value = CONN_OPERATION_FAILED;
  1016. conn->state = STATE_DISCONNECTED;
  1017. conn->last_operation = CONN_INIT;
  1018. conn->last_ldap_error = LDAP_LOCAL_ERROR;
  1019. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1020. "%s: Failed to establish %s%sconnection to the consumer\n",
  1021. agmt_get_long_name(conn->agmt),
  1022. secure ? "secure " : "",
  1023. (secure == 2) ? "startTLS " : "");
  1024. goto done;
  1025. }
  1026. /* slapi_ch_strdup is OK with NULL strings */
  1027. binddn = slapi_ch_strdup(conn->binddn);
  1028. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1029. "%s: binddn = %s, passwd = %s\n",
  1030. agmt_get_long_name(conn->agmt),
  1031. binddn?binddn:"NULL", creds->bv_val?creds->bv_val:"NULL");
  1032. /* Set some options for the connection. */
  1033. optdata = LDAP_DEREF_NEVER; /* Don't dereference aliases */
  1034. ldap_set_option(conn->ld, LDAP_OPT_DEREF, &optdata);
  1035. optdata = LDAP_VERSION3; /* We need LDAP version 3 */
  1036. ldap_set_option(conn->ld, LDAP_OPT_PROTOCOL_VERSION, &optdata);
  1037. /* Don't chase any referrals (although we shouldn't get any) */
  1038. ldap_set_option(conn->ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
  1039. /* override the default timeout with the specified timeout */
  1040. #if defined(USE_OPENLDAP)
  1041. ldap_set_option(conn->ld, LDAP_OPT_NETWORK_TIMEOUT, &conn->timeout);
  1042. #else
  1043. io_timeout_ms = conn->timeout.tv_sec * 1000 + conn->timeout.tv_usec / 1000;
  1044. prldap_set_session_option(conn->ld, NULL, PRLDAP_OPT_IO_MAX_TIMEOUT,
  1045. io_timeout_ms);
  1046. #endif
  1047. /* We've got an ld. Now bind to the server. */
  1048. conn->last_operation = CONN_BIND;
  1049. }
  1050. if ( bind_and_check_pwp(conn, binddn, conn->plain) == CONN_OPERATION_FAILED )
  1051. {
  1052. conn->last_ldap_error = slapi_ldap_get_lderrno (conn->ld, NULL, NULL);
  1053. conn->state = STATE_DISCONNECTED;
  1054. return_value = CONN_OPERATION_FAILED;
  1055. }
  1056. else
  1057. {
  1058. conn->last_ldap_error = LDAP_SUCCESS;
  1059. conn->state = STATE_CONNECTED;
  1060. return_value = CONN_OPERATION_SUCCESS;
  1061. }
  1062. done:
  1063. ber_bvfree(creds);
  1064. creds = NULL;
  1065. slapi_ch_free((void**)&binddn);
  1066. if(return_value == CONN_OPERATION_SUCCESS)
  1067. {
  1068. conn->last_ldap_error = LDAP_SUCCESS;
  1069. conn->state = STATE_CONNECTED;
  1070. } else
  1071. {
  1072. close_connection_internal(conn);
  1073. }
  1074. return return_value;
  1075. }
  1076. static void
  1077. close_connection_internal(Repl_Connection *conn)
  1078. {
  1079. conn->state = STATE_DISCONNECTED;
  1080. conn->status = STATUS_DISCONNECTED;
  1081. conn->supports_ds50_repl = -1;
  1082. conn->supports_ds71_repl = -1;
  1083. conn->supports_ds90_repl = -1;
  1084. /* do this last, to minimize the chance that another thread
  1085. might read conn->state as not disconnected and attempt
  1086. to use conn->ld */
  1087. if (NULL != conn->ld)
  1088. {
  1089. /* Since we call slapi_ldap_init, we must call slapi_ldap_unbind */
  1090. slapi_ldap_unbind(conn->ld);
  1091. }
  1092. conn->ld = NULL;
  1093. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1094. "%s: Disconnected from the consumer\n", agmt_get_long_name(conn->agmt));
  1095. }
  1096. void
  1097. conn_disconnect(Repl_Connection *conn)
  1098. {
  1099. PR_ASSERT(NULL != conn);
  1100. PR_Lock(conn->lock);
  1101. close_connection_internal(conn);
  1102. PR_Unlock(conn->lock);
  1103. }
  1104. /*
  1105. * Determine if the remote replica supports DS 5.0 replication.
  1106. * Return codes:
  1107. * CONN_SUPPORTS_DS5_REPL - the remote replica suport DS5 replication
  1108. * CONN_DOES_NOT_SUPPORT_DS5_REPL - the remote replica does not
  1109. * support DS5 replication.
  1110. * CONN_OPERATION_FAILED - it could not be determined if the remote
  1111. * replica supports DS5 replication.
  1112. * CONN_NOT_CONNECTED - no connection was active.
  1113. */
  1114. ConnResult
  1115. conn_replica_supports_ds5_repl(Repl_Connection *conn)
  1116. {
  1117. ConnResult return_value;
  1118. int ldap_rc;
  1119. if (conn_connected(conn))
  1120. {
  1121. if (conn->supports_ds50_repl == -1) {
  1122. LDAPMessage *res = NULL;
  1123. LDAPMessage *entry = NULL;
  1124. char *attrs[] = {"supportedcontrol", "supportedextension", NULL};
  1125. conn->status = STATUS_SEARCHING;
  1126. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1127. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1128. NULL /* server controls */, NULL /* client controls */,
  1129. &conn->timeout, LDAP_NO_LIMIT, &res);
  1130. if (LDAP_SUCCESS == ldap_rc)
  1131. {
  1132. conn->supports_ds50_repl = 0;
  1133. entry = ldap_first_entry(conn->ld, res);
  1134. if (!attribute_string_value_present(conn->ld, entry, "supportedcontrol", REPL_NSDS50_UPDATE_INFO_CONTROL_OID))
  1135. {
  1136. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1137. }
  1138. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_START_NSDS50_REPLICATION_REQUEST_OID))
  1139. {
  1140. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1141. }
  1142. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_END_NSDS50_REPLICATION_REQUEST_OID))
  1143. {
  1144. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1145. }
  1146. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_ENTRY_REQUEST_OID))
  1147. {
  1148. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1149. }
  1150. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_RESPONSE_OID))
  1151. {
  1152. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1153. }
  1154. else
  1155. {
  1156. conn->supports_ds50_repl = 1;
  1157. return_value = CONN_SUPPORTS_DS5_REPL;
  1158. }
  1159. }
  1160. else
  1161. {
  1162. if (IS_DISCONNECT_ERROR(ldap_rc))
  1163. {
  1164. conn->last_ldap_error = ldap_rc; /* specific reason */
  1165. conn_disconnect(conn);
  1166. return_value = CONN_NOT_CONNECTED;
  1167. }
  1168. else
  1169. {
  1170. return_value = CONN_OPERATION_FAILED;
  1171. }
  1172. }
  1173. if (NULL != res)
  1174. ldap_msgfree(res);
  1175. }
  1176. else {
  1177. return_value = conn->supports_ds50_repl ? CONN_SUPPORTS_DS5_REPL : CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1178. }
  1179. }
  1180. else
  1181. {
  1182. /* Not connected */
  1183. return_value = CONN_NOT_CONNECTED;
  1184. }
  1185. return return_value;
  1186. }
  1187. /*
  1188. * Determine if the remote replica supports DS 7.1 replication.
  1189. * Return codes:
  1190. * CONN_SUPPORTS_DS71_REPL - the remote replica suport DS7.1 replication
  1191. * CONN_DOES_NOT_SUPPORT_DS71_REPL - the remote replica does not
  1192. * support DS7.1 replication.
  1193. * CONN_OPERATION_FAILED - it could not be determined if the remote
  1194. * replica supports DS5 replication.
  1195. * CONN_NOT_CONNECTED - no connection was active.
  1196. */
  1197. ConnResult
  1198. conn_replica_supports_ds71_repl(Repl_Connection *conn)
  1199. {
  1200. ConnResult return_value;
  1201. int ldap_rc;
  1202. if (conn_connected(conn))
  1203. {
  1204. if (conn->supports_ds71_repl == -1) {
  1205. LDAPMessage *res = NULL;
  1206. LDAPMessage *entry = NULL;
  1207. char *attrs[] = {"supportedcontrol", "supportedextension", NULL};
  1208. conn->status = STATUS_SEARCHING;
  1209. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1210. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1211. NULL /* server controls */, NULL /* client controls */,
  1212. &conn->timeout, LDAP_NO_LIMIT, &res);
  1213. if (LDAP_SUCCESS == ldap_rc)
  1214. {
  1215. conn->supports_ds71_repl = 0;
  1216. entry = ldap_first_entry(conn->ld, res);
  1217. if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS71_REPLICATION_ENTRY_REQUEST_OID))
  1218. {
  1219. return_value = CONN_DOES_NOT_SUPPORT_DS71_REPL;
  1220. }
  1221. else
  1222. {
  1223. conn->supports_ds71_repl = 1;
  1224. return_value = CONN_SUPPORTS_DS71_REPL;
  1225. }
  1226. }
  1227. else
  1228. {
  1229. if (IS_DISCONNECT_ERROR(ldap_rc))
  1230. {
  1231. conn->last_ldap_error = ldap_rc; /* specific reason */
  1232. conn_disconnect(conn);
  1233. return_value = CONN_NOT_CONNECTED;
  1234. }
  1235. else
  1236. {
  1237. return_value = CONN_OPERATION_FAILED;
  1238. }
  1239. }
  1240. if (NULL != res)
  1241. ldap_msgfree(res);
  1242. }
  1243. else {
  1244. return_value = conn->supports_ds71_repl ? CONN_SUPPORTS_DS71_REPL : CONN_DOES_NOT_SUPPORT_DS71_REPL;
  1245. }
  1246. }
  1247. else
  1248. {
  1249. /* Not connected */
  1250. return_value = CONN_NOT_CONNECTED;
  1251. }
  1252. return return_value;
  1253. }
  1254. /*
  1255. * Determine if the remote replica supports DS 9.0 replication.
  1256. * Return codes:
  1257. * CONN_SUPPORTS_DS90_REPL - the remote replica suport DS5 replication
  1258. * CONN_DOES_NOT_SUPPORT_DS90_REPL - the remote replica does not
  1259. * support DS9.0 replication.
  1260. * CONN_OPERATION_FAILED - it could not be determined if the remote
  1261. * replica supports DS9.0 replication.
  1262. * CONN_NOT_CONNECTED - no connection was active.
  1263. */
  1264. ConnResult
  1265. conn_replica_supports_ds90_repl(Repl_Connection *conn)
  1266. {
  1267. ConnResult return_value;
  1268. int ldap_rc;
  1269. if (conn_connected(conn))
  1270. {
  1271. if (conn->supports_ds90_repl == -1) {
  1272. LDAPMessage *res = NULL;
  1273. LDAPMessage *entry = NULL;
  1274. char *attrs[] = {"supportedcontrol", "supportedextension", NULL};
  1275. conn->status = STATUS_SEARCHING;
  1276. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1277. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1278. NULL /* server controls */, NULL /* client controls */,
  1279. &conn->timeout, LDAP_NO_LIMIT, &res);
  1280. if (LDAP_SUCCESS == ldap_rc)
  1281. {
  1282. conn->supports_ds90_repl = 0;
  1283. entry = ldap_first_entry(conn->ld, res);
  1284. if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_START_NSDS90_REPLICATION_REQUEST_OID))
  1285. {
  1286. return_value = CONN_DOES_NOT_SUPPORT_DS90_REPL;
  1287. }
  1288. else
  1289. {
  1290. conn->supports_ds90_repl = 1;
  1291. return_value = CONN_SUPPORTS_DS90_REPL;
  1292. }
  1293. }
  1294. else
  1295. {
  1296. if (IS_DISCONNECT_ERROR(ldap_rc))
  1297. {
  1298. conn->last_ldap_error = ldap_rc; /* specific reason */
  1299. conn_disconnect(conn);
  1300. return_value = CONN_NOT_CONNECTED;
  1301. }
  1302. else
  1303. {
  1304. return_value = CONN_OPERATION_FAILED;
  1305. }
  1306. }
  1307. if (NULL != res)
  1308. ldap_msgfree(res);
  1309. }
  1310. else
  1311. {
  1312. return_value = conn->supports_ds90_repl ? CONN_SUPPORTS_DS90_REPL : CONN_DOES_NOT_SUPPORT_DS90_REPL;
  1313. }
  1314. }
  1315. else
  1316. {
  1317. /* Not connected */
  1318. return_value = CONN_NOT_CONNECTED;
  1319. }
  1320. return return_value;
  1321. }
  1322. /* Determine if the replica is read-only */
  1323. ConnResult
  1324. conn_replica_is_readonly(Repl_Connection *conn)
  1325. {
  1326. ReplicaId rid = agmt_get_consumer_rid( (Repl_Agmt *) conn->agmt, conn );
  1327. if (rid == READ_ONLY_REPLICA_ID)
  1328. {
  1329. return CONN_IS_READONLY;
  1330. } else
  1331. {
  1332. return CONN_IS_NOT_READONLY;
  1333. }
  1334. }
  1335. /*
  1336. * Return 1 if "value" is a value of attribute type "type" in entry "entry".
  1337. * Otherwise, return 0.
  1338. */
  1339. static int
  1340. attribute_string_value_present(LDAP *ld, LDAPMessage *entry, const char *type,
  1341. const char *value)
  1342. {
  1343. int return_value = 0;
  1344. ber_len_t vallen;
  1345. if (NULL != entry)
  1346. {
  1347. char *atype = NULL;
  1348. BerElement *ber = NULL;
  1349. vallen = strlen(value);
  1350. atype = ldap_first_attribute(ld, entry, &ber);
  1351. while (NULL != atype && 0 == return_value)
  1352. {
  1353. if (strcasecmp(atype, type) == 0)
  1354. {
  1355. struct berval **vals = ldap_get_values_len(ld, entry, atype);
  1356. int i;
  1357. for (i = 0; return_value == 0 && NULL != vals && NULL != vals[i]; i++)
  1358. {
  1359. if ((vallen == vals[i]->bv_len) && !strncmp(vals[i]->bv_val, value, vallen))
  1360. {
  1361. return_value = 1;
  1362. }
  1363. }
  1364. if (NULL != vals)
  1365. {
  1366. ldap_value_free_len(vals);
  1367. }
  1368. }
  1369. ldap_memfree(atype);
  1370. atype = ldap_next_attribute(ld, entry, ber);
  1371. }
  1372. if (NULL != ber)
  1373. ber_free(ber, 0);
  1374. /* The last atype has not been freed yet */
  1375. if (NULL != atype)
  1376. ldap_memfree(atype);
  1377. }
  1378. return return_value;
  1379. }
  1380. /*
  1381. * Read the remote server's schema entry, then read the local schema entry,
  1382. * and compare the nsschemacsn attribute. If the local csn is newer, or
  1383. * the remote csn is absent, push the schema down to the consumer.
  1384. * Return codes:
  1385. * CONN_SCHEMA_UPDATED if the schema was pushed successfully
  1386. * CONN_SCHEMA_NO_UPDATE_NEEDED if the schema was as new or newer than
  1387. * the local server's schema
  1388. * CONN_OPERATION_FAILED if an error occurred
  1389. * CONN_NOT_CONNECTED if no connection was active
  1390. * NOTE: Should only be called when a replication session has been
  1391. * established by sending a startReplication extended operation.
  1392. */
  1393. ConnResult
  1394. conn_push_schema(Repl_Connection *conn, CSN **remotecsn)
  1395. {
  1396. ConnResult return_value = CONN_OPERATION_SUCCESS;
  1397. char *nsschemacsn = "nsschemacsn";
  1398. Slapi_Entry **entries = NULL;
  1399. Slapi_Entry *schema_entry = NULL;
  1400. CSN *localcsn = NULL;
  1401. Slapi_PBlock *spb = NULL;
  1402. char localcsnstr[CSN_STRSIZE + 1] = {0};
  1403. if (!remotecsn)
  1404. {
  1405. return_value = CONN_OPERATION_FAILED;
  1406. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "NULL remote CSN\n");
  1407. }
  1408. else if (!conn_connected(conn))
  1409. {
  1410. return_value = CONN_NOT_CONNECTED;
  1411. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1412. "%s: Schema replication update failed: not connected to consumer\n",
  1413. agmt_get_long_name(conn->agmt));
  1414. }
  1415. else
  1416. {
  1417. localcsn = dup_global_schema_csn();
  1418. if (NULL == localcsn)
  1419. {
  1420. /* Local server has epoch CSN, so don't push schema */
  1421. return_value = CONN_SCHEMA_NO_UPDATE_NEEDED;
  1422. }
  1423. else if ( *remotecsn && csn_compare(localcsn, *remotecsn) <= 0 )
  1424. {
  1425. /* Local server schema is not newer than the remote one */
  1426. return_value = CONN_SCHEMA_NO_UPDATE_NEEDED;
  1427. }
  1428. else
  1429. {
  1430. struct berval **remote_schema_csn_bervals = NULL;
  1431. /* Get remote server's schema */
  1432. return_value = conn_read_entry_attribute(conn, "cn=schema", nsschemacsn,
  1433. &remote_schema_csn_bervals);
  1434. if (CONN_OPERATION_SUCCESS == return_value)
  1435. {
  1436. if (NULL != remote_schema_csn_bervals && NULL != remote_schema_csn_bervals[0])
  1437. {
  1438. char remotecsnstr[CSN_STRSIZE + 1] = {0};
  1439. memcpy(remotecsnstr, remote_schema_csn_bervals[0]->bv_val,
  1440. remote_schema_csn_bervals[0]->bv_len);
  1441. remotecsnstr[remote_schema_csn_bervals[0]->bv_len] = '\0';
  1442. *remotecsn = csn_new_by_string(remotecsnstr);
  1443. if (*remotecsn && (csn_compare(localcsn, *remotecsn) <= 0))
  1444. {
  1445. return_value = CONN_SCHEMA_NO_UPDATE_NEEDED;
  1446. }
  1447. /* Need to free the remote_schema_csn_bervals */
  1448. ber_bvecfree(remote_schema_csn_bervals);
  1449. }
  1450. if (return_value != CONN_SCHEMA_NO_UPDATE_NEEDED) {
  1451. struct berval **remote_schema_objectclasses_bervals = NULL;
  1452. struct berval **remote_schema_attributetypes_bervals = NULL;
  1453. /* before pushing the schema do some checking */
  1454. /* First objectclasses */
  1455. return_value = conn_read_entry_attribute(conn, "cn=schema", "objectclasses",
  1456. &remote_schema_objectclasses_bervals);
  1457. if (return_value == CONN_OPERATION_SUCCESS) {
  1458. /* Check if the consumer objectclasses are a superset of the local supplier schema */
  1459. if (schema_objectclasses_superset_check(remote_schema_objectclasses_bervals, OC_SUPPLIER)) {
  1460. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1461. "Schema %s must not be overwritten (set replication log for additional info)\n",
  1462. agmt_get_long_name(conn->agmt));
  1463. return_value = CONN_OPERATION_FAILED;
  1464. }
  1465. if(remote_schema_objectclasses_bervals){
  1466. ber_bvecfree(remote_schema_objectclasses_bervals);
  1467. }
  1468. } else {
  1469. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1470. "%s: Fail to retrieve the remote schema objectclasses\n",
  1471. agmt_get_long_name(conn->agmt));
  1472. }
  1473. if (return_value == CONN_OPERATION_SUCCESS) {
  1474. /* Next attribute types */
  1475. return_value = conn_read_entry_attribute(conn, "cn=schema", "attributetypes",
  1476. &remote_schema_attributetypes_bervals);
  1477. if (return_value == CONN_OPERATION_SUCCESS) {
  1478. /* Check if the consumer attributes are a superset of the local supplier schema */
  1479. if (schema_attributetypes_superset_check(remote_schema_attributetypes_bervals, OC_SUPPLIER)) {
  1480. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1481. "Schema %s must not be overwritten (set replication log for additional info)\n",
  1482. agmt_get_long_name(conn->agmt));
  1483. return_value = CONN_OPERATION_FAILED;
  1484. }
  1485. if(remote_schema_attributetypes_bervals){
  1486. ber_bvecfree(remote_schema_attributetypes_bervals);
  1487. }
  1488. } else {
  1489. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1490. "%s: Fail to retrieve the remote schema attribute types\n",
  1491. agmt_get_long_name(conn->agmt));
  1492. }
  1493. }
  1494. /* In case of success, possibly log a message */
  1495. if (return_value == CONN_OPERATION_SUCCESS) {
  1496. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1497. "Schema checking successful: ok to push the schema (%s)\n", agmt_get_long_name(conn->agmt));
  1498. }
  1499. }
  1500. }
  1501. }
  1502. }
  1503. if (CONN_OPERATION_SUCCESS == return_value)
  1504. {
  1505. /* We know we need to push the schema out. */
  1506. LDAPMod ocmod = {0};
  1507. LDAPMod atmod = {0};
  1508. LDAPMod csnmod = {0};
  1509. LDAPMod *attrs[4] = {0};
  1510. int numvalues = 0;
  1511. Slapi_Attr *attr = NULL;
  1512. char *csnvalues[2];
  1513. ocmod.mod_type = "objectclasses";
  1514. ocmod.mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  1515. ocmod.mod_bvalues = NULL;
  1516. atmod.mod_type = "attributetypes";
  1517. atmod.mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  1518. atmod.mod_bvalues = NULL;
  1519. csnmod.mod_type = nsschemacsn;
  1520. csnmod.mod_op = LDAP_MOD_REPLACE;
  1521. csn_as_string (localcsn, PR_FALSE, localcsnstr);
  1522. csnvalues[0] = localcsnstr;
  1523. csnvalues[1] = NULL;
  1524. csnmod.mod_values = csnvalues;
  1525. attrs[0] = &ocmod;
  1526. attrs[1] = &atmod;
  1527. attrs[2] = &csnmod;
  1528. attrs[3] = NULL;
  1529. return_value = CONN_OPERATION_FAILED; /* assume failure */
  1530. /* Get local schema */
  1531. spb = slapi_search_internal("cn=schema", LDAP_SCOPE_BASE, "(objectclass=*)",
  1532. NULL /* controls */, NULL /* schema_csn_attrs */, 0 /* attrsonly */);
  1533. slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1534. if (NULL == entries || NULL == entries[0])
  1535. {
  1536. /* Whoops - couldn't read our own schema! */
  1537. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1538. "%s: Error: unable to read local schema definitions.\n",
  1539. agmt_get_long_name(conn->agmt));
  1540. return_value = CONN_OPERATION_FAILED;
  1541. }
  1542. else
  1543. {
  1544. schema_entry = entries[0];
  1545. if (slapi_entry_attr_find(schema_entry, "objectclasses", &attr) != -1)
  1546. {
  1547. int i, ind;
  1548. Slapi_Value *value;
  1549. slapi_attr_get_numvalues(attr, &numvalues);
  1550. ocmod.mod_bvalues = (struct berval **)slapi_ch_malloc((numvalues + 1) *
  1551. sizeof(struct berval *));
  1552. for (i = 0, ind = slapi_attr_first_value(attr, &value);
  1553. ind != -1; ind = slapi_attr_next_value(attr, ind, &value), i++)
  1554. {
  1555. /* XXXggood had to cast away const below */
  1556. ocmod.mod_bvalues[i] = (struct berval *)slapi_value_get_berval(value);
  1557. }
  1558. ocmod.mod_bvalues[numvalues] = NULL;
  1559. if (slapi_entry_attr_find(schema_entry, "attributetypes", &attr) != -1)
  1560. {
  1561. ConnResult result;
  1562. slapi_attr_get_numvalues(attr, &numvalues);
  1563. atmod.mod_bvalues = (struct berval **)slapi_ch_malloc((numvalues + 1) *
  1564. sizeof(struct berval *));
  1565. for (i = 0, ind = slapi_attr_first_value(attr, &value);
  1566. ind != -1; ind = slapi_attr_next_value(attr, ind, &value), i++)
  1567. {
  1568. /* XXXggood had to cast away const below */
  1569. atmod.mod_bvalues[i] = (struct berval *)slapi_value_get_berval(value);
  1570. }
  1571. atmod.mod_bvalues[numvalues] = NULL;
  1572. result = conn_send_modify(conn, "cn=schema", attrs, NULL, NULL); /* DBDB: this needs to be fixed to use async */
  1573. result = conn_read_result(conn,NULL);
  1574. switch (result)
  1575. {
  1576. case CONN_OPERATION_FAILED:
  1577. {
  1578. int ldaperr = -1, optype = -1;
  1579. conn_get_error(conn, &optype, &ldaperr);
  1580. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1581. "%s: Schema replication update failed: %s\n",
  1582. agmt_get_long_name(conn->agmt),
  1583. ldaperr == -1 ? "Unknown Error" : ldap_err2string(ldaperr));
  1584. return_value = CONN_OPERATION_FAILED;
  1585. break;
  1586. }
  1587. case CONN_NOT_CONNECTED:
  1588. return_value = CONN_NOT_CONNECTED;
  1589. break;
  1590. case CONN_OPERATION_SUCCESS:
  1591. return_value = CONN_SCHEMA_UPDATED;
  1592. break;
  1593. default:
  1594. break;
  1595. }
  1596. }
  1597. }
  1598. else
  1599. {
  1600. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1601. "%s: Schema replication update failed: "
  1602. "unable to prepare schema entry for transmission.\n",
  1603. agmt_get_long_name(conn->agmt));
  1604. }
  1605. }
  1606. /* slapi_ch_free accepts NULL pointer */
  1607. slapi_ch_free((void **)&ocmod.mod_bvalues);
  1608. slapi_ch_free((void **)&atmod.mod_bvalues);
  1609. }
  1610. if (NULL != spb)
  1611. {
  1612. slapi_free_search_results_internal(spb);
  1613. slapi_pblock_destroy(spb);
  1614. spb = NULL;
  1615. }
  1616. if (NULL != localcsn)
  1617. {
  1618. csn_free(&localcsn);
  1619. }
  1620. return return_value;
  1621. }
  1622. void
  1623. conn_set_timeout(Repl_Connection *conn, long timeout)
  1624. {
  1625. PR_ASSERT(NULL != conn);
  1626. PR_ASSERT(timeout >= 0);
  1627. PR_Lock(conn->lock);
  1628. conn->timeout.tv_sec = timeout;
  1629. PR_Unlock(conn->lock);
  1630. }
  1631. long
  1632. conn_get_timeout(Repl_Connection *conn)
  1633. {
  1634. long retval = 0;
  1635. PR_ASSERT(NULL != conn);
  1636. retval = conn->timeout.tv_sec;
  1637. return retval;
  1638. }
  1639. LDAP *
  1640. conn_get_ldap(Repl_Connection *conn)
  1641. {
  1642. if(conn){
  1643. return conn->ld;
  1644. } else {
  1645. return NULL;
  1646. }
  1647. }
  1648. void conn_set_agmt_changed(Repl_Connection *conn)
  1649. {
  1650. PR_ASSERT(NULL != conn);
  1651. PR_Lock(conn->lock);
  1652. if (NULL != conn->agmt)
  1653. conn->flag_agmt_changed = 1;
  1654. PR_Unlock(conn->lock);
  1655. }
  1656. static const char *
  1657. bind_method_to_mech(int bindmethod)
  1658. {
  1659. switch (bindmethod) {
  1660. case BINDMETHOD_SSL_CLIENTAUTH:
  1661. return LDAP_SASL_EXTERNAL;
  1662. break;
  1663. case BINDMETHOD_SASL_GSSAPI:
  1664. return "GSSAPI";
  1665. break;
  1666. case BINDMETHOD_SASL_DIGEST_MD5:
  1667. return "DIGEST-MD5";
  1668. break;
  1669. default: /* anything else */
  1670. return LDAP_SASL_SIMPLE;
  1671. }
  1672. return LDAP_SASL_SIMPLE;
  1673. }
  1674. const char*
  1675. conn_get_bindmethod(Repl_Connection *conn)
  1676. {
  1677. return (bind_method_to_mech(conn->bindmethod));
  1678. }
  1679. /*
  1680. * Check the result of an ldap BIND operation to see we it
  1681. * contains the expiration controls
  1682. * return: -1 error, not bound
  1683. * 0, OK bind has succeeded
  1684. */
  1685. static int
  1686. bind_and_check_pwp(Repl_Connection *conn, char * binddn, char *password)
  1687. {
  1688. LDAPControl **ctrls = NULL;
  1689. LDAP *ld = conn->ld;
  1690. int rc;
  1691. const char *mech = bind_method_to_mech(conn->bindmethod);
  1692. rc = slapi_ldap_bind(conn->ld, binddn, password, mech, NULL,
  1693. &ctrls, &conn->timeout, NULL);
  1694. if ( rc == LDAP_SUCCESS )
  1695. {
  1696. if (conn->last_ldap_error != rc)
  1697. {
  1698. conn->last_ldap_error = rc;
  1699. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1700. "%s: Replication bind with %s auth resumed\n",
  1701. agmt_get_long_name(conn->agmt),
  1702. mech ? mech : "SIMPLE");
  1703. }
  1704. if ( ctrls )
  1705. {
  1706. int i;
  1707. for( i = 0; ctrls[ i ] != NULL; ++i )
  1708. {
  1709. if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRED)) )
  1710. {
  1711. /* Bind is successfull but password has expired */
  1712. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1713. "%s: Successfully bound %s to consumer, "
  1714. "but password has expired on consumer.\n",
  1715. agmt_get_long_name(conn->agmt), binddn);
  1716. }
  1717. else if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRING)) )
  1718. {
  1719. /* The password is expiring in n seconds */
  1720. if ( (ctrls[ i ]->ldctl_value.bv_val != NULL) &&
  1721. (ctrls[ i ]->ldctl_value.bv_len > 0) )
  1722. {
  1723. int password_expiring = atoi( ctrls[ i ]->ldctl_value.bv_val );
  1724. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1725. "%s: Successfully bound %s to consumer, "
  1726. "but password is expiring on consumer in %d seconds.\n",
  1727. agmt_get_long_name(conn->agmt), binddn, password_expiring);
  1728. }
  1729. }
  1730. }
  1731. ldap_controls_free( ctrls );
  1732. }
  1733. return (CONN_OPERATION_SUCCESS);
  1734. }
  1735. else
  1736. {
  1737. ldap_controls_free( ctrls );
  1738. /* Do not report the same error over and over again
  1739. * unless replication level logging is enabled. */
  1740. if (conn->last_ldap_error != rc)
  1741. {
  1742. char *errmsg = NULL;
  1743. conn->last_ldap_error = rc;
  1744. /* errmsg is a pointer directly into the ld structure - do not free */
  1745. rc = slapi_ldap_get_lderrno( ld, NULL, &errmsg );
  1746. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1747. "%s: Replication bind with %s auth failed: LDAP error %d (%s) (%s)\n",
  1748. agmt_get_long_name(conn->agmt),
  1749. mech ? mech : "SIMPLE", rc,
  1750. ldap_err2string(rc), errmsg ? errmsg : "");
  1751. } else {
  1752. char *errmsg = NULL;
  1753. /* errmsg is a pointer directly into the ld structure - do not free */
  1754. rc = slapi_ldap_get_lderrno( ld, NULL, &errmsg );
  1755. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1756. "%s: Replication bind with %s auth failed: LDAP error %d (%s) (%s)\n",
  1757. agmt_get_long_name(conn->agmt),
  1758. mech ? mech : "SIMPLE", rc,
  1759. ldap_err2string(rc), errmsg ? errmsg : "");
  1760. }
  1761. return (CONN_OPERATION_FAILED);
  1762. }
  1763. }
  1764. void
  1765. repl5_set_debug_timeout(const char *val)
  1766. {
  1767. /* val looks like this: seconds[:debuglevel] */
  1768. /* seconds is the number of seconds to wait until turning on the debug level */
  1769. /* this should be less than the ldap connection timeout (default 10 minutes) */
  1770. /* the optional debug level is the error log debugging level to use (default repl) */
  1771. if (val) {
  1772. const char *p = strchr(val, ':');
  1773. s_debug_timeout = atoi(val);
  1774. if (p) {
  1775. s_debug_level = atoi(p+1);
  1776. } else {
  1777. s_debug_level = 8192;
  1778. }
  1779. }
  1780. }
  1781. #ifdef FOR_DEBUGGING
  1782. static time_t
  1783. PRTime2time_t (PRTime tm)
  1784. {
  1785. PRInt64 rt;
  1786. PR_ASSERT (tm);
  1787. LL_DIV(rt, tm, PR_USEC_PER_SEC);
  1788. return (time_t)rt;
  1789. }
  1790. #endif
  1791. static Slapi_Eq_Context
  1792. repl5_start_debug_timeout(int *setlevel)
  1793. {
  1794. Slapi_Eq_Context eqctx = 0;
  1795. if (s_debug_timeout && s_debug_level) {
  1796. time_t now = time(NULL);
  1797. eqctx = slapi_eq_once(repl5_debug_timeout_callback, setlevel,
  1798. s_debug_timeout + now);
  1799. }
  1800. return eqctx;
  1801. }
  1802. static void
  1803. repl5_stop_debug_timeout(Slapi_Eq_Context eqctx, int *setlevel)
  1804. {
  1805. char buf[20];
  1806. char msg[SLAPI_DSE_RETURNTEXT_SIZE];
  1807. if (eqctx && !*setlevel) {
  1808. (void)slapi_eq_cancel(eqctx);
  1809. }
  1810. if (s_debug_timeout && s_debug_level && *setlevel) {
  1811. void config_set_errorlog_level(const char *type, char *buf, char *msg, int apply);
  1812. sprintf(buf, "%d", 0);
  1813. config_set_errorlog_level("nsslapd-errorlog-level", buf, msg, 1);
  1814. }
  1815. }
  1816. static void
  1817. repl5_debug_timeout_callback(time_t when, void *arg)
  1818. {
  1819. int *setlevel = (int *)arg;
  1820. void config_set_errorlog_level(const char *type, char *buf, char *msg, int apply);
  1821. char buf[20];
  1822. char msg[SLAPI_DSE_RETURNTEXT_SIZE];
  1823. *setlevel = 1;
  1824. sprintf(buf, "%d", s_debug_level);
  1825. config_set_errorlog_level("nsslapd-errorlog-level", buf, msg, 1);
  1826. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1827. "repl5_debug_timeout_callback: set debug level to %d at %ld\n",
  1828. s_debug_level, when);
  1829. }
  1830. void
  1831. conn_lock(Repl_Connection *conn)
  1832. {
  1833. if(conn){
  1834. PR_Lock(conn->lock);
  1835. }
  1836. }
  1837. void
  1838. conn_unlock(Repl_Connection *conn)
  1839. {
  1840. if(conn){
  1841. PR_Unlock(conn->lock);
  1842. }
  1843. }