opshared.c 52 KB

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