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-2005 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>
27 /* define to get an error if requesting limit higher than hard */
28 #undef ABOVE_HARD_LIMIT_IS_ERROR
31 limits2str( unsigned i )
34 case SLAP_LIMITS_UNDEFINED:
37 case SLAP_LIMITS_EXACT:
43 case SLAP_LIMITS_SUBTREE:
46 case SLAP_LIMITS_CHILDREN:
49 case SLAP_LIMITS_REGEX:
52 case SLAP_LIMITS_ANONYMOUS:
55 case SLAP_LIMITS_USERS:
70 struct slap_limits_set **limit
73 struct slap_limits **lm;
78 Debug( LDAP_DEBUG_TRACE, "==> limits_get: %s dn=\"%s\"\n",
80 BER_BVISNULL( ndn ) ? "[anonymous]" : ndn->bv_val, 0 );
84 *limit = &op->o_bd->be_def_limit;
86 if ( op->o_bd->be_limits == NULL ) {
90 for ( lm = op->o_bd->be_limits; lm[0] != NULL; lm++ ) {
91 unsigned style = lm[0]->lm_flags & SLAP_LIMITS_MASK;
92 unsigned type = lm[0]->lm_flags & SLAP_LIMITS_TYPE_MASK;
95 case SLAP_LIMITS_EXACT:
96 if ( ndn->bv_len == 0 ) {
100 if ( type == SLAP_LIMITS_TYPE_GROUP ) {
103 rc = backend_group( op, NULL,
106 lm[0]->lm_group_ad );
108 *limit = &lm[0]->lm_limits;
109 Debug( LDAP_DEBUG_TRACE, "<== limits_get: type=GROUP match=EXACT "
110 "dn=\"%s\" oc=\"%s\" ad=\"%s\"\n",
111 lm[0]->lm_pat.bv_val,
112 lm[0]->lm_group_oc->soc_cname.bv_val,
113 lm[0]->lm_group_ad->ad_cname.bv_val );
119 if ( dn_match( &lm[0]->lm_pat, ndn ) ) {
120 *limit = &lm[0]->lm_limits;
121 Debug( LDAP_DEBUG_TRACE, "<== limits_get: type=DN match=EXACT dn=\"%s\"\n",
122 lm[0]->lm_pat.bv_val, 0, 0 );
128 case SLAP_LIMITS_ONE:
129 case SLAP_LIMITS_SUBTREE:
130 case SLAP_LIMITS_CHILDREN: {
133 if ( ndn->bv_len == 0 ) {
137 /* ndn shorter than dn_pat */
138 if ( ndn->bv_len < lm[0]->lm_pat.bv_len ) {
141 d = ndn->bv_len - lm[0]->lm_pat.bv_len;
143 /* allow exact match for SUBTREE only */
145 if ( style != SLAP_LIMITS_SUBTREE ) {
149 /* check for unescaped rdn separator */
150 if ( !DN_SEPARATOR( ndn->bv_val[d-1] ) ) {
155 /* in case of (sub)match ... */
156 if ( lm[0]->lm_pat.bv_len == ( ndn->bv_len - d )
157 && strcmp( lm[0]->lm_pat.bv_val,
158 &ndn->bv_val[d] ) == 0 )
160 /* check for exactly one rdn in case of ONE */
161 if ( style == SLAP_LIMITS_ONE ) {
163 * if ndn is more that one rdn
164 * below dn_pat, continue
166 if ( (size_t) dn_rdnlen( NULL, ndn )
173 *limit = &lm[0]->lm_limits;
174 Debug( LDAP_DEBUG_TRACE, "<== limits_get: type=DN match=%s dn=\"%s\"\n",
175 limits2str( style ), lm[0]->lm_pat.bv_val, 0 );
182 case SLAP_LIMITS_REGEX:
183 if ( ndn->bv_len == 0 ) {
186 if ( regexec( &lm[0]->lm_regex, ndn->bv_val,
189 *limit = &lm[0]->lm_limits;
190 Debug( LDAP_DEBUG_TRACE, "<== limits_get: type=DN match=%s dn=\"%s\"\n",
191 limits2str( style ), lm[0]->lm_pat.bv_val, 0 );
196 case SLAP_LIMITS_ANONYMOUS:
197 if ( ndn->bv_len == 0 ) {
198 Debug( LDAP_DEBUG_TRACE, "<== limits_get: type=DN match=%s\n",
199 limits2str( style ), 0, 0 );
200 *limit = &lm[0]->lm_limits;
205 case SLAP_LIMITS_USERS:
206 if ( ndn->bv_len != 0 ) {
207 *limit = &lm[0]->lm_limits;
208 Debug( LDAP_DEBUG_TRACE, "<== limits_get: type=DN match=%s\n",
209 limits2str( style ), 0, 0 );
214 case SLAP_LIMITS_ANY:
215 *limit = &lm[0]->lm_limits;
219 assert( 0 ); /* unreachable */
232 ObjectClass *group_oc,
233 AttributeDescription *group_ad,
234 struct slap_limits_set *limit
238 struct slap_limits *lm;
239 unsigned type, style;
244 type = flags & SLAP_LIMITS_TYPE_MASK;
245 style = flags & SLAP_LIMITS_MASK;
248 case SLAP_LIMITS_ANONYMOUS:
249 case SLAP_LIMITS_USERS:
250 case SLAP_LIMITS_ANY:
251 for ( i = 0; be->be_limits && be->be_limits[ i ]; i++ ) {
252 if ( be->be_limits[ i ]->lm_flags == style ) {
260 lm = ( struct slap_limits * )ch_calloc( sizeof( struct slap_limits ), 1 );
263 case SLAP_LIMITS_UNDEFINED:
264 style = SLAP_LIMITS_EXACT;
265 /* continue to next cases */
266 case SLAP_LIMITS_EXACT:
267 case SLAP_LIMITS_ONE:
268 case SLAP_LIMITS_SUBTREE:
269 case SLAP_LIMITS_CHILDREN:
270 lm->lm_flags = style | type;
274 bv.bv_val = (char *) pattern;
275 bv.bv_len = strlen( pattern );
277 rc = dnNormalize( 0, NULL, NULL, &bv, &lm->lm_pat, NULL );
278 if ( rc != LDAP_SUCCESS ) {
285 case SLAP_LIMITS_REGEX:
286 lm->lm_flags = style | type;
287 ber_str2bv( pattern, 0, 1, &lm->lm_pat );
288 if ( regcomp( &lm->lm_regex, lm->lm_pat.bv_val,
289 REG_EXTENDED | REG_ICASE ) ) {
290 free( lm->lm_pat.bv_val );
296 case SLAP_LIMITS_ANONYMOUS:
297 case SLAP_LIMITS_USERS:
298 case SLAP_LIMITS_ANY:
299 lm->lm_flags = style | type;
300 lm->lm_pat.bv_val = NULL;
301 lm->lm_pat.bv_len = 0;
306 case SLAP_LIMITS_TYPE_GROUP:
309 lm->lm_group_oc = group_oc;
310 lm->lm_group_ad = group_ad;
314 lm->lm_limits = *limit;
317 if ( be->be_limits != NULL ) {
318 for ( ; be->be_limits[i]; i++ );
321 be->be_limits = ( struct slap_limits ** )ch_realloc( be->be_limits,
322 sizeof( struct slap_limits * ) * ( i + 2 ) );
323 be->be_limits[i] = lm;
324 be->be_limits[i+1] = NULL;
338 int flags = SLAP_LIMITS_UNDEFINED;
340 struct slap_limits_set limit;
342 ObjectClass *group_oc = NULL;
343 AttributeDescription *group_ad = NULL;
348 Debug( LDAP_DEBUG_ANY,
349 "%s : line %d: missing arg(s) in "
350 "\"limits <pattern> <limits>\" line.\n%s",
355 limit = be->be_def_limit;
360 * "limits" <pattern> <limit> [ ... ]
367 * [ "dn" [ "." { "exact" | "base" | "onelevel" | "subtree" | children"
368 * | "regex" | "anonymous" } ] "=" ] <dn pattern>
371 * "exact" and "base" are the same (exact match);
372 * "onelevel" means exactly one rdn below, NOT including pattern
373 * "subtree" means any rdn below, including pattern
374 * "children" means any rdn below, NOT including pattern
376 * "anonymous" may be deprecated in favour
377 * of the pattern = "anonymous" form
379 * "group[/objectClass[/attributeType]]" "=" "<dn pattern>"
383 * "time" [ "." { "soft" | "hard" } ] "=" <integer>
385 * "size" [ "." { "soft" | "hard" | "unchecked" } ] "=" <integer>
389 if ( strcmp( pattern, "*" ) == 0) {
390 flags = SLAP_LIMITS_ANY;
392 } else if ( strcasecmp( pattern, "anonymous" ) == 0 ) {
393 flags = SLAP_LIMITS_ANONYMOUS;
395 } else if ( strcasecmp( pattern, "users" ) == 0 ) {
396 flags = SLAP_LIMITS_USERS;
398 } else if ( strncasecmp( pattern, "dn", STRLENOF( "dn" ) ) == 0 ) {
399 pattern += STRLENOF( "dn" );
400 if ( pattern[0] == '.' ) {
402 if ( strncasecmp( pattern, "exact", STRLENOF( "exact" )) == 0 ) {
403 flags = SLAP_LIMITS_EXACT;
404 pattern += STRLENOF( "exact" );
406 } else if ( strncasecmp( pattern, "base", STRLENOF( "base" ) ) == 0 ) {
407 flags = SLAP_LIMITS_BASE;
408 pattern += STRLENOF( "base" );
410 } else if ( strncasecmp( pattern, "one", STRLENOF( "one" ) ) == 0 ) {
411 flags = SLAP_LIMITS_ONE;
412 pattern += STRLENOF( "one" );
413 if ( strncasecmp( pattern, "level", STRLENOF( "level" ) ) == 0 ) {
414 pattern += STRLENOF( "level" );
417 Debug( LDAP_DEBUG_ANY,
418 "%s : line %d: deprecated \"one\" style "
419 "\"limits <pattern> <limits>\" line; "
420 "use \"onelevel\" instead.\n", fname, lineno, 0 );
423 } else if ( strncasecmp( pattern, "sub", STRLENOF( "sub" ) ) == 0 ) {
424 flags = SLAP_LIMITS_SUBTREE;
425 pattern += STRLENOF( "sub" );
426 if ( strncasecmp( pattern, "tree", STRLENOF( "tree" ) ) == 0 ) {
427 pattern += STRLENOF( "tree" );
430 Debug( LDAP_DEBUG_ANY,
431 "%s : line %d: deprecated \"sub\" style "
432 "\"limits <pattern> <limits>\" line; "
433 "use \"subtree\" instead.\n", fname, lineno, 0 );
436 } else if ( strncasecmp( pattern, "children", STRLENOF( "children" ) ) == 0 ) {
437 flags = SLAP_LIMITS_CHILDREN;
438 pattern += STRLENOF( "children" );
440 } else if ( strncasecmp( pattern, "regex", STRLENOF( "regex" ) ) == 0 ) {
441 flags = SLAP_LIMITS_REGEX;
442 pattern += STRLENOF( "regex" );
445 * this could be deprecated in favour
446 * of the pattern = "anonymous" form
448 } else if ( strncasecmp( pattern, "anonymous", STRLENOF( "anonymous" ) ) == 0 ) {
449 flags = SLAP_LIMITS_ANONYMOUS;
454 /* pre-check the data */
456 case SLAP_LIMITS_ANONYMOUS:
457 case SLAP_LIMITS_USERS:
459 /* no need for pattern */
464 if ( pattern[0] != '=' ) {
465 Debug( LDAP_DEBUG_ANY,
466 "%s : line %d: missing '=' in "
467 "\"dn[.{exact|base|onelevel|subtree"
468 "|children|regex|anonymous}]"
470 "\"limits <pattern> <limits>\" "
476 /* skip '=' (required) */
479 /* trim obvious cases */
480 if ( strcmp( pattern, "*" ) == 0 ) {
481 flags = SLAP_LIMITS_ANY;
484 } else if ( flags == SLAP_LIMITS_REGEX
485 && strcmp( pattern, ".*" ) == 0 ) {
486 flags = SLAP_LIMITS_ANY;
491 } else if (strncasecmp( pattern, "group", STRLENOF( "group" ) ) == 0 ) {
492 pattern += STRLENOF( "group" );
494 if ( pattern[0] == '/' ) {
495 struct berval oc, ad;
497 oc.bv_val = pattern + 1;
498 pattern = strchr( pattern, '=' );
499 if ( pattern == NULL ) {
503 ad.bv_val = strchr( oc.bv_val, '/' );
504 if ( ad.bv_val != NULL ) {
505 const char *text = NULL;
508 oc.bv_len = ad.bv_val - oc.bv_val;
511 ad.bv_len = pattern - ad.bv_val;
512 rc = slap_bv2ad( &ad, &group_ad, &text );
513 if ( rc != LDAP_SUCCESS ) {
518 oc.bv_len = pattern - oc.bv_val;
521 group_oc = oc_bvfind( &oc );
522 if ( group_oc == NULL ) {
527 if ( group_oc == NULL ) {
528 group_oc = oc_find( SLAPD_GROUP_CLASS );
529 if ( group_oc == NULL ) {
535 if ( group_ad == NULL ) {
536 const char *text = NULL;
539 rc = slap_str2ad( SLAPD_GROUP_ATTR, &group_ad, &text );
541 if ( rc != LDAP_SUCCESS ) {
547 flags = SLAP_LIMITS_TYPE_GROUP | SLAP_LIMITS_EXACT;
549 if ( pattern[0] != '=' ) {
550 Debug( LDAP_DEBUG_ANY,
551 "%s : line %d: missing '=' in "
552 "\"group[/objectClass[/attributeType]]"
554 "\"limits <pattern> <limits>\" line.\n",
559 /* skip '=' (required) */
564 for ( i = 2; i < argc; i++ ) {
565 if ( limits_parse_one( argv[i], &limit ) ) {
567 Debug( LDAP_DEBUG_ANY,
568 "%s : line %d: unknown limit values \"%s\" in "
569 "\"limits <pattern> <limits>\" line.\n",
570 fname, lineno, argv[i] );
579 * FIXME: add warnings?
581 if ( limit.lms_t_hard > 0 &&
582 ( limit.lms_t_hard < limit.lms_t_soft
583 || limit.lms_t_soft == -1 ) ) {
584 limit.lms_t_hard = limit.lms_t_soft;
587 if ( limit.lms_s_hard > 0 &&
588 ( limit.lms_s_hard < limit.lms_s_soft
589 || limit.lms_s_soft == -1 ) ) {
590 limit.lms_s_hard = limit.lms_s_soft;
599 * > 0 => limit (in seconds)
604 * > 0 => limit (in entries)
607 * -2 => disable the control
610 * > 0 => limit (in entries)
615 * > 0 => limit size (in entries)
617 if ( limit.lms_s_pr_total > 0 &&
618 limit.lms_s_pr > limit.lms_s_pr_total ) {
619 limit.lms_s_pr = limit.lms_s_pr_total;
622 rc = limits_add( be, flags, pattern, group_oc, group_ad, &limit );
625 Debug( LDAP_DEBUG_ANY,
626 "%s : line %d: unable to add limit in "
627 "\"limits <pattern> <limits>\" line.\n",
637 struct slap_limits_set *limit
643 if ( strncasecmp( arg, "time", STRLENOF( "time" ) ) == 0 ) {
644 arg += STRLENOF( "time" );
646 if ( arg[0] == '.' ) {
648 if ( strncasecmp( arg, "soft=", STRLENOF( "soft=" ) ) == 0 ) {
649 arg += STRLENOF( "soft=" );
650 if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
651 limit->lms_t_soft = -1;
655 int soft = strtol( arg, &next, 10 );
657 if ( next == arg || next[ 0 ] != '\0' ) {
666 /* FIXME: use "unlimited" instead; issue warning? */
669 limit->lms_t_soft = soft;
672 } else if ( strncasecmp( arg, "hard=", STRLENOF( "hard=" ) ) == 0 ) {
673 arg += STRLENOF( "hard=" );
674 if ( strcasecmp( arg, "soft" ) == 0 ) {
675 limit->lms_t_hard = 0;
677 } else if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
678 limit->lms_t_hard = -1;
682 int hard = strtol( arg, &next, 10 );
684 if ( next == arg || next[ 0 ] != '\0' ) {
693 /* FIXME: use "unlimited" instead */
697 /* FIXME: use "soft" instead */
700 limit->lms_t_hard = hard;
707 } else if ( arg[0] == '=' ) {
709 if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
710 limit->lms_t_soft = -1;
715 limit->lms_t_soft = strtol( arg, &next, 10 );
716 if ( next == arg || limit->lms_t_soft < -1 ) {
720 limit->lms_t_hard = 0;
726 } else if ( strncasecmp( arg, "size", STRLENOF( "size" ) ) == 0 ) {
727 arg += STRLENOF( "size" );
729 if ( arg[0] == '.' ) {
731 if ( strncasecmp( arg, "soft=", STRLENOF( "soft=" ) ) == 0 ) {
732 arg += STRLENOF( "soft=" );
733 if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
734 limit->lms_s_soft = -1;
738 int soft = strtol( arg, &next, 10 );
740 if ( next == arg || next[ 0 ] != '\0' ) {
749 /* FIXME: use "unlimited" instead */
752 limit->lms_s_soft = soft;
755 } else if ( strncasecmp( arg, "hard=", STRLENOF( "hard=" ) ) == 0 ) {
756 arg += STRLENOF( "hard=" );
757 if ( strcasecmp( arg, "soft" ) == 0 ) {
758 limit->lms_s_hard = 0;
760 } else if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
761 limit->lms_s_hard = -1;
765 int hard = strtol( arg, &next, 10 );
767 if ( next == arg || next[ 0 ] != '\0' ) {
776 /* FIXME: use "unlimited" instead */
780 /* FIXME: use "soft" instead */
783 limit->lms_s_hard = hard;
786 } else if ( strncasecmp( arg, "unchecked=", STRLENOF( "unchecked=" ) ) == 0 ) {
787 arg += STRLENOF( "unchecked=" );
788 if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
789 limit->lms_s_unchecked = -1;
791 } else if ( strcasecmp( arg, "disabled" ) == 0 ) {
792 limit->lms_s_unchecked = 0;
796 int unchecked = strtol( arg, &next, 10 );
798 if ( next == arg || next[ 0 ] != '\0' ) {
802 if ( unchecked < -1 ) {
806 if ( unchecked == -1 ) {
807 /* FIXME: use "unlimited" instead */
810 limit->lms_s_unchecked = unchecked;
813 } else if ( strncasecmp( arg, "pr=", STRLENOF( "pr=" ) ) == 0 ) {
814 arg += STRLENOF( "pr=" );
815 if ( strcasecmp( arg, "noEstimate" ) == 0 ) {
816 limit->lms_s_pr_hide = 1;
818 } else if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
819 limit->lms_s_pr = -1;
823 int pr = strtol( arg, &next, 10 );
825 if ( next == arg || next[ 0 ] != '\0' ) {
834 /* FIXME: use "unlimited" instead */
837 limit->lms_s_pr = pr;
840 } else if ( strncasecmp( arg, "prtotal=", STRLENOF( "prtotal=" ) ) == 0 ) {
841 arg += STRLENOF( "prtotal=" );
843 if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
844 limit->lms_s_pr_total = -1;
846 } else if ( strcasecmp( arg, "disabled" ) == 0 ) {
847 limit->lms_s_pr_total = -2;
849 } else if ( strcasecmp( arg, "hard" ) == 0 ) {
850 limit->lms_s_pr_total = 0;
856 total = strtol( arg, &next, 10 );
857 if ( next == arg || next[ 0 ] != '\0' ) {
866 /* FIXME: use "unlimited" instead */
870 /* FIXME: use "pr=disable" instead */
873 limit->lms_s_pr_total = total;
880 } else if ( arg[0] == '=' ) {
882 if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
883 limit->lms_s_soft = -1;
888 limit->lms_s_soft = strtol( arg, &next, 10 );
889 if ( next == arg || limit->lms_s_soft < -1 ) {
893 limit->lms_s_hard = 0;
903 static const char *lmpats[] = {
915 /* Caller must provide an adequately sized buffer in bv */
917 limits_unparse( struct slap_limits *lim, struct berval *bv )
923 if ( !bv || !bv->bv_val ) return;
927 if (( lim->lm_flags & SLAP_LIMITS_TYPE_MASK ) == SLAP_LIMITS_TYPE_GROUP ) {
928 ptr = lutil_strcopy( ptr, "group/" );
929 ptr = lutil_strcopy( ptr, lim->lm_group_oc->soc_cname.bv_val );
931 ptr = lutil_strcopy( ptr, lim->lm_group_ad->ad_cname.bv_val );
933 lm = lim->lm_flags & SLAP_LIMITS_MASK;
935 case SLAP_LIMITS_ANONYMOUS:
936 case SLAP_LIMITS_USERS:
937 case SLAP_LIMITS_ANY:
938 ptr = lutil_strcopy( ptr, lmpats[lm] );
940 case SLAP_LIMITS_UNDEFINED:
941 case SLAP_LIMITS_EXACT:
942 case SLAP_LIMITS_ONE:
943 case SLAP_LIMITS_SUBTREE:
944 case SLAP_LIMITS_CHILDREN:
945 case SLAP_LIMITS_REGEX:
946 ptr = lutil_strcopy( ptr, "dn." );
947 ptr = lutil_strcopy( ptr, lmpats[lm] );
950 ptr = lutil_strcopy( ptr, lim->lm_pat.bv_val );
956 bv->bv_len = ptr - bv->bv_val;
959 limits_unparse_one( &lim->lm_limits, SLAP_LIMIT_SIZE|SLAP_LIMIT_TIME, &btmp );
960 bv->bv_len += btmp.bv_len;
963 /* Caller must provide an adequately sized buffer in bv */
965 limits_unparse_one( struct slap_limits_set *lim, int which, struct berval *bv )
969 if ( !bv || !bv->bv_val ) return;
973 if ( which & SLAP_LIMIT_SIZE ) {
974 if ( lim->lms_s_soft != SLAPD_DEFAULT_SIZELIMIT ) {
976 /* If there's also a hard limit, fully qualify this one */
977 if ( lim->lms_s_hard )
978 ptr = lutil_strcopy( ptr, " size.soft=" );
980 /* If doing both size & time, qualify this */
981 else if ( which & SLAP_LIMIT_TIME )
982 ptr = lutil_strcopy( ptr, " size=" );
984 /* Otherwise if same as global limit, drop it */
985 else if ( lim != &frontendDB->be_def_limit &&
986 lim->lms_s_soft == frontendDB->be_def_limit.lms_s_soft )
988 if ( lim->lms_s_soft == -1 )
989 ptr = lutil_strcopy( ptr, "unlimited" );
991 ptr += sprintf( ptr, "%d", lim->lms_s_soft );
994 if ( lim->lms_s_hard ) {
995 ptr = lutil_strcopy( ptr, " size.hard=" );
996 if ( lim->lms_s_hard == -1 )
997 ptr = lutil_strcopy( ptr, "unlimited" );
999 ptr += sprintf( ptr, "%d", lim->lms_s_hard );
1002 if ( lim->lms_s_unchecked != -1 ) {
1003 ptr = lutil_strcopy( ptr, " size.unchecked=" );
1004 if ( lim->lms_s_unchecked == 0 )
1005 ptr = lutil_strcopy( ptr, "disabled" );
1007 ptr += sprintf( ptr, "%d", lim->lms_s_unchecked );
1010 if ( lim->lms_s_pr_hide ) {
1011 ptr = lutil_strcopy( ptr, " size.pr=noEstimate " );
1013 if ( lim->lms_s_pr ) {
1014 ptr = lutil_strcopy( ptr, " size.pr=" );
1015 if ( lim->lms_s_pr == -1 )
1016 ptr = lutil_strcopy( ptr, "unlimited" );
1018 ptr += sprintf( ptr, "%d", lim->lms_s_pr );
1021 if ( lim->lms_s_pr_total ) {
1022 ptr = lutil_strcopy( ptr, " size.prtotal=" );
1023 if ( lim->lms_s_pr_total == -1 )
1024 ptr = lutil_strcopy( ptr, "unlimited" );
1025 else if ( lim->lms_s_pr_total == -2 )
1026 ptr = lutil_strcopy( ptr, "disabled" );
1028 ptr += sprintf( ptr, "%d", lim->lms_s_pr_total );
1032 if ( which & SLAP_LIMIT_TIME ) {
1033 if ( lim->lms_t_soft != SLAPD_DEFAULT_TIMELIMIT ) {
1035 /* If there's also a hard limit, fully qualify this one */
1036 if ( lim->lms_t_hard )
1037 ptr = lutil_strcopy( ptr, " time.soft=" );
1039 /* If doing both size & time, qualify this */
1040 else if ( which & SLAP_LIMIT_SIZE )
1041 ptr = lutil_strcopy( ptr, " time=" );
1043 /* Otherwise, if same as global limit, drop it */
1044 else if ( lim != &frontendDB->be_def_limit &&
1045 lim->lms_t_soft == frontendDB->be_def_limit.lms_t_soft )
1048 if ( lim->lms_t_soft == -1 )
1049 ptr = lutil_strcopy( ptr, "unlimited" );
1051 ptr += sprintf( ptr, "%d", lim->lms_t_soft );
1054 if ( lim->lms_t_hard ) {
1055 ptr = lutil_strcopy( ptr, " time.hard=" );
1056 if ( lim->lms_t_hard == -1 )
1057 ptr = lutil_strcopy( ptr, "unlimited" );
1059 ptr += sprintf( ptr, "%d", lim->lms_t_hard );
1063 if ( ptr != bv->bv_val ) {
1066 bv->bv_len = ptr - bv->bv_val;
1071 limits_check( Operation *op, SlapReply *rs )
1075 /* FIXME: should this be always true? */
1076 assert( op->o_tag == LDAP_REQ_SEARCH);
1078 /* protocol only allows 0..maxInt;
1080 * internal searches:
1081 * - may use SLAP_NO_LIMIT ( = -1 ) to indicate no limits;
1082 * - should use slimit = N and tlimit = SLAP_NO_LIMIT to
1083 * indicate searches that should return exactly N matches,
1084 * and handle errors thru a callback (see for instance
1085 * slap_sasl_match() and slap_sasl2dn())
1087 if ( op->ors_tlimit == SLAP_NO_LIMIT && op->ors_slimit == SLAP_NO_LIMIT ) {
1091 /* allow root to set no limit */
1092 if ( be_isroot( op ) ) {
1093 op->ors_limit = NULL;
1095 if ( op->ors_tlimit == 0 ) {
1096 op->ors_tlimit = SLAP_NO_LIMIT;
1099 if ( op->ors_slimit == 0 ) {
1100 op->ors_slimit = SLAP_NO_LIMIT;
1103 /* if not root, get appropriate limits */
1105 ( void ) limits_get( op, &op->o_ndn, &op->ors_limit );
1107 assert( op->ors_limit != NULL );
1109 /* if no limit is required, use soft limit */
1110 if ( op->ors_tlimit == 0 ) {
1111 op->ors_tlimit = op->ors_limit->lms_t_soft;
1113 /* limit required: check if legal */
1115 if ( op->ors_limit->lms_t_hard == 0 ) {
1116 if ( op->ors_limit->lms_t_soft > 0
1117 && ( op->ors_tlimit > op->ors_limit->lms_t_soft ) ) {
1118 op->ors_tlimit = op->ors_limit->lms_t_soft;
1121 } else if ( op->ors_limit->lms_t_hard > 0 ) {
1122 #ifdef ABOVE_HARD_LIMIT_IS_ERROR
1123 if ( op->ors_tlimit == SLAP_MAX_LIMIT ) {
1124 op->ors_tlimit = op->ors_limit->lms_t_hard;
1126 } else if ( op->ors_tlimit > op->ors_limit->lms_t_hard ) {
1127 /* error if exceeding hard limit */
1128 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1129 send_ldap_result( op, rs );
1130 rs->sr_err = LDAP_SUCCESS;
1133 #else /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1134 if ( op->ors_tlimit > op->ors_limit->lms_t_hard ) {
1135 op->ors_tlimit = op->ors_limit->lms_t_hard;
1137 #endif /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1141 /* else leave as is */
1143 /* don't even get to backend if candidate check is disabled */
1144 if ( op->ors_limit->lms_s_unchecked == 0 ) {
1145 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1146 send_ldap_result( op, rs );
1147 rs->sr_err = LDAP_SUCCESS;
1151 /* if paged results is requested */
1152 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
1155 PagedResultsState *ps = op->o_pagedresults_state;
1157 /* paged results is not allowed */
1158 if ( op->ors_limit->lms_s_pr_total == -2 ) {
1159 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1160 rs->sr_text = "pagedResults control not allowed";
1161 send_ldap_result( op, rs );
1162 rs->sr_err = LDAP_SUCCESS;
1167 if ( op->ors_limit->lms_s_pr > 0 && ps->ps_size > op->ors_limit->lms_s_pr ) {
1168 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1169 rs->sr_text = "illegal pagedResults page size";
1170 send_ldap_result( op, rs );
1171 rs->sr_err = LDAP_SUCCESS;
1176 if ( op->ors_limit->lms_s_pr_total == 0 ) {
1177 if ( op->ors_limit->lms_s_hard == 0 ) {
1178 pr_total = op->ors_limit->lms_s_soft;
1180 pr_total = op->ors_limit->lms_s_hard;
1183 pr_total = op->ors_limit->lms_s_pr_total;
1186 if ( pr_total == -1 ) {
1187 if ( op->ors_slimit == 0 || op->ors_slimit == SLAP_MAX_LIMIT ) {
1191 slimit = op->ors_slimit - ps->ps_count;
1194 #ifdef ABOVE_HARD_LIMIT_IS_ERROR
1195 } else if ( pr_total > 0 && op->ors_slimit != SLAP_MAX_LIMIT
1196 && ( op->ors_slimit == SLAP_NO_LIMIT || op->ors_slimit > pr_total ) )
1198 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1199 send_ldap_result( op, rs );
1200 rs->sr_err = LDAP_SUCCESS;
1202 #endif /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1205 /* if no limit is required, use soft limit */
1209 /* first round of pagedResults: set count to any appropriate limit */
1211 /* if the limit is set, check that it does not violate any server-side limit */
1212 #ifdef ABOVE_HARD_LIMIT_IS_ERROR
1213 if ( op->ors_slimit == SLAP_MAX_LIMIT ) {
1214 slimit2 = op->ors_slimit = pr_total;
1215 #else /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1216 if ( op->ors_slimit == SLAP_MAX_LIMIT || op->ors_slimit > pr_total ) {
1217 slimit2 = op->ors_slimit = pr_total;
1218 #endif /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1220 } else if ( op->ors_slimit == 0 ) {
1224 slimit2 = op->ors_slimit;
1227 total = slimit2 - ps->ps_count;
1230 if ( op->ors_limit->lms_s_pr > 0 ) {
1231 /* use the smallest limit set by total/per page */
1232 if ( total < op->ors_limit->lms_s_pr ) {
1236 /* use the perpage limit if any
1237 * NOTE: + 1 because the given value must be legal */
1238 slimit = op->ors_limit->lms_s_pr + 1;
1242 /* use the total limit if any */
1246 } else if ( op->ors_limit->lms_s_pr > 0 ) {
1247 /* use the perpage limit if any
1248 * NOTE: + 1 because the given value must be legal */
1249 slimit = op->ors_limit->lms_s_pr + 1;
1252 /* use the standard hard/soft limit if any */
1253 slimit = op->ors_limit->lms_s_hard;
1257 /* if got any limit, use it */
1258 if ( slimit != -2 ) {
1259 if ( op->ors_slimit == 0 ) {
1260 op->ors_slimit = slimit;
1262 } else if ( slimit > 0 ) {
1263 if ( op->ors_slimit - ps->ps_count > slimit ) {
1264 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1265 send_ldap_result( op, rs );
1266 rs->sr_err = LDAP_SUCCESS;
1269 op->ors_slimit = slimit;
1273 /* use the standard hard/soft limit if any */
1274 op->ors_slimit = pr_total;
1277 /* no limit requested: use soft, whatever it is */
1278 } else if ( op->ors_slimit == 0 ) {
1279 op->ors_slimit = op->ors_limit->lms_s_soft;
1281 /* limit requested: check if legal */
1283 /* hard limit as soft (traditional behavior) */
1284 if ( op->ors_limit->lms_s_hard == 0 ) {
1285 if ( op->ors_limit->lms_s_soft > 0
1286 && op->ors_slimit > op->ors_limit->lms_s_soft ) {
1287 op->ors_slimit = op->ors_limit->lms_s_soft;
1290 /* explicit hard limit: error if violated */
1291 } else if ( op->ors_limit->lms_s_hard > 0 ) {
1292 #ifdef ABOVE_HARD_LIMIT_IS_ERROR
1293 if ( op->ors_slimit == SLAP_MAX_LIMIT ) {
1294 op->ors_slimit = op->ors_limit->lms_s_hard;
1296 } else if ( op->ors_slimit > op->ors_limit->lms_s_hard ) {
1297 /* if limit exceeds hard, error */
1298 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1299 send_ldap_result( op, rs );
1300 rs->sr_err = LDAP_SUCCESS;
1303 #else /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1304 if ( op->ors_slimit > op->ors_limit->lms_s_hard ) {
1305 op->ors_slimit = op->ors_limit->lms_s_hard;
1307 #endif /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1311 /* else leave as is */