1 /* limits.c - routines to handle regex-based size and time limits */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
22 #include <ac/string.h>
26 /* define to get an error if requesting limit higher than hard */
27 #undef ABOVE_HARD_LIMIT_IS_ERROR
30 limits2str( unsigned i )
33 case SLAP_LIMITS_UNDEFINED:
36 case SLAP_LIMITS_EXACT:
42 case SLAP_LIMITS_SUBTREE:
45 case SLAP_LIMITS_CHILDREN:
48 case SLAP_LIMITS_REGEX:
51 case SLAP_LIMITS_ANONYMOUS:
54 case SLAP_LIMITS_USERS:
69 struct slap_limits_set **limit
72 struct slap_limits **lm;
78 LDAP_LOG( SLAPD, DETAIL1, "==> limits_get: conn=%lu op=%lu dn=\"%s\"\n",
79 op->o_connid, op->o_opid,
80 BER_BVISNULL( ndn ) ? "[anonymous]" : ndn->bv_val );
83 Debug( LDAP_DEBUG_TRACE, "==> limits_get: conn=%lu op=%lu dn=\"%s\"\n",
84 op->o_connid, op->o_opid,
85 BER_BVISNULL( ndn ) ? "[anonymous]" : ndn->bv_val );
90 *limit = &op->o_bd->be_def_limit;
92 if ( op->o_bd->be_limits == NULL ) {
96 for ( lm = op->o_bd->be_limits; lm[0] != NULL; lm++ ) {
97 unsigned style = lm[0]->lm_flags & SLAP_LIMITS_MASK;
98 unsigned type = lm[0]->lm_flags & SLAP_LIMITS_TYPE_MASK;
101 case SLAP_LIMITS_EXACT:
102 if ( ndn->bv_len == 0 ) {
106 if ( type == SLAP_LIMITS_TYPE_GROUP ) {
109 rc = backend_group( op, NULL,
112 lm[0]->lm_group_ad );
114 *limit = &lm[0]->lm_limits;
116 LDAP_LOG( SLAPD, DETAIL1, "<== limits_get: type=GROUP match=EXACT "
117 "dn=\"%s\" oc=\"%s\" ad=\"%s\"\n",
118 lm[0]->lm_pat.bv_val,
119 lm[0]->lm_group_oc->soc_cname.bv_val,
120 lm[0]->lm_group_ad->ad_cname.bv_val );
122 Debug( LDAP_DEBUG_TRACE, "<== limits_get: type=GROUP match=EXACT "
123 "dn=\"%s\" oc=\"%s\" ad=\"%s\"\n",
124 lm[0]->lm_pat.bv_val,
125 lm[0]->lm_group_oc->soc_cname.bv_val,
126 lm[0]->lm_group_ad->ad_cname.bv_val );
133 if ( dn_match( &lm[0]->lm_pat, ndn ) ) {
134 *limit = &lm[0]->lm_limits;
136 LDAP_LOG( SLAPD, DETAIL1, "<== limits_get: type=DN match=EXACT dn=\"%s\"\n",
137 lm[0]->lm_pat.bv_val, 0, 0 );
139 Debug( LDAP_DEBUG_TRACE, "<== limits_get: type=DN match=EXACT dn=\"%s\"\n",
140 lm[0]->lm_pat.bv_val, 0, 0 );
147 case SLAP_LIMITS_ONE:
148 case SLAP_LIMITS_SUBTREE:
149 case SLAP_LIMITS_CHILDREN: {
152 if ( ndn->bv_len == 0 ) {
156 /* ndn shorter than dn_pat */
157 if ( ndn->bv_len < lm[0]->lm_pat.bv_len ) {
160 d = ndn->bv_len - lm[0]->lm_pat.bv_len;
162 /* allow exact match for SUBTREE only */
164 if ( style != SLAP_LIMITS_SUBTREE ) {
168 /* check for unescaped rdn separator */
169 if ( !DN_SEPARATOR( ndn->bv_val[d-1] ) ) {
174 /* in case of (sub)match ... */
175 if ( lm[0]->lm_pat.bv_len == ( ndn->bv_len - d )
176 && strcmp( lm[0]->lm_pat.bv_val,
177 &ndn->bv_val[d] ) == 0 )
179 /* check for exactly one rdn in case of ONE */
180 if ( style == SLAP_LIMITS_ONE ) {
182 * if ndn is more that one rdn
183 * below dn_pat, continue
185 if ( (size_t) dn_rdnlen( NULL, ndn )
192 *limit = &lm[0]->lm_limits;
194 LDAP_LOG( SLAPD, DETAIL1, "<== limits_get: type=DN match=%s dn=\"%s\"\n",
195 limits2str( style ), lm[0]->lm_pat.bv_val, 0 );
197 Debug( LDAP_DEBUG_TRACE, "<== limits_get: type=DN match=%s dn=\"%s\"\n",
198 limits2str( style ), lm[0]->lm_pat.bv_val, 0 );
206 case SLAP_LIMITS_REGEX:
207 if ( ndn->bv_len == 0 ) {
210 if ( regexec( &lm[0]->lm_regex, ndn->bv_val,
213 *limit = &lm[0]->lm_limits;
215 LDAP_LOG( SLAPD, DETAIL1, "<== limits_get: type=DN match=%s dn=\"%s\"\n",
216 limits2str( style ), lm[0]->lm_pat.bv_val, 0 );
218 Debug( LDAP_DEBUG_TRACE, "<== limits_get: type=DN match=%s dn=\"%s\"\n",
219 limits2str( style ), lm[0]->lm_pat.bv_val, 0 );
225 case SLAP_LIMITS_ANONYMOUS:
226 if ( ndn->bv_len == 0 ) {
228 LDAP_LOG( SLAPD, DETAIL1, "<== limits_get: type=DN match=%s\n",
229 limits2str( style ), 0, 0 );
231 Debug( LDAP_DEBUG_TRACE, "<== limits_get: type=DN match=%s\n",
232 limits2str( style ), 0, 0 );
234 *limit = &lm[0]->lm_limits;
239 case SLAP_LIMITS_USERS:
240 if ( ndn->bv_len != 0 ) {
241 *limit = &lm[0]->lm_limits;
243 LDAP_LOG( SLAPD, DETAIL1, "<== limits_get: type=DN match=%s\n",
244 limits2str( style ), 0, 0 );
246 Debug( LDAP_DEBUG_TRACE, "<== limits_get: type=DN match=%s\n",
247 limits2str( style ), 0, 0 );
253 case SLAP_LIMITS_ANY:
254 *limit = &lm[0]->lm_limits;
258 assert( 0 ); /* unreachable */
271 ObjectClass *group_oc,
272 AttributeDescription *group_ad,
273 struct slap_limits_set *limit
277 struct slap_limits *lm;
278 unsigned type, style;
283 type = flags & SLAP_LIMITS_TYPE_MASK;
284 style = flags & SLAP_LIMITS_MASK;
287 case SLAP_LIMITS_ANONYMOUS:
288 case SLAP_LIMITS_USERS:
289 case SLAP_LIMITS_ANY:
290 for ( i = 0; be->be_limits && be->be_limits[ i ]; i++ ) {
291 if ( be->be_limits[ i ]->lm_flags == style ) {
299 lm = ( struct slap_limits * )ch_calloc( sizeof( struct slap_limits ), 1 );
302 case SLAP_LIMITS_UNDEFINED:
303 style = SLAP_LIMITS_EXACT;
304 /* continue to next cases */
305 case SLAP_LIMITS_EXACT:
306 case SLAP_LIMITS_ONE:
307 case SLAP_LIMITS_SUBTREE:
308 case SLAP_LIMITS_CHILDREN:
309 lm->lm_flags = style | type;
313 bv.bv_val = (char *) pattern;
314 bv.bv_len = strlen( pattern );
316 rc = dnNormalize( 0, NULL, NULL, &bv, &lm->lm_pat, NULL );
317 if ( rc != LDAP_SUCCESS ) {
324 case SLAP_LIMITS_REGEX:
325 lm->lm_flags = style | type;
326 ber_str2bv( pattern, 0, 1, &lm->lm_pat );
327 if ( regcomp( &lm->lm_regex, lm->lm_pat.bv_val,
328 REG_EXTENDED | REG_ICASE ) ) {
329 free( lm->lm_pat.bv_val );
335 case SLAP_LIMITS_ANONYMOUS:
336 case SLAP_LIMITS_USERS:
337 case SLAP_LIMITS_ANY:
338 lm->lm_flags = style | type;
339 lm->lm_pat.bv_val = NULL;
340 lm->lm_pat.bv_len = 0;
345 case SLAP_LIMITS_TYPE_GROUP:
348 lm->lm_group_oc = group_oc;
349 lm->lm_group_ad = group_ad;
353 lm->lm_limits = *limit;
356 if ( be->be_limits != NULL ) {
357 for ( ; be->be_limits[i]; i++ );
360 be->be_limits = ( struct slap_limits ** )ch_realloc( be->be_limits,
361 sizeof( struct slap_limits * ) * ( i + 2 ) );
362 be->be_limits[i] = lm;
363 be->be_limits[i+1] = NULL;
377 int flags = SLAP_LIMITS_UNDEFINED;
379 struct slap_limits_set limit;
381 ObjectClass *group_oc = NULL;
382 AttributeDescription *group_ad = NULL;
388 LDAP_LOG( CONFIG, CRIT,
389 "%s : line %d: missing arg(s) in "
390 "\"limits <pattern> <limits>\" line.\n", fname, lineno, 0 );
392 Debug( LDAP_DEBUG_ANY,
393 "%s : line %d: missing arg(s) in "
394 "\"limits <pattern> <limits>\" line.\n%s",
400 limit = be->be_def_limit;
405 * "limits" <pattern> <limit> [ ... ]
412 * [ "dn" [ "." { "exact" | "base" | "onelevel" | "subtree" | children"
413 * | "regex" | "anonymous" } ] "=" ] <dn pattern>
416 * "exact" and "base" are the same (exact match);
417 * "onelevel" means exactly one rdn below, NOT including pattern
418 * "subtree" means any rdn below, including pattern
419 * "children" means any rdn below, NOT including pattern
421 * "anonymous" may be deprecated in favour
422 * of the pattern = "anonymous" form
424 * "group[/objectClass[/attributeType]]" "=" "<dn pattern>"
428 * "time" [ "." { "soft" | "hard" } ] "=" <integer>
430 * "size" [ "." { "soft" | "hard" | "unchecked" } ] "=" <integer>
434 if ( strcmp( pattern, "*" ) == 0) {
435 flags = SLAP_LIMITS_ANY;
437 } else if ( strcasecmp( pattern, "anonymous" ) == 0 ) {
438 flags = SLAP_LIMITS_ANONYMOUS;
440 } else if ( strcasecmp( pattern, "users" ) == 0 ) {
441 flags = SLAP_LIMITS_USERS;
443 } else if ( strncasecmp( pattern, "dn", STRLENOF( "dn" ) ) == 0 ) {
444 pattern += STRLENOF( "dn" );
445 if ( pattern[0] == '.' ) {
447 if ( strncasecmp( pattern, "exact", STRLENOF( "exact" )) == 0 ) {
448 flags = SLAP_LIMITS_EXACT;
449 pattern += STRLENOF( "exact" );
451 } else if ( strncasecmp( pattern, "base", STRLENOF( "base" ) ) == 0 ) {
452 flags = SLAP_LIMITS_BASE;
453 pattern += STRLENOF( "base" );
455 } else if ( strncasecmp( pattern, "one", STRLENOF( "one" ) ) == 0 ) {
456 flags = SLAP_LIMITS_ONE;
457 pattern += STRLENOF( "one" );
458 if ( strncasecmp( pattern, "level", STRLENOF( "level" ) ) == 0 ) {
459 pattern += STRLENOF( "level" );
463 LDAP_LOG( CONFIG, WARNING ,
464 "%s : line %d: deprecated \"one\" style "
465 "\"limits <pattern> <limits>\" line; "
466 "use \"onelevel\" instead.\n", fname, lineno, 0 );
468 Debug( LDAP_DEBUG_ANY,
469 "%s : line %d: deprecated \"one\" style "
470 "\"limits <pattern> <limits>\" line; "
471 "use \"onelevel\" instead.\n", fname, lineno, 0 );
475 } else if ( strncasecmp( pattern, "sub", STRLENOF( "sub" ) ) == 0 ) {
476 flags = SLAP_LIMITS_SUBTREE;
477 pattern += STRLENOF( "sub" );
478 if ( strncasecmp( pattern, "tree", STRLENOF( "tree" ) ) == 0 ) {
479 pattern += STRLENOF( "tree" );
483 LDAP_LOG( CONFIG, WARNING ,
484 "%s : line %d: deprecated \"sub\" style "
485 "\"limits <pattern> <limits>\" line; "
486 "use \"subtree\" instead.\n", fname, lineno, 0 );
488 Debug( LDAP_DEBUG_ANY,
489 "%s : line %d: deprecated \"sub\" style "
490 "\"limits <pattern> <limits>\" line; "
491 "use \"subtree\" instead.\n", fname, lineno, 0 );
495 } else if ( strncasecmp( pattern, "children", STRLENOF( "children" ) ) == 0 ) {
496 flags = SLAP_LIMITS_CHILDREN;
497 pattern += STRLENOF( "children" );
499 } else if ( strncasecmp( pattern, "regex", STRLENOF( "regex" ) ) == 0 ) {
500 flags = SLAP_LIMITS_REGEX;
501 pattern += STRLENOF( "regex" );
504 * this could be deprecated in favour
505 * of the pattern = "anonymous" form
507 } else if ( strncasecmp( pattern, "anonymous", STRLENOF( "anonymous" ) ) == 0 ) {
508 flags = SLAP_LIMITS_ANONYMOUS;
513 /* pre-check the data */
515 case SLAP_LIMITS_ANONYMOUS:
516 case SLAP_LIMITS_USERS:
518 /* no need for pattern */
523 if ( pattern[0] != '=' ) {
525 LDAP_LOG( CONFIG, CRIT,
526 "%s : line %d: missing '=' in "
527 "\"dn[.{exact|base|onelevel|subtree"
528 "|children|regex|anonymous}]" "=<pattern>\" in "
529 "\"limits <pattern> <limits>\" line.\n", fname, lineno, 0 );
531 Debug( LDAP_DEBUG_ANY,
532 "%s : line %d: missing '=' in "
533 "\"dn[.{exact|base|onelevel|subtree"
534 "|children|regex|anonymous}]"
536 "\"limits <pattern> <limits>\" "
543 /* skip '=' (required) */
546 /* trim obvious cases */
547 if ( strcmp( pattern, "*" ) == 0 ) {
548 flags = SLAP_LIMITS_ANY;
551 } else if ( flags == SLAP_LIMITS_REGEX
552 && strcmp( pattern, ".*" ) == 0 ) {
553 flags = SLAP_LIMITS_ANY;
558 } else if (strncasecmp( pattern, "group", STRLENOF( "group" ) ) == 0 ) {
559 pattern += STRLENOF( "group" );
561 if ( pattern[0] == '/' ) {
562 struct berval oc, ad;
564 oc.bv_val = pattern + 1;
566 ad.bv_val = strchr(pattern, '/');
567 if ( ad.bv_val != NULL ) {
568 const char *text = NULL;
571 oc.bv_len = ad.bv_val - oc.bv_val;
574 ad.bv_len = strlen( ad.bv_val );
575 rc = slap_bv2ad( &ad, &group_ad, &text );
576 if ( rc != LDAP_SUCCESS ) {
580 pattern = ad.bv_val + ad.bv_len;
583 oc.bv_len = strlen( oc.bv_val );
585 pattern = oc.bv_val + oc.bv_len;
588 group_oc = oc_bvfind( &oc );
589 if ( group_oc == NULL ) {
594 if ( group_oc == NULL ) {
595 group_oc = oc_find( SLAPD_GROUP_CLASS );
596 if ( group_oc == NULL ) {
602 if ( group_ad == NULL ) {
603 const char *text = NULL;
606 rc = slap_str2ad( SLAPD_GROUP_ATTR, &group_ad, &text );
608 if ( rc != LDAP_SUCCESS ) {
614 flags = SLAP_LIMITS_TYPE_GROUP | SLAP_LIMITS_EXACT;
616 if ( pattern[0] != '=' ) {
618 LDAP_LOG( CONFIG, CRIT,
619 "%s : line %d: missing '=' in "
620 "\"group[/objectClass[/attributeType]]"
622 "\"limits <pattern> <limits>\" line.\n",
625 Debug( LDAP_DEBUG_ANY,
626 "%s : line %d: missing '=' in "
627 "\"group[/objectClass[/attributeType]]"
629 "\"limits <pattern> <limits>\" line.\n",
635 /* skip '=' (required) */
640 for ( i = 2; i < argc; i++ ) {
641 if ( limits_parse_one( argv[i], &limit ) ) {
644 LDAP_LOG( CONFIG, CRIT,
645 "%s : line %d: unknown limit values \"%s\" in "
646 "\"limits <pattern> <limits>\" line.\n",
647 fname, lineno, argv[i] );
649 Debug( LDAP_DEBUG_ANY,
650 "%s : line %d: unknown limit values \"%s\" in "
651 "\"limits <pattern> <limits>\" line.\n",
652 fname, lineno, argv[i] );
662 * FIXME: add warnings?
664 if ( limit.lms_t_hard > 0 &&
665 ( limit.lms_t_hard < limit.lms_t_soft
666 || limit.lms_t_soft == -1 ) ) {
667 limit.lms_t_hard = limit.lms_t_soft;
670 if ( limit.lms_s_hard > 0 &&
671 ( limit.lms_s_hard < limit.lms_s_soft
672 || limit.lms_s_soft == -1 ) ) {
673 limit.lms_s_hard = limit.lms_s_soft;
682 * > 0 => limit (in seconds)
687 * > 0 => limit (in entries)
690 * -2 => disable the control
693 * > 0 => limit (in entries)
698 * > 0 => limit size (in entries)
700 if ( limit.lms_s_pr_total > 0 &&
701 limit.lms_s_pr > limit.lms_s_pr_total ) {
702 limit.lms_s_pr = limit.lms_s_pr_total;
705 rc = limits_add( be, flags, pattern, group_oc, group_ad, &limit );
709 LDAP_LOG( CONFIG, CRIT,
710 "%s : line %d: unable to add limit in "
711 "\"limits <pattern> <limits>\" line.\n",
714 Debug( LDAP_DEBUG_ANY,
715 "%s : line %d: unable to add limit in "
716 "\"limits <pattern> <limits>\" line.\n",
727 struct slap_limits_set *limit
733 if ( strncasecmp( arg, "time", STRLENOF( "time" ) ) == 0 ) {
734 arg += STRLENOF( "time" );
736 if ( arg[0] == '.' ) {
738 if ( strncasecmp( arg, "soft=", STRLENOF( "soft=" ) ) == 0 ) {
739 arg += STRLENOF( "soft=" );
740 if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
741 limit->lms_t_soft = -1;
745 int soft = strtol( arg, &next, 10 );
747 if ( next == arg || next[ 0 ] != '\0' ) {
756 /* FIXME: use "unlimited" instead; issue warning? */
759 limit->lms_t_soft = soft;
762 } else if ( strncasecmp( arg, "hard=", STRLENOF( "hard=" ) ) == 0 ) {
763 arg += STRLENOF( "hard=" );
764 if ( strcasecmp( arg, "soft" ) == 0 ) {
765 limit->lms_t_hard = 0;
767 } else if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
768 limit->lms_t_hard = -1;
772 int hard = strtol( arg, &next, 10 );
774 if ( next == arg || next[ 0 ] != '\0' ) {
783 /* FIXME: use "unlimited" instead */
787 /* FIXME: use "soft" instead */
790 limit->lms_t_hard = hard;
797 } else if ( arg[0] == '=' ) {
799 if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
800 limit->lms_t_soft = -1;
805 limit->lms_t_soft = strtol( arg, &next, 10 );
806 if ( next == arg || limit->lms_t_soft < -1 ) {
810 limit->lms_t_hard = 0;
816 } else if ( strncasecmp( arg, "size", STRLENOF( "size" ) ) == 0 ) {
817 arg += STRLENOF( "size" );
819 if ( arg[0] == '.' ) {
821 if ( strncasecmp( arg, "soft=", STRLENOF( "soft=" ) ) == 0 ) {
822 arg += STRLENOF( "soft=" );
823 if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
824 limit->lms_s_soft = -1;
828 int soft = strtol( arg, &next, 10 );
830 if ( next == arg || next[ 0 ] != '\0' ) {
839 /* FIXME: use "unlimited" instead */
842 limit->lms_s_soft = soft;
845 } else if ( strncasecmp( arg, "hard=", STRLENOF( "hard=" ) ) == 0 ) {
846 arg += STRLENOF( "hard=" );
847 if ( strcasecmp( arg, "soft" ) == 0 ) {
848 limit->lms_s_hard = 0;
850 } else if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
851 limit->lms_s_hard = -1;
855 int hard = strtol( arg, &next, 10 );
857 if ( next == arg || next[ 0 ] != '\0' ) {
866 /* FIXME: use "unlimited" instead */
870 /* FIXME: use "soft" instead */
873 limit->lms_s_hard = hard;
876 } else if ( strncasecmp( arg, "unchecked=", STRLENOF( "unchecked=" ) ) == 0 ) {
877 arg += STRLENOF( "unchecked=" );
878 if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
879 limit->lms_s_unchecked = -1;
881 } else if ( strcasecmp( arg, "disabled" ) == 0 ) {
882 limit->lms_s_unchecked = 0;
886 int unchecked = strtol( arg, &next, 10 );
888 if ( next == arg || next[ 0 ] != '\0' ) {
892 if ( unchecked < -1 ) {
896 if ( unchecked == -1 ) {
897 /* FIXME: use "unlimited" instead */
900 limit->lms_s_unchecked = unchecked;
903 } else if ( strncasecmp( arg, "pr=", STRLENOF( "pr=" ) ) == 0 ) {
904 arg += STRLENOF( "pr=" );
905 if ( strcasecmp( arg, "noEstimate" ) == 0 ) {
906 limit->lms_s_pr_hide = 1;
908 } else if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
909 limit->lms_s_pr = -1;
913 int pr = strtol( arg, &next, 10 );
915 if ( next == arg || next[ 0 ] != '\0' ) {
924 /* FIXME: use "unlimited" instead */
927 limit->lms_s_pr = pr;
930 } else if ( strncasecmp( arg, "prtotal=", STRLENOF( "prtotal=" ) ) == 0 ) {
931 arg += STRLENOF( "prtotal=" );
933 if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
934 limit->lms_s_pr_total = -1;
936 } else if ( strcasecmp( arg, "disabled" ) == 0 ) {
937 limit->lms_s_pr_total = -2;
939 } else if ( strcasecmp( arg, "hard" ) == 0 ) {
940 limit->lms_s_pr_total = 0;
946 total = strtol( arg, &next, 10 );
947 if ( next == arg || next[ 0 ] != '\0' ) {
956 /* FIXME: use "unlimited" instead */
960 /* FIXME: use "pr=disable" instead */
963 limit->lms_s_pr_total = total;
970 } else if ( arg[0] == '=' ) {
972 if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
973 limit->lms_s_soft = -1;
978 limit->lms_s_soft = strtol( arg, &next, 10 );
979 if ( next == arg || limit->lms_s_soft < -1 ) {
983 limit->lms_s_hard = 0;
995 limits_check( Operation *op, SlapReply *rs )
999 /* FIXME: should this be always true? */
1000 assert( op->o_tag == LDAP_REQ_SEARCH);
1002 /* protocol only allows 0..maxInt;
1004 * internal searches:
1005 * - may use SLAP_NO_LIMIT ( = -1 ) to indicate no limits;
1006 * - should use slimit = N and tlimit = SLAP_NO_LIMIT to
1007 * indicate searches that should return exactly N matches,
1008 * and handle errors thru a callback (see for instance
1009 * slap_sasl_match() and slap_sasl2dn())
1011 if ( op->ors_tlimit == SLAP_NO_LIMIT && op->ors_slimit == SLAP_NO_LIMIT ) {
1015 /* allow root to set no limit */
1016 if ( be_isroot( op ) ) {
1017 op->ors_limit = NULL;
1019 if ( op->ors_tlimit == 0 ) {
1020 op->ors_tlimit = SLAP_NO_LIMIT;
1023 if ( op->ors_slimit == 0 ) {
1024 op->ors_slimit = SLAP_NO_LIMIT;
1027 /* if not root, get appropriate limits */
1029 ( void ) limits_get( op, &op->o_ndn, &op->ors_limit );
1031 assert( op->ors_limit != NULL );
1033 /* if no limit is required, use soft limit */
1034 if ( op->ors_tlimit == 0 ) {
1035 op->ors_tlimit = op->ors_limit->lms_t_soft;
1037 /* limit required: check if legal */
1039 if ( op->ors_limit->lms_t_hard == 0 ) {
1040 if ( op->ors_limit->lms_t_soft > 0
1041 && ( op->ors_tlimit > op->ors_limit->lms_t_soft ) ) {
1042 op->ors_tlimit = op->ors_limit->lms_t_soft;
1045 } else if ( op->ors_limit->lms_t_hard > 0 ) {
1046 #ifdef ABOVE_HARD_LIMIT_IS_ERROR
1047 if ( op->ors_tlimit == SLAP_MAX_LIMIT ) {
1048 op->ors_tlimit = op->ors_limit->lms_t_hard;
1050 } else if ( op->ors_tlimit > op->ors_limit->lms_t_hard ) {
1051 /* error if exceeding hard limit */
1052 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1053 send_ldap_result( op, rs );
1054 rs->sr_err = LDAP_SUCCESS;
1057 #else /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1058 if ( op->ors_tlimit > op->ors_limit->lms_t_hard ) {
1059 op->ors_tlimit = op->ors_limit->lms_t_hard;
1061 #endif /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1065 /* else leave as is */
1067 /* don't even get to backend if candidate check is disabled */
1068 if ( op->ors_limit->lms_s_unchecked == 0 ) {
1069 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1070 send_ldap_result( op, rs );
1071 rs->sr_err = LDAP_SUCCESS;
1075 /* if paged results is requested */
1076 if ( get_pagedresults( op ) > SLAP_NO_CONTROL ) {
1080 /* paged results is not allowed */
1081 if ( op->ors_limit->lms_s_pr_total == -2 ) {
1082 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1083 rs->sr_text = "pagedResults control not allowed";
1084 send_ldap_result( op, rs );
1085 rs->sr_err = LDAP_SUCCESS;
1090 if ( op->ors_limit->lms_s_pr > 0 && op->o_pagedresults_size > op->ors_limit->lms_s_pr ) {
1091 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1092 rs->sr_text = "illegal pagedResults page size";
1093 send_ldap_result( op, rs );
1094 rs->sr_err = LDAP_SUCCESS;
1099 if ( op->ors_limit->lms_s_pr_total == 0 ) {
1100 if ( op->ors_limit->lms_s_hard == 0 ) {
1101 pr_total = op->ors_limit->lms_s_soft;
1103 pr_total = op->ors_limit->lms_s_hard;
1106 pr_total = op->ors_limit->lms_s_pr_total;
1109 if ( pr_total == -1 ) {
1110 if ( op->ors_slimit == 0 || op->ors_slimit == SLAP_MAX_LIMIT ) {
1114 slimit = op->ors_slimit - op->o_pagedresults_state.ps_count;
1117 #ifdef ABOVE_HARD_LIMIT_IS_ERROR
1118 } else if ( pr_total > 0 && op->ors_slimit != SLAP_MAX_LIMIT
1119 && ( op->ors_slimit == SLAP_NO_LIMIT || op->ors_slimit > pr_total ) )
1121 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1122 send_ldap_result( op, rs );
1123 rs->sr_err = LDAP_SUCCESS;
1125 #endif /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1128 /* if no limit is required, use soft limit */
1132 /* first round of pagedResults: set count to any appropriate limit */
1134 /* if the limit is set, check that it does not violate any server-side limit */
1135 #ifdef ABOVE_HARD_LIMIT_IS_ERROR
1136 if ( op->ors_slimit == SLAP_MAX_LIMIT ) {
1137 slimit2 = op->ors_slimit = pr_total;
1138 #else /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1139 if ( op->ors_slimit == SLAP_MAX_LIMIT || op->ors_slimit > pr_total ) {
1140 slimit2 = op->ors_slimit = pr_total;
1141 #endif /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1143 } else if ( op->ors_slimit == 0 ) {
1147 slimit2 = op->ors_slimit;
1150 total = slimit2 - op->o_pagedresults_state.ps_count;
1153 if ( op->ors_limit->lms_s_pr > 0 ) {
1154 /* use the smallest limit set by total/per page */
1155 if ( total < op->ors_limit->lms_s_pr ) {
1159 /* use the perpage limit if any
1160 * NOTE: + 1 because the given value must be legal */
1161 slimit = op->ors_limit->lms_s_pr + 1;
1165 /* use the total limit if any */
1169 } else if ( op->ors_limit->lms_s_pr > 0 ) {
1170 /* use the perpage limit if any
1171 * NOTE: + 1 because the given value must be legal */
1172 slimit = op->ors_limit->lms_s_pr + 1;
1175 /* use the standard hard/soft limit if any */
1176 slimit = op->ors_limit->lms_s_hard;
1180 /* if got any limit, use it */
1181 if ( slimit != -2 ) {
1182 if ( op->ors_slimit == 0 ) {
1183 op->ors_slimit = slimit;
1185 } else if ( slimit > 0 ) {
1186 if ( op->ors_slimit - op->o_pagedresults_state.ps_count > slimit ) {
1187 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1188 send_ldap_result( op, rs );
1189 rs->sr_err = LDAP_SUCCESS;
1192 op->ors_slimit = slimit;
1196 /* use the standard hard/soft limit if any */
1197 op->ors_slimit = pr_total;
1200 /* no limit requested: use soft, whatever it is */
1201 } else if ( op->ors_slimit == 0 ) {
1202 op->ors_slimit = op->ors_limit->lms_s_soft;
1204 /* limit requested: check if legal */
1206 /* hard limit as soft (traditional behavior) */
1207 if ( op->ors_limit->lms_s_hard == 0 ) {
1208 if ( op->ors_limit->lms_s_soft > 0
1209 && op->ors_slimit > op->ors_limit->lms_s_soft ) {
1210 op->ors_slimit = op->ors_limit->lms_s_soft;
1213 /* explicit hard limit: error if violated */
1214 } else if ( op->ors_limit->lms_s_hard > 0 ) {
1215 #ifdef ABOVE_HARD_LIMIT_IS_ERROR
1216 if ( op->ors_slimit == SLAP_MAX_LIMIT ) {
1217 op->ors_slimit = op->ors_limit->lms_s_hard;
1219 } else if ( op->ors_slimit > op->ors_limit->lms_s_hard ) {
1220 /* if limit exceeds hard, error */
1221 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1222 send_ldap_result( op, rs );
1223 rs->sr_err = LDAP_SUCCESS;
1226 #else /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1227 if ( op->ors_slimit > op->ors_limit->lms_s_hard ) {
1228 op->ors_slimit = op->ors_limit->lms_s_hard;
1230 #endif /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1234 /* else leave as is */