opshared.c 49 KB

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