opshared.c 35 KB

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