repl5_connection.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright 2001 Sun Microsystems, Inc.
  3. * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. /* repl5_connection.c */
  7. /*
  8. The connection object manages a connection to a single replication
  9. consumer.
  10. XXXggood what to do on timeout? If we close connection, then we won't leave a
  11. replica locked. Seems like right thing to do.
  12. */
  13. #include "repl5.h"
  14. #include "ldappr.h"
  15. typedef struct repl_connection
  16. {
  17. char *hostname;
  18. int port;
  19. char *binddn;
  20. int bindmethod;
  21. int state;
  22. int last_operation;
  23. int last_ldap_error;
  24. const char *status;
  25. char *last_ldap_errmsg;
  26. PRUint32 transport_flags;
  27. LDAP *ld;
  28. int supports_ldapv3; /* 1 if does, 0 if doesn't, -1 if not determined */
  29. int supports_ds50_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  30. int supports_ds40_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  31. int linger_time; /* time in seconds to leave an idle connection open */
  32. PRBool linger_active;
  33. Slapi_Eq_Context *linger_event;
  34. PRBool delete_after_linger;
  35. int refcnt;
  36. const Repl_Agmt *agmt;
  37. PRLock *lock;
  38. struct timeval timeout;
  39. int flag_agmt_changed;
  40. char *plain;
  41. } repl_connection;
  42. /* #define DEFAULT_LINGER_TIME (5 * 60) */ /* 5 minutes */
  43. #define DEFAULT_LINGER_TIME (60)
  44. /* Controls we add on every outbound operation */
  45. static LDAPControl manageDSAITControl = {LDAP_CONTROL_MANAGEDSAIT, {0, ""}, '\0'};
  46. static int attribute_string_value_present(LDAP *ld, LDAPMessage *entry,
  47. const char *type, const char *value);
  48. static int bind_and_check_pwp(Repl_Connection *conn, char * binddn, char *password);
  49. static int do_simple_bind (Repl_Connection *conn, LDAP *ld, char * binddn, char *password);
  50. static int s_debug_timeout = 0;
  51. static int s_debug_level = 0;
  52. static Slapi_Eq_Context repl5_start_debug_timeout(int *setlevel);
  53. static void repl5_stop_debug_timeout(Slapi_Eq_Context eqctx, int *setlevel);
  54. static void repl5_debug_timeout_callback(time_t when, void *arg);
  55. #ifndef DSE_RETURNTEXT_SIZE
  56. #define SLAPI_DSE_RETURNTEXT_SIZE 512
  57. #endif
  58. #define STATE_CONNECTED 600
  59. #define STATE_DISCONNECTED 601
  60. #define STATUS_DISCONNECTED "disconnected"
  61. #define STATUS_CONNECTED "connected"
  62. #define STATUS_PROCESSING_ADD "processing add operation"
  63. #define STATUS_PROCESSING_DELETE "processing delete operation"
  64. #define STATUS_PROCESSING_MODIFY "processing modify operation"
  65. #define STATUS_PROCESSING_RENAME "processing rename operation"
  66. #define STATUS_PROCESSING_EXTENDED_OPERATION "processing extended operation"
  67. #define STATUS_LINGERING "lingering"
  68. #define STATUS_SHUTTING_DOWN "shutting down"
  69. #define STATUS_BINDING "connecting and binding"
  70. #define STATUS_SEARCHING "processing search operation"
  71. #define CONN_NO_OPERATION 0
  72. #define CONN_ADD 1
  73. #define CONN_DELETE 2
  74. #define CONN_MODIFY 3
  75. #define CONN_RENAME 4
  76. #define CONN_EXTENDED_OPERATION 5
  77. #define CONN_BIND 6
  78. #define CONN_INIT 7
  79. /* These are errors returned from ldap operations which should cause us to disconnect and
  80. retry the connection later */
  81. #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)
  82. /* Forward declarations */
  83. static void close_connection_internal(Repl_Connection *conn);
  84. /*
  85. * Create a new conenction object. Returns a pointer to the object, or
  86. * NULL if an error occurs.
  87. */
  88. Repl_Connection *
  89. conn_new(Repl_Agmt *agmt)
  90. {
  91. Repl_Connection *rpc;
  92. rpc = (Repl_Connection *)slapi_ch_malloc(sizeof(repl_connection));
  93. if ((rpc->lock = PR_NewLock()) == NULL)
  94. {
  95. goto loser;
  96. }
  97. rpc->hostname = agmt_get_hostname(agmt);
  98. rpc->port = agmt_get_port(agmt);
  99. rpc->binddn = agmt_get_binddn(agmt);
  100. rpc->bindmethod = agmt_get_bindmethod(agmt);
  101. rpc->transport_flags = agmt_get_transport_flags(agmt);
  102. rpc->ld = NULL;
  103. rpc->state = STATE_DISCONNECTED;
  104. rpc->last_operation = CONN_NO_OPERATION;
  105. rpc->last_ldap_error = LDAP_SUCCESS;
  106. rpc->last_ldap_errmsg = NULL;
  107. rpc->supports_ldapv3 = -1;
  108. rpc->supports_ds40_repl = -1;
  109. rpc->supports_ds50_repl = -1;
  110. rpc->linger_active = PR_FALSE;
  111. rpc->delete_after_linger = PR_FALSE;
  112. rpc->linger_event = NULL;
  113. rpc->linger_time = DEFAULT_LINGER_TIME;
  114. rpc->status = STATUS_DISCONNECTED;
  115. rpc->agmt = agmt;
  116. rpc->refcnt = 1;
  117. rpc->timeout.tv_sec = agmt_get_timeout(agmt);
  118. rpc->timeout.tv_usec = 0;
  119. rpc->flag_agmt_changed = 0;
  120. rpc->plain = NULL;
  121. return rpc;
  122. loser:
  123. conn_delete(rpc);
  124. return NULL;
  125. }
  126. /*
  127. * Return PR_TRUE if the connection is in the connected state
  128. */
  129. static PRBool
  130. conn_connected(Repl_Connection *conn)
  131. {
  132. PRBool return_value;
  133. PR_Lock(conn->lock);
  134. return_value = STATE_CONNECTED == conn->state;
  135. PR_Unlock(conn->lock);
  136. return return_value;
  137. }
  138. /*
  139. * Destroy a connection object.
  140. */
  141. static void
  142. conn_delete_internal(Repl_Connection *conn)
  143. {
  144. PR_ASSERT(NULL != conn);
  145. close_connection_internal(conn);
  146. /* slapi_ch_free accepts NULL pointer */
  147. slapi_ch_free((void **)&conn->hostname);
  148. slapi_ch_free((void **)&conn->binddn);
  149. slapi_ch_free((void **)&conn->plain);
  150. }
  151. /*
  152. * Destroy a connection. It is an error to use the connection object
  153. * after conn_delete() has been called.
  154. */
  155. void
  156. conn_delete(Repl_Connection *conn)
  157. {
  158. PRBool destroy_it = PR_FALSE;
  159. PR_ASSERT(NULL != conn);
  160. PR_Lock(conn->lock);
  161. if (conn->linger_active)
  162. {
  163. if (slapi_eq_cancel(conn->linger_event) == 1)
  164. {
  165. /* Event was found and cancelled. Destroy the connection object. */
  166. PR_Unlock(conn->lock);
  167. destroy_it = PR_TRUE;
  168. }
  169. else
  170. {
  171. /*
  172. * The event wasn't found, but we think it's still active.
  173. * That means an event is in the process of being fired
  174. * off, so arrange for the event to destroy the object .
  175. */
  176. conn->delete_after_linger = PR_TRUE;
  177. PR_Unlock(conn->lock);
  178. }
  179. }
  180. if (destroy_it)
  181. {
  182. conn_delete_internal(conn);
  183. }
  184. }
  185. /*
  186. * Return the last operation type processed by the connection
  187. * object, and the LDAP error encountered.
  188. */
  189. void
  190. conn_get_error(Repl_Connection *conn, int *operation, int *error)
  191. {
  192. PR_Lock(conn->lock);
  193. *operation = conn->last_operation;
  194. *error = conn->last_ldap_error;
  195. PR_Unlock(conn->lock);
  196. }
  197. /*
  198. * Common code to send an LDAPv3 operation and collect the result.
  199. * Return values:
  200. * CONN_OPERATION_SUCCESS - the operation succeeded
  201. * CONN_OPERATION_FAILED - the operation was sent to the consumer
  202. * and failed. Use conn_get_error() to determine the LDAP error
  203. * code.
  204. * CONN_NOT_CONNECTED - no connection is active. The caller should
  205. * use conn_connect() to connect to the replica and bind, then should
  206. * reacquire the replica (if needed).
  207. * CONN_BUSY - the server is busy with previous requests, must wait for a while
  208. * before retrying
  209. */
  210. static ConnResult
  211. perform_operation(Repl_Connection *conn, int optype, const char *dn,
  212. LDAPMod **attrs, const char *newrdn, const char *newparent,
  213. int deleteoldrdn, LDAPControl *update_control,
  214. const char *extop_oid, struct berval *extop_payload, char **retoidp,
  215. struct berval **retdatap, LDAPControl ***returned_controls)
  216. {
  217. int rc;
  218. ConnResult return_value;
  219. LDAPControl *server_controls[3];
  220. LDAPControl **loc_returned_controls;
  221. const char *op_string = NULL;
  222. const char *extra_op_string = NULL;
  223. server_controls[0] = &manageDSAITControl;
  224. server_controls[1] = update_control;
  225. server_controls[2] = NULL;
  226. if (conn_connected(conn))
  227. {
  228. int msgid;
  229. conn->last_operation = optype;
  230. switch (optype)
  231. {
  232. case CONN_ADD:
  233. conn->status = STATUS_PROCESSING_ADD;
  234. op_string = "add";
  235. rc = ldap_add_ext(conn->ld, dn, attrs, server_controls,
  236. NULL /* clientctls */, &msgid);
  237. break;
  238. case CONN_MODIFY:
  239. conn->status = STATUS_PROCESSING_MODIFY;
  240. op_string = "modify";
  241. rc = ldap_modify_ext(conn->ld, dn, attrs, server_controls,
  242. NULL /* clientctls */, &msgid);
  243. break;
  244. case CONN_DELETE:
  245. conn->status = STATUS_PROCESSING_DELETE;
  246. op_string = "delete";
  247. rc = ldap_delete_ext(conn->ld, dn, server_controls,
  248. NULL /* clientctls */, &msgid);
  249. break;
  250. case CONN_RENAME:
  251. conn->status = STATUS_PROCESSING_RENAME;
  252. op_string = "rename";
  253. rc = ldap_rename(conn->ld, dn, newrdn, newparent, deleteoldrdn,
  254. server_controls, NULL /* clientctls */, &msgid);
  255. break;
  256. case CONN_EXTENDED_OPERATION:
  257. conn->status = STATUS_PROCESSING_EXTENDED_OPERATION;
  258. op_string = "extended";
  259. extra_op_string = extop_oid;
  260. rc = ldap_extended_operation(conn->ld, extop_oid, extop_payload,
  261. server_controls, NULL /* clientctls */, &msgid);
  262. }
  263. if (LDAP_SUCCESS == rc)
  264. {
  265. LDAPMessage *res = NULL;
  266. int setlevel = 0;
  267. Slapi_Eq_Context eqctx = repl5_start_debug_timeout(&setlevel);
  268. rc = ldap_result(conn->ld, msgid, 1, &conn->timeout, &res);
  269. repl5_stop_debug_timeout(eqctx, &setlevel);
  270. if (0 == rc)
  271. {
  272. /* Timeout */
  273. rc = ldap_get_lderrno(conn->ld, NULL, NULL);
  274. conn->last_ldap_error = LDAP_TIMEOUT;
  275. return_value = CONN_TIMEOUT;
  276. }
  277. else if (-1 == rc)
  278. {
  279. /* Error */
  280. char *s = NULL;
  281. rc = ldap_get_lderrno(conn->ld, NULL, &s);
  282. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  283. "%s: Received error %d: %s for %s operation\n",
  284. agmt_get_long_name(conn->agmt),
  285. rc, s ? s : "NULL",
  286. op_string ? op_string : "NULL");
  287. conn->last_ldap_error = rc;
  288. /* some errors will require a disconnect and retry the connection
  289. later */
  290. if (IS_DISCONNECT_ERROR(rc))
  291. {
  292. conn_disconnect(conn);
  293. return_value = CONN_NOT_CONNECTED;
  294. }
  295. else
  296. {
  297. conn->status = STATUS_CONNECTED;
  298. return_value = CONN_OPERATION_FAILED;
  299. }
  300. }
  301. else
  302. {
  303. int err;
  304. char *errmsg = NULL;
  305. char **referrals = NULL;
  306. char *matched = NULL;
  307. rc = ldap_parse_result(conn->ld, res, &err, &matched,
  308. &errmsg, &referrals, &loc_returned_controls,
  309. 0 /* Don't free the result */);
  310. if (IS_DISCONNECT_ERROR(rc))
  311. {
  312. conn->last_ldap_error = rc;
  313. conn_disconnect(conn);
  314. return_value = CONN_NOT_CONNECTED;
  315. }
  316. else if (IS_DISCONNECT_ERROR(err))
  317. {
  318. conn->last_ldap_error = err;
  319. conn_disconnect(conn);
  320. return_value = CONN_NOT_CONNECTED;
  321. }
  322. /* Got a result */
  323. else if (CONN_EXTENDED_OPERATION == optype)
  324. {
  325. if ((rc == LDAP_SUCCESS) && (err == LDAP_BUSY))
  326. return_value = CONN_BUSY;
  327. else {
  328. if (rc == LDAP_SUCCESS) {
  329. rc = ldap_parse_extended_result(conn->ld, res, retoidp,
  330. retdatap, 0 /* Don't Free it */);
  331. }
  332. conn->last_ldap_error = rc;
  333. return_value = (LDAP_SUCCESS == conn->last_ldap_error ?
  334. CONN_OPERATION_SUCCESS : CONN_OPERATION_FAILED);
  335. }
  336. }
  337. else /* regular operation, result returned */
  338. {
  339. if (NULL != returned_controls)
  340. {
  341. *returned_controls = loc_returned_controls;
  342. }
  343. if (LDAP_SUCCESS != rc)
  344. {
  345. conn->last_ldap_error = rc;
  346. }
  347. else
  348. {
  349. conn->last_ldap_error = err;
  350. }
  351. return_value = LDAP_SUCCESS == conn->last_ldap_error ? CONN_OPERATION_SUCCESS : CONN_OPERATION_FAILED;
  352. }
  353. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  354. "%s: Received result code %d for %s operation %s%s\n",
  355. agmt_get_long_name(conn->agmt),
  356. conn->last_ldap_error,
  357. op_string == NULL ? "" : op_string,
  358. extra_op_string == NULL ? "" : extra_op_string,
  359. extra_op_string == NULL ? "" : " ");
  360. /*
  361. * XXXggood do I need to free matched, referrals,
  362. * anything else? Or can I pass NULL for the args
  363. * I'm not interested in?
  364. */
  365. /* Good question! Meanwhile, as RTM aproaches, let's free them... */
  366. slapi_ch_free((void **) &errmsg);
  367. slapi_ch_free((void **) &matched);
  368. charray_free(referrals);
  369. conn->status = STATUS_CONNECTED;
  370. }
  371. if (res) ldap_msgfree(res);
  372. }
  373. else
  374. {
  375. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  376. "%s: Failed to send %s operation: LDAP error %d (%s)\n",
  377. agmt_get_long_name(conn->agmt),
  378. op_string ? op_string : "NULL", rc, ldap_err2string(rc));
  379. conn->last_ldap_error = rc;
  380. if (IS_DISCONNECT_ERROR(rc))
  381. {
  382. conn_disconnect(conn);
  383. return_value = CONN_NOT_CONNECTED;
  384. }
  385. else
  386. {
  387. conn->status = STATUS_CONNECTED;
  388. return_value = CONN_OPERATION_FAILED;
  389. }
  390. }
  391. }
  392. else
  393. {
  394. /* conn->last_ldap_error has been set to a more specific value
  395. * in conn_connected()
  396. * conn->last_ldap_error = LDAP_SERVER_DOWN;
  397. */
  398. return_value = CONN_NOT_CONNECTED;
  399. }
  400. return return_value;
  401. }
  402. /*
  403. * Send an LDAP add operation.
  404. */
  405. ConnResult
  406. conn_send_add(Repl_Connection *conn, const char *dn, LDAPMod **attrs,
  407. LDAPControl *update_control, LDAPControl ***returned_controls)
  408. {
  409. return perform_operation(conn, CONN_ADD, dn, attrs, NULL /* newrdn */,
  410. NULL /* newparent */, 0 /* deleteoldrdn */, update_control,
  411. NULL /* extop OID */, NULL /* extop payload */, NULL /* retoidp */,
  412. NULL /* retdatap */, returned_controls);
  413. }
  414. /*
  415. * Send an LDAP delete operation.
  416. */
  417. ConnResult
  418. conn_send_delete(Repl_Connection *conn, const char *dn,
  419. LDAPControl *update_control, LDAPControl ***returned_controls)
  420. {
  421. return perform_operation(conn, CONN_DELETE, dn, NULL /* attrs */,
  422. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  423. update_control, NULL /* extop OID */, NULL /* extop payload */,
  424. NULL /* retoidp */, NULL /* retdatap */, returned_controls);
  425. }
  426. /*
  427. * Send an LDAP modify operation.
  428. */
  429. ConnResult
  430. conn_send_modify(Repl_Connection *conn, const char *dn, LDAPMod **mods,
  431. LDAPControl *update_control, LDAPControl ***returned_controls)
  432. {
  433. return perform_operation(conn, CONN_MODIFY, dn, mods, NULL /* newrdn */,
  434. NULL /* newparent */, 0 /* deleteoldrdn */, update_control,
  435. NULL /* extop OID */, NULL /* extop payload */, NULL /* retoidp */,
  436. NULL /* retdatap */, returned_controls);
  437. }
  438. /*
  439. * Send an LDAP moddn operation.
  440. */
  441. ConnResult
  442. conn_send_rename(Repl_Connection *conn, const char *dn,
  443. const char *newrdn, const char *newparent, int deleteoldrdn,
  444. LDAPControl *update_control, LDAPControl ***returned_controls)
  445. {
  446. return perform_operation(conn, CONN_RENAME, dn, NULL /* attrs */,
  447. newrdn, newparent, deleteoldrdn, update_control,
  448. NULL /* extop OID */, NULL /* extop payload */, NULL /* retoidp */,
  449. NULL /* retdatap */, returned_controls);
  450. }
  451. /*
  452. * Send an LDAP extended operation.
  453. */
  454. ConnResult
  455. conn_send_extended_operation(Repl_Connection *conn, const char *extop_oid,
  456. struct berval *payload, char **retoidp, struct berval **retdatap,
  457. LDAPControl *update_control, LDAPControl ***returned_controls)
  458. {
  459. return perform_operation(conn, CONN_EXTENDED_OPERATION, NULL /* dn */, NULL /* attrs */,
  460. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  461. update_control, extop_oid, payload, retoidp, retdatap,
  462. returned_controls);
  463. }
  464. /*
  465. * Synchronously read an entry and return a specific attribute's values.
  466. * Returns CONN_OPERATION_SUCCESS if successful. Returns
  467. * CONN_OPERATION_FAILED if the operation was sent but an LDAP error
  468. * occurred (conn->last_ldap_error is set in this case), and
  469. * CONN_NOT_CONNECTED if no connection was active.
  470. *
  471. * The caller must free the returned_bvals.
  472. */
  473. ConnResult
  474. conn_read_entry_attribute(Repl_Connection *conn, const char *dn,
  475. char *type, struct berval ***returned_bvals)
  476. {
  477. ConnResult return_value;
  478. int ldap_rc;
  479. LDAPControl *server_controls[2];
  480. LDAPMessage *res = NULL;
  481. char *attrs[2];
  482. PR_ASSERT(NULL != type);
  483. if (conn_connected(conn))
  484. {
  485. server_controls[0] = &manageDSAITControl;
  486. server_controls[1] = NULL;
  487. attrs[0] = type;
  488. attrs[1] = NULL;
  489. ldap_rc = ldap_search_ext_s(conn->ld, dn, LDAP_SCOPE_BASE,
  490. "(objectclass=*)", attrs, 0 /* attrsonly */,
  491. server_controls, NULL /* client controls */,
  492. &conn->timeout, 0 /* sizelimit */, &res);
  493. if (LDAP_SUCCESS == ldap_rc)
  494. {
  495. LDAPMessage *entry = ldap_first_entry(conn->ld, res);
  496. if (NULL != entry)
  497. {
  498. *returned_bvals = ldap_get_values_len(conn->ld, entry, type);
  499. }
  500. return_value = CONN_OPERATION_SUCCESS;
  501. }
  502. else if (IS_DISCONNECT_ERROR(ldap_rc))
  503. {
  504. conn_disconnect(conn);
  505. return_value = CONN_NOT_CONNECTED;
  506. }
  507. else
  508. {
  509. return_value = CONN_OPERATION_FAILED;
  510. }
  511. conn->last_ldap_error = ldap_rc;
  512. if (NULL != res)
  513. {
  514. ldap_msgfree(res);
  515. res = NULL;
  516. }
  517. }
  518. else
  519. {
  520. return_value = CONN_NOT_CONNECTED;
  521. }
  522. return return_value;
  523. }
  524. /*
  525. * Return an pointer to a string describing the connection's status.
  526. */
  527. const char *
  528. conn_get_status(Repl_Connection *conn)
  529. {
  530. return conn->status;
  531. }
  532. /*
  533. * Cancel any outstanding linger timer. Should be called when
  534. * a replication session is beginning.
  535. */
  536. void
  537. conn_cancel_linger(Repl_Connection *conn)
  538. {
  539. PR_ASSERT(NULL != conn);
  540. PR_Lock(conn->lock);
  541. if (conn->linger_active)
  542. {
  543. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  544. "%s: Cancelling linger on the connection\n",
  545. agmt_get_long_name(conn->agmt));
  546. conn->linger_active = PR_FALSE;
  547. if (slapi_eq_cancel(conn->linger_event) == 1)
  548. {
  549. conn->refcnt--;
  550. }
  551. conn->linger_event = NULL;
  552. conn->status = STATUS_CONNECTED;
  553. }
  554. else
  555. {
  556. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  557. "%s: No linger to cancel on the connection\n",
  558. agmt_get_long_name(conn->agmt));
  559. }
  560. PR_Unlock(conn->lock);
  561. }
  562. /*
  563. * Called when our linger timeout timer expires. This means
  564. * we should check to see if perhaps the connection's become
  565. * active again, in which case we do nothing. Otherwise,
  566. * we close the connection.
  567. */
  568. static void
  569. linger_timeout(time_t event_time, void *arg)
  570. {
  571. PRBool delete_now;
  572. Repl_Connection *conn = (Repl_Connection *)arg;
  573. PR_ASSERT(NULL != conn);
  574. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  575. "%s: Linger timeout has expired on the connection\n",
  576. agmt_get_long_name(conn->agmt));
  577. PR_Lock(conn->lock);
  578. if (conn->linger_active)
  579. {
  580. conn->linger_active = PR_FALSE;
  581. conn->linger_event = NULL;
  582. close_connection_internal(conn);
  583. }
  584. delete_now = conn->delete_after_linger;
  585. PR_Unlock(conn->lock);
  586. if (delete_now)
  587. {
  588. conn_delete_internal(conn);
  589. }
  590. }
  591. /*
  592. * Indicate that a session is ending. The linger timer starts when
  593. * this function is called.
  594. */
  595. void
  596. conn_start_linger(Repl_Connection *conn)
  597. {
  598. time_t now;
  599. PR_ASSERT(NULL != conn);
  600. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  601. "%s: Beginning linger on the connection\n",
  602. agmt_get_long_name(conn->agmt));
  603. if (!conn_connected(conn))
  604. {
  605. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  606. "%s: No linger on the closed conn\n",
  607. agmt_get_long_name(conn->agmt));
  608. return;
  609. }
  610. time(&now);
  611. PR_Lock(conn->lock);
  612. if (conn->linger_active)
  613. {
  614. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  615. "%s: Linger already active on the connection\n",
  616. agmt_get_long_name(conn->agmt));
  617. }
  618. else
  619. {
  620. conn->linger_active = PR_TRUE;
  621. conn->linger_event = slapi_eq_once(linger_timeout, conn, now + conn->linger_time);
  622. conn->status = STATUS_LINGERING;
  623. }
  624. PR_Unlock(conn->lock);
  625. }
  626. /*
  627. * If no connection is currently active, opens a connection and binds to
  628. * the remote server. If a connection is open (e.g. lingering) then
  629. * this is a no-op.
  630. *
  631. * Returns CONN_OPERATION_SUCCESS on success, or CONN_OPERATION_FAILED
  632. * on failure. Sets conn->last_ldap_error and conn->last_operation;
  633. */
  634. ConnResult
  635. conn_connect(Repl_Connection *conn)
  636. {
  637. int ldap_rc;
  638. int optdata;
  639. int secure = 0;
  640. char* binddn = NULL;
  641. struct berval *creds;
  642. ConnResult return_value = CONN_OPERATION_SUCCESS;
  643. int pw_ret = 1;
  644. /** Connection already open just return SUCCESS **/
  645. if(conn->state == STATE_CONNECTED) return return_value;
  646. PR_Lock(conn->lock);
  647. if (conn->flag_agmt_changed) {
  648. /* So far we cannot change Hostname and Port */
  649. /* slapi_ch_free((void **)&conn->hostname); */
  650. /* conn->hostname = agmt_get_hostname(conn->agmt); */
  651. /* conn->port = agmt_get_port(conn->agmt); */
  652. slapi_ch_free((void **)&conn->binddn);
  653. conn->binddn = agmt_get_binddn(conn->agmt);
  654. conn->bindmethod = agmt_get_bindmethod(conn->agmt);
  655. conn->transport_flags = agmt_get_transport_flags(conn->agmt);
  656. conn->timeout.tv_sec = agmt_get_timeout(conn->agmt);
  657. conn->flag_agmt_changed = 0;
  658. slapi_ch_free((void **)&conn->plain);
  659. }
  660. PR_Unlock(conn->lock);
  661. creds = agmt_get_credentials(conn->agmt);
  662. if (conn->plain == NULL) {
  663. char *plain = NULL;
  664. /* kexcoff: for reversible encryption */
  665. /* We need to test the return code of pw_rever_decode in order to decide
  666. * if a free for plain will be needed (pw_ret == 0) or not (pw_ret != 0) */
  667. pw_ret = pw_rever_decode(creds->bv_val, &plain, type_nsds5ReplicaCredentials);
  668. /* Pb occured in decryption: stop now, binding will fail */
  669. if ( pw_ret == -1 )
  670. {
  671. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  672. "%s: Decoding of the credentials failed.\n",
  673. agmt_get_long_name(conn->agmt));
  674. return_value = CONN_OPERATION_FAILED;
  675. conn->last_ldap_error = LDAP_INVALID_CREDENTIALS;
  676. conn->state = STATE_DISCONNECTED;
  677. return (return_value);
  678. } /* Else, does not mean that the plain is correct, only means the we had no internal
  679. decoding pb */
  680. conn->plain = slapi_ch_strdup (plain);
  681. if (!pw_ret) slapi_ch_free((void**)&plain);
  682. }
  683. /* ugaston: if SSL has been selected in the replication agreement, SSL client
  684. * initialisation should be done before ever trying to open any connection at all.
  685. */
  686. if (conn->transport_flags == TRANSPORT_FLAG_TLS)
  687. {
  688. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  689. "%s: Replication secured by StartTLS not currently supported\n",
  690. agmt_get_long_name(conn->agmt));
  691. return_value = CONN_OPERATION_FAILED;
  692. conn->last_ldap_error = LDAP_STRONG_AUTH_NOT_SUPPORTED;
  693. conn->state = STATE_DISCONNECTED;
  694. } else if(conn->transport_flags == TRANSPORT_FLAG_SSL)
  695. {
  696. /** Make sure the SSL Library has been initialized before anything else **/
  697. if(slapd_security_library_is_initialized() != 1)
  698. {
  699. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  700. "%s: SSL Not Initialized, Replication over SSL FAILED\n",
  701. agmt_get_long_name(conn->agmt));
  702. conn->last_ldap_error = LDAP_INAPPROPRIATE_AUTH;
  703. conn->last_operation = CONN_INIT;
  704. ber_bvfree(creds);
  705. creds = NULL;
  706. return CONN_SSL_NOT_ENABLED;
  707. } else
  708. {
  709. secure = 1;
  710. }
  711. }
  712. if (return_value == CONN_OPERATION_SUCCESS) {
  713. int io_timeout_ms;
  714. /* Now we initialize the LDAP Structure and set options */
  715. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  716. "%s: Trying %s slapi_ldap_init\n",
  717. agmt_get_long_name(conn->agmt),
  718. secure ? "secure" : "non-secure");
  719. conn->ld = slapi_ldap_init(conn->hostname, conn->port, secure, 0);
  720. if (NULL == conn->ld)
  721. {
  722. return_value = CONN_OPERATION_FAILED;
  723. conn->state = STATE_DISCONNECTED;
  724. conn->last_operation = CONN_INIT;
  725. conn->last_ldap_error = LDAP_LOCAL_ERROR;
  726. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  727. "%s: Failed to establish %sconnection to the consumer\n",
  728. agmt_get_long_name(conn->agmt),
  729. secure ? "secure " : "");
  730. ber_bvfree(creds);
  731. creds = NULL;
  732. return return_value;
  733. }
  734. /* slapi_ch_strdup is OK with NULL strings */
  735. binddn = slapi_ch_strdup(conn->binddn);
  736. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  737. "%s: binddn = %s, passwd = %s\n",
  738. agmt_get_long_name(conn->agmt),
  739. binddn?binddn:"NULL", creds->bv_val?creds->bv_val:"NULL");
  740. /* Set some options for the connection. */
  741. optdata = LDAP_DEREF_NEVER; /* Don't dereference aliases */
  742. ldap_set_option(conn->ld, LDAP_OPT_DEREF, &optdata);
  743. optdata = LDAP_VERSION3; /* We need LDAP version 3 */
  744. ldap_set_option(conn->ld, LDAP_OPT_PROTOCOL_VERSION, &optdata);
  745. /* Don't chase any referrals (although we shouldn't get any) */
  746. ldap_set_option(conn->ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
  747. /* override the default timeout with the specified timeout */
  748. io_timeout_ms = conn->timeout.tv_sec * 1000 + conn->timeout.tv_usec / 1000;
  749. prldap_set_session_option(conn->ld, NULL, PRLDAP_OPT_IO_MAX_TIMEOUT,
  750. io_timeout_ms);
  751. /* We've got an ld. Now bind to the server. */
  752. conn->last_operation = CONN_BIND;
  753. }
  754. if ( bind_and_check_pwp(conn, binddn, conn->plain) == CONN_OPERATION_FAILED )
  755. {
  756. conn->last_ldap_error = ldap_get_lderrno (conn->ld, NULL, NULL);
  757. conn->state = STATE_DISCONNECTED;
  758. return_value = CONN_OPERATION_FAILED;
  759. }
  760. else
  761. {
  762. conn->last_ldap_error = ldap_rc = LDAP_SUCCESS;
  763. conn->state = STATE_CONNECTED;
  764. return_value = CONN_OPERATION_SUCCESS;
  765. }
  766. ber_bvfree(creds);
  767. creds = NULL;
  768. slapi_ch_free((void**)&binddn);
  769. if(return_value == CONN_OPERATION_FAILED)
  770. {
  771. close_connection_internal(conn);
  772. } else
  773. {
  774. conn->last_ldap_error = ldap_rc = LDAP_SUCCESS;
  775. conn->state = STATE_CONNECTED;
  776. }
  777. return return_value;
  778. }
  779. static void
  780. close_connection_internal(Repl_Connection *conn)
  781. {
  782. if (NULL != conn->ld)
  783. {
  784. /* Since we call slapi_ldap_init,
  785. we must call slapi_ldap_unbind */
  786. slapi_ldap_unbind(conn->ld);
  787. }
  788. conn->ld = NULL;
  789. conn->state = STATE_DISCONNECTED;
  790. conn->status = STATUS_DISCONNECTED;
  791. conn->supports_ds50_repl = -1;
  792. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  793. "%s: Disconnected from the consumer\n", agmt_get_long_name(conn->agmt));
  794. }
  795. void
  796. conn_disconnect(Repl_Connection *conn)
  797. {
  798. PR_ASSERT(NULL != conn);
  799. PR_Lock(conn->lock);
  800. close_connection_internal(conn);
  801. PR_Unlock(conn->lock);
  802. }
  803. /*
  804. * Determine if the remote replica supports DS 5.0 replication.
  805. * Return codes:
  806. * CONN_SUPPORTS_DS5_REPL - the remote replica suport DS5 replication
  807. * CONN_DOES_NOT_SUPPORT_DS5_REPL - the remote replica does not
  808. * support DS5 replication.
  809. * CONN_OPERATION_FAILED - it could not be determined if the remote
  810. * replica supports DS5 replication.
  811. * CONN_NOT_CONNECTED - no connection was active.
  812. */
  813. ConnResult
  814. conn_replica_supports_ds5_repl(Repl_Connection *conn)
  815. {
  816. ConnResult return_value;
  817. int ldap_rc;
  818. if (conn_connected(conn))
  819. {
  820. if (conn->supports_ds50_repl == -1) {
  821. LDAPMessage *res = NULL;
  822. LDAPMessage *entry = NULL;
  823. char *attrs[] = {"supportedcontrol", "supportedextension", NULL};
  824. conn->status = STATUS_SEARCHING;
  825. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  826. "(objectclass=*)", attrs, 0 /* attrsonly */,
  827. NULL /* server controls */, NULL /* client controls */,
  828. &conn->timeout, LDAP_NO_LIMIT, &res);
  829. if (LDAP_SUCCESS == ldap_rc)
  830. {
  831. conn->supports_ds50_repl = 0;
  832. entry = ldap_first_entry(conn->ld, res);
  833. if (!attribute_string_value_present(conn->ld, entry, "supportedcontrol", REPL_NSDS50_UPDATE_INFO_CONTROL_OID))
  834. {
  835. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  836. }
  837. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_START_NSDS50_REPLICATION_REQUEST_OID))
  838. {
  839. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  840. }
  841. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_END_NSDS50_REPLICATION_REQUEST_OID))
  842. {
  843. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  844. }
  845. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_ENTRY_REQUEST_OID))
  846. {
  847. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  848. }
  849. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_RESPONSE_OID))
  850. {
  851. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  852. }
  853. else
  854. {
  855. conn->supports_ds50_repl = 1;
  856. return_value = CONN_SUPPORTS_DS5_REPL;
  857. }
  858. }
  859. else
  860. {
  861. if (IS_DISCONNECT_ERROR(ldap_rc))
  862. {
  863. conn->last_ldap_error = ldap_rc; /* specific reason */
  864. conn_disconnect(conn);
  865. return_value = CONN_NOT_CONNECTED;
  866. }
  867. else
  868. {
  869. return_value = CONN_OPERATION_FAILED;
  870. }
  871. }
  872. if (NULL != res)
  873. ldap_msgfree(res);
  874. }
  875. else {
  876. return_value = conn->supports_ds50_repl ? CONN_SUPPORTS_DS5_REPL : CONN_DOES_NOT_SUPPORT_DS5_REPL;
  877. }
  878. }
  879. else
  880. {
  881. /* Not connected */
  882. return_value = CONN_NOT_CONNECTED;
  883. }
  884. return return_value;
  885. }
  886. /*
  887. * Return 1 if "value" is a value of attribute type "type" in entry "entry".
  888. * Otherwise, return 0.
  889. */
  890. static int
  891. attribute_string_value_present(LDAP *ld, LDAPMessage *entry, const char *type,
  892. const char *value)
  893. {
  894. int return_value = 0;
  895. if (NULL != entry)
  896. {
  897. char *atype = NULL;
  898. BerElement *ber = NULL;
  899. atype = ldap_first_attribute(ld, entry, &ber);
  900. while (NULL != atype && 0 == return_value)
  901. {
  902. if (strcasecmp(atype, type) == 0)
  903. {
  904. char **strvals = ldap_get_values(ld, entry, atype);
  905. int i;
  906. for (i = 0; return_value == 0 && NULL != strvals && NULL != strvals[i]; i++)
  907. {
  908. if (strcmp(strvals[i], value) == 0)
  909. {
  910. return_value = 1;
  911. }
  912. }
  913. if (NULL != strvals)
  914. {
  915. ldap_value_free(strvals);
  916. }
  917. }
  918. ldap_memfree(atype);
  919. atype = ldap_next_attribute(ld, entry, ber);
  920. }
  921. if (NULL != ber)
  922. ldap_ber_free(ber, 0);
  923. /* The last atype has not been freed yet */
  924. if (NULL != atype)
  925. ldap_memfree(atype);
  926. }
  927. return return_value;
  928. }
  929. /*
  930. * Read the remote server's schema entry, then read the local schema entry,
  931. * and compare the nsschemacsn attribute. If the local csn is newer, or
  932. * the remote csn is absent, push the schema down to the consumer.
  933. * Return codes:
  934. * CONN_SCHEMA_UPDATED if the schema was pushed successfully
  935. * CONN_SCHEMA_NO_UPDATE_NEEDED if the schema was as new or newer than
  936. * the local server's schema
  937. * CONN_OPERATION_FAILED if an error occurred
  938. * CONN_NOT_CONNECTED if no connection was active
  939. * NOTE: Should only be called when a replication session has been
  940. * established by sending a startReplication extended operation.
  941. */
  942. ConnResult
  943. conn_push_schema(Repl_Connection *conn, CSN **remotecsn)
  944. {
  945. ConnResult return_value = CONN_OPERATION_SUCCESS;
  946. char *nsschemacsn = "nsschemacsn";
  947. Slapi_Entry **entries = NULL;
  948. Slapi_Entry *schema_entry = NULL;
  949. int push_schema = 1; /* Assume we need to push for now */
  950. int local_error = 0; /* No local error encountered yet */
  951. int remote_error = 0; /* No remote error encountered yet */
  952. CSN *localcsn = NULL;
  953. Slapi_PBlock *spb = NULL;
  954. char localcsnstr[CSN_STRSIZE + 1] = {0};
  955. if (!conn_connected(conn))
  956. {
  957. return_value = CONN_NOT_CONNECTED;
  958. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  959. "%s: Schema replication update failed: not connected to consumer\n",
  960. agmt_get_long_name(conn->agmt));
  961. }
  962. else
  963. {
  964. localcsn = dup_global_schema_csn();
  965. if (NULL == localcsn)
  966. {
  967. /* Local server has epoch CSN, so don't push schema */
  968. return_value = CONN_SCHEMA_NO_UPDATE_NEEDED;
  969. }
  970. else if ( remotecsn && *remotecsn && csn_compare(localcsn, *remotecsn) <= 0 )
  971. {
  972. /* Local server schema is not newer than the remote one */
  973. return_value = CONN_SCHEMA_NO_UPDATE_NEEDED;
  974. }
  975. else
  976. {
  977. struct berval **remote_schema_csn_bervals = NULL;
  978. /* Get remote server's schema */
  979. return_value = conn_read_entry_attribute(conn, "cn=schema", nsschemacsn,
  980. &remote_schema_csn_bervals);
  981. if (CONN_OPERATION_SUCCESS == return_value)
  982. {
  983. if (NULL != remote_schema_csn_bervals && NULL != remote_schema_csn_bervals[0])
  984. {
  985. char remotecsnstr[CSN_STRSIZE + 1] = {0};
  986. memcpy(remotecsnstr, remote_schema_csn_bervals[0]->bv_val,
  987. remote_schema_csn_bervals[0]->bv_len);
  988. remotecsnstr[remote_schema_csn_bervals[0]->bv_len] = '\0';
  989. *remotecsn = csn_new_by_string(remotecsnstr);
  990. if (NULL != remotecsn && (csn_compare(localcsn, *remotecsn) <= 0))
  991. {
  992. return_value = CONN_SCHEMA_NO_UPDATE_NEEDED;
  993. }
  994. /* Need to free the remote_schema_csn_bervals */
  995. ber_bvecfree(remote_schema_csn_bervals);
  996. }
  997. }
  998. }
  999. }
  1000. if (CONN_OPERATION_SUCCESS == return_value)
  1001. {
  1002. /* We know we need to push the schema out. */
  1003. LDAPMod ocmod = {0};
  1004. LDAPMod atmod = {0};
  1005. LDAPMod csnmod = {0};
  1006. LDAPMod *attrs[4] = {0};
  1007. int numvalues = 0;
  1008. Slapi_Attr *attr = NULL;
  1009. char *csnvalues[2];
  1010. ocmod.mod_type = "objectclasses";
  1011. ocmod.mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  1012. ocmod.mod_bvalues = NULL;
  1013. atmod.mod_type = "attributetypes";
  1014. atmod.mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  1015. atmod.mod_bvalues = NULL;
  1016. csnmod.mod_type = nsschemacsn;
  1017. csnmod.mod_op = LDAP_MOD_REPLACE;
  1018. csn_as_string (localcsn, PR_FALSE, localcsnstr);
  1019. csnvalues[0] = localcsnstr;
  1020. csnvalues[1] = NULL;
  1021. csnmod.mod_values = csnvalues;
  1022. attrs[0] = &ocmod;
  1023. attrs[1] = &atmod;
  1024. attrs[2] = &csnmod;
  1025. attrs[3] = NULL;
  1026. return_value = CONN_OPERATION_FAILED; /* assume failure */
  1027. /* Get local schema */
  1028. spb = slapi_search_internal("cn=schema", LDAP_SCOPE_BASE, "(objectclass=*)",
  1029. NULL /* controls */, NULL /* schema_csn_attrs */, 0 /* attrsonly */);
  1030. slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1031. if (NULL == entries || NULL == entries[0])
  1032. {
  1033. /* Whoops - couldn't read our own schema! */
  1034. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1035. "%s: Error: unable to read local schema definitions.\n",
  1036. agmt_get_long_name(conn->agmt));
  1037. return_value = CONN_OPERATION_FAILED;
  1038. }
  1039. else
  1040. {
  1041. schema_entry = entries[0];
  1042. if (slapi_entry_attr_find(schema_entry, "objectclasses", &attr) != -1)
  1043. {
  1044. int i, ind;
  1045. Slapi_Value *value;
  1046. slapi_attr_get_numvalues(attr, &numvalues);
  1047. ocmod.mod_bvalues = (struct berval **)slapi_ch_malloc((numvalues + 1) *
  1048. sizeof(struct berval *));
  1049. for (i = 0, ind = slapi_attr_first_value(attr, &value);
  1050. ind != -1; ind = slapi_attr_next_value(attr, ind, &value), i++)
  1051. {
  1052. /* XXXggood had to cast away const below */
  1053. ocmod.mod_bvalues[i] = (struct berval *)slapi_value_get_berval(value);
  1054. }
  1055. ocmod.mod_bvalues[numvalues] = NULL;
  1056. if (slapi_entry_attr_find(schema_entry, "attributetypes", &attr) != -1)
  1057. {
  1058. ConnResult result;
  1059. slapi_attr_get_numvalues(attr, &numvalues);
  1060. atmod.mod_bvalues = (struct berval **)slapi_ch_malloc((numvalues + 1) *
  1061. sizeof(struct berval *));
  1062. for (i = 0, ind = slapi_attr_first_value(attr, &value);
  1063. ind != -1; ind = slapi_attr_next_value(attr, ind, &value), i++)
  1064. {
  1065. /* XXXggood had to cast away const below */
  1066. atmod.mod_bvalues[i] = (struct berval *)slapi_value_get_berval(value);
  1067. }
  1068. atmod.mod_bvalues[numvalues] = NULL;
  1069. result = conn_send_modify(conn, "cn=schema", attrs, NULL, NULL);
  1070. switch (result)
  1071. {
  1072. case CONN_OPERATION_FAILED:
  1073. {
  1074. int ldaperr = -1, optype = -1;
  1075. conn_get_error(conn, &optype, &ldaperr);
  1076. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1077. "%s: Schema replication update failed: %s\n",
  1078. agmt_get_long_name(conn->agmt),
  1079. ldaperr == -1 ? "Unknown Error" : ldap_err2string(ldaperr));
  1080. }
  1081. case CONN_NOT_CONNECTED:
  1082. return_value = CONN_NOT_CONNECTED;
  1083. break;
  1084. case CONN_OPERATION_SUCCESS:
  1085. return_value = CONN_SCHEMA_UPDATED;
  1086. break;
  1087. }
  1088. }
  1089. }
  1090. else
  1091. {
  1092. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1093. "%s: Schema replication update failed: "
  1094. "unable to prepare schema entry for transmission.\n",
  1095. agmt_get_long_name(conn->agmt));
  1096. }
  1097. }
  1098. /* slapi_ch_free accepts NULL pointer */
  1099. slapi_ch_free((void **)&ocmod.mod_bvalues);
  1100. slapi_ch_free((void **)&atmod.mod_bvalues);
  1101. }
  1102. if (NULL != spb)
  1103. {
  1104. slapi_free_search_results_internal(spb);
  1105. slapi_pblock_destroy(spb);
  1106. spb = NULL;
  1107. }
  1108. if (NULL != localcsn)
  1109. {
  1110. csn_free(&localcsn);
  1111. }
  1112. return return_value;
  1113. }
  1114. void
  1115. conn_set_timeout(Repl_Connection *conn, long timeout)
  1116. {
  1117. PR_ASSERT(NULL != conn);
  1118. PR_ASSERT(timeout >= 0);
  1119. PR_Lock(conn->lock);
  1120. conn->timeout.tv_sec = timeout;
  1121. PR_Unlock(conn->lock);
  1122. }
  1123. void conn_set_agmt_changed(Repl_Connection *conn)
  1124. {
  1125. PR_ASSERT(NULL != conn);
  1126. PR_Lock(conn->lock);
  1127. if (NULL != conn->agmt)
  1128. conn->flag_agmt_changed = 1;
  1129. PR_Unlock(conn->lock);
  1130. }
  1131. /*
  1132. * Check the result of an ldap_simple_bind operation to see we it
  1133. * contains the expiration controls
  1134. * return: -1 error, not bound
  1135. * 0, OK bind has succeeded
  1136. */
  1137. static int
  1138. bind_and_check_pwp(Repl_Connection *conn, char * binddn, char *password)
  1139. {
  1140. LDAPControl **ctrls = NULL;
  1141. LDAPMessage *res = NULL;
  1142. char *errmsg = NULL;
  1143. LDAP *ld = conn->ld;
  1144. int msgid;
  1145. int *msgidAdr = &msgid;
  1146. int rc;
  1147. char * optype; /* ldap_simple_bind or slapd_SSL_client_bind */
  1148. if ( conn->transport_flags == TRANSPORT_FLAG_SSL )
  1149. {
  1150. char *auth;
  1151. optype = "ldap_sasl_bind";
  1152. if ( conn->bindmethod == BINDMETHOD_SSL_CLIENTAUTH )
  1153. {
  1154. rc = slapd_sasl_ext_client_bind(conn->ld, &msgidAdr);
  1155. auth = "SSL client authentication";
  1156. if ( rc == LDAP_SUCCESS )
  1157. {
  1158. if (conn->last_ldap_error != rc)
  1159. {
  1160. conn->last_ldap_error = rc;
  1161. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1162. "%s: Replication bind with %s resumed\n",
  1163. agmt_get_long_name(conn->agmt), auth);
  1164. }
  1165. }
  1166. else
  1167. {
  1168. /* Do not report the same error over and over again */
  1169. if (conn->last_ldap_error != rc)
  1170. {
  1171. conn->last_ldap_error = rc;
  1172. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1173. "%s: Replication bind with %s failed: LDAP error %d (%s)\n",
  1174. agmt_get_long_name(conn->agmt), auth, rc,
  1175. ldap_err2string(rc));
  1176. }
  1177. return (CONN_OPERATION_FAILED);
  1178. }
  1179. }
  1180. else
  1181. {
  1182. if( ( msgid = do_simple_bind( conn, ld, binddn, password ) ) == -1 )
  1183. {
  1184. return (CONN_OPERATION_FAILED);
  1185. }
  1186. }
  1187. }
  1188. else
  1189. {
  1190. optype = "ldap_simple_bind";
  1191. if( ( msgid = do_simple_bind( conn, ld, binddn, password ) ) == -1 )
  1192. {
  1193. return (CONN_OPERATION_FAILED);
  1194. }
  1195. }
  1196. /* Wait for the result */
  1197. if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &res ) == -1 )
  1198. {
  1199. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1200. "%s: Received error from consumer for %s operation\n",
  1201. agmt_get_long_name(conn->agmt), optype);
  1202. return (CONN_OPERATION_FAILED);
  1203. }
  1204. /* Don't check ldap_result against 0 because, no timeout is specified */
  1205. /* Free res as we won't use it any longer */
  1206. if ( ldap_parse_result( ld, res, &rc, NULL, NULL, NULL, &ctrls, 1 /* Free res */)
  1207. != LDAP_SUCCESS )
  1208. {
  1209. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1210. "%s: Received error from consumer for %s operation\n",
  1211. agmt_get_long_name(conn->agmt), optype);
  1212. return (CONN_OPERATION_FAILED);
  1213. }
  1214. if ( rc == LDAP_SUCCESS )
  1215. {
  1216. if ( ctrls )
  1217. {
  1218. int i;
  1219. for( i = 0; ctrls[ i ] != NULL; ++i )
  1220. {
  1221. if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRED)) )
  1222. {
  1223. /* Bind is successfull but password has expired */
  1224. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1225. "%s: Succesfully bound %s to consumer, "
  1226. "but password has expired on consumer.\n",
  1227. agmt_get_long_name(conn->agmt), binddn);
  1228. }
  1229. else if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRING)) )
  1230. {
  1231. /* The password is expiring in n seconds */
  1232. if ( (ctrls[ i ]->ldctl_value.bv_val != NULL) &&
  1233. (ctrls[ i ]->ldctl_value.bv_len > 0) )
  1234. {
  1235. int password_expiring = atoi( ctrls[ i ]->ldctl_value.bv_val );
  1236. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1237. "%s: Succesfully bound %s to consumer, "
  1238. "but password is expiring on consumer in %d seconds.\n",
  1239. agmt_get_long_name(conn->agmt), binddn, password_expiring);
  1240. }
  1241. }
  1242. }
  1243. ldap_controls_free( ctrls );
  1244. }
  1245. return (CONN_OPERATION_SUCCESS);
  1246. }
  1247. else
  1248. {
  1249. /* errmsg is a pointer directly into the ld structure - do not free */
  1250. rc = ldap_get_lderrno( ld, NULL, &errmsg );
  1251. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1252. "%s: Replication bind to %s on consumer failed: %d (%s)\n",
  1253. agmt_get_long_name(conn->agmt), binddn, rc, errmsg);
  1254. conn->last_ldap_error = rc; /* specific error */
  1255. return (CONN_OPERATION_FAILED);
  1256. }
  1257. }
  1258. static int
  1259. do_simple_bind (Repl_Connection *conn, LDAP *ld, char * binddn, char *password)
  1260. {
  1261. int msgid;
  1262. if( ( msgid = ldap_simple_bind( ld, binddn, password ) ) == -1 )
  1263. {
  1264. char *ldaperrtext = NULL;
  1265. int ldaperr;
  1266. int prerr = PR_GetError();
  1267. ldaperr = ldap_get_lderrno( ld, NULL, &ldaperrtext );
  1268. /* Do not report the same error over and over again */
  1269. if (conn->last_ldap_error != ldaperr)
  1270. {
  1271. conn->last_ldap_error = ldaperr;
  1272. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1273. "%s: Simple bind failed, "
  1274. SLAPI_COMPONENT_NAME_LDAPSDK " error %d (%s), "
  1275. SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
  1276. agmt_get_long_name(conn->agmt),
  1277. ldaperr, ldaperrtext ? ldaperrtext : ldap_err2string(ldaperr),
  1278. prerr, slapd_pr_strerror(prerr));
  1279. }
  1280. }
  1281. else if (conn->last_ldap_error != LDAP_SUCCESS)
  1282. {
  1283. conn->last_ldap_error = LDAP_SUCCESS;
  1284. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1285. "%s: Simple bind resumed\n",
  1286. agmt_get_long_name(conn->agmt));
  1287. }
  1288. return msgid;
  1289. }
  1290. void
  1291. repl5_set_debug_timeout(const char *val)
  1292. {
  1293. /* val looks like this: seconds[:debuglevel] */
  1294. /* seconds is the number of seconds to wait until turning on the debug level */
  1295. /* this should be less than the ldap connection timeout (default 10 minutes) */
  1296. /* the optional debug level is the error log debugging level to use (default repl) */
  1297. if (val) {
  1298. const char *p = strchr(val, ':');
  1299. s_debug_timeout = atoi(val);
  1300. if (p) {
  1301. s_debug_level = atoi(p+1);
  1302. } else {
  1303. s_debug_level = 8192;
  1304. }
  1305. }
  1306. }
  1307. static time_t
  1308. PRTime2time_t (PRTime tm)
  1309. {
  1310. PRInt64 rt;
  1311. PR_ASSERT (tm);
  1312. LL_DIV(rt, tm, PR_USEC_PER_SEC);
  1313. return (time_t)rt;
  1314. }
  1315. static Slapi_Eq_Context
  1316. repl5_start_debug_timeout(int *setlevel)
  1317. {
  1318. Slapi_Eq_Context eqctx = 0;
  1319. if (s_debug_timeout && s_debug_level) {
  1320. time_t now = time(NULL);
  1321. eqctx = slapi_eq_once(repl5_debug_timeout_callback, setlevel,
  1322. s_debug_timeout + now);
  1323. }
  1324. return eqctx;
  1325. }
  1326. static void
  1327. repl5_stop_debug_timeout(Slapi_Eq_Context eqctx, int *setlevel)
  1328. {
  1329. char buf[20];
  1330. char msg[SLAPI_DSE_RETURNTEXT_SIZE];
  1331. if (eqctx && !*setlevel) {
  1332. int found = slapi_eq_cancel(eqctx);
  1333. }
  1334. if (s_debug_timeout && s_debug_level && *setlevel) {
  1335. void config_set_errorlog_level(const char *type, char *buf, char *msg, int apply);
  1336. sprintf(buf, "%d", 0);
  1337. config_set_errorlog_level("nsslapd-errorlog-level", buf, msg, 1);
  1338. }
  1339. }
  1340. static void
  1341. repl5_debug_timeout_callback(time_t when, void *arg)
  1342. {
  1343. int *setlevel = (int *)arg;
  1344. void config_set_errorlog_level(const char *type, char *buf, char *msg, int apply);
  1345. char buf[20];
  1346. char msg[SLAPI_DSE_RETURNTEXT_SIZE];
  1347. *setlevel = 1;
  1348. sprintf(buf, "%d", s_debug_level);
  1349. config_set_errorlog_level("nsslapd-errorlog-level", buf, msg, 1);
  1350. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1351. "repl5_debug_timeout_callback: set debug level to %d at %d\n",
  1352. s_debug_level, when);
  1353. }