cb_search.c 26 KB

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