cb_search.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include "cb.h"
  42. /*
  43. * Build a candidate list for this backentry and scope.
  44. * Could be a BASE, ONELEVEL, or SUBTREE search.
  45. *
  46. * Returns:
  47. * 0 - success
  48. * <0 - fail
  49. *
  50. */
  51. int
  52. chainingdb_build_candidate_list ( Slapi_PBlock *pb )
  53. {
  54. Slapi_Backend * be;
  55. Slapi_Operation * op;
  56. char *target, *filter;
  57. int scope,attrsonly,sizelimit,timelimit,rc,searchreferral;
  58. char **attrs=NULL;
  59. LDAPControl **controls=NULL;
  60. LDAPControl **ctrls=NULL;
  61. LDAP *ld=NULL;
  62. cb_backend_instance *cb = NULL;
  63. cb_searchContext *ctx=NULL;
  64. struct timeval timeout;
  65. time_t optime;
  66. int doit,parse_rc;
  67. LDAPMessage *res=NULL;
  68. char *matched_msg,*error_msg;
  69. LDAPControl **serverctrls=NULL;
  70. char **referrals=NULL;
  71. char *cnxerrbuf=NULL;
  72. time_t endbefore=0;
  73. time_t endtime;
  74. cb_outgoing_conn *cnx;
  75. slapi_pblock_get( pb, SLAPI_BACKEND, &be );
  76. cb = cb_get_instance(be);
  77. slapi_pblock_get( pb, SLAPI_OPERATION, &op );
  78. slapi_pblock_get( pb, SLAPI_SEARCH_STRFILTER, &filter );
  79. slapi_pblock_get( pb, SLAPI_SEARCH_SCOPE, &scope );
  80. slapi_pblock_get( pb, SLAPI_OPINITIATED_TIME, &optime );
  81. slapi_pblock_get( pb, SLAPI_SEARCH_TARGET, &target );
  82. if ( LDAP_SUCCESS != (parse_rc=cb_forward_operation(pb) )) {
  83. /* Don't return errors */
  84. if (cb_debug_on()) {
  85. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  86. "local search: base:<%s> scope:<%s> filter:<%s>\n",target,
  87. scope==LDAP_SCOPE_SUBTREE?"SUBTREE":scope==LDAP_SCOPE_ONELEVEL ? "ONE-LEVEL" : "BASE" , filter);
  88. }
  89. ctx = (cb_searchContext *)slapi_ch_calloc(1,sizeof(cb_searchContext));
  90. ctx->type = CB_SEARCHCONTEXT_ENTRY;
  91. ctx->data=NULL;
  92. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,ctx);
  93. return 0;
  94. }
  95. cb_update_monitor_info(pb,cb,SLAPI_OPERATION_SEARCH);
  96. /* Check wether the chaining BE is available or not */
  97. if ( cb_check_availability( cb, pb ) == FARMSERVER_UNAVAILABLE ){
  98. return -1;
  99. }
  100. if (cb_debug_on()) {
  101. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  102. "chained search: base:<%s> scope:<%s> filter:<%s>\n",target,
  103. scope==LDAP_SCOPE_SUBTREE?"SUBTREE":scope==LDAP_SCOPE_ONELEVEL ? "ONE-LEVEL" : "BASE" , filter);
  104. }
  105. slapi_pblock_get( pb, SLAPI_SEARCH_ATTRS, &attrs );
  106. slapi_pblock_get( pb, SLAPI_SEARCH_ATTRSONLY, &attrsonly );
  107. slapi_pblock_get( pb, SLAPI_REQCONTROLS, &controls );
  108. slapi_pblock_get( pb, SLAPI_SEARCH_TIMELIMIT, &timelimit );
  109. slapi_pblock_get( pb, SLAPI_SEARCH_SIZELIMIT, &sizelimit );
  110. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,NULL);
  111. if ((scope != LDAP_SCOPE_BASE) && (scope != LDAP_SCOPE_ONELEVEL) && (scope != LDAP_SCOPE_SUBTREE)) {
  112. cb_send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL, "Bad scope", 0, NULL );
  113. return 1;
  114. }
  115. searchreferral=cb->searchreferral;
  116. if (( scope != LDAP_SCOPE_BASE ) && ( searchreferral )) {
  117. int i;
  118. struct berval bv,*bvals[2];
  119. Slapi_Entry ** aciArray=(Slapi_Entry **) slapi_ch_malloc(2*sizeof(Slapi_Entry *));
  120. Slapi_Entry *anEntry = slapi_entry_alloc();
  121. slapi_entry_set_dn(anEntry,slapi_ch_strdup(target));
  122. bvals[1]=NULL;
  123. bvals[0]=&bv;
  124. bv.bv_val="referral";
  125. bv.bv_len=strlen(bv.bv_val);
  126. slapi_entry_add_values( anEntry, "objectclass", bvals);
  127. PR_RWLock_Rlock(cb->rwl_config_lock);
  128. for (i=0; cb->url_array && cb->url_array[i]; i++) {
  129. char * anUrl = slapi_ch_smprintf("%s%s",cb->url_array[i],target);
  130. bv.bv_val=anUrl;
  131. bv.bv_len=strlen(bv.bv_val);
  132. slapi_entry_attr_merge( anEntry, "ref", bvals);
  133. slapi_ch_free((void **)&anUrl);
  134. }
  135. PR_RWLock_Unlock(cb->rwl_config_lock);
  136. aciArray[0]=anEntry;
  137. aciArray[1]=NULL;
  138. ctx = (cb_searchContext *)slapi_ch_calloc(1,sizeof(cb_searchContext));
  139. ctx->type = CB_SEARCHCONTEXT_ENTRY;
  140. ctx->data=aciArray;
  141. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,ctx);
  142. return 0;
  143. }
  144. /*
  145. ** Time limit management.
  146. ** Make sure the operation has not expired
  147. */
  148. if ( timelimit == -1 ) {
  149. timeout.tv_sec = timeout.tv_usec = 0;
  150. } else {
  151. time_t now=current_time();
  152. endbefore=optime + timelimit;
  153. if (now >= endbefore) {
  154. cb_send_ldap_result( pb, LDAP_TIMELIMIT_EXCEEDED, NULL,NULL, 0, NULL);
  155. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY, NULL );
  156. return 1;
  157. }
  158. timeout.tv_sec=(time_t)timelimit-(now-optime);
  159. timeout.tv_usec=0;
  160. }
  161. /* Operational attribute support for internal searches: */
  162. /* The front-end relies on the fact that operational attributes */
  163. /* are returned along with standard attrs when the attr list is */
  164. /* NULL. To make it work, we need to explicitly request for all*/
  165. /* possible operational attrs. Too bad. */
  166. if ( (attrs == NULL) && operation_is_flag_set(op, OP_FLAG_INTERNAL) ) {
  167. attrs = cb->every_attribute;
  168. }
  169. else
  170. {
  171. int i;
  172. if ( attrs != NULL )
  173. {
  174. for ( i = 0; attrs[i] != NULL; i++ ) {
  175. if ( strcasecmp( "nsrole", attrs[i] ) == 0 )
  176. {
  177. attrs = cb->every_attribute;
  178. break;
  179. }
  180. }
  181. }
  182. }
  183. /* Grab a connection handle */
  184. if ( LDAP_SUCCESS != (rc = cb_get_connection(cb->pool,&ld,&cnx,&timeout,&cnxerrbuf))) {
  185. if (rc == LDAP_TIMELIMIT_EXCEEDED)
  186. cb_send_ldap_result( pb, rc, NULL,cnxerrbuf, 0, NULL);
  187. else
  188. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,cnxerrbuf, 0, NULL);
  189. if (cnxerrbuf) {
  190. PR_smprintf_free(cnxerrbuf);
  191. }
  192. /* ping the farm. If the farm is unreachable, we increment the counter */
  193. cb_ping_farm(cb,NULL,0);
  194. return 1;
  195. }
  196. /*
  197. * Control management
  198. */
  199. if ( LDAP_SUCCESS != (rc = cb_update_controls( pb,ld,&ctrls,CB_UPDATE_CONTROLS_ADDAUTH ))) {
  200. cb_send_ldap_result( pb, rc, NULL,NULL, 0, NULL);
  201. cb_release_op_connection(cb->pool,ld,0);
  202. return 1;
  203. }
  204. if ( slapi_op_abandoned( pb )) {
  205. cb_release_op_connection(cb->pool,ld,0);
  206. if ( NULL != ctrls)
  207. ldap_controls_free(ctrls);
  208. return 1;
  209. }
  210. ctx = (cb_searchContext *) slapi_ch_calloc(1,sizeof(cb_searchContext));
  211. /*
  212. ** We need to store the connection handle in the search context
  213. ** to make sure we reuse it in the next_entry iteration
  214. ** Indeed, if another thread on this connection detects a problem
  215. ** on this connection, it may reallocate a new connection and
  216. ** a call to get_connection may return a new cnx. Too bad.
  217. */
  218. ctx->ld=ld;
  219. ctx->cnx=cnx;
  220. /* for some reasons, it is an error to pass in a zero'd timeval */
  221. /* to ldap_search_ext() */
  222. if ((timeout.tv_sec==0) && (timeout.tv_usec==0))
  223. timeout.tv_sec=timeout.tv_usec=-1;
  224. /* heart-beat management */
  225. if (cb->max_idle_time>0)
  226. endtime=current_time() + cb->max_idle_time;
  227. rc=ldap_search_ext(ld ,target,scope,filter,attrs,attrsonly,
  228. ctrls, NULL, &timeout,sizelimit, &(ctx->msgid) );
  229. if ( NULL != ctrls)
  230. ldap_controls_free(ctrls);
  231. if ( LDAP_SUCCESS != rc ) {
  232. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL, ldap_err2string(rc), 0, NULL);
  233. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  234. slapi_ch_free((void **) &ctx);
  235. return 1;
  236. }
  237. /*
  238. ** Need to get the very first result to handle
  239. ** errors properly, especially no search base.
  240. */
  241. doit=1;
  242. while (doit) {
  243. if (cb_check_forward_abandon(cb,pb,ctx->ld,ctx->msgid)) {
  244. slapi_ch_free((void **) &ctx);
  245. return 1;
  246. }
  247. rc=ldap_result(ld,ctx->msgid,LDAP_MSG_ONE,&cb->abandon_timeout,&res);
  248. switch ( rc ) {
  249. case -1:
  250. /* An error occurred. return now */
  251. rc = slapi_ldap_get_lderrno(ld,NULL,NULL);
  252. /* tuck away some errors in a OPERATION_ERROR */
  253. if (CB_LDAP_CONN_ERROR(rc)) {
  254. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  255. ldap_err2string( rc ), 0, NULL);
  256. } else {
  257. cb_send_ldap_result(pb,rc, NULL, NULL,0,NULL);
  258. }
  259. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  260. if (res)
  261. ldap_msgfree(res);
  262. slapi_ch_free((void **)&ctx);
  263. return 1;
  264. case 0:
  265. /* Local timeout management */
  266. if (timelimit != -1) {
  267. if (current_time() > endbefore) {
  268. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  269. "Local timeout expiration\n");
  270. cb_send_ldap_result(pb,LDAP_TIMELIMIT_EXCEEDED,
  271. NULL,NULL, 0, NULL);
  272. /* Force connection close */
  273. cb_release_op_connection(cb->pool,ld,1);
  274. if (res)
  275. ldap_msgfree(res);
  276. slapi_ch_free((void **)&ctx);
  277. return 1;
  278. }
  279. }
  280. /* heart-beat management */
  281. if ((rc=cb_ping_farm(cb,cnx,endtime)) != LDAP_SUCCESS) {
  282. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  283. ldap_err2string(rc), 0, NULL);
  284. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  285. if (res)
  286. ldap_msgfree(res);
  287. slapi_ch_free((void **)&ctx);
  288. return 1;
  289. }
  290. #ifdef CB_YIELD
  291. DS_Sleep(PR_INTERVAL_NO_WAIT);
  292. #endif
  293. break;
  294. case LDAP_RES_SEARCH_ENTRY:
  295. case LDAP_RES_SEARCH_REFERENCE:
  296. /* Some results received */
  297. /* don't parse result here */
  298. ctx->pending_result=res;
  299. ctx->pending_result_type=rc;
  300. doit=0;
  301. break;
  302. case LDAP_RES_SEARCH_RESULT:
  303. matched_msg=NULL;
  304. error_msg=NULL;
  305. referrals=NULL;
  306. serverctrls=NULL;
  307. parse_rc=ldap_parse_result(ld,res,&rc,&matched_msg,
  308. &error_msg,&referrals, &serverctrls, 0 );
  309. if ( parse_rc != LDAP_SUCCESS ) {
  310. cb_send_ldap_result(pb,parse_rc,
  311. matched_msg,error_msg,0,NULL);
  312. rc=-1;
  313. } else
  314. if ( rc != LDAP_SUCCESS ) {
  315. slapi_ldap_get_lderrno( ctx->ld, &matched_msg, &error_msg );
  316. cb_send_ldap_result( pb, rc, matched_msg,
  317. error_msg,0,NULL);
  318. /* BEWARE: matched_msg and error_msg points */
  319. /* to ld fields. */
  320. matched_msg=NULL;
  321. error_msg=NULL;
  322. rc=-1;
  323. }
  324. slapi_ch_free((void **)&matched_msg);
  325. slapi_ch_free((void **)&error_msg);
  326. if (serverctrls)
  327. ldap_controls_free(serverctrls);
  328. if (referrals)
  329. charray_free(referrals);
  330. if (rc!=LDAP_SUCCESS) {
  331. cb_release_op_connection(cb->pool,ld,
  332. CB_LDAP_CONN_ERROR(rc));
  333. ldap_msgfree(res);
  334. slapi_ch_free((void **)&ctx);
  335. return -1;
  336. }
  337. /* Store the msg in the ctx */
  338. /* Parsed in iterate. */
  339. ctx->pending_result=res;
  340. ctx->pending_result_type=LDAP_RES_SEARCH_RESULT;
  341. doit=0;
  342. }
  343. }
  344. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,ctx);
  345. return 0;
  346. }
  347. /*
  348. * Return the next entry in the result set. The entry is returned
  349. * in the pblock.
  350. * Returns 0 normally. If -1 is returned, it means that some
  351. * exceptional condition, e.g. timelimit exceeded has occurred,
  352. * and this routine has sent a result to the client. If zero
  353. * is returned and no entry is available in the PBlock, then
  354. * we've iterated through all the entries.
  355. */
  356. int
  357. chainingdb_next_search_entry ( Slapi_PBlock *pb )
  358. {
  359. char *target;
  360. int sizelimit, timelimit;
  361. int rc, parse_rc, retcode;
  362. int i, attrsonly;
  363. time_t optime;
  364. LDAPMessage *res=NULL;
  365. char *matched_msg,*error_msg;
  366. cb_searchContext *ctx=NULL;
  367. Slapi_Entry *entry;
  368. LDAPControl **serverctrls=NULL;
  369. char **referrals=NULL;
  370. cb_backend_instance * cb=NULL;
  371. Slapi_Backend * be;
  372. time_t endtime;
  373. matched_msg=error_msg=NULL;
  374. slapi_pblock_get( pb, SLAPI_SEARCH_RESULT_SET, &ctx );
  375. slapi_pblock_get( pb, SLAPI_BACKEND, &be );
  376. slapi_pblock_get( pb, SLAPI_SEARCH_TIMELIMIT, &timelimit );
  377. slapi_pblock_get( pb, SLAPI_SEARCH_SIZELIMIT, &sizelimit );
  378. slapi_pblock_get( pb, SLAPI_SEARCH_TARGET, &target );
  379. slapi_pblock_get( pb, SLAPI_OPINITIATED_TIME, &optime );
  380. slapi_pblock_get( pb, SLAPI_SEARCH_ATTRSONLY, &attrsonly );
  381. cb = cb_get_instance(be);
  382. if ( NULL == ctx ) {
  383. /* End of local search */
  384. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,NULL);
  385. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,NULL);
  386. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  387. "Unexpected NULL ctx in chainingdb_next_search_entry\n");
  388. return 0;
  389. }
  390. if ( NULL != ctx->readahead) {
  391. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET, ctx);
  392. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY, ctx->readahead);
  393. if (ctx->tobefreed != ctx->readahead) {
  394. slapi_entry_free(ctx->tobefreed);
  395. }
  396. ctx->tobefreed = ctx->readahead;
  397. ctx->readahead = NULL;
  398. cb_set_acl_policy(pb);
  399. return 0;
  400. }
  401. if ( NULL != ctx->tobefreed ) {
  402. slapi_entry_free(ctx->tobefreed);
  403. ctx->tobefreed=NULL;
  404. }
  405. if ( ctx->type == CB_SEARCHCONTEXT_ENTRY ) {
  406. int n;
  407. Slapi_Entry ** ptr;
  408. if ( (timelimit != -1) && (timelimit != 0)) {
  409. time_t now=current_time();
  410. if (now > (optime + timelimit)) {
  411. cb_send_ldap_result( pb, LDAP_TIMELIMIT_EXCEEDED, NULL,NULL, 0, NULL);
  412. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,NULL );
  413. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,NULL);
  414. for ( n = 0, ptr=(Slapi_Entry **)ctx->data; ptr != NULL && ptr[n] != NULL; n++ ) {
  415. slapi_entry_free(ptr[n]);
  416. }
  417. if (ctx->data)
  418. slapi_ch_free((void **)&ctx->data);
  419. slapi_ch_free((void **)&ctx);
  420. return -1;
  421. }
  422. }
  423. /*
  424. ** Return the Slapi_Entry of the result set one
  425. ** by one
  426. */
  427. for ( n = 0, ptr=(Slapi_Entry **)ctx->data; ptr != NULL && ptr[n] != NULL; n++ );
  428. if ( n != 0) {
  429. Slapi_Entry * anEntry=ptr[n-1];
  430. ptr[n-1]=NULL;
  431. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,anEntry);
  432. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,ctx);
  433. cb_set_acl_policy(pb);
  434. ctx->tobefreed=anEntry;
  435. } else {
  436. slapi_ch_free((void **) &ctx);
  437. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,NULL );
  438. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,NULL);
  439. }
  440. return 0;
  441. }
  442. /*
  443. * Grab a connection handle. Should be the same as the one
  444. * used in the build_candidate list. To be certain of that, grab it from
  445. * the context.
  446. */
  447. /* Poll the server for the results of the search operation.
  448. * Passing LDAP_MSG_ONE indicates that you want to receive
  449. * the entries one at a time, as they come in. If the next
  450. * entry that you retrieve is NULL, there are no more entries.
  451. */
  452. /* heart-beat management */
  453. if (cb->max_idle_time>0)
  454. endtime=current_time() + cb->max_idle_time;
  455. while (1) {
  456. if (cb_check_forward_abandon(cb,pb,ctx->ld,ctx->msgid)) {
  457. /* cnx handle released */
  458. if (ctx->pending_result)
  459. ldap_msgfree(ctx->pending_result);
  460. slapi_ch_free((void **) &ctx);
  461. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,NULL );
  462. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,NULL);
  463. return -1;
  464. }
  465. /* Check for time limit done by the remote farm server */
  466. /* Check for size limit done by the remote farm server */
  467. /* Use pending msg if one is available */
  468. if (ctx->pending_result) {
  469. res=ctx->pending_result;
  470. rc=ctx->pending_result_type;
  471. ctx->pending_result=NULL;
  472. } else {
  473. rc=ldap_result(ctx->ld,ctx->msgid,
  474. LDAP_MSG_ONE, &cb->abandon_timeout, &res );
  475. }
  476. /* The server can return three types of results back to the client,
  477. * and the return value of ldap_result() indicates the result type:
  478. * LDAP_RES_SEARCH_ENTRY identifies an entry found by the search,
  479. * LDAP_RES_SEARCH_REFERENCE identifies a search reference returned
  480. * by the server, and LDAP_RES_SEARCH_RESULT is the last result
  481. * sent from the server to the client after the operation completes.
  482. * We need to check for each of these types of results.
  483. */
  484. switch ( rc ) {
  485. case -1:
  486. /* An error occurred. */
  487. rc = slapi_ldap_get_lderrno( ctx->ld, NULL, NULL );
  488. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,NULL);
  489. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,NULL);
  490. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL, ldap_err2string( rc ), 0, NULL);
  491. if (res)
  492. ldap_msgfree(res);
  493. cb_release_op_connection(cb->pool,ctx->ld,CB_LDAP_CONN_ERROR(rc));
  494. slapi_ch_free((void **)&ctx);
  495. return -1;
  496. case 0:
  497. /* heart-beat management */
  498. if ((rc=cb_ping_farm(cb,ctx->cnx,endtime)) != LDAP_SUCCESS) {
  499. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,NULL);
  500. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,NULL);
  501. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  502. ldap_err2string(rc), 0, NULL);
  503. if (res)
  504. ldap_msgfree(res);
  505. cb_release_op_connection(cb->pool,ctx->ld,CB_LDAP_CONN_ERROR(rc));
  506. slapi_ch_free((void **)&ctx);
  507. return -1;
  508. }
  509. #ifdef CB_YIELD
  510. DS_Sleep(PR_INTERVAL_NO_WAIT);
  511. #endif
  512. break;
  513. case LDAP_RES_SEARCH_ENTRY:
  514. /* heart-beat management */
  515. if (cb->max_idle_time>0)
  516. endtime=current_time() + cb->max_idle_time;
  517. /* The server sent one of the entries found by the search */
  518. if ((entry = cb_LDAPMessage2Entry(ctx->ld,res,attrsonly)) == NULL) {
  519. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,"Invalid entry received.\n");
  520. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,NULL);
  521. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,NULL);
  522. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL, NULL , 0, NULL);
  523. ldap_msgfree(res);
  524. cb_release_op_connection(cb->pool,ctx->ld,0);
  525. slapi_ch_free((void **)&ctx);
  526. return -1;
  527. }
  528. ctx->tobefreed=entry;
  529. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,ctx);
  530. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,entry);
  531. cb_set_acl_policy(pb);
  532. ldap_msgfree(res);
  533. return 0;
  534. case LDAP_RES_SEARCH_REFERENCE:
  535. /* The server sent a search reference encountered during the
  536. * search operation.
  537. */
  538. /* heart-beat management */
  539. if (cb->max_idle_time>0)
  540. endtime=current_time() + cb->max_idle_time;
  541. parse_rc = ldap_parse_reference( ctx->ld, res, &referrals, NULL, 1 );
  542. if ( parse_rc != LDAP_SUCCESS ) {
  543. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  544. ldap_err2string( parse_rc ), 0, NULL);
  545. cb_release_op_connection(cb->pool,ctx->ld,CB_LDAP_CONN_ERROR(parse_rc));
  546. slapi_ch_free((void **)&ctx);
  547. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,NULL);
  548. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,NULL);
  549. return -1;
  550. }
  551. /*
  552. ** build a dummy entry on the fly with a ref attribute
  553. */
  554. {
  555. struct berval bv;
  556. int i;
  557. struct berval *bvals[2];
  558. Slapi_Entry *anEntry = slapi_entry_alloc();
  559. slapi_entry_set_dn(anEntry,slapi_ch_strdup(target));
  560. bvals[1]=NULL;
  561. bvals[0]=&bv;
  562. bv.bv_val="referral";
  563. bv.bv_len=strlen(bv.bv_val);
  564. slapi_entry_add_values( anEntry, "objectclass", bvals);
  565. for (i=0;referrals[i] != NULL; i++) {
  566. bv.bv_val=referrals[i];
  567. bv.bv_len=strlen(bv.bv_val);
  568. slapi_entry_add_values( anEntry, "ref", bvals);
  569. }
  570. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,ctx);
  571. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,anEntry);
  572. cb_set_acl_policy(pb);
  573. }
  574. if (referrals != NULL) {
  575. slapi_ldap_value_free( referrals );
  576. }
  577. return 0;
  578. case LDAP_RES_SEARCH_RESULT:
  579. /* Parse the final result received from the server. Note the last
  580. * argument is a non-zero value, which indicates that the
  581. * LDAPMessage structure will be freed when done.
  582. */
  583. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,NULL);
  584. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,NULL);
  585. parse_rc = ldap_parse_result( ctx->ld, res,
  586. &rc,&matched_msg,&error_msg, &referrals, &serverctrls, 1 );
  587. if ( parse_rc != LDAP_SUCCESS ) {
  588. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, matched_msg,
  589. ldap_err2string( parse_rc ), 0, NULL);
  590. retcode=-1;
  591. } else
  592. if ( rc != LDAP_SUCCESS ) {
  593. slapi_ldap_get_lderrno( ctx->ld, &matched_msg, &error_msg );
  594. cb_send_ldap_result( pb, rc, matched_msg, NULL, 0, NULL);
  595. /* BEWARE: Don't free matched_msg && error_msg */
  596. /* Points to the ld fields */
  597. matched_msg=NULL;
  598. error_msg=NULL;
  599. retcode=-1;
  600. } else {
  601. /* Add control response sent by the farm server */
  602. for (i=0; serverctrls && serverctrls[i];i++)
  603. slapi_pblock_set( pb, SLAPI_ADD_RESCONTROL, serverctrls[i]);
  604. retcode=0;
  605. }
  606. if (serverctrls)
  607. ldap_controls_free(serverctrls);
  608. slapi_ch_free((void **)&matched_msg);
  609. slapi_ch_free((void **)&error_msg);
  610. if (referrals)
  611. charray_free(referrals);
  612. cb_release_op_connection(cb->pool,ctx->ld,0);
  613. slapi_ch_free((void **)&ctx);
  614. return retcode;
  615. default:
  616. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  617. "chainingdb_next_search_entry:default case.\n");
  618. }
  619. }
  620. /* Not reached */
  621. /* return 0; */
  622. }
  623. int
  624. chaining_back_entry_release ( Slapi_PBlock *pb ) {
  625. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM, "chaining_back_entry_release\n");
  626. return 0;
  627. }
  628. void
  629. chaining_back_search_results_release ( void **sr )
  630. {
  631. cb_searchContext *ctx = (cb_searchContext *)(*sr);
  632. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  633. "chaining_back_search_results_release\n");
  634. if (ctx->readahead != ctx->tobefreed) {
  635. slapi_entry_free(ctx->readahead);
  636. }
  637. slapi_entry_free(ctx->tobefreed);
  638. ctx->tobefreed = NULL;
  639. slapi_ch_free((void **)&ctx->data);
  640. slapi_ch_free((void **)&ctx);
  641. return;
  642. }
  643. void
  644. chainingdb_prev_search_results ( Slapi_PBlock *pb )
  645. {
  646. cb_searchContext *ctx = NULL;
  647. Slapi_Entry *entry = NULL;
  648. slapi_pblock_get( pb, SLAPI_SEARCH_RESULT_SET, &ctx );
  649. slapi_pblock_get( pb, SLAPI_SEARCH_RESULT_ENTRY, &entry );
  650. ctx->readahead = entry;
  651. return;
  652. }