opshared.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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. /* opshared.c - functions shared between regular and internal operations */
  42. #include "slap.h"
  43. #include "index_subsys.h"
  44. /* helper functions */
  45. static void compute_limits (Slapi_PBlock *pb);
  46. /* attributes that no clients are allowed to add or modify */
  47. static char *protected_attrs_all [] = { PSEUDO_ATTR_UNHASHEDUSERPASSWORD,
  48. NULL
  49. };
  50. static char *pwpolicy_lock_attrs_all [] = { "passwordRetryCount",
  51. "retryCountResetTime",
  52. "accountUnlockTime",
  53. NULL};
  54. /* Forward declarations */
  55. static void compute_limits (Slapi_PBlock *pb);
  56. static int send_results (Slapi_PBlock *pb, int send_result, int *nentries);
  57. static int process_entry(Slapi_PBlock *pb, Slapi_Entry *e, int send_result);
  58. int op_shared_is_allowed_attr (const char *attr_name, int replicated_op)
  59. {
  60. int i;
  61. slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
  62. /* check list of attributes that no client is allowed to specify */
  63. for (i = 0; protected_attrs_all[i]; i ++)
  64. {
  65. if (strcasecmp (attr_name, protected_attrs_all[i]) == 0)
  66. {
  67. /* this attribute is not allowed */
  68. return 0;
  69. }
  70. }
  71. /* ONREPL - should allow backends to plugin here to specify
  72. attributes that are not allowed */
  73. if (!replicated_op)
  74. {
  75. /*
  76. * check to see if attribute is marked as one clients can't modify
  77. */
  78. struct asyntaxinfo *asi;
  79. int no_user_mod = 0;
  80. asi = attr_syntax_get_by_name( attr_name );
  81. if ( NULL != asi &&
  82. 0 != ( asi->asi_flags & SLAPI_ATTR_FLAG_NOUSERMOD ))
  83. {
  84. /* this attribute is not allowed */
  85. no_user_mod = 1;
  86. }
  87. attr_syntax_return( asi );
  88. if ( no_user_mod ) {
  89. return( 0 );
  90. }
  91. } else if (!slapdFrontendConfig->pw_is_global_policy) {
  92. /* check list of password policy attributes for locking accounts */
  93. for (i = 0; pwpolicy_lock_attrs_all[i]; i ++)
  94. {
  95. if (strcasecmp (attr_name, pwpolicy_lock_attrs_all[i]) == 0)
  96. {
  97. /* this attribute is not allowed */
  98. return 0;
  99. }
  100. }
  101. }
  102. /* this attribute is ok */
  103. return 1;
  104. }
  105. static ps_service_fn_ptr ps_service_fn = NULL;
  106. void
  107. do_ps_service(Slapi_Entry *e, Slapi_Entry *eprev, ber_int_t chgtype, ber_int_t chgnum)
  108. {
  109. if (NULL == ps_service_fn) {
  110. if (get_entry_point(ENTRY_POINT_PS_SERVICE, (caddr_t *)(&ps_service_fn)) < 0) {
  111. return;
  112. }
  113. }
  114. (ps_service_fn)(e, eprev, chgtype, chgnum);
  115. }
  116. void modify_update_last_modified_attr(Slapi_PBlock *pb, Slapi_Mods *smods)
  117. {
  118. char buf[20];
  119. struct berval bv;
  120. struct berval *bvals[2];
  121. time_t curtime;
  122. struct tm utm;
  123. Operation *op;
  124. LDAPDebug(LDAP_DEBUG_TRACE, "modify_update_last_modified_attr\n", 0, 0, 0);
  125. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  126. bvals[0] = &bv;
  127. bvals[1] = NULL;
  128. /* fill in modifiersname */
  129. if (slapi_sdn_isempty(&op->o_sdn)) {
  130. bv.bv_val = "";
  131. bv.bv_len = strlen(bv.bv_val);
  132. } else {
  133. bv.bv_val = (char*)slapi_sdn_get_dn(&op->o_sdn);
  134. bv.bv_len = strlen(bv.bv_val);
  135. }
  136. slapi_mods_add_modbvps(smods, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES,
  137. "modifiersname", bvals);
  138. /* fill in modifytimestamp */
  139. curtime = current_time();
  140. #ifdef _WIN32
  141. {
  142. struct tm *pt;
  143. pt = gmtime(&curtime);
  144. memcpy(&utm, pt, sizeof(struct tm));
  145. }
  146. #else
  147. gmtime_r(&curtime, &utm);
  148. #endif
  149. strftime(buf, sizeof(buf), "%Y%m%d%H%M%SZ", &utm);
  150. bv.bv_val = buf;
  151. bv.bv_len = strlen(bv.bv_val);
  152. slapi_mods_add_modbvps(smods, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES,
  153. "modifytimestamp", bvals);
  154. }
  155. /*
  156. * Returns: 0 - if the operation is successful
  157. * < 0 - if operation fails.
  158. * Note that an operation is considered "failed" if a result is sent
  159. * directly to the client when send_result is 0.
  160. */
  161. void
  162. op_shared_search (Slapi_PBlock *pb, int send_result)
  163. {
  164. char *base, *fstr;
  165. int scope;
  166. Slapi_Backend *be = NULL;
  167. Slapi_Backend *be_single = NULL;
  168. Slapi_Backend *be_list[BE_LIST_SIZE];
  169. Slapi_Entry *referral_list[BE_LIST_SIZE];
  170. char ebuf[ BUFSIZ ];
  171. char attrlistbuf[ 1024 ], *attrliststr, **attrs = NULL;
  172. int rc = 0;
  173. int internal_op;
  174. Slapi_DN sdn;
  175. Slapi_Operation *operation;
  176. Slapi_Entry *referral = NULL;
  177. char errorbuf[BUFSIZ];
  178. int nentries,pnentries;
  179. int flag_search_base_found = 0;
  180. int flag_no_such_object = 0;
  181. int flag_referral = 0;
  182. int flag_psearch = 0;
  183. int err_code = LDAP_SUCCESS;
  184. LDAPControl **ctrlp;
  185. struct berval *ctl_value = NULL;
  186. int iscritical = 0;
  187. char * be_name = NULL;
  188. int index = 0;
  189. int sent_result = 0;
  190. be_list[0] = NULL;
  191. referral_list[0] = NULL;
  192. /* get search parameters */
  193. slapi_pblock_get(pb, SLAPI_SEARCH_TARGET, &base);
  194. slapi_pblock_get(pb, SLAPI_SEARCH_SCOPE, &scope);
  195. slapi_pblock_get(pb, SLAPI_SEARCH_STRFILTER, &fstr);
  196. slapi_pblock_get(pb, SLAPI_SEARCH_ATTRS, &attrs);
  197. slapi_pblock_get (pb, SLAPI_OPERATION, &operation);
  198. internal_op= operation_is_flag_set(operation, OP_FLAG_INTERNAL);
  199. flag_psearch = operation_is_flag_set(operation, OP_FLAG_PS);
  200. slapi_sdn_init_dn_byref(&sdn, base);
  201. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_ACCESS))
  202. {
  203. char *fmtstr;
  204. #define SLAPD_SEARCH_FMTSTR_BASE "conn=%" NSPRIu64 " op=%d SRCH base=\"%s\" scope=%d "
  205. #define SLAPD_SEARCH_FMTSTR_BASE_INT "conn=%s op=%d SRCH base=\"%s\" scope=%d "
  206. #define SLAPD_SEARCH_FMTSTR_REMAINDER " attrs=%s%s\n"
  207. PR_ASSERT(fstr);
  208. if ( strlen(fstr) > 1024 )
  209. {
  210. /*
  211. * slapi_log_access() throws away log lines that are longer than
  212. * 2048 characters, so we limit the filter string to 1024 (better
  213. * to log something rather than nothing)
  214. */
  215. if ( !internal_op )
  216. {
  217. fmtstr = SLAPD_SEARCH_FMTSTR_BASE "filter=\"%.1024s...\"" SLAPD_SEARCH_FMTSTR_REMAINDER;
  218. }
  219. else
  220. {
  221. fmtstr = SLAPD_SEARCH_FMTSTR_BASE_INT "filter=\"%.1024s...\"" SLAPD_SEARCH_FMTSTR_REMAINDER;
  222. }
  223. } else {
  224. if ( !internal_op )
  225. {
  226. fmtstr = SLAPD_SEARCH_FMTSTR_BASE "filter=\"%s\"" SLAPD_SEARCH_FMTSTR_REMAINDER;
  227. }
  228. else
  229. {
  230. fmtstr = SLAPD_SEARCH_FMTSTR_BASE_INT "filter=\"%s\"" SLAPD_SEARCH_FMTSTR_REMAINDER;
  231. }
  232. }
  233. if ( NULL == attrs ) {
  234. attrliststr = "ALL";
  235. } else {
  236. strarray2str( attrs, attrlistbuf, sizeof( attrlistbuf ),
  237. 1 /* include quotes */ );
  238. attrliststr = attrlistbuf;
  239. }
  240. if ( !internal_op )
  241. {
  242. slapi_log_access(LDAP_DEBUG_STATS, fmtstr,
  243. pb->pb_conn->c_connid,
  244. pb->pb_op->o_opid,
  245. escape_string(slapi_sdn_get_dn (&sdn), ebuf),
  246. scope, fstr, attrliststr,
  247. flag_psearch ? " options=persistent" : "");
  248. }
  249. else
  250. {
  251. slapi_log_access(LDAP_DEBUG_ARGS, fmtstr,
  252. LOG_INTERNAL_OP_CON_ID,
  253. LOG_INTERNAL_OP_OP_ID,
  254. escape_string(slapi_sdn_get_dn (&sdn), ebuf),
  255. scope, fstr, attrliststr,
  256. flag_psearch ? " options=persistent" : "");
  257. }
  258. }
  259. slapi_pblock_set(pb, SLAPI_SEARCH_TARGET, (void*)slapi_sdn_get_ndn (&sdn));
  260. /* target spec is used to decide which plugins are applicable for the operation */
  261. operation_set_target_spec (pb->pb_op, &sdn);
  262. /* this is time to check if mapping tree specific control
  263. * was used to specify that we want to parse only
  264. * one backend
  265. */
  266. slapi_pblock_get(pb, SLAPI_REQCONTROLS, &ctrlp);
  267. if (NULL != ctrlp)
  268. {
  269. if (slapi_control_present(ctrlp, MTN_CONTROL_USE_ONE_BACKEND_EXT_OID,
  270. &ctl_value, &iscritical))
  271. {
  272. /* this control is the smart version of MTN_CONTROL_USE_ONE_BACKEND_OID,
  273. * it works out for itself what back end is required (thereby relieving
  274. * the client of working out which backend it needs) by looking at the
  275. * base of the search if no value is supplied
  276. */
  277. if((ctl_value->bv_len != 0) && ctl_value->bv_val)
  278. {
  279. be_name = ctl_value->bv_val;
  280. }
  281. else
  282. {
  283. /* we don't need no steenkin values */
  284. Slapi_Backend *searchbe = slapi_be_select( &sdn );
  285. if(searchbe && searchbe != defbackend_get_backend())
  286. {
  287. be_name = slapi_be_get_name(searchbe);
  288. }
  289. }
  290. }
  291. else
  292. {
  293. if (slapi_control_present(ctrlp, MTN_CONTROL_USE_ONE_BACKEND_OID,
  294. &ctl_value, &iscritical))
  295. {
  296. if ((ctl_value->bv_len == 0) || (ctl_value->bv_val == NULL))
  297. {
  298. rc = -1;
  299. send_ldap_result(pb, LDAP_PROTOCOL_ERROR, NULL, NULL, 0, NULL);
  300. goto free_and_return_nolock;
  301. }
  302. else
  303. {
  304. be_name = ctl_value->bv_val;
  305. if (be_name == NULL)
  306. {
  307. rc = -1;
  308. send_ldap_result(pb, LDAP_PROTOCOL_ERROR, NULL, NULL, 0, NULL);
  309. goto free_and_return_nolock;
  310. }
  311. }
  312. }
  313. }
  314. if ( slapi_control_present (ctrlp, LDAP_CONTROL_GET_EFFECTIVE_RIGHTS,
  315. &ctl_value, &iscritical) )
  316. {
  317. operation->o_flags |= OP_FLAG_GET_EFFECTIVE_RIGHTS;
  318. }
  319. }
  320. if (be_name == NULL)
  321. {
  322. /* no specific backend was requested, use the mapping tree
  323. */
  324. err_code = slapi_mapping_tree_select_all(pb, be_list, referral_list, errorbuf);
  325. if (((err_code != LDAP_SUCCESS) && (err_code != LDAP_OPERATIONS_ERROR) && (err_code != LDAP_REFERRAL))
  326. || ((err_code == LDAP_OPERATIONS_ERROR) && ((be_list == NULL) || be_list[0] == NULL)))
  327. {
  328. send_ldap_result(pb, err_code, NULL, errorbuf, 0, NULL);
  329. rc = -1;
  330. goto free_and_return;
  331. }
  332. if (be_list[0] != NULL)
  333. {
  334. index = 0;
  335. while (be_list[index] && be_list[index+1])
  336. index++;
  337. be = be_list[index];
  338. }
  339. else
  340. be = NULL;
  341. }
  342. else
  343. {
  344. /* specific backend be_name was requested, use slapi_be_select_by_instance_name
  345. */
  346. be_single = be = slapi_be_select_by_instance_name(be_name);
  347. if (be_single)
  348. slapi_be_Rlock(be_single);
  349. be_list[0] = NULL;
  350. referral_list[0] = NULL;
  351. referral = NULL;
  352. }
  353. slapi_pblock_set(pb, SLAPI_BACKEND_COUNT, &index);
  354. if (be)
  355. {
  356. slapi_pblock_set(pb, SLAPI_BACKEND, be);
  357. /* adjust time and size limits */
  358. compute_limits (pb);
  359. /* call the pre-search plugins. if they succeed, call the backend
  360. search function. then call the post-search plugins. */
  361. /* ONREPL - should regular plugin be called for internal searches ? */
  362. if (plugin_call_plugins(pb, SLAPI_PLUGIN_PRE_SEARCH_FN) == 0)
  363. {
  364. slapi_pblock_set(pb, SLAPI_PLUGIN, be->be_database);
  365. set_db_default_result_handlers(pb);
  366. /* Now would be a good time to call the search rewriters for computed attrs */
  367. rc = compute_rewrite_search_filter(pb);
  368. switch (rc)
  369. {
  370. case 1: /* A rewriter says that we should refuse to perform this search.
  371. The potential exists that we will refuse to perform a search
  372. which we were going to refer, perhaps to a server which would
  373. be willing to perform the search. That's bad. The rewriter
  374. could be clever enough to spot this and do the right thing though. */
  375. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL, "Search not supported", 0, NULL);
  376. rc = -1;
  377. goto free_and_return;
  378. case -2: /* memory was allocated */
  379. /* take note of any changes */
  380. slapi_pblock_get(pb, SLAPI_SEARCH_TARGET, &base);
  381. slapi_pblock_get(pb, SLAPI_SEARCH_SCOPE, &scope);
  382. slapi_sdn_set_dn_byref(&sdn, base);
  383. break;
  384. case -1:
  385. case 0: /* OK */
  386. break;
  387. case 2: /* Operations error */
  388. send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL, "search rewriter", 0, NULL);
  389. rc = -1;
  390. goto free_and_return;
  391. }
  392. } else {
  393. /*
  394. * A pre-operation plugin handled this search. Grab the return code
  395. * (it may have been set by a plugin) and return.
  396. *
  397. * In DS 5.x, the following two lines of code did not exist, which
  398. * means a pre-search function might return a non-zero value (which
  399. * indicates that a result was returned to the client) but the code
  400. * below would execute the search anyway. This was a regression from
  401. * the documented plugin API behavior (and from DS 3.x and 4.x).
  402. */
  403. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &rc);
  404. goto free_and_return;
  405. }
  406. }
  407. /* PAR: now filters have been rewritten, we can assign plugins to work on them */
  408. index_subsys_assign_filter_decoders(pb);
  409. nentries = 0;
  410. rc = -1; /* zero backends would mean failure */
  411. while (be)
  412. {
  413. const Slapi_DN * be_suffix;
  414. int err = 0;
  415. if (be->be_search == NULL)
  416. {
  417. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL, "Function not implemented", 0, NULL);
  418. rc = -1;
  419. goto free_and_return;
  420. }
  421. pnentries = 0;
  422. /* the backends returns no such object when a
  423. * search is attempted in a node above their nsslapd-suffix
  424. * this is correct but a bit annoying when a backends
  425. * is below another backend because in that case the
  426. * such searches should sometimes succeed
  427. * To allow this we therefore have to change the
  428. * SLAPI_SEARCH_TARGET parameter in the pblock
  429. *
  430. * Also when we climb down the mapping tree we have to
  431. * change ONE-LEVEL searches to BASE
  432. */
  433. /* that's mean we only support one suffix per backend */
  434. be_suffix = slapi_be_getsuffix(be, 0);
  435. /* be_suffix null means that we are searching the default backend
  436. * -> don't change the search parameters in pblock
  437. */
  438. if (be_suffix != NULL)
  439. {
  440. if ((be_name == NULL) && (scope == LDAP_SCOPE_ONELEVEL))
  441. {
  442. /* one level searches
  443. * - depending on the suffix of the backend we might have to
  444. * do a one level search or a base search
  445. * - we might also have to change the search target
  446. */
  447. if (slapi_sdn_isparent(&sdn, be_suffix)
  448. || (slapi_sdn_get_ndn_len(&sdn) == 0))
  449. {
  450. int tmp_scope = LDAP_SCOPE_BASE;
  451. slapi_pblock_set(pb, SLAPI_SEARCH_SCOPE, &tmp_scope);
  452. slapi_pblock_set(pb, SLAPI_SEARCH_TARGET,
  453. (void *)slapi_sdn_get_ndn(be_suffix));
  454. }
  455. else if (slapi_sdn_issuffix(&sdn, be_suffix))
  456. {
  457. int tmp_scope = LDAP_SCOPE_ONELEVEL;
  458. slapi_pblock_set(pb, SLAPI_SEARCH_SCOPE, &tmp_scope);
  459. slapi_pblock_set(pb, SLAPI_SEARCH_TARGET,
  460. (void *)slapi_sdn_get_ndn (&sdn));
  461. }
  462. else
  463. goto next_be;
  464. }
  465. /* subtree searches :
  466. * if the search was started above the backend suffix
  467. * - temporarily set the SLAPI_SEARCH_TARGET to the
  468. * base of the node so that we don't get a NO SUCH OBJECT error
  469. * - do not change the scope
  470. */
  471. if (scope == LDAP_SCOPE_SUBTREE)
  472. {
  473. if (slapi_sdn_issuffix(be_suffix, &sdn))
  474. {
  475. slapi_pblock_set(pb, SLAPI_SEARCH_TARGET,
  476. (void *)slapi_sdn_get_ndn(be_suffix));
  477. }
  478. else
  479. slapi_pblock_set(pb, SLAPI_SEARCH_TARGET, (void *)slapi_sdn_get_ndn(&sdn));
  480. }
  481. }
  482. slapi_pblock_set(pb, SLAPI_BACKEND, be);
  483. slapi_pblock_set(pb, SLAPI_PLUGIN, be->be_database);
  484. slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET, NULL);
  485. /* ONREPL - we need to be able to tell the backend not to send results directly */
  486. rc = (*be->be_search)(pb);
  487. switch (rc)
  488. {
  489. case 1: /* if the backend returned LDAP_NO_SUCH_OBJECT for a SEARCH request,
  490. it will not have sent back a result - otherwise, it will have
  491. sent a result */
  492. rc = SLAPI_FAIL_GENERAL;
  493. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &err);
  494. if (err == LDAP_NO_SUCH_OBJECT)
  495. {
  496. /* may be the object exist somewhere else
  497. * wait the end of the loop to send back this error
  498. */
  499. flag_no_such_object = 1;
  500. break;
  501. }
  502. /* err something other than LDAP_NO_SUCH_OBJECT, so the backend will have sent the result -
  503. Set a flag here so we don't return another result. */
  504. sent_result = 1;
  505. /* fall through */
  506. case -1: /* an error occurred */
  507. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &err);
  508. if (err == LDAP_NO_SUCH_OBJECT)
  509. {
  510. /* may be the object exist somewhere else
  511. * wait the end of the loop to send back this error
  512. */
  513. flag_no_such_object = 1;
  514. break;
  515. }
  516. else
  517. {
  518. /* for error other than LDAP_NO_SUCH_OBJECT
  519. * the error has already been sent
  520. * stop the search here
  521. */
  522. goto free_and_return;
  523. }
  524. /* when rc == SLAPI_FAIL_DISKFULL this case is executed */
  525. case SLAPI_FAIL_DISKFULL:
  526. operation_out_of_disk_space();
  527. goto free_and_return;
  528. case 0: /* search was successful and we need to send the result */
  529. flag_search_base_found++;
  530. rc = send_results (pb, 1, &pnentries);
  531. /* if rc != 0 an error occurred while sending back the entries
  532. * to the LDAP client
  533. * LDAP error should already have been sent to the client
  534. * stop the search, free and return
  535. */
  536. if (rc != 0)
  537. goto free_and_return;
  538. break;
  539. }
  540. nentries += pnentries;
  541. next_be:
  542. if (be_list[0] == NULL)
  543. {
  544. be = NULL;
  545. }
  546. else
  547. {
  548. index--;
  549. if (index>=0)
  550. be = be_list[index];
  551. else
  552. be = NULL;
  553. }
  554. }
  555. /* if referrals were sent back by the mapping tree
  556. * add them to the list of referral in the pblock instead
  557. * of searching the backend
  558. */
  559. index = 0;
  560. while ((referral = referral_list[index++]) != NULL)
  561. {
  562. slapi_pblock_set(pb, SLAPI_BACKEND, NULL);
  563. if (err_code == LDAP_REFERRAL)
  564. {
  565. send_referrals_from_entry(pb,referral);
  566. goto free_and_return;
  567. }
  568. else
  569. {
  570. if (process_entry(pb, referral, 1))
  571. {
  572. flag_referral++;
  573. }
  574. else
  575. {
  576. /* Manage DSA was set, referral must be sent as an entry */
  577. int attrsonly;
  578. char **attrs = NULL;
  579. slapi_pblock_get(pb, SLAPI_SEARCH_ATTRS, &attrs);
  580. slapi_pblock_get(pb, SLAPI_SEARCH_ATTRSONLY, &attrsonly);
  581. slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_ENTRY, referral);
  582. switch (send_ldap_search_entry(pb, referral, NULL, attrs, attrsonly))
  583. {
  584. case 0:
  585. flag_search_base_found++;
  586. nentries++;
  587. break;
  588. case 1: /* entry not sent */
  589. case -1: /* connection closed */
  590. break;
  591. }
  592. }
  593. }
  594. }
  595. if (flag_search_base_found || flag_referral) {
  596. rc = 0;
  597. }
  598. /* ONREPL - we currently call postop only if operation is successful;
  599. We should always send result and pass error code to the plugin */
  600. if (rc == 0) {
  601. plugin_call_plugins(pb, SLAPI_PLUGIN_POST_SEARCH_FN);
  602. }
  603. else
  604. {
  605. plugin_call_plugins(pb, SLAPI_PLUGIN_POST_SEARCH_FAIL_FN);
  606. }
  607. if (send_result) {
  608. if (rc == 0)
  609. {
  610. /* at least one backend returned something and there was no critical error
  611. * from the LDAP client point of view the search was successful
  612. */
  613. struct berval **urls = NULL;
  614. slapi_pblock_get(pb, SLAPI_SEARCH_REFERRALS, &urls);
  615. send_ldap_result(pb, err_code, NULL, NULL, nentries, urls);
  616. }
  617. else if (flag_no_such_object)
  618. {
  619. /* there was at least 1 backend that was called to process
  620. * the operation and all backends returned NO SUCH OBJECTS.
  621. * Don't send the result if it's already been handled above.
  622. */
  623. if (!sent_result) {
  624. slapi_send_ldap_result_from_pb(pb);
  625. }
  626. }
  627. else
  628. {
  629. /* No backend was found in the mapping tree to process
  630. * the operation : return NO SUCH OBJECT
  631. */
  632. send_ldap_result(pb, LDAP_NO_SUCH_OBJECT, NULL, NULL, 0, NULL);
  633. }
  634. } else {
  635. /* persistent search: ignore error locating base entry */
  636. rc = 0;
  637. }
  638. free_and_return:
  639. if ((be_list[0] != NULL) || (referral_list[0] != NULL))
  640. slapi_mapping_tree_free_all(be_list, referral_list);
  641. else if (be_single)
  642. slapi_be_Unlock(be_single);
  643. free_and_return_nolock:
  644. slapi_pblock_set(pb, SLAPI_SEARCH_TARGET, base);
  645. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, &rc);
  646. index_subsys_filter_decoders_done(pb);
  647. slapi_sdn_done(&sdn);
  648. }
  649. /* Returns 1 if this processing on this entry is finished
  650. * and doesn't need to be sent.
  651. */
  652. static int
  653. process_entry(Slapi_PBlock *pb, Slapi_Entry *e, int send_result)
  654. {
  655. int managedsait;
  656. Slapi_Attr *a=NULL;
  657. int numValues=0, i;
  658. if (!send_result)
  659. {
  660. /* server requested that we don't send results to the client,
  661. for instance, in case of a persistent search
  662. */
  663. return 1;
  664. }
  665. /* ONREPL - check if the entry should be referred (because of the copyingFrom) */
  666. /*
  667. * If this is a referral, and the managedsait control is not present,
  668. * arrange for a referral to be sent. For v2 connections,
  669. * the referrals are just squirreled away and sent with the
  670. * final result. For v3, the referrals are sent in separate LDAP messages.
  671. */
  672. slapi_pblock_get(pb, SLAPI_MANAGEDSAIT, &managedsait);
  673. if (!managedsait && slapi_entry_attr_find(e, "ref", &a)== 0)
  674. {
  675. /* to fix 522189: when rootDSE, don't interpret attribute ref as a referral entry */
  676. if ( slapi_is_rootdse(slapi_entry_get_dn_const(e)) )
  677. return 0; /* more to do for this entry, e.g., send it back to the client */
  678. /* end fix */
  679. slapi_attr_get_numvalues(a, &numValues );
  680. if (numValues == 0)
  681. {
  682. char ebuf[ BUFSIZ ];
  683. LDAPDebug(LDAP_DEBUG_ANY, "null ref in (%s)\n",
  684. escape_string(slapi_entry_get_dn_const(e), ebuf), 0, 0);
  685. }
  686. else
  687. {
  688. Slapi_Value *val=NULL;
  689. struct berval **refscopy=NULL;
  690. struct berval **urls, **tmpUrls=NULL;
  691. tmpUrls=(struct berval **) slapi_ch_malloc((numValues + 1) * sizeof(struct berval*));
  692. for ( i = slapi_attr_first_value(a, &val); i != -1;
  693. i = slapi_attr_next_value(a, i, &val)) {
  694. tmpUrls[i]=(struct berval*)slapi_value_get_berval(val);
  695. }
  696. tmpUrls[numValues]=NULL;
  697. refscopy = ref_adjust(pb, tmpUrls, slapi_entry_get_sdn_const(e), 1);
  698. slapi_pblock_get(pb, SLAPI_SEARCH_REFERRALS, &urls);
  699. send_ldap_referral(pb, e, refscopy, &urls);
  700. slapi_pblock_set(pb, SLAPI_SEARCH_REFERRALS, urls);
  701. if (NULL != refscopy)
  702. {
  703. ber_bvecfree(refscopy);
  704. refscopy = NULL;
  705. }
  706. if( NULL != tmpUrls) {
  707. slapi_ch_free( (void **)&tmpUrls );
  708. }
  709. }
  710. return 1; /* done with this entry */
  711. }
  712. return 0;
  713. }
  714. #if 0
  715. /* Loops through search entries and sends them to the client.
  716. * returns -1 on error, 0 if result packet was sent or 1 if
  717. * result packet wasn't sent
  718. */
  719. static int
  720. iterate_with_lookahead(Slapi_PBlock *pb, Slapi_Backend *be, int send_result, int *pnentries)
  721. {
  722. int rc;
  723. int attrsonly;
  724. int done = 0;
  725. Slapi_Entry *e;
  726. void *backend_info_ptr;
  727. Slapi_Entry *next_e;
  728. void *next_backend_info_ptr;
  729. char **attrs = NULL;
  730. int send_result_status = 0;
  731. slapi_pblock_get(pb, SLAPI_SEARCH_ATTRS, &attrs);
  732. slapi_pblock_get(pb, SLAPI_SEARCH_ATTRSONLY, &attrsonly);
  733. /* setup for the loop */
  734. rc = be->be_next_search_entry_ext(pb, 1);
  735. if (rc < 0)
  736. {
  737. /*
  738. * Some exceptional condition occurred. Results
  739. * have been sent, so we're finished.
  740. */
  741. if (rc == SLAPI_FAIL_DISKFULL)
  742. {
  743. operation_out_of_disk_space();
  744. }
  745. return -1;
  746. }
  747. slapi_pblock_get(pb, SLAPI_SEARCH_RESULT_ENTRY, &next_e);
  748. slapi_pblock_get(pb, SLAPI_SEARCH_RESULT_ENTRY_EXT, &next_backend_info_ptr);
  749. if (NULL == next_e)
  750. {
  751. /* no entries */
  752. done = 1;
  753. }
  754. backend_info_ptr = NULL;
  755. /* Done setting up the loop, now here it comes */
  756. while (!done)
  757. {
  758. /* Allow the backend to free the entry we just finished using */
  759. /* It is ok to call this when backend_info_ptr is NULL */
  760. be->be_entry_release(pb, backend_info_ptr);
  761. e = next_e;
  762. backend_info_ptr = next_backend_info_ptr;
  763. rc = be->be_next_search_entry_ext(pb, 1);
  764. if (rc < 0)
  765. {
  766. /*
  767. * Some exceptional condition occurred. Results
  768. * have been sent, so we're finished.
  769. */
  770. if (rc == SLAPI_FAIL_DISKFULL)
  771. {
  772. operation_out_of_disk_space();
  773. }
  774. return -1;
  775. }
  776. else
  777. {
  778. slapi_pblock_get(pb, SLAPI_SEARCH_RESULT_ENTRY, &next_e);
  779. slapi_pblock_get(pb, SLAPI_SEARCH_RESULT_ENTRY_EXT, &next_backend_info_ptr);
  780. if (next_e == NULL)
  781. {
  782. /* no more entries */
  783. done = 1;
  784. }
  785. }
  786. if (process_entry(pb, e, send_result))
  787. {
  788. /* shouldn't send this entry */
  789. continue;
  790. }
  791. /*
  792. * It's a regular entry, or it's a referral and
  793. * managedsait control is on. In either case, send the entry.
  794. */
  795. if (done)
  796. {
  797. struct berval **urls = NULL;
  798. /* Send the entry and the result at the same time */
  799. slapi_pblock_get(pb, SLAPI_SEARCH_REFERRALS, &urls);
  800. rc = send_ldap_search_entry_ext(pb, e, NULL, attrs, attrsonly, 1,
  801. (*pnentries)+1, urls);
  802. if (rc == 1)
  803. {
  804. /* this means we didn't have access to the entry. Since the
  805. * entry was not sent, we need to send the done packet.
  806. */
  807. send_result_status = 1;
  808. }
  809. }
  810. else
  811. {
  812. /* Send the entry */
  813. rc = send_ldap_search_entry(pb, e, NULL, attrs,
  814. attrsonly);
  815. }
  816. switch (rc)
  817. {
  818. case 0: /* entry sent ok */
  819. (*pnentries)++;
  820. slapi_pblock_set(pb, SLAPI_NENTRIES, pnentries);
  821. break;
  822. case 1: /* entry not sent */
  823. break;
  824. case -1: /* connection closed */
  825. /*
  826. * mark the operation as abandoned so the backend
  827. * next entry function gets called again and has
  828. * a chance to clean things up.
  829. */
  830. pb->pb_op->o_status = SLAPI_OP_STATUS_ABANDONED;
  831. break;
  832. }
  833. }
  834. be->be_entry_release(pb, backend_info_ptr);
  835. if (*pnentries == 0 || send_result_status)
  836. {
  837. /* We didn't send the result done message so the caller
  838. * must send it */
  839. return 1;
  840. }
  841. else
  842. {
  843. /* The result message has been sent */
  844. return 0;
  845. }
  846. }
  847. #endif
  848. /* Loops through search entries and sends them to the client.
  849. * returns -1 on error or 1 if result packet wasn't sent.
  850. * This function never returns 0 because it doesn't send
  851. * the result packet back with the last entry like
  852. * iterate_with_lookahead trys to do.
  853. */
  854. static int
  855. iterate(Slapi_PBlock *pb, Slapi_Backend *be, int send_result, int *pnentries)
  856. {
  857. int rc;
  858. int attrsonly;
  859. int done = 0;
  860. Slapi_Entry *e = NULL;
  861. char **attrs = NULL;
  862. slapi_pblock_get(pb, SLAPI_SEARCH_ATTRS, &attrs);
  863. slapi_pblock_get(pb, SLAPI_SEARCH_ATTRSONLY, &attrsonly);
  864. *pnentries = 0;
  865. while (!done)
  866. {
  867. Slapi_Entry *gerentry = NULL;
  868. Slapi_Operation *operation;
  869. rc = be->be_next_search_entry(pb);
  870. if (rc < 0)
  871. {
  872. /*
  873. * Some exceptional condition occurred. Results have been sent, so we're finished.
  874. */
  875. if (rc == SLAPI_FAIL_DISKFULL)
  876. {
  877. operation_out_of_disk_space();
  878. }
  879. return -1;
  880. }
  881. slapi_pblock_get(pb, SLAPI_SEARCH_RESULT_ENTRY, &e);
  882. /* Check for possible get_effective_rights control */
  883. slapi_pblock_get (pb, SLAPI_OPERATION, &operation);
  884. if ( operation->o_flags & OP_FLAG_GET_EFFECTIVE_RIGHTS )
  885. {
  886. char *errbuf = NULL;
  887. char **gerattrs = NULL;
  888. char **gerattrsdup = NULL;
  889. char **gap = NULL;
  890. char *gapnext = NULL;
  891. slapi_pblock_get( pb, SLAPI_SEARCH_GERATTRS, &gerattrs );
  892. gerattrsdup = cool_charray_dup(gerattrs);
  893. gap = gerattrsdup;
  894. do
  895. {
  896. gapnext = NULL;
  897. if (gap)
  898. {
  899. if (*gap && *(gap+1))
  900. {
  901. gapnext = *(gap+1);
  902. *(gap+1) = NULL;
  903. }
  904. slapi_pblock_set( pb, SLAPI_SEARCH_GERATTRS, gap );
  905. rc = plugin_call_acl_plugin (pb, e, attrs, NULL,
  906. SLAPI_ACL_ALL, ACLPLUGIN_ACCESS_GET_EFFECTIVE_RIGHTS,
  907. &errbuf);
  908. if (NULL != gapnext)
  909. {
  910. *(gap+1) = gapnext;
  911. }
  912. }
  913. else if (NULL != e)
  914. {
  915. rc = plugin_call_acl_plugin (pb, e, attrs, NULL,
  916. SLAPI_ACL_ALL, ACLPLUGIN_ACCESS_GET_EFFECTIVE_RIGHTS,
  917. &errbuf);
  918. }
  919. if (NULL == e) {
  920. /* get the template entry, if any */
  921. slapi_pblock_get(pb, SLAPI_SEARCH_RESULT_ENTRY, &e);
  922. if (NULL == e) {
  923. /* everything is ok - don't send the result */
  924. return 1;
  925. }
  926. gerentry = e;
  927. }
  928. if ( rc != LDAP_SUCCESS ) {
  929. /* Send error result and
  930. abort op if the control is critical */
  931. LDAPDebug( LDAP_DEBUG_ANY,
  932. "Failed to get effective rights for entry (%s), rc=%d\n",
  933. slapi_entry_get_dn_const(e), rc, 0 );
  934. send_ldap_result( pb, rc, NULL, errbuf, 0, NULL );
  935. slapi_ch_free ( (void**)&errbuf );
  936. if (gerentry)
  937. {
  938. slapi_pblock_set(pb,
  939. SLAPI_SEARCH_RESULT_ENTRY, NULL);
  940. slapi_entry_free(gerentry);
  941. gerentry = e = NULL;
  942. }
  943. return( -1 );
  944. }
  945. slapi_ch_free ( (void**)&errbuf );
  946. if (process_entry(pb, e, send_result))
  947. {
  948. /* shouldn't send this entry */
  949. if (gerentry)
  950. {
  951. slapi_pblock_set(pb,
  952. SLAPI_SEARCH_RESULT_ENTRY, NULL);
  953. slapi_entry_free(gerentry);
  954. gerentry = e = NULL;
  955. }
  956. continue;
  957. }
  958. /*
  959. * It's a regular entry, or it's a referral and
  960. * managedsait control is on. In either case, send
  961. * the entry.
  962. */
  963. switch (send_ldap_search_entry(pb, e,
  964. NULL, attrs, attrsonly))
  965. {
  966. case 0: /* entry sent ok */
  967. (*pnentries)++;
  968. slapi_pblock_set(pb, SLAPI_NENTRIES, pnentries);
  969. break;
  970. case 1: /* entry not sent */
  971. break;
  972. case -1: /* connection closed */
  973. /*
  974. * mark the operation as abandoned so the backend
  975. * next entry function gets called again and has
  976. * a chance to clean things up.
  977. */
  978. pb->pb_op->o_status = SLAPI_OP_STATUS_ABANDONED;
  979. break;
  980. }
  981. if (gerentry)
  982. {
  983. slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_ENTRY, NULL);
  984. slapi_entry_free(gerentry);
  985. gerentry = e = NULL;
  986. }
  987. }
  988. while (gap && ++gap && *gap);
  989. slapi_pblock_set( pb, SLAPI_SEARCH_GERATTRS, gerattrs );
  990. cool_charray_free(gerattrsdup);
  991. if (NULL == e)
  992. {
  993. /* no more entries */
  994. done = 1;
  995. }
  996. }
  997. else if (e)
  998. {
  999. if (process_entry(pb, e, send_result))
  1000. {
  1001. /* shouldn't send this entry */
  1002. continue;
  1003. }
  1004. /*
  1005. * It's a regular entry, or it's a referral and
  1006. * managedsait control is on. In either case, send
  1007. * the entry.
  1008. */
  1009. switch (send_ldap_search_entry(pb, e, NULL, attrs, attrsonly))
  1010. {
  1011. case 0: /* entry sent ok */
  1012. (*pnentries)++;
  1013. slapi_pblock_set(pb, SLAPI_NENTRIES, pnentries);
  1014. break;
  1015. case 1: /* entry not sent */
  1016. break;
  1017. case -1: /* connection closed */
  1018. /*
  1019. * mark the operation as abandoned so the backend
  1020. * next entry function gets called again and has
  1021. * a chance to clean things up.
  1022. */
  1023. pb->pb_op->o_status = SLAPI_OP_STATUS_ABANDONED;
  1024. break;
  1025. }
  1026. }
  1027. else
  1028. {
  1029. /* no more entries */
  1030. done = 1;
  1031. }
  1032. }
  1033. return 1;
  1034. }
  1035. static int timelimit_reslimit_handle = -1;
  1036. static int sizelimit_reslimit_handle = -1;
  1037. /*
  1038. * Register size and time limit with the binder-based resource limits
  1039. * subsystem. A SLAPI_RESLIMIT_STATUS_... code is returned.
  1040. */
  1041. int
  1042. search_register_reslimits( void )
  1043. {
  1044. int rc1, rc2;
  1045. rc1 = slapi_reslimit_register( SLAPI_RESLIMIT_TYPE_INT,
  1046. "nsSizeLimit" , &sizelimit_reslimit_handle );
  1047. rc2 = slapi_reslimit_register( SLAPI_RESLIMIT_TYPE_INT,
  1048. "nsTimeLimit", &timelimit_reslimit_handle );
  1049. if ( rc1 != SLAPI_RESLIMIT_STATUS_SUCCESS ) {
  1050. return( rc1 );
  1051. } else {
  1052. return( rc2 );
  1053. }
  1054. }
  1055. /*
  1056. * Compute size and time limits based on the connection (bind identity).
  1057. * Binder-based resource limits get top priority, followed by those associated
  1058. * with the backend we are using.
  1059. *
  1060. * If the binder is the root DN and there is nothing in the root DN's entry
  1061. * to say otherwise, no limits are used. Otherwise, the lower of the limit
  1062. * that was sent in the LDAP request and that available based on the
  1063. * connection bind identity or configured backend limit is used.
  1064. */
  1065. static void
  1066. compute_limits (Slapi_PBlock *pb)
  1067. {
  1068. int timelimit, sizelimit;
  1069. int requested_timelimit, max_timelimit, requested_sizelimit, max_sizelimit;
  1070. int isroot;
  1071. int isCertAuth;
  1072. Slapi_ComponentId *component_id = NULL;
  1073. Slapi_Backend *be;
  1074. slapi_pblock_get (pb, SLAPI_SEARCH_TIMELIMIT, &requested_timelimit);
  1075. slapi_pblock_get (pb, SLAPI_SEARCH_SIZELIMIT, &requested_sizelimit);
  1076. slapi_pblock_get (pb, SLAPI_REQUESTOR_ISROOT, &isroot);
  1077. slapi_pblock_get (pb, SLAPI_BACKEND, &be);
  1078. /* If the search belongs to the client authentication process, take the value at
  1079. * nsslapd-timelimit as the actual time limit.
  1080. */
  1081. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &component_id);
  1082. if (component_id) {
  1083. isCertAuth = (! strcasecmp(component_id->sci_component_name, COMPONENT_CERT_AUTH) ) ? 1 : 0;
  1084. if (isCertAuth) {
  1085. timelimit = config_get_timelimit();
  1086. goto set_timelimit;
  1087. }
  1088. }
  1089. /*
  1090. * Compute the time limit.
  1091. */
  1092. if ( slapi_reslimit_get_integer_limit( pb->pb_conn,
  1093. timelimit_reslimit_handle, &max_timelimit )
  1094. != SLAPI_RESLIMIT_STATUS_SUCCESS ) {
  1095. /*
  1096. * no limit associated with binder/connection or some other error
  1097. * occurred. use the default maximum.
  1098. */
  1099. if ( isroot ) {
  1100. max_timelimit = -1; /* no limit */
  1101. } else {
  1102. max_timelimit = be->be_timelimit;
  1103. }
  1104. }
  1105. if ( requested_timelimit == 0 ) {
  1106. timelimit = ( max_timelimit == -1 ) ? -1 : max_timelimit;
  1107. } else if ( max_timelimit == -1 || requested_timelimit < max_timelimit ) {
  1108. timelimit = requested_timelimit;
  1109. } else {
  1110. timelimit = max_timelimit;
  1111. }
  1112. set_timelimit:
  1113. slapi_pblock_set(pb, SLAPI_SEARCH_TIMELIMIT, &timelimit);
  1114. /*
  1115. * Compute the size limit.
  1116. */
  1117. if ( slapi_reslimit_get_integer_limit( pb->pb_conn,
  1118. sizelimit_reslimit_handle, &max_sizelimit )
  1119. != SLAPI_RESLIMIT_STATUS_SUCCESS ) {
  1120. /*
  1121. * no limit associated with binder/connection or some other error
  1122. * occurred. use the default maximum.
  1123. */
  1124. if ( isroot ) {
  1125. max_sizelimit = -1; /* no limit */
  1126. } else {
  1127. max_sizelimit = be->be_sizelimit;
  1128. }
  1129. }
  1130. if ( requested_sizelimit == 0 ) {
  1131. sizelimit = ( max_sizelimit == -1 ) ? -1 : max_sizelimit;
  1132. } else if ( max_sizelimit == -1 || requested_sizelimit < max_sizelimit ) {
  1133. sizelimit = requested_sizelimit;
  1134. } else {
  1135. sizelimit = max_sizelimit;
  1136. }
  1137. slapi_pblock_set(pb, SLAPI_SEARCH_SIZELIMIT, &sizelimit);
  1138. LDAPDebug( LDAP_DEBUG_TRACE,
  1139. "=> compute_limits: sizelimit=%d, timelimit=%d\n",
  1140. sizelimit, timelimit, 0 );
  1141. }
  1142. /* Iterates through results and send them to the client.
  1143. * Returns 0 if successful and -1 otherwise
  1144. */
  1145. static int send_results (Slapi_PBlock *pb, int send_result, int * nentries)
  1146. {
  1147. Slapi_Backend *be;
  1148. int rc;
  1149. slapi_pblock_get (pb, SLAPI_BACKEND, &be);
  1150. if (be->be_next_search_entry == NULL)
  1151. {
  1152. /* we need to send the result, but the function to iterate through
  1153. the result set is not implemented */
  1154. /* ONREPL - log error */
  1155. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL, "Search not supported", 0, NULL);
  1156. return -1;
  1157. }
  1158. /* Iterate through the returned result set */
  1159. if (be->be_next_search_entry_ext != NULL)
  1160. {
  1161. /* The iterate look ahead is causing a whole mess with the ACL.
  1162. ** the entries are now visiting the ACL land in a random way
  1163. ** and not the ordered way it was before. Until we figure out
  1164. ** let's not change the behavior.
  1165. **
  1166. ** Don't use iterate_with_lookahead because it sends the result
  1167. * in the same times as the entry and this can cause failure
  1168. * of the mapping tree scanning algorithme
  1169. * if (getFrontendConfig()->result_tweak)
  1170. * {
  1171. * rc = iterate_with_lookahead(pb, be, send_result, nentries);
  1172. * }
  1173. * else
  1174. */
  1175. rc = iterate(pb, be, send_result, nentries);
  1176. }
  1177. else
  1178. {
  1179. rc = iterate(pb, be, send_result, nentries);
  1180. }
  1181. switch(rc)
  1182. {
  1183. case -1: /* an error occured */
  1184. case 0 : /* everything is ok - result is sent */
  1185. /* If this happens we are dead but hopefully iterate
  1186. * never sends the result itself
  1187. */
  1188. break;
  1189. case 1: /* everything is ok - don't send the result */
  1190. rc = 0;
  1191. }
  1192. return rc;
  1193. }
  1194. void op_shared_log_error_access (Slapi_PBlock *pb, const char *type, const char *dn, const char *msg)
  1195. {
  1196. char ebuf[BUFSIZ];
  1197. slapi_log_access( LDAP_DEBUG_STATS, "conn=%" NSPRIu64 " op=%d %s dn=\"%s\", %s\n",
  1198. ( pb->pb_conn ? pb->pb_conn->c_connid : 0),
  1199. ( pb->pb_op ? pb->pb_op->o_opid : 0),
  1200. type,
  1201. escape_string( dn, ebuf ),
  1202. msg ? msg : "" );
  1203. }