repl5_connection.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  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. #include "ldap-extension.h"
  16. typedef struct repl_connection
  17. {
  18. char *hostname;
  19. int port;
  20. char *binddn;
  21. int bindmethod;
  22. int state;
  23. int last_operation;
  24. int last_ldap_error;
  25. const char *status;
  26. char *last_ldap_errmsg;
  27. PRUint32 transport_flags;
  28. LDAP *ld;
  29. int supports_ldapv3; /* 1 if does, 0 if doesn't, -1 if not determined */
  30. int supports_ds50_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  31. int supports_ds40_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  32. int supports_ds71_repl; /* 1 if does, 0 if doesn't, -1 if not determined */
  33. int linger_time; /* time in seconds to leave an idle connection open */
  34. PRBool linger_active;
  35. Slapi_Eq_Context *linger_event;
  36. PRBool delete_after_linger;
  37. int refcnt;
  38. const Repl_Agmt *agmt;
  39. PRLock *lock;
  40. struct timeval timeout;
  41. int flag_agmt_changed;
  42. char *plain;
  43. } repl_connection;
  44. /* #define DEFAULT_LINGER_TIME (5 * 60) */ /* 5 minutes */
  45. #define DEFAULT_LINGER_TIME (60)
  46. /* Controls we add on every outbound operation */
  47. static LDAPControl manageDSAITControl = {LDAP_CONTROL_MANAGEDSAIT, {0, ""}, '\0'};
  48. static int attribute_string_value_present(LDAP *ld, LDAPMessage *entry,
  49. const char *type, const char *value);
  50. static int bind_and_check_pwp(Repl_Connection *conn, char * binddn, char *password);
  51. static int do_simple_bind (Repl_Connection *conn, LDAP *ld, char * binddn, char *password);
  52. static int s_debug_timeout = 0;
  53. static int s_debug_level = 0;
  54. static Slapi_Eq_Context repl5_start_debug_timeout(int *setlevel);
  55. static void repl5_stop_debug_timeout(Slapi_Eq_Context eqctx, int *setlevel);
  56. static void repl5_debug_timeout_callback(time_t when, void *arg);
  57. #define STATE_CONNECTED 600
  58. #define STATE_DISCONNECTED 601
  59. #define STATUS_DISCONNECTED "disconnected"
  60. #define STATUS_CONNECTED "connected"
  61. #define STATUS_PROCESSING_ADD "processing add operation"
  62. #define STATUS_PROCESSING_DELETE "processing delete operation"
  63. #define STATUS_PROCESSING_MODIFY "processing modify operation"
  64. #define STATUS_PROCESSING_RENAME "processing rename operation"
  65. #define STATUS_PROCESSING_EXTENDED_OPERATION "processing extended operation"
  66. #define STATUS_LINGERING "lingering"
  67. #define STATUS_SHUTTING_DOWN "shutting down"
  68. #define STATUS_BINDING "connecting and binding"
  69. #define STATUS_SEARCHING "processing search operation"
  70. #define CONN_NO_OPERATION 0
  71. #define CONN_ADD 1
  72. #define CONN_DELETE 2
  73. #define CONN_MODIFY 3
  74. #define CONN_RENAME 4
  75. #define CONN_EXTENDED_OPERATION 5
  76. #define CONN_BIND 6
  77. #define CONN_INIT 7
  78. /* These are errors returned from ldap operations which should cause us to disconnect and
  79. retry the connection later */
  80. #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)
  81. /* Forward declarations */
  82. static void close_connection_internal(Repl_Connection *conn);
  83. /*
  84. * Create a new conenction object. Returns a pointer to the object, or
  85. * NULL if an error occurs.
  86. */
  87. Repl_Connection *
  88. conn_new(Repl_Agmt *agmt)
  89. {
  90. Repl_Connection *rpc;
  91. rpc = (Repl_Connection *)slapi_ch_malloc(sizeof(repl_connection));
  92. if ((rpc->lock = PR_NewLock()) == NULL)
  93. {
  94. goto loser;
  95. }
  96. rpc->hostname = agmt_get_hostname(agmt);
  97. rpc->port = agmt_get_port(agmt);
  98. rpc->binddn = agmt_get_binddn(agmt);
  99. rpc->bindmethod = agmt_get_bindmethod(agmt);
  100. rpc->transport_flags = agmt_get_transport_flags(agmt);
  101. rpc->ld = NULL;
  102. rpc->state = STATE_DISCONNECTED;
  103. rpc->last_operation = CONN_NO_OPERATION;
  104. rpc->last_ldap_error = LDAP_SUCCESS;
  105. rpc->last_ldap_errmsg = NULL;
  106. rpc->supports_ldapv3 = -1;
  107. rpc->supports_ds40_repl = -1;
  108. rpc->supports_ds50_repl = -1;
  109. rpc->supports_ds71_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. * Return the last operation type processed by the connection
  199. * object, and the LDAP error encountered.
  200. * Beware that the error string will only be in scope and valid
  201. * before the next operation result has been read from the connection
  202. * (so don't alias the pointer).
  203. */
  204. void
  205. conn_get_error_ex(Repl_Connection *conn, int *operation, int *error, char **error_string)
  206. {
  207. PR_Lock(conn->lock);
  208. *operation = conn->last_operation;
  209. *error = conn->last_ldap_error;
  210. *error_string = conn->last_ldap_errmsg;
  211. PR_Unlock(conn->lock);
  212. }
  213. /* Returns the result (asyncronously) from an opertation and also returns that operations message ID */
  214. /* The _ex version handles a bunch of parameters (retoidp et al) that were present in the original
  215. * sync operation functions, but were never actually used) */
  216. ConnResult
  217. conn_read_result_ex(Repl_Connection *conn, char **retoidp, struct berval **retdatap, LDAPControl ***returned_controls, int *message_id, int block)
  218. {
  219. LDAPMessage *res = NULL;
  220. int setlevel = 0;
  221. int rc = 0;
  222. int return_value = 0;
  223. LDAPControl **loc_returned_controls = NULL;
  224. struct timeval local_timeout = {0};
  225. time_t time_now = 0;
  226. time_t start_time = time( NULL );
  227. int backoff_time = 1;
  228. Slapi_Eq_Context eqctx = repl5_start_debug_timeout(&setlevel);
  229. /* Here, we want to not block inside ldap_result().
  230. * Reason is that blocking there will deadlock with a
  231. * concurrent sender. We send concurrently, and hence
  232. * blocking is not good : deadlock results.
  233. * So, instead, we call ldap_result() with a zero timeout.
  234. * This makes it do a non-blocking poll and return to us
  235. * if there's no data to read.
  236. * We can then handle our timeout here by sleeping and re-trying.
  237. * In order that we do pickup results reasonably quickly,
  238. * we implement a backoff algorithm for the sleep: if we
  239. * keep getting results quickly then we won't spend much time sleeping.
  240. */
  241. while (1)
  242. {
  243. rc = ldap_result(conn->ld, LDAP_RES_ANY , 1, &local_timeout, &res);
  244. if (0 != rc)
  245. {
  246. /* Something other than a timeout happened */
  247. break;
  248. }
  249. if (block)
  250. {
  251. /* Did the connection's timeout expire ? */
  252. time_now = time( NULL );
  253. if (conn->timeout.tv_sec <= ( time_now - start_time ))
  254. {
  255. /* We timed out */
  256. rc = 0;
  257. break;
  258. }
  259. /* Otherwise we backoff */
  260. DS_Sleep(PR_MillisecondsToInterval(backoff_time));
  261. if (backoff_time < 1000)
  262. {
  263. backoff_time <<= 1;
  264. }
  265. } else
  266. {
  267. rc = 0;
  268. break;
  269. }
  270. }
  271. repl5_stop_debug_timeout(eqctx, &setlevel);
  272. if (0 == rc)
  273. {
  274. /* Timeout */
  275. rc = ldap_get_lderrno(conn->ld, NULL, NULL);
  276. conn->last_ldap_error = LDAP_TIMEOUT;
  277. return_value = CONN_TIMEOUT;
  278. }
  279. else if (-1 == rc)
  280. {
  281. /* Error */
  282. char *s = NULL;
  283. rc = ldap_get_lderrno(conn->ld, NULL, &s);
  284. conn->last_ldap_errmsg = s;
  285. conn->last_ldap_error = rc;
  286. /* some errors will require a disconnect and retry the connection
  287. later */
  288. if (IS_DISCONNECT_ERROR(rc))
  289. {
  290. conn_disconnect(conn);
  291. return_value = CONN_NOT_CONNECTED;
  292. }
  293. else
  294. {
  295. conn->status = STATUS_CONNECTED;
  296. return_value = CONN_OPERATION_FAILED;
  297. }
  298. }
  299. else
  300. {
  301. int err;
  302. char *errmsg = NULL;
  303. char **referrals = NULL;
  304. char *matched = NULL;
  305. if (message_id)
  306. {
  307. *message_id = ldap_msgid(res);
  308. }
  309. rc = ldap_parse_result(conn->ld, res, &err, &matched,
  310. &errmsg, &referrals, &loc_returned_controls,
  311. 0 /* Don't free the result */);
  312. if (IS_DISCONNECT_ERROR(rc))
  313. {
  314. conn->last_ldap_error = rc;
  315. conn_disconnect(conn);
  316. return_value = CONN_NOT_CONNECTED;
  317. }
  318. else if (IS_DISCONNECT_ERROR(err))
  319. {
  320. conn->last_ldap_error = err;
  321. conn_disconnect(conn);
  322. return_value = CONN_NOT_CONNECTED;
  323. }
  324. /* Got a result */
  325. if ((rc == LDAP_SUCCESS) && (err == LDAP_BUSY))
  326. return_value = CONN_BUSY;
  327. else if (retoidp)
  328. {
  329. if (!((rc == LDAP_SUCCESS) && (err == LDAP_BUSY)))
  330. {
  331. if (rc == LDAP_SUCCESS) {
  332. rc = ldap_parse_extended_result(conn->ld, res, retoidp,
  333. retdatap, 0 /* Don't Free it */);
  334. }
  335. conn->last_ldap_error = rc;
  336. return_value = (LDAP_SUCCESS == conn->last_ldap_error ?
  337. CONN_OPERATION_SUCCESS : CONN_OPERATION_FAILED);
  338. }
  339. }
  340. else /* regular operation, result returned */
  341. {
  342. if (NULL != returned_controls)
  343. {
  344. *returned_controls = loc_returned_controls;
  345. }
  346. if (LDAP_SUCCESS != rc)
  347. {
  348. conn->last_ldap_error = rc;
  349. }
  350. else
  351. {
  352. conn->last_ldap_error = err;
  353. }
  354. return_value = LDAP_SUCCESS == conn->last_ldap_error ? CONN_OPERATION_SUCCESS : CONN_OPERATION_FAILED;
  355. }
  356. /*
  357. * XXXggood do I need to free matched, referrals,
  358. * anything else? Or can I pass NULL for the args
  359. * I'm not interested in?
  360. */
  361. /* Good question! Meanwhile, as RTM aproaches, let's free them... */
  362. slapi_ch_free((void **) &errmsg);
  363. slapi_ch_free((void **) &matched);
  364. charray_free(referrals);
  365. conn->status = STATUS_CONNECTED;
  366. }
  367. if (res) ldap_msgfree(res);
  368. return return_value;
  369. }
  370. ConnResult
  371. conn_read_result(Repl_Connection *conn, int *message_id)
  372. {
  373. return conn_read_result_ex(conn,NULL,NULL,NULL,message_id,1);
  374. }
  375. /*
  376. * Common code to send an LDAPv3 operation and collect the result.
  377. * Return values:
  378. * CONN_OPERATION_SUCCESS - the operation succeeded
  379. * CONN_OPERATION_FAILED - the operation was sent to the consumer
  380. * and failed. Use conn_get_error() to determine the LDAP error
  381. * code.
  382. * CONN_NOT_CONNECTED - no connection is active. The caller should
  383. * use conn_connect() to connect to the replica and bind, then should
  384. * reacquire the replica (if needed).
  385. * CONN_BUSY - the server is busy with previous requests, must wait for a while
  386. * before retrying
  387. * DBDB: also returns the operation's message ID, if it was successfully sent, now that
  388. * we're reading results async.
  389. */
  390. static ConnResult
  391. perform_operation(Repl_Connection *conn, int optype, const char *dn,
  392. LDAPMod **attrs, const char *newrdn, const char *newparent,
  393. int deleteoldrdn, LDAPControl *update_control,
  394. const char *extop_oid, struct berval *extop_payload, int *message_id)
  395. {
  396. int rc;
  397. ConnResult return_value = CONN_OPERATION_FAILED;
  398. LDAPControl *server_controls[3];
  399. /* LDAPControl **loc_returned_controls; */
  400. const char *op_string = NULL;
  401. const char *extra_op_string = NULL;
  402. int msgid = 0;
  403. server_controls[0] = &manageDSAITControl;
  404. server_controls[1] = update_control;
  405. server_controls[2] = NULL;
  406. if (conn_connected(conn))
  407. {
  408. int setlevel = 0;
  409. Slapi_Eq_Context eqctx = repl5_start_debug_timeout(&setlevel);
  410. /* Because the SDK isn't really thread-safe (it can deadlock between
  411. * a thread sending an operation and a thread trying to retrieve a response
  412. * on the same connection), we need to _first_ verify that the connection
  413. * is writable. If it isn't, we can deadlock if we proceed any further...
  414. */
  415. {
  416. struct PRPollDesc pr_pd;
  417. PRIntervalTime timeout = PR_SecondsToInterval(conn->timeout.tv_sec);
  418. PRFileDesc *prfd = NULL;
  419. struct lextiof_socket_private *socketargp = NULL;
  420. PRLDAPSocketInfo soi;
  421. if ( (rc = ldap_get_option(conn->ld, LDAP_X_OPT_SOCKETARG, &socketargp)) != LDAP_SUCCESS)
  422. {
  423. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  424. "%s: Failed call to ldap_get_option in perform_operation: LDAP error %d (%s)\n",
  425. agmt_get_long_name(conn->agmt),
  426. op_string ? op_string : "NULL", rc, ldap_err2string(rc));
  427. conn->last_ldap_error = rc;
  428. return CONN_OPERATION_FAILED;
  429. }
  430. memset( &soi, 0, sizeof(soi));
  431. soi.soinfo_size = PRLDAP_SOCKETINFO_SIZE;
  432. if (LDAP_SUCCESS != (rc = prldap_get_socket_info(0, socketargp, &soi)))
  433. {
  434. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  435. "%s: Failed call to prldap_get_socket_info in perform_operation: LDAP error %d (%s)\n",
  436. agmt_get_long_name(conn->agmt),
  437. op_string ? op_string : "NULL", rc, ldap_err2string(rc));
  438. conn->last_ldap_error = rc;
  439. return CONN_OPERATION_FAILED;
  440. }
  441. prfd = soi.soinfo_prfd;
  442. /* Before we connect, the prfd can be null */
  443. if (prfd)
  444. {
  445. pr_pd.fd = prfd;
  446. pr_pd.in_flags = PR_POLL_WRITE;
  447. pr_pd.out_flags = 0;
  448. rc = PR_Poll(&pr_pd, 1, timeout);
  449. /* Did we time out ? */
  450. if (rc == 0)
  451. {
  452. return CONN_TIMEOUT;
  453. }
  454. }
  455. }
  456. conn->last_operation = optype;
  457. switch (optype)
  458. {
  459. case CONN_ADD:
  460. conn->status = STATUS_PROCESSING_ADD;
  461. op_string = "add";
  462. rc = ldap_add_ext(conn->ld, dn, attrs, server_controls,
  463. NULL /* clientctls */, &msgid);
  464. break;
  465. case CONN_MODIFY:
  466. conn->status = STATUS_PROCESSING_MODIFY;
  467. op_string = "modify";
  468. rc = ldap_modify_ext(conn->ld, dn, attrs, server_controls,
  469. NULL /* clientctls */, &msgid);
  470. break;
  471. case CONN_DELETE:
  472. conn->status = STATUS_PROCESSING_DELETE;
  473. op_string = "delete";
  474. rc = ldap_delete_ext(conn->ld, dn, server_controls,
  475. NULL /* clientctls */, &msgid);
  476. break;
  477. case CONN_RENAME:
  478. conn->status = STATUS_PROCESSING_RENAME;
  479. op_string = "rename";
  480. rc = ldap_rename(conn->ld, dn, newrdn, newparent, deleteoldrdn,
  481. server_controls, NULL /* clientctls */, &msgid);
  482. break;
  483. case CONN_EXTENDED_OPERATION:
  484. conn->status = STATUS_PROCESSING_EXTENDED_OPERATION;
  485. op_string = "extended";
  486. extra_op_string = extop_oid;
  487. rc = ldap_extended_operation(conn->ld, extop_oid, extop_payload,
  488. server_controls, NULL /* clientctls */, &msgid);
  489. }
  490. repl5_stop_debug_timeout(eqctx, &setlevel);
  491. if (LDAP_SUCCESS == rc)
  492. {
  493. /* DBDB: The code that used to be here has been moved for async operation
  494. * Results are now picked up in another thread. All we need to do here is
  495. * queue the operation details in the outstanding operation list.
  496. */
  497. return_value = CONN_OPERATION_SUCCESS;
  498. }
  499. else
  500. {
  501. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  502. "%s: Failed to send %s operation: LDAP error %d (%s)\n",
  503. agmt_get_long_name(conn->agmt),
  504. op_string ? op_string : "NULL", rc, ldap_err2string(rc));
  505. conn->last_ldap_error = rc;
  506. if (IS_DISCONNECT_ERROR(rc))
  507. {
  508. conn_disconnect(conn);
  509. return_value = CONN_NOT_CONNECTED;
  510. }
  511. else
  512. {
  513. conn->status = STATUS_CONNECTED;
  514. return_value = CONN_OPERATION_FAILED;
  515. }
  516. }
  517. }
  518. else
  519. {
  520. /* conn->last_ldap_error has been set to a more specific value
  521. * in conn_connected()
  522. * conn->last_ldap_error = LDAP_SERVER_DOWN;
  523. */
  524. return_value = CONN_NOT_CONNECTED;
  525. }
  526. if (message_id)
  527. {
  528. *message_id = msgid;
  529. }
  530. return return_value;
  531. }
  532. /*
  533. * Send an LDAP add operation.
  534. */
  535. ConnResult
  536. conn_send_add(Repl_Connection *conn, const char *dn, LDAPMod **attrs,
  537. LDAPControl *update_control, int *message_id)
  538. {
  539. return perform_operation(conn, CONN_ADD, dn, attrs, NULL /* newrdn */,
  540. NULL /* newparent */, 0 /* deleteoldrdn */, update_control,
  541. NULL /* extop OID */, NULL /* extop payload */, message_id);
  542. }
  543. /*
  544. * Send an LDAP delete operation.
  545. */
  546. ConnResult
  547. conn_send_delete(Repl_Connection *conn, const char *dn,
  548. LDAPControl *update_control, int *message_id)
  549. {
  550. return perform_operation(conn, CONN_DELETE, dn, NULL /* attrs */,
  551. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  552. update_control, NULL /* extop OID */, NULL /* extop payload */, message_id);
  553. }
  554. /*
  555. * Send an LDAP modify operation.
  556. */
  557. ConnResult
  558. conn_send_modify(Repl_Connection *conn, const char *dn, LDAPMod **mods,
  559. LDAPControl *update_control, int *message_id)
  560. {
  561. return perform_operation(conn, CONN_MODIFY, dn, mods, NULL /* newrdn */,
  562. NULL /* newparent */, 0 /* deleteoldrdn */, update_control,
  563. NULL /* extop OID */, NULL /* extop payload */, message_id);
  564. }
  565. /*
  566. * Send an LDAP moddn operation.
  567. */
  568. ConnResult
  569. conn_send_rename(Repl_Connection *conn, const char *dn,
  570. const char *newrdn, const char *newparent, int deleteoldrdn,
  571. LDAPControl *update_control, int *message_id)
  572. {
  573. return perform_operation(conn, CONN_RENAME, dn, NULL /* attrs */,
  574. newrdn, newparent, deleteoldrdn, update_control,
  575. NULL /* extop OID */, NULL /* extop payload */, message_id);
  576. }
  577. /*
  578. * Send an LDAP extended operation.
  579. */
  580. ConnResult
  581. conn_send_extended_operation(Repl_Connection *conn, const char *extop_oid,
  582. struct berval *payload,
  583. LDAPControl *update_control, int *message_id)
  584. {
  585. return perform_operation(conn, CONN_EXTENDED_OPERATION, NULL /* dn */, NULL /* attrs */,
  586. NULL /* newrdn */, NULL /* newparent */, 0 /* deleteoldrdn */,
  587. update_control, extop_oid, payload, message_id);
  588. }
  589. /*
  590. * Synchronously read an entry and return a specific attribute's values.
  591. * Returns CONN_OPERATION_SUCCESS if successful. Returns
  592. * CONN_OPERATION_FAILED if the operation was sent but an LDAP error
  593. * occurred (conn->last_ldap_error is set in this case), and
  594. * CONN_NOT_CONNECTED if no connection was active.
  595. *
  596. * The caller must free the returned_bvals.
  597. */
  598. ConnResult
  599. conn_read_entry_attribute(Repl_Connection *conn, const char *dn,
  600. char *type, struct berval ***returned_bvals)
  601. {
  602. ConnResult return_value;
  603. int ldap_rc;
  604. LDAPControl *server_controls[2];
  605. LDAPMessage *res = NULL;
  606. char *attrs[2];
  607. PR_ASSERT(NULL != type);
  608. if (conn_connected(conn))
  609. {
  610. server_controls[0] = &manageDSAITControl;
  611. server_controls[1] = NULL;
  612. attrs[0] = type;
  613. attrs[1] = NULL;
  614. ldap_rc = ldap_search_ext_s(conn->ld, dn, LDAP_SCOPE_BASE,
  615. "(objectclass=*)", attrs, 0 /* attrsonly */,
  616. server_controls, NULL /* client controls */,
  617. &conn->timeout, 0 /* sizelimit */, &res);
  618. if (LDAP_SUCCESS == ldap_rc)
  619. {
  620. LDAPMessage *entry = ldap_first_entry(conn->ld, res);
  621. if (NULL != entry)
  622. {
  623. *returned_bvals = ldap_get_values_len(conn->ld, entry, type);
  624. }
  625. return_value = CONN_OPERATION_SUCCESS;
  626. }
  627. else if (IS_DISCONNECT_ERROR(ldap_rc))
  628. {
  629. conn_disconnect(conn);
  630. return_value = CONN_NOT_CONNECTED;
  631. }
  632. else
  633. {
  634. return_value = CONN_OPERATION_FAILED;
  635. }
  636. conn->last_ldap_error = ldap_rc;
  637. if (NULL != res)
  638. {
  639. ldap_msgfree(res);
  640. res = NULL;
  641. }
  642. }
  643. else
  644. {
  645. return_value = CONN_NOT_CONNECTED;
  646. }
  647. return return_value;
  648. }
  649. /*
  650. * Return an pointer to a string describing the connection's status.
  651. */
  652. const char *
  653. conn_get_status(Repl_Connection *conn)
  654. {
  655. return conn->status;
  656. }
  657. /*
  658. * Cancel any outstanding linger timer. Should be called when
  659. * a replication session is beginning.
  660. */
  661. void
  662. conn_cancel_linger(Repl_Connection *conn)
  663. {
  664. PR_ASSERT(NULL != conn);
  665. PR_Lock(conn->lock);
  666. if (conn->linger_active)
  667. {
  668. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  669. "%s: Cancelling linger on the connection\n",
  670. agmt_get_long_name(conn->agmt));
  671. conn->linger_active = PR_FALSE;
  672. if (slapi_eq_cancel(conn->linger_event) == 1)
  673. {
  674. conn->refcnt--;
  675. }
  676. conn->linger_event = NULL;
  677. conn->status = STATUS_CONNECTED;
  678. }
  679. else
  680. {
  681. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  682. "%s: No linger to cancel on the connection\n",
  683. agmt_get_long_name(conn->agmt));
  684. }
  685. PR_Unlock(conn->lock);
  686. }
  687. /*
  688. * Called when our linger timeout timer expires. This means
  689. * we should check to see if perhaps the connection's become
  690. * active again, in which case we do nothing. Otherwise,
  691. * we close the connection.
  692. */
  693. static void
  694. linger_timeout(time_t event_time, void *arg)
  695. {
  696. PRBool delete_now;
  697. Repl_Connection *conn = (Repl_Connection *)arg;
  698. PR_ASSERT(NULL != conn);
  699. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  700. "%s: Linger timeout has expired on the connection\n",
  701. agmt_get_long_name(conn->agmt));
  702. PR_Lock(conn->lock);
  703. if (conn->linger_active)
  704. {
  705. conn->linger_active = PR_FALSE;
  706. conn->linger_event = NULL;
  707. close_connection_internal(conn);
  708. }
  709. delete_now = conn->delete_after_linger;
  710. PR_Unlock(conn->lock);
  711. if (delete_now)
  712. {
  713. conn_delete_internal(conn);
  714. }
  715. }
  716. /*
  717. * Indicate that a session is ending. The linger timer starts when
  718. * this function is called.
  719. */
  720. void
  721. conn_start_linger(Repl_Connection *conn)
  722. {
  723. time_t now;
  724. PR_ASSERT(NULL != conn);
  725. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  726. "%s: Beginning linger on the connection\n",
  727. agmt_get_long_name(conn->agmt));
  728. if (!conn_connected(conn))
  729. {
  730. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  731. "%s: No linger on the closed conn\n",
  732. agmt_get_long_name(conn->agmt));
  733. return;
  734. }
  735. time(&now);
  736. PR_Lock(conn->lock);
  737. if (conn->linger_active)
  738. {
  739. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  740. "%s: Linger already active on the connection\n",
  741. agmt_get_long_name(conn->agmt));
  742. }
  743. else
  744. {
  745. conn->linger_active = PR_TRUE;
  746. conn->linger_event = slapi_eq_once(linger_timeout, conn, now + conn->linger_time);
  747. conn->status = STATUS_LINGERING;
  748. }
  749. PR_Unlock(conn->lock);
  750. }
  751. /*
  752. * If no connection is currently active, opens a connection and binds to
  753. * the remote server. If a connection is open (e.g. lingering) then
  754. * this is a no-op.
  755. *
  756. * Returns CONN_OPERATION_SUCCESS on success, or CONN_OPERATION_FAILED
  757. * on failure. Sets conn->last_ldap_error and conn->last_operation;
  758. */
  759. ConnResult
  760. conn_connect(Repl_Connection *conn)
  761. {
  762. int ldap_rc;
  763. int optdata;
  764. int secure = 0;
  765. char* binddn = NULL;
  766. struct berval *creds;
  767. ConnResult return_value = CONN_OPERATION_SUCCESS;
  768. int pw_ret = 1;
  769. /** Connection already open just return SUCCESS **/
  770. if(conn->state == STATE_CONNECTED) return return_value;
  771. PR_Lock(conn->lock);
  772. if (conn->flag_agmt_changed) {
  773. /* So far we cannot change Hostname and Port */
  774. /* slapi_ch_free((void **)&conn->hostname); */
  775. /* conn->hostname = agmt_get_hostname(conn->agmt); */
  776. /* conn->port = agmt_get_port(conn->agmt); */
  777. slapi_ch_free((void **)&conn->binddn);
  778. conn->binddn = agmt_get_binddn(conn->agmt);
  779. conn->bindmethod = agmt_get_bindmethod(conn->agmt);
  780. conn->transport_flags = agmt_get_transport_flags(conn->agmt);
  781. conn->timeout.tv_sec = agmt_get_timeout(conn->agmt);
  782. conn->flag_agmt_changed = 0;
  783. slapi_ch_free((void **)&conn->plain);
  784. }
  785. PR_Unlock(conn->lock);
  786. creds = agmt_get_credentials(conn->agmt);
  787. if (conn->plain == NULL) {
  788. char *plain = NULL;
  789. /* kexcoff: for reversible encryption */
  790. /* We need to test the return code of pw_rever_decode in order to decide
  791. * if a free for plain will be needed (pw_ret == 0) or not (pw_ret != 0) */
  792. pw_ret = pw_rever_decode(creds->bv_val, &plain, type_nsds5ReplicaCredentials);
  793. /* Pb occured in decryption: stop now, binding will fail */
  794. if ( pw_ret == -1 )
  795. {
  796. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  797. "%s: Decoding of the credentials failed.\n",
  798. agmt_get_long_name(conn->agmt));
  799. return_value = CONN_OPERATION_FAILED;
  800. conn->last_ldap_error = LDAP_INVALID_CREDENTIALS;
  801. conn->state = STATE_DISCONNECTED;
  802. return (return_value);
  803. } /* Else, does not mean that the plain is correct, only means the we had no internal
  804. decoding pb */
  805. conn->plain = slapi_ch_strdup (plain);
  806. if (!pw_ret) slapi_ch_free((void**)&plain);
  807. }
  808. /* ugaston: if SSL has been selected in the replication agreement, SSL client
  809. * initialisation should be done before ever trying to open any connection at all.
  810. */
  811. if (conn->transport_flags == TRANSPORT_FLAG_TLS)
  812. {
  813. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  814. "%s: Replication secured by StartTLS not currently supported\n",
  815. agmt_get_long_name(conn->agmt));
  816. return_value = CONN_OPERATION_FAILED;
  817. conn->last_ldap_error = LDAP_STRONG_AUTH_NOT_SUPPORTED;
  818. conn->state = STATE_DISCONNECTED;
  819. } else if(conn->transport_flags == TRANSPORT_FLAG_SSL)
  820. {
  821. /** Make sure the SSL Library has been initialized before anything else **/
  822. if(slapd_security_library_is_initialized() != 1)
  823. {
  824. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  825. "%s: SSL Not Initialized, Replication over SSL FAILED\n",
  826. agmt_get_long_name(conn->agmt));
  827. conn->last_ldap_error = LDAP_INAPPROPRIATE_AUTH;
  828. conn->last_operation = CONN_INIT;
  829. ber_bvfree(creds);
  830. creds = NULL;
  831. return CONN_SSL_NOT_ENABLED;
  832. } else
  833. {
  834. secure = 1;
  835. }
  836. }
  837. if (return_value == CONN_OPERATION_SUCCESS) {
  838. int io_timeout_ms;
  839. /* Now we initialize the LDAP Structure and set options */
  840. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  841. "%s: Trying %s slapi_ldap_init\n",
  842. agmt_get_long_name(conn->agmt),
  843. secure ? "secure" : "non-secure");
  844. /* shared = 1 because we will read results from a second thread */
  845. conn->ld = slapi_ldap_init(conn->hostname, conn->port, secure, 1);
  846. if (NULL == conn->ld)
  847. {
  848. return_value = CONN_OPERATION_FAILED;
  849. conn->state = STATE_DISCONNECTED;
  850. conn->last_operation = CONN_INIT;
  851. conn->last_ldap_error = LDAP_LOCAL_ERROR;
  852. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  853. "%s: Failed to establish %sconnection to the consumer\n",
  854. agmt_get_long_name(conn->agmt),
  855. secure ? "secure " : "");
  856. ber_bvfree(creds);
  857. creds = NULL;
  858. return return_value;
  859. }
  860. /* slapi_ch_strdup is OK with NULL strings */
  861. binddn = slapi_ch_strdup(conn->binddn);
  862. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  863. "%s: binddn = %s, passwd = %s\n",
  864. agmt_get_long_name(conn->agmt),
  865. binddn?binddn:"NULL", creds->bv_val?creds->bv_val:"NULL");
  866. /* Set some options for the connection. */
  867. optdata = LDAP_DEREF_NEVER; /* Don't dereference aliases */
  868. ldap_set_option(conn->ld, LDAP_OPT_DEREF, &optdata);
  869. optdata = LDAP_VERSION3; /* We need LDAP version 3 */
  870. ldap_set_option(conn->ld, LDAP_OPT_PROTOCOL_VERSION, &optdata);
  871. /* Don't chase any referrals (although we shouldn't get any) */
  872. ldap_set_option(conn->ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
  873. /* override the default timeout with the specified timeout */
  874. io_timeout_ms = conn->timeout.tv_sec * 1000 + conn->timeout.tv_usec / 1000;
  875. prldap_set_session_option(conn->ld, NULL, PRLDAP_OPT_IO_MAX_TIMEOUT,
  876. io_timeout_ms);
  877. /* We've got an ld. Now bind to the server. */
  878. conn->last_operation = CONN_BIND;
  879. }
  880. if ( bind_and_check_pwp(conn, binddn, conn->plain) == CONN_OPERATION_FAILED )
  881. {
  882. conn->last_ldap_error = ldap_get_lderrno (conn->ld, NULL, NULL);
  883. conn->state = STATE_DISCONNECTED;
  884. return_value = CONN_OPERATION_FAILED;
  885. }
  886. else
  887. {
  888. conn->last_ldap_error = ldap_rc = LDAP_SUCCESS;
  889. conn->state = STATE_CONNECTED;
  890. return_value = CONN_OPERATION_SUCCESS;
  891. }
  892. ber_bvfree(creds);
  893. creds = NULL;
  894. slapi_ch_free((void**)&binddn);
  895. if(return_value == CONN_OPERATION_FAILED)
  896. {
  897. close_connection_internal(conn);
  898. } else
  899. {
  900. conn->last_ldap_error = ldap_rc = LDAP_SUCCESS;
  901. conn->state = STATE_CONNECTED;
  902. }
  903. return return_value;
  904. }
  905. static void
  906. close_connection_internal(Repl_Connection *conn)
  907. {
  908. if (NULL != conn->ld)
  909. {
  910. /* Since we call slapi_ldap_init,
  911. we must call slapi_ldap_unbind */
  912. slapi_ldap_unbind(conn->ld);
  913. }
  914. conn->ld = NULL;
  915. conn->state = STATE_DISCONNECTED;
  916. conn->status = STATUS_DISCONNECTED;
  917. conn->supports_ds50_repl = -1;
  918. conn->supports_ds71_repl = -1;
  919. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  920. "%s: Disconnected from the consumer\n", agmt_get_long_name(conn->agmt));
  921. }
  922. void
  923. conn_disconnect(Repl_Connection *conn)
  924. {
  925. PR_ASSERT(NULL != conn);
  926. PR_Lock(conn->lock);
  927. close_connection_internal(conn);
  928. PR_Unlock(conn->lock);
  929. }
  930. /*
  931. * Determine if the remote replica supports DS 5.0 replication.
  932. * Return codes:
  933. * CONN_SUPPORTS_DS5_REPL - the remote replica suport DS5 replication
  934. * CONN_DOES_NOT_SUPPORT_DS5_REPL - the remote replica does not
  935. * support DS5 replication.
  936. * CONN_OPERATION_FAILED - it could not be determined if the remote
  937. * replica supports DS5 replication.
  938. * CONN_NOT_CONNECTED - no connection was active.
  939. */
  940. ConnResult
  941. conn_replica_supports_ds5_repl(Repl_Connection *conn)
  942. {
  943. ConnResult return_value;
  944. int ldap_rc;
  945. if (conn_connected(conn))
  946. {
  947. if (conn->supports_ds50_repl == -1) {
  948. LDAPMessage *res = NULL;
  949. LDAPMessage *entry = NULL;
  950. char *attrs[] = {"supportedcontrol", "supportedextension", NULL};
  951. conn->status = STATUS_SEARCHING;
  952. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  953. "(objectclass=*)", attrs, 0 /* attrsonly */,
  954. NULL /* server controls */, NULL /* client controls */,
  955. &conn->timeout, LDAP_NO_LIMIT, &res);
  956. if (LDAP_SUCCESS == ldap_rc)
  957. {
  958. conn->supports_ds50_repl = 0;
  959. entry = ldap_first_entry(conn->ld, res);
  960. if (!attribute_string_value_present(conn->ld, entry, "supportedcontrol", REPL_NSDS50_UPDATE_INFO_CONTROL_OID))
  961. {
  962. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  963. }
  964. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_START_NSDS50_REPLICATION_REQUEST_OID))
  965. {
  966. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  967. }
  968. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_END_NSDS50_REPLICATION_REQUEST_OID))
  969. {
  970. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  971. }
  972. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_ENTRY_REQUEST_OID))
  973. {
  974. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  975. }
  976. else if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS50_REPLICATION_RESPONSE_OID))
  977. {
  978. return_value = CONN_DOES_NOT_SUPPORT_DS5_REPL;
  979. }
  980. else
  981. {
  982. conn->supports_ds50_repl = 1;
  983. return_value = CONN_SUPPORTS_DS5_REPL;
  984. }
  985. }
  986. else
  987. {
  988. if (IS_DISCONNECT_ERROR(ldap_rc))
  989. {
  990. conn->last_ldap_error = ldap_rc; /* specific reason */
  991. conn_disconnect(conn);
  992. return_value = CONN_NOT_CONNECTED;
  993. }
  994. else
  995. {
  996. return_value = CONN_OPERATION_FAILED;
  997. }
  998. }
  999. if (NULL != res)
  1000. ldap_msgfree(res);
  1001. }
  1002. else {
  1003. return_value = conn->supports_ds50_repl ? CONN_SUPPORTS_DS5_REPL : CONN_DOES_NOT_SUPPORT_DS5_REPL;
  1004. }
  1005. }
  1006. else
  1007. {
  1008. /* Not connected */
  1009. return_value = CONN_NOT_CONNECTED;
  1010. }
  1011. return return_value;
  1012. }
  1013. /*
  1014. * Determine if the remote replica supports DS 5.0 replication.
  1015. * Return codes:
  1016. * CONN_SUPPORTS_DS71_REPL - the remote replica suport DS5 replication
  1017. * CONN_DOES_NOT_SUPPORT_DS71_REPL - the remote replica does not
  1018. * support DS5 replication.
  1019. * CONN_OPERATION_FAILED - it could not be determined if the remote
  1020. * replica supports DS5 replication.
  1021. * CONN_NOT_CONNECTED - no connection was active.
  1022. */
  1023. ConnResult
  1024. conn_replica_supports_ds71_repl(Repl_Connection *conn)
  1025. {
  1026. ConnResult return_value;
  1027. int ldap_rc;
  1028. if (conn_connected(conn))
  1029. {
  1030. if (conn->supports_ds71_repl == -1) {
  1031. LDAPMessage *res = NULL;
  1032. LDAPMessage *entry = NULL;
  1033. char *attrs[] = {"supportedcontrol", "supportedextension", NULL};
  1034. conn->status = STATUS_SEARCHING;
  1035. ldap_rc = ldap_search_ext_s(conn->ld, "", LDAP_SCOPE_BASE,
  1036. "(objectclass=*)", attrs, 0 /* attrsonly */,
  1037. NULL /* server controls */, NULL /* client controls */,
  1038. &conn->timeout, LDAP_NO_LIMIT, &res);
  1039. if (LDAP_SUCCESS == ldap_rc)
  1040. {
  1041. conn->supports_ds71_repl = 0;
  1042. entry = ldap_first_entry(conn->ld, res);
  1043. if (!attribute_string_value_present(conn->ld, entry, "supportedextension", REPL_NSDS71_REPLICATION_ENTRY_REQUEST_OID))
  1044. {
  1045. return_value = CONN_DOES_NOT_SUPPORT_DS71_REPL;
  1046. }
  1047. else
  1048. {
  1049. conn->supports_ds71_repl = 1;
  1050. return_value = CONN_SUPPORTS_DS71_REPL;
  1051. }
  1052. }
  1053. else
  1054. {
  1055. if (IS_DISCONNECT_ERROR(ldap_rc))
  1056. {
  1057. conn->last_ldap_error = ldap_rc; /* specific reason */
  1058. conn_disconnect(conn);
  1059. return_value = CONN_NOT_CONNECTED;
  1060. }
  1061. else
  1062. {
  1063. return_value = CONN_OPERATION_FAILED;
  1064. }
  1065. }
  1066. if (NULL != res)
  1067. ldap_msgfree(res);
  1068. }
  1069. else {
  1070. return_value = conn->supports_ds71_repl ? CONN_SUPPORTS_DS71_REPL : CONN_DOES_NOT_SUPPORT_DS71_REPL;
  1071. }
  1072. }
  1073. else
  1074. {
  1075. /* Not connected */
  1076. return_value = CONN_NOT_CONNECTED;
  1077. }
  1078. return return_value;
  1079. }
  1080. /* Determine if the replica is read-only */
  1081. ConnResult
  1082. conn_replica_is_readonly(Repl_Connection *conn)
  1083. {
  1084. ReplicaId rid = agmt_get_consumer_rid( (Repl_Agmt *) conn->agmt, conn );
  1085. if (rid == READ_ONLY_REPLICA_ID)
  1086. {
  1087. return CONN_IS_READONLY;
  1088. } else
  1089. {
  1090. return CONN_IS_NOT_READONLY;
  1091. }
  1092. }
  1093. /*
  1094. * Return 1 if "value" is a value of attribute type "type" in entry "entry".
  1095. * Otherwise, return 0.
  1096. */
  1097. static int
  1098. attribute_string_value_present(LDAP *ld, LDAPMessage *entry, const char *type,
  1099. const char *value)
  1100. {
  1101. int return_value = 0;
  1102. if (NULL != entry)
  1103. {
  1104. char *atype = NULL;
  1105. BerElement *ber = NULL;
  1106. atype = ldap_first_attribute(ld, entry, &ber);
  1107. while (NULL != atype && 0 == return_value)
  1108. {
  1109. if (strcasecmp(atype, type) == 0)
  1110. {
  1111. char **strvals = ldap_get_values(ld, entry, atype);
  1112. int i;
  1113. for (i = 0; return_value == 0 && NULL != strvals && NULL != strvals[i]; i++)
  1114. {
  1115. if (strcmp(strvals[i], value) == 0)
  1116. {
  1117. return_value = 1;
  1118. }
  1119. }
  1120. if (NULL != strvals)
  1121. {
  1122. ldap_value_free(strvals);
  1123. }
  1124. }
  1125. ldap_memfree(atype);
  1126. atype = ldap_next_attribute(ld, entry, ber);
  1127. }
  1128. if (NULL != ber)
  1129. ldap_ber_free(ber, 0);
  1130. /* The last atype has not been freed yet */
  1131. if (NULL != atype)
  1132. ldap_memfree(atype);
  1133. }
  1134. return return_value;
  1135. }
  1136. /*
  1137. * Read the remote server's schema entry, then read the local schema entry,
  1138. * and compare the nsschemacsn attribute. If the local csn is newer, or
  1139. * the remote csn is absent, push the schema down to the consumer.
  1140. * Return codes:
  1141. * CONN_SCHEMA_UPDATED if the schema was pushed successfully
  1142. * CONN_SCHEMA_NO_UPDATE_NEEDED if the schema was as new or newer than
  1143. * the local server's schema
  1144. * CONN_OPERATION_FAILED if an error occurred
  1145. * CONN_NOT_CONNECTED if no connection was active
  1146. * NOTE: Should only be called when a replication session has been
  1147. * established by sending a startReplication extended operation.
  1148. */
  1149. ConnResult
  1150. conn_push_schema(Repl_Connection *conn, CSN **remotecsn)
  1151. {
  1152. ConnResult return_value = CONN_OPERATION_SUCCESS;
  1153. char *nsschemacsn = "nsschemacsn";
  1154. Slapi_Entry **entries = NULL;
  1155. Slapi_Entry *schema_entry = NULL;
  1156. CSN *localcsn = NULL;
  1157. Slapi_PBlock *spb = NULL;
  1158. char localcsnstr[CSN_STRSIZE + 1] = {0};
  1159. if (!conn_connected(conn))
  1160. {
  1161. return_value = CONN_NOT_CONNECTED;
  1162. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1163. "%s: Schema replication update failed: not connected to consumer\n",
  1164. agmt_get_long_name(conn->agmt));
  1165. }
  1166. else
  1167. {
  1168. localcsn = dup_global_schema_csn();
  1169. if (NULL == localcsn)
  1170. {
  1171. /* Local server has epoch CSN, so don't push schema */
  1172. return_value = CONN_SCHEMA_NO_UPDATE_NEEDED;
  1173. }
  1174. else if ( remotecsn && *remotecsn && csn_compare(localcsn, *remotecsn) <= 0 )
  1175. {
  1176. /* Local server schema is not newer than the remote one */
  1177. return_value = CONN_SCHEMA_NO_UPDATE_NEEDED;
  1178. }
  1179. else
  1180. {
  1181. struct berval **remote_schema_csn_bervals = NULL;
  1182. /* Get remote server's schema */
  1183. return_value = conn_read_entry_attribute(conn, "cn=schema", nsschemacsn,
  1184. &remote_schema_csn_bervals);
  1185. if (CONN_OPERATION_SUCCESS == return_value)
  1186. {
  1187. if (NULL != remote_schema_csn_bervals && NULL != remote_schema_csn_bervals[0])
  1188. {
  1189. char remotecsnstr[CSN_STRSIZE + 1] = {0};
  1190. memcpy(remotecsnstr, remote_schema_csn_bervals[0]->bv_val,
  1191. remote_schema_csn_bervals[0]->bv_len);
  1192. remotecsnstr[remote_schema_csn_bervals[0]->bv_len] = '\0';
  1193. *remotecsn = csn_new_by_string(remotecsnstr);
  1194. if (NULL != remotecsn && (csn_compare(localcsn, *remotecsn) <= 0))
  1195. {
  1196. return_value = CONN_SCHEMA_NO_UPDATE_NEEDED;
  1197. }
  1198. /* Need to free the remote_schema_csn_bervals */
  1199. ber_bvecfree(remote_schema_csn_bervals);
  1200. }
  1201. }
  1202. }
  1203. }
  1204. if (CONN_OPERATION_SUCCESS == return_value)
  1205. {
  1206. /* We know we need to push the schema out. */
  1207. LDAPMod ocmod = {0};
  1208. LDAPMod atmod = {0};
  1209. LDAPMod csnmod = {0};
  1210. LDAPMod *attrs[4] = {0};
  1211. int numvalues = 0;
  1212. Slapi_Attr *attr = NULL;
  1213. char *csnvalues[2];
  1214. ocmod.mod_type = "objectclasses";
  1215. ocmod.mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  1216. ocmod.mod_bvalues = NULL;
  1217. atmod.mod_type = "attributetypes";
  1218. atmod.mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  1219. atmod.mod_bvalues = NULL;
  1220. csnmod.mod_type = nsschemacsn;
  1221. csnmod.mod_op = LDAP_MOD_REPLACE;
  1222. csn_as_string (localcsn, PR_FALSE, localcsnstr);
  1223. csnvalues[0] = localcsnstr;
  1224. csnvalues[1] = NULL;
  1225. csnmod.mod_values = csnvalues;
  1226. attrs[0] = &ocmod;
  1227. attrs[1] = &atmod;
  1228. attrs[2] = &csnmod;
  1229. attrs[3] = NULL;
  1230. return_value = CONN_OPERATION_FAILED; /* assume failure */
  1231. /* Get local schema */
  1232. spb = slapi_search_internal("cn=schema", LDAP_SCOPE_BASE, "(objectclass=*)",
  1233. NULL /* controls */, NULL /* schema_csn_attrs */, 0 /* attrsonly */);
  1234. slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1235. if (NULL == entries || NULL == entries[0])
  1236. {
  1237. /* Whoops - couldn't read our own schema! */
  1238. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1239. "%s: Error: unable to read local schema definitions.\n",
  1240. agmt_get_long_name(conn->agmt));
  1241. return_value = CONN_OPERATION_FAILED;
  1242. }
  1243. else
  1244. {
  1245. schema_entry = entries[0];
  1246. if (slapi_entry_attr_find(schema_entry, "objectclasses", &attr) != -1)
  1247. {
  1248. int i, ind;
  1249. Slapi_Value *value;
  1250. slapi_attr_get_numvalues(attr, &numvalues);
  1251. ocmod.mod_bvalues = (struct berval **)slapi_ch_malloc((numvalues + 1) *
  1252. sizeof(struct berval *));
  1253. for (i = 0, ind = slapi_attr_first_value(attr, &value);
  1254. ind != -1; ind = slapi_attr_next_value(attr, ind, &value), i++)
  1255. {
  1256. /* XXXggood had to cast away const below */
  1257. ocmod.mod_bvalues[i] = (struct berval *)slapi_value_get_berval(value);
  1258. }
  1259. ocmod.mod_bvalues[numvalues] = NULL;
  1260. if (slapi_entry_attr_find(schema_entry, "attributetypes", &attr) != -1)
  1261. {
  1262. ConnResult result;
  1263. slapi_attr_get_numvalues(attr, &numvalues);
  1264. atmod.mod_bvalues = (struct berval **)slapi_ch_malloc((numvalues + 1) *
  1265. sizeof(struct berval *));
  1266. for (i = 0, ind = slapi_attr_first_value(attr, &value);
  1267. ind != -1; ind = slapi_attr_next_value(attr, ind, &value), i++)
  1268. {
  1269. /* XXXggood had to cast away const below */
  1270. atmod.mod_bvalues[i] = (struct berval *)slapi_value_get_berval(value);
  1271. }
  1272. atmod.mod_bvalues[numvalues] = NULL;
  1273. result = conn_send_modify(conn, "cn=schema", attrs, NULL, NULL); /* DBDB: this needs to be fixed to use async */
  1274. result = conn_read_result(conn,NULL);
  1275. switch (result)
  1276. {
  1277. case CONN_OPERATION_FAILED:
  1278. {
  1279. int ldaperr = -1, optype = -1;
  1280. conn_get_error(conn, &optype, &ldaperr);
  1281. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1282. "%s: Schema replication update failed: %s\n",
  1283. agmt_get_long_name(conn->agmt),
  1284. ldaperr == -1 ? "Unknown Error" : ldap_err2string(ldaperr));
  1285. }
  1286. case CONN_NOT_CONNECTED:
  1287. return_value = CONN_NOT_CONNECTED;
  1288. break;
  1289. case CONN_OPERATION_SUCCESS:
  1290. return_value = CONN_SCHEMA_UPDATED;
  1291. break;
  1292. default:
  1293. break;
  1294. }
  1295. }
  1296. }
  1297. else
  1298. {
  1299. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1300. "%s: Schema replication update failed: "
  1301. "unable to prepare schema entry for transmission.\n",
  1302. agmt_get_long_name(conn->agmt));
  1303. }
  1304. }
  1305. /* slapi_ch_free accepts NULL pointer */
  1306. slapi_ch_free((void **)&ocmod.mod_bvalues);
  1307. slapi_ch_free((void **)&atmod.mod_bvalues);
  1308. }
  1309. if (NULL != spb)
  1310. {
  1311. slapi_free_search_results_internal(spb);
  1312. slapi_pblock_destroy(spb);
  1313. spb = NULL;
  1314. }
  1315. if (NULL != localcsn)
  1316. {
  1317. csn_free(&localcsn);
  1318. }
  1319. return return_value;
  1320. }
  1321. void
  1322. conn_set_timeout(Repl_Connection *conn, long timeout)
  1323. {
  1324. PR_ASSERT(NULL != conn);
  1325. PR_ASSERT(timeout >= 0);
  1326. PR_Lock(conn->lock);
  1327. conn->timeout.tv_sec = timeout;
  1328. PR_Unlock(conn->lock);
  1329. }
  1330. long
  1331. conn_get_timeout(Repl_Connection *conn)
  1332. {
  1333. long retval = 0;
  1334. PR_ASSERT(NULL != conn);
  1335. retval = conn->timeout.tv_sec;
  1336. return retval;
  1337. }
  1338. void conn_set_agmt_changed(Repl_Connection *conn)
  1339. {
  1340. PR_ASSERT(NULL != conn);
  1341. PR_Lock(conn->lock);
  1342. if (NULL != conn->agmt)
  1343. conn->flag_agmt_changed = 1;
  1344. PR_Unlock(conn->lock);
  1345. }
  1346. /*
  1347. * Check the result of an ldap_simple_bind operation to see we it
  1348. * contains the expiration controls
  1349. * return: -1 error, not bound
  1350. * 0, OK bind has succeeded
  1351. */
  1352. static int
  1353. bind_and_check_pwp(Repl_Connection *conn, char * binddn, char *password)
  1354. {
  1355. LDAPControl **ctrls = NULL;
  1356. LDAPMessage *res = NULL;
  1357. char *errmsg = NULL;
  1358. LDAP *ld = conn->ld;
  1359. int msgid;
  1360. int *msgidAdr = &msgid;
  1361. int rc;
  1362. char * optype; /* ldap_simple_bind or slapd_SSL_client_bind */
  1363. if ( conn->transport_flags == TRANSPORT_FLAG_SSL )
  1364. {
  1365. char *auth;
  1366. optype = "ldap_sasl_bind";
  1367. if ( conn->bindmethod == BINDMETHOD_SSL_CLIENTAUTH )
  1368. {
  1369. rc = slapd_sasl_ext_client_bind(conn->ld, &msgidAdr);
  1370. auth = "SSL client authentication";
  1371. if ( rc == LDAP_SUCCESS )
  1372. {
  1373. if (conn->last_ldap_error != rc)
  1374. {
  1375. conn->last_ldap_error = rc;
  1376. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1377. "%s: Replication bind with %s resumed\n",
  1378. agmt_get_long_name(conn->agmt), auth);
  1379. }
  1380. }
  1381. else
  1382. {
  1383. /* Do not report the same error over and over again */
  1384. if (conn->last_ldap_error != rc)
  1385. {
  1386. conn->last_ldap_error = rc;
  1387. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1388. "%s: Replication bind with %s failed: LDAP error %d (%s)\n",
  1389. agmt_get_long_name(conn->agmt), auth, rc,
  1390. ldap_err2string(rc));
  1391. }
  1392. return (CONN_OPERATION_FAILED);
  1393. }
  1394. }
  1395. else
  1396. {
  1397. if( ( msgid = do_simple_bind( conn, ld, binddn, password ) ) == -1 )
  1398. {
  1399. return (CONN_OPERATION_FAILED);
  1400. }
  1401. }
  1402. }
  1403. else
  1404. {
  1405. optype = "ldap_simple_bind";
  1406. if( ( msgid = do_simple_bind( conn, ld, binddn, password ) ) == -1 )
  1407. {
  1408. return (CONN_OPERATION_FAILED);
  1409. }
  1410. }
  1411. /* Wait for the result */
  1412. if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &res ) == -1 )
  1413. {
  1414. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1415. "%s: Received error from consumer for %s operation\n",
  1416. agmt_get_long_name(conn->agmt), optype);
  1417. return (CONN_OPERATION_FAILED);
  1418. }
  1419. /* Don't check ldap_result against 0 because, no timeout is specified */
  1420. /* Free res as we won't use it any longer */
  1421. if ( ldap_parse_result( ld, res, &rc, NULL, NULL, NULL, &ctrls, 1 /* Free res */)
  1422. != LDAP_SUCCESS )
  1423. {
  1424. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1425. "%s: Received error from consumer for %s operation\n",
  1426. agmt_get_long_name(conn->agmt), optype);
  1427. return (CONN_OPERATION_FAILED);
  1428. }
  1429. if ( rc == LDAP_SUCCESS )
  1430. {
  1431. if ( ctrls )
  1432. {
  1433. int i;
  1434. for( i = 0; ctrls[ i ] != NULL; ++i )
  1435. {
  1436. if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRED)) )
  1437. {
  1438. /* Bind is successfull but password has expired */
  1439. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1440. "%s: Succesfully bound %s to consumer, "
  1441. "but password has expired on consumer.\n",
  1442. agmt_get_long_name(conn->agmt), binddn);
  1443. }
  1444. else if ( !(strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_PWEXPIRING)) )
  1445. {
  1446. /* The password is expiring in n seconds */
  1447. if ( (ctrls[ i ]->ldctl_value.bv_val != NULL) &&
  1448. (ctrls[ i ]->ldctl_value.bv_len > 0) )
  1449. {
  1450. int password_expiring = atoi( ctrls[ i ]->ldctl_value.bv_val );
  1451. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1452. "%s: Succesfully bound %s to consumer, "
  1453. "but password is expiring on consumer in %d seconds.\n",
  1454. agmt_get_long_name(conn->agmt), binddn, password_expiring);
  1455. }
  1456. }
  1457. }
  1458. ldap_controls_free( ctrls );
  1459. }
  1460. return (CONN_OPERATION_SUCCESS);
  1461. }
  1462. else
  1463. {
  1464. /* errmsg is a pointer directly into the ld structure - do not free */
  1465. rc = ldap_get_lderrno( ld, NULL, &errmsg );
  1466. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1467. "%s: Replication bind to %s on consumer failed: %d (%s)\n",
  1468. agmt_get_long_name(conn->agmt), binddn, rc, errmsg);
  1469. conn->last_ldap_error = rc; /* specific error */
  1470. return (CONN_OPERATION_FAILED);
  1471. }
  1472. }
  1473. static int
  1474. do_simple_bind (Repl_Connection *conn, LDAP *ld, char * binddn, char *password)
  1475. {
  1476. int msgid;
  1477. if( ( msgid = ldap_simple_bind( ld, binddn, password ) ) == -1 )
  1478. {
  1479. char *ldaperrtext = NULL;
  1480. int ldaperr;
  1481. int prerr = PR_GetError();
  1482. ldaperr = ldap_get_lderrno( ld, NULL, &ldaperrtext );
  1483. /* Do not report the same error over and over again */
  1484. if (conn->last_ldap_error != ldaperr)
  1485. {
  1486. conn->last_ldap_error = ldaperr;
  1487. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1488. "%s: Simple bind failed, "
  1489. SLAPI_COMPONENT_NAME_LDAPSDK " error %d (%s), "
  1490. SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
  1491. agmt_get_long_name(conn->agmt),
  1492. ldaperr, ldaperrtext ? ldaperrtext : ldap_err2string(ldaperr),
  1493. prerr, slapd_pr_strerror(prerr));
  1494. }
  1495. }
  1496. else if (conn->last_ldap_error != LDAP_SUCCESS)
  1497. {
  1498. conn->last_ldap_error = LDAP_SUCCESS;
  1499. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1500. "%s: Simple bind resumed\n",
  1501. agmt_get_long_name(conn->agmt));
  1502. }
  1503. return msgid;
  1504. }
  1505. void
  1506. repl5_set_debug_timeout(const char *val)
  1507. {
  1508. /* val looks like this: seconds[:debuglevel] */
  1509. /* seconds is the number of seconds to wait until turning on the debug level */
  1510. /* this should be less than the ldap connection timeout (default 10 minutes) */
  1511. /* the optional debug level is the error log debugging level to use (default repl) */
  1512. if (val) {
  1513. const char *p = strchr(val, ':');
  1514. s_debug_timeout = atoi(val);
  1515. if (p) {
  1516. s_debug_level = atoi(p+1);
  1517. } else {
  1518. s_debug_level = 8192;
  1519. }
  1520. }
  1521. }
  1522. static time_t
  1523. PRTime2time_t (PRTime tm)
  1524. {
  1525. PRInt64 rt;
  1526. PR_ASSERT (tm);
  1527. LL_DIV(rt, tm, PR_USEC_PER_SEC);
  1528. return (time_t)rt;
  1529. }
  1530. static Slapi_Eq_Context
  1531. repl5_start_debug_timeout(int *setlevel)
  1532. {
  1533. Slapi_Eq_Context eqctx = 0;
  1534. if (s_debug_timeout && s_debug_level) {
  1535. time_t now = time(NULL);
  1536. eqctx = slapi_eq_once(repl5_debug_timeout_callback, setlevel,
  1537. s_debug_timeout + now);
  1538. }
  1539. return eqctx;
  1540. }
  1541. static void
  1542. repl5_stop_debug_timeout(Slapi_Eq_Context eqctx, int *setlevel)
  1543. {
  1544. char buf[20];
  1545. char msg[SLAPI_DSE_RETURNTEXT_SIZE];
  1546. if (eqctx && !*setlevel) {
  1547. int found = slapi_eq_cancel(eqctx);
  1548. }
  1549. if (s_debug_timeout && s_debug_level && *setlevel) {
  1550. void config_set_errorlog_level(const char *type, char *buf, char *msg, int apply);
  1551. sprintf(buf, "%d", 0);
  1552. config_set_errorlog_level("nsslapd-errorlog-level", buf, msg, 1);
  1553. }
  1554. }
  1555. static void
  1556. repl5_debug_timeout_callback(time_t when, void *arg)
  1557. {
  1558. int *setlevel = (int *)arg;
  1559. void config_set_errorlog_level(const char *type, char *buf, char *msg, int apply);
  1560. char buf[20];
  1561. char msg[SLAPI_DSE_RETURNTEXT_SIZE];
  1562. *setlevel = 1;
  1563. sprintf(buf, "%d", s_debug_level);
  1564. config_set_errorlog_level("nsslapd-errorlog-level", buf, msg, 1);
  1565. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1566. "repl5_debug_timeout_callback: set debug level to %d at %d\n",
  1567. s_debug_level, when);
  1568. }