cb_search.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. slapi_rwlock_rdlock(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. slapi_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. rc = cb_get_connection(cb->pool, &ld, &cnx, &timeout, &cnxerrbuf);
  185. if (LDAP_SUCCESS != rc) {
  186. static int warned_get_conn = 0;
  187. if (!warned_get_conn) {
  188. slapi_log_error(SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  189. "cb_get_connection failed (%d) %s\n",
  190. rc, ldap_err2string(rc));
  191. warned_get_conn = 1;
  192. }
  193. if (rc == LDAP_TIMELIMIT_EXCEEDED) {
  194. cb_send_ldap_result(pb, rc, NULL, cnxerrbuf, 0, NULL);
  195. } else {
  196. cb_send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL,
  197. cnxerrbuf, 0, NULL);
  198. }
  199. slapi_ch_free_string(&cnxerrbuf);
  200. /* ping the farm.
  201. * If the farm is unreachable, we increment the counter */
  202. cb_ping_farm(cb, NULL, 0);
  203. return 1;
  204. }
  205. /*
  206. * Control management
  207. */
  208. if ( LDAP_SUCCESS != (rc = cb_update_controls( pb,ld,&ctrls,CB_UPDATE_CONTROLS_ADDAUTH ))) {
  209. cb_send_ldap_result( pb, rc, NULL,NULL, 0, NULL);
  210. cb_release_op_connection(cb->pool,ld,0);
  211. return 1;
  212. }
  213. if ( slapi_op_abandoned( pb )) {
  214. cb_release_op_connection(cb->pool,ld,0);
  215. if ( NULL != ctrls)
  216. ldap_controls_free(ctrls);
  217. return 1;
  218. }
  219. ctx = (cb_searchContext *) slapi_ch_calloc(1,sizeof(cb_searchContext));
  220. /*
  221. ** We need to store the connection handle in the search context
  222. ** to make sure we reuse it in the next_entry iteration
  223. ** Indeed, if another thread on this connection detects a problem
  224. ** on this connection, it may reallocate a new connection and
  225. ** a call to get_connection may return a new cnx. Too bad.
  226. */
  227. ctx->ld=ld;
  228. ctx->cnx=cnx;
  229. /* for some reasons, it is an error to pass in a zero'd timeval */
  230. /* to ldap_search_ext() */
  231. if ((timeout.tv_sec==0) && (timeout.tv_usec==0))
  232. timeout.tv_sec=timeout.tv_usec=-1;
  233. /* heart-beat management */
  234. if (cb->max_idle_time>0)
  235. endtime=current_time() + cb->max_idle_time;
  236. rc=ldap_search_ext(ld ,target,scope,filter,attrs,attrsonly,
  237. ctrls, NULL, &timeout,sizelimit, &(ctx->msgid) );
  238. if ( NULL != ctrls)
  239. ldap_controls_free(ctrls);
  240. if ( LDAP_SUCCESS != rc ) {
  241. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL, ldap_err2string(rc), 0, NULL);
  242. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  243. slapi_ch_free((void **) &ctx);
  244. return 1;
  245. }
  246. /*
  247. ** Need to get the very first result to handle
  248. ** errors properly, especially no search base.
  249. */
  250. doit=1;
  251. while (doit) {
  252. if (cb_check_forward_abandon(cb,pb,ctx->ld,ctx->msgid)) {
  253. slapi_ch_free((void **) &ctx);
  254. return 1;
  255. }
  256. rc=ldap_result(ld,ctx->msgid,LDAP_MSG_ONE,&cb->abandon_timeout,&res);
  257. switch ( rc ) {
  258. case -1:
  259. /* An error occurred. return now */
  260. rc = slapi_ldap_get_lderrno(ld,NULL,NULL);
  261. /* tuck away some errors in a OPERATION_ERROR */
  262. if (CB_LDAP_CONN_ERROR(rc)) {
  263. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  264. ldap_err2string( rc ), 0, NULL);
  265. } else {
  266. cb_send_ldap_result(pb,rc, NULL, NULL,0,NULL);
  267. }
  268. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  269. if (res)
  270. ldap_msgfree(res);
  271. slapi_ch_free((void **)&ctx);
  272. return 1;
  273. case 0:
  274. /* Local timeout management */
  275. if (timelimit != -1) {
  276. if (current_time() > endbefore) {
  277. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  278. "Local timeout expiration\n");
  279. cb_send_ldap_result(pb,LDAP_TIMELIMIT_EXCEEDED,
  280. NULL,NULL, 0, NULL);
  281. /* Force connection close */
  282. cb_release_op_connection(cb->pool,ld,1);
  283. if (res)
  284. ldap_msgfree(res);
  285. slapi_ch_free((void **)&ctx);
  286. return 1;
  287. }
  288. }
  289. /* heart-beat management */
  290. if ((rc=cb_ping_farm(cb,cnx,endtime)) != LDAP_SUCCESS) {
  291. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  292. ldap_err2string(rc), 0, NULL);
  293. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  294. if (res)
  295. ldap_msgfree(res);
  296. slapi_ch_free((void **)&ctx);
  297. return 1;
  298. }
  299. #ifdef CB_YIELD
  300. DS_Sleep(PR_INTERVAL_NO_WAIT);
  301. #endif
  302. break;
  303. case LDAP_RES_SEARCH_ENTRY:
  304. case LDAP_RES_SEARCH_REFERENCE:
  305. /* Some results received */
  306. /* don't parse result here */
  307. ctx->pending_result=res;
  308. ctx->pending_result_type=rc;
  309. doit=0;
  310. break;
  311. case LDAP_RES_SEARCH_RESULT:
  312. matched_msg=NULL;
  313. error_msg=NULL;
  314. referrals=NULL;
  315. serverctrls=NULL;
  316. parse_rc=ldap_parse_result(ld,res,&rc,&matched_msg,
  317. &error_msg,&referrals, &serverctrls, 0 );
  318. if ( parse_rc != LDAP_SUCCESS ) {
  319. static int warned_parse_rc = 0;
  320. if (!warned_parse_rc && error_msg) {
  321. slapi_log_error( SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  322. "%s%s%s\n",
  323. matched_msg?matched_msg:"",
  324. (matched_msg&&(*matched_msg!='\0'))?": ":"",
  325. error_msg );
  326. warned_parse_rc = 1;
  327. }
  328. cb_send_ldap_result( pb, parse_rc, NULL,
  329. ENDUSERMSG, 0, NULL );
  330. rc=-1;
  331. } else if ( rc != LDAP_SUCCESS ) {
  332. static int warned_rc = 0;
  333. if (!warned_rc) {
  334. slapi_ldap_get_lderrno( ctx->ld,
  335. &matched_msg, &error_msg );
  336. slapi_log_error( SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  337. "%s%s%s\n",
  338. matched_msg?matched_msg:"",
  339. (matched_msg&&(*matched_msg!='\0'))?": ":"",
  340. error_msg );
  341. warned_rc = 1;
  342. }
  343. cb_send_ldap_result( pb, rc, NULL, ENDUSERMSG, 0, NULL);
  344. /* BEWARE: matched_msg and error_msg points */
  345. /* to ld fields. */
  346. matched_msg=NULL;
  347. error_msg=NULL;
  348. rc=-1;
  349. }
  350. slapi_ch_free((void **)&matched_msg);
  351. slapi_ch_free((void **)&error_msg);
  352. if (serverctrls)
  353. ldap_controls_free(serverctrls);
  354. if (referrals)
  355. charray_free(referrals);
  356. if (rc!=LDAP_SUCCESS) {
  357. cb_release_op_connection(cb->pool,ld,
  358. CB_LDAP_CONN_ERROR(rc));
  359. ldap_msgfree(res);
  360. slapi_ch_free((void **)&ctx);
  361. return -1;
  362. }
  363. /* Store the msg in the ctx */
  364. /* Parsed in iterate. */
  365. ctx->pending_result=res;
  366. ctx->pending_result_type=LDAP_RES_SEARCH_RESULT;
  367. doit=0;
  368. }
  369. }
  370. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,ctx);
  371. return 0;
  372. }
  373. /*
  374. * Return the next entry in the result set. The entry is returned
  375. * in the pblock.
  376. * Returns 0 normally. If -1 is returned, it means that some
  377. * exceptional condition, e.g. timelimit exceeded has occurred,
  378. * and this routine has sent a result to the client. If zero
  379. * is returned and no entry is available in the PBlock, then
  380. * we've iterated through all the entries.
  381. */
  382. int
  383. chainingdb_next_search_entry ( Slapi_PBlock *pb )
  384. {
  385. char *target;
  386. int sizelimit, timelimit;
  387. int rc, parse_rc, retcode;
  388. int i, attrsonly;
  389. time_t optime;
  390. LDAPMessage *res=NULL;
  391. char *matched_msg,*error_msg;
  392. cb_searchContext *ctx=NULL;
  393. Slapi_Entry *entry;
  394. LDAPControl **serverctrls=NULL;
  395. char **referrals=NULL;
  396. cb_backend_instance * cb=NULL;
  397. Slapi_Backend * be;
  398. time_t endtime;
  399. matched_msg=error_msg=NULL;
  400. slapi_pblock_get( pb, SLAPI_SEARCH_RESULT_SET, &ctx );
  401. slapi_pblock_get( pb, SLAPI_BACKEND, &be );
  402. slapi_pblock_get( pb, SLAPI_SEARCH_TIMELIMIT, &timelimit );
  403. slapi_pblock_get( pb, SLAPI_SEARCH_SIZELIMIT, &sizelimit );
  404. slapi_pblock_get( pb, SLAPI_SEARCH_TARGET, &target );
  405. slapi_pblock_get( pb, SLAPI_OPINITIATED_TIME, &optime );
  406. slapi_pblock_get( pb, SLAPI_SEARCH_ATTRSONLY, &attrsonly );
  407. cb = cb_get_instance(be);
  408. if ( NULL == ctx ) {
  409. /* End of local search */
  410. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,NULL);
  411. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,NULL);
  412. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  413. "Unexpected NULL ctx in chainingdb_next_search_entry\n");
  414. return 0;
  415. }
  416. if ( NULL != ctx->readahead) {
  417. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET, ctx);
  418. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY, ctx->readahead);
  419. if (ctx->tobefreed != ctx->readahead) {
  420. slapi_entry_free(ctx->tobefreed);
  421. }
  422. ctx->tobefreed = ctx->readahead;
  423. ctx->readahead = NULL;
  424. cb_set_acl_policy(pb);
  425. return 0;
  426. }
  427. if ( NULL != ctx->tobefreed ) {
  428. slapi_entry_free(ctx->tobefreed);
  429. ctx->tobefreed=NULL;
  430. }
  431. if ( ctx->type == CB_SEARCHCONTEXT_ENTRY ) {
  432. int n;
  433. Slapi_Entry ** ptr;
  434. if ( (timelimit != -1) && (timelimit != 0)) {
  435. time_t now=current_time();
  436. if (now > (optime + timelimit)) {
  437. cb_send_ldap_result( pb, LDAP_TIMELIMIT_EXCEEDED, NULL,NULL, 0, NULL);
  438. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,NULL );
  439. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,NULL);
  440. for ( n = 0, ptr=(Slapi_Entry **)ctx->data; ptr != NULL && ptr[n] != NULL; n++ ) {
  441. slapi_entry_free(ptr[n]);
  442. }
  443. if (ctx->data)
  444. slapi_ch_free((void **)&ctx->data);
  445. slapi_ch_free((void **)&ctx);
  446. return -1;
  447. }
  448. }
  449. /*
  450. ** Return the Slapi_Entry of the result set one
  451. ** by one
  452. */
  453. for ( n = 0, ptr=(Slapi_Entry **)ctx->data; ptr != NULL && ptr[n] != NULL; n++ );
  454. if ( n != 0) {
  455. Slapi_Entry * anEntry=ptr[n-1];
  456. ptr[n-1]=NULL;
  457. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,anEntry);
  458. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,ctx);
  459. cb_set_acl_policy(pb);
  460. ctx->tobefreed=anEntry;
  461. } else {
  462. slapi_ch_free((void **) &ctx);
  463. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,NULL );
  464. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,NULL);
  465. }
  466. return 0;
  467. }
  468. /*
  469. * Grab a connection handle. Should be the same as the one
  470. * used in the build_candidate list. To be certain of that, grab it from
  471. * the context.
  472. */
  473. /* Poll the server for the results of the search operation.
  474. * Passing LDAP_MSG_ONE indicates that you want to receive
  475. * the entries one at a time, as they come in. If the next
  476. * entry that you retrieve is NULL, there are no more entries.
  477. */
  478. /* heart-beat management */
  479. if (cb->max_idle_time>0)
  480. endtime=current_time() + cb->max_idle_time;
  481. while (1) {
  482. if (cb_check_forward_abandon(cb,pb,ctx->ld,ctx->msgid)) {
  483. /* cnx handle released */
  484. if (ctx->pending_result)
  485. ldap_msgfree(ctx->pending_result);
  486. slapi_ch_free((void **) &ctx);
  487. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,NULL );
  488. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,NULL);
  489. return -1;
  490. }
  491. /* Check for time limit done by the remote farm server */
  492. /* Check for size limit done by the remote farm server */
  493. /* Use pending msg if one is available */
  494. if (ctx->pending_result) {
  495. res=ctx->pending_result;
  496. rc=ctx->pending_result_type;
  497. ctx->pending_result=NULL;
  498. } else {
  499. rc=ldap_result(ctx->ld,ctx->msgid,
  500. LDAP_MSG_ONE, &cb->abandon_timeout, &res );
  501. }
  502. /* The server can return three types of results back to the client,
  503. * and the return value of ldap_result() indicates the result type:
  504. * LDAP_RES_SEARCH_ENTRY identifies an entry found by the search,
  505. * LDAP_RES_SEARCH_REFERENCE identifies a search reference returned
  506. * by the server, and LDAP_RES_SEARCH_RESULT is the last result
  507. * sent from the server to the client after the operation completes.
  508. * We need to check for each of these types of results.
  509. */
  510. switch ( rc ) {
  511. case -1:
  512. /* An error occurred. */
  513. rc = slapi_ldap_get_lderrno( ctx->ld, NULL, NULL );
  514. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,NULL);
  515. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,NULL);
  516. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL, ldap_err2string( rc ), 0, NULL);
  517. if (res)
  518. ldap_msgfree(res);
  519. cb_release_op_connection(cb->pool,ctx->ld,CB_LDAP_CONN_ERROR(rc));
  520. slapi_ch_free((void **)&ctx);
  521. return -1;
  522. case 0:
  523. /* heart-beat management */
  524. if ((rc=cb_ping_farm(cb,ctx->cnx,endtime)) != LDAP_SUCCESS) {
  525. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,NULL);
  526. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,NULL);
  527. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  528. ldap_err2string(rc), 0, NULL);
  529. if (res)
  530. ldap_msgfree(res);
  531. cb_release_op_connection(cb->pool,ctx->ld,CB_LDAP_CONN_ERROR(rc));
  532. slapi_ch_free((void **)&ctx);
  533. return -1;
  534. }
  535. #ifdef CB_YIELD
  536. DS_Sleep(PR_INTERVAL_NO_WAIT);
  537. #endif
  538. break;
  539. case LDAP_RES_SEARCH_ENTRY:
  540. /* heart-beat management */
  541. if (cb->max_idle_time>0)
  542. endtime=current_time() + cb->max_idle_time;
  543. /* The server sent one of the entries found by the search */
  544. if ((entry = cb_LDAPMessage2Entry(ctx->ld,res,attrsonly)) == NULL) {
  545. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,"Invalid entry received.\n");
  546. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,NULL);
  547. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,NULL);
  548. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL, NULL , 0, NULL);
  549. ldap_msgfree(res);
  550. cb_release_op_connection(cb->pool,ctx->ld,0);
  551. slapi_ch_free((void **)&ctx);
  552. return -1;
  553. }
  554. ctx->tobefreed=entry;
  555. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,ctx);
  556. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,entry);
  557. cb_set_acl_policy(pb);
  558. ldap_msgfree(res);
  559. return 0;
  560. case LDAP_RES_SEARCH_REFERENCE:
  561. /* The server sent a search reference encountered during the
  562. * search operation.
  563. */
  564. /* heart-beat management */
  565. if (cb->max_idle_time>0)
  566. endtime=current_time() + cb->max_idle_time;
  567. parse_rc = ldap_parse_reference( ctx->ld, res, &referrals, NULL, 1 );
  568. if ( parse_rc != LDAP_SUCCESS ) {
  569. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  570. ldap_err2string( parse_rc ), 0, NULL);
  571. cb_release_op_connection(cb->pool,ctx->ld,CB_LDAP_CONN_ERROR(parse_rc));
  572. slapi_ch_free((void **)&ctx);
  573. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,NULL);
  574. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,NULL);
  575. return -1;
  576. }
  577. /*
  578. ** build a dummy entry on the fly with a ref attribute
  579. */
  580. {
  581. struct berval bv;
  582. int i;
  583. struct berval *bvals[2];
  584. Slapi_Entry *anEntry = slapi_entry_alloc();
  585. slapi_entry_set_dn(anEntry,slapi_ch_strdup(target));
  586. bvals[1]=NULL;
  587. bvals[0]=&bv;
  588. bv.bv_val="referral";
  589. bv.bv_len=strlen(bv.bv_val);
  590. slapi_entry_add_values( anEntry, "objectclass", bvals);
  591. for (i=0;referrals[i] != NULL; i++) {
  592. bv.bv_val=referrals[i];
  593. bv.bv_len=strlen(bv.bv_val);
  594. slapi_entry_add_values( anEntry, "ref", bvals);
  595. }
  596. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,ctx);
  597. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,anEntry);
  598. cb_set_acl_policy(pb);
  599. }
  600. if (referrals != NULL) {
  601. slapi_ldap_value_free( referrals );
  602. }
  603. return 0;
  604. case LDAP_RES_SEARCH_RESULT:
  605. /* Parse the final result received from the server. Note the last
  606. * argument is a non-zero value, which indicates that the
  607. * LDAPMessage structure will be freed when done.
  608. */
  609. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_SET,NULL);
  610. slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,NULL);
  611. parse_rc = ldap_parse_result( ctx->ld, res,
  612. &rc,&matched_msg,&error_msg, &referrals, &serverctrls, 1 );
  613. if ( parse_rc != LDAP_SUCCESS ) {
  614. static int warned_parse_rc = 0;
  615. if (!warned_parse_rc) {
  616. slapi_log_error( SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  617. "%s%s%s\n",
  618. matched_msg?matched_msg:"",
  619. (matched_msg&&(*matched_msg!='\0'))?": ":"",
  620. ldap_err2string( parse_rc ));
  621. warned_parse_rc = 1;
  622. }
  623. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  624. ENDUSERMSG, 0, NULL );
  625. retcode=-1;
  626. } else
  627. if ( rc != LDAP_SUCCESS ) {
  628. static int warned_rc = 0;
  629. slapi_ldap_get_lderrno( ctx->ld, &matched_msg, &error_msg );
  630. if (!warned_rc) {
  631. slapi_log_error( SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  632. "%s%s%s\n",
  633. matched_msg?matched_msg:"",
  634. (matched_msg&&(*matched_msg!='\0'))?": ":"",
  635. error_msg );
  636. warned_rc = 1;
  637. }
  638. cb_send_ldap_result( pb, rc, matched_msg, ENDUSERMSG, 0, NULL );
  639. /* BEWARE: Don't free matched_msg && error_msg */
  640. /* Points to the ld fields */
  641. matched_msg=NULL;
  642. error_msg=NULL;
  643. retcode=-1;
  644. } else {
  645. /* Add control response sent by the farm server */
  646. for (i=0; serverctrls && serverctrls[i];i++)
  647. slapi_pblock_set( pb, SLAPI_ADD_RESCONTROL, serverctrls[i]);
  648. retcode=0;
  649. }
  650. if (serverctrls)
  651. ldap_controls_free(serverctrls);
  652. slapi_ch_free((void **)&matched_msg);
  653. slapi_ch_free((void **)&error_msg);
  654. if (referrals)
  655. charray_free(referrals);
  656. cb_release_op_connection(cb->pool,ctx->ld,0);
  657. slapi_ch_free((void **)&ctx);
  658. return retcode;
  659. default:
  660. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  661. "chainingdb_next_search_entry:default case.\n");
  662. }
  663. }
  664. /* Not reached */
  665. /* return 0; */
  666. }
  667. int
  668. chaining_back_entry_release ( Slapi_PBlock *pb ) {
  669. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM, "chaining_back_entry_release\n");
  670. return 0;
  671. }
  672. void
  673. chaining_back_search_results_release ( void **sr )
  674. {
  675. cb_searchContext *ctx = (cb_searchContext *)(*sr);
  676. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  677. "chaining_back_search_results_release\n");
  678. if (ctx->readahead != ctx->tobefreed) {
  679. slapi_entry_free(ctx->readahead);
  680. }
  681. slapi_entry_free(ctx->tobefreed);
  682. ctx->tobefreed = NULL;
  683. slapi_ch_free((void **)&ctx->data);
  684. slapi_ch_free((void **)&ctx);
  685. return;
  686. }
  687. void
  688. chainingdb_prev_search_results ( Slapi_PBlock *pb )
  689. {
  690. cb_searchContext *ctx = NULL;
  691. Slapi_Entry *entry = NULL;
  692. slapi_pblock_get( pb, SLAPI_SEARCH_RESULT_SET, &ctx );
  693. slapi_pblock_get( pb, SLAPI_SEARCH_RESULT_ENTRY, &entry );
  694. ctx->readahead = entry;
  695. return;
  696. }