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-2006 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;
76 assert( limit != NULL );
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 ( BER_BVISEMPTY( ndn ) ) {
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 ( BER_BVISEMPTY( ndn ) ) {
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 ( BER_BVISEMPTY( ndn ) ) {
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 ( BER_BVISEMPTY( ndn ) ) {
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 ( !BER_BVISEMPTY( ndn ) ) {
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;
241 assert( be != NULL );
242 assert( limit != NULL );
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;
275 ber_str2bv( pattern, 0, 0, &bv );
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 BER_BVZERO( &lm->lm_pat );
305 case SLAP_LIMITS_TYPE_GROUP:
306 assert( group_oc != NULL );
307 assert( group_ad != NULL );
308 lm->lm_group_oc = group_oc;
309 lm->lm_group_ad = group_ad;
313 lm->lm_limits = *limit;
316 if ( be->be_limits != NULL ) {
317 for ( ; be->be_limits[i]; i++ );
320 be->be_limits = ( struct slap_limits ** )ch_realloc( be->be_limits,
321 sizeof( struct slap_limits * ) * ( i + 2 ) );
322 be->be_limits[i] = lm;
323 be->be_limits[i+1] = NULL;
337 int flags = SLAP_LIMITS_UNDEFINED;
339 struct slap_limits_set limit;
341 ObjectClass *group_oc = NULL;
342 AttributeDescription *group_ad = NULL;
344 assert( be != NULL );
347 Debug( LDAP_DEBUG_ANY,
348 "%s : line %d: missing arg(s) in "
349 "\"limits <pattern> <limits>\" line.\n%s",
354 limit = be->be_def_limit;
359 * "limits" <pattern> <limit> [ ... ]
366 * [ "dn" [ "." { "exact" | "base" | "onelevel" | "subtree" | children"
367 * | "regex" | "anonymous" } ] "=" ] <dn pattern>
370 * "exact" and "base" are the same (exact match);
371 * "onelevel" means exactly one rdn below, NOT including pattern
372 * "subtree" means any rdn below, including pattern
373 * "children" means any rdn below, NOT including pattern
375 * "anonymous" may be deprecated in favour
376 * of the pattern = "anonymous" form
378 * "group[/objectClass[/attributeType]]" "=" "<dn pattern>"
382 * "time" [ "." { "soft" | "hard" } ] "=" <integer>
384 * "size" [ "." { "soft" | "hard" | "unchecked" } ] "=" <integer>
388 if ( strcmp( pattern, "*" ) == 0) {
389 flags = SLAP_LIMITS_ANY;
391 } else if ( strcasecmp( pattern, "anonymous" ) == 0 ) {
392 flags = SLAP_LIMITS_ANONYMOUS;
394 } else if ( strcasecmp( pattern, "users" ) == 0 ) {
395 flags = SLAP_LIMITS_USERS;
397 } else if ( strncasecmp( pattern, "dn", STRLENOF( "dn" ) ) == 0 ) {
398 pattern += STRLENOF( "dn" );
399 if ( pattern[0] == '.' ) {
401 if ( strncasecmp( pattern, "exact", STRLENOF( "exact" )) == 0 ) {
402 flags = SLAP_LIMITS_EXACT;
403 pattern += STRLENOF( "exact" );
405 } else if ( strncasecmp( pattern, "base", STRLENOF( "base" ) ) == 0 ) {
406 flags = SLAP_LIMITS_BASE;
407 pattern += STRLENOF( "base" );
409 } else if ( strncasecmp( pattern, "one", STRLENOF( "one" ) ) == 0 ) {
410 flags = SLAP_LIMITS_ONE;
411 pattern += STRLENOF( "one" );
412 if ( strncasecmp( pattern, "level", STRLENOF( "level" ) ) == 0 ) {
413 pattern += STRLENOF( "level" );
416 Debug( LDAP_DEBUG_ANY,
417 "%s : line %d: deprecated \"one\" style "
418 "\"limits <pattern> <limits>\" line; "
419 "use \"onelevel\" instead.\n", fname, lineno, 0 );
422 } else if ( strncasecmp( pattern, "sub", STRLENOF( "sub" ) ) == 0 ) {
423 flags = SLAP_LIMITS_SUBTREE;
424 pattern += STRLENOF( "sub" );
425 if ( strncasecmp( pattern, "tree", STRLENOF( "tree" ) ) == 0 ) {
426 pattern += STRLENOF( "tree" );
429 Debug( LDAP_DEBUG_ANY,
430 "%s : line %d: deprecated \"sub\" style "
431 "\"limits <pattern> <limits>\" line; "
432 "use \"subtree\" instead.\n", fname, lineno, 0 );
435 } else if ( strncasecmp( pattern, "children", STRLENOF( "children" ) ) == 0 ) {
436 flags = SLAP_LIMITS_CHILDREN;
437 pattern += STRLENOF( "children" );
439 } else if ( strncasecmp( pattern, "regex", STRLENOF( "regex" ) ) == 0 ) {
440 flags = SLAP_LIMITS_REGEX;
441 pattern += STRLENOF( "regex" );
444 * this could be deprecated in favour
445 * of the pattern = "anonymous" form
447 } else if ( strncasecmp( pattern, "anonymous", STRLENOF( "anonymous" ) ) == 0 ) {
448 flags = SLAP_LIMITS_ANONYMOUS;
453 /* pre-check the data */
455 case SLAP_LIMITS_ANONYMOUS:
456 case SLAP_LIMITS_USERS:
458 /* no need for pattern */
463 if ( pattern[0] != '=' ) {
464 Debug( LDAP_DEBUG_ANY,
465 "%s : line %d: missing '=' in "
466 "\"dn[.{exact|base|onelevel|subtree"
467 "|children|regex|anonymous}]"
469 "\"limits <pattern> <limits>\" "
475 /* skip '=' (required) */
478 /* trim obvious cases */
479 if ( strcmp( pattern, "*" ) == 0 ) {
480 flags = SLAP_LIMITS_ANY;
483 } else if ( flags == SLAP_LIMITS_REGEX
484 && strcmp( pattern, ".*" ) == 0 ) {
485 flags = SLAP_LIMITS_ANY;
490 } else if (strncasecmp( pattern, "group", STRLENOF( "group" ) ) == 0 ) {
491 pattern += STRLENOF( "group" );
493 if ( pattern[0] == '/' ) {
494 struct berval oc, ad;
496 oc.bv_val = pattern + 1;
497 pattern = strchr( pattern, '=' );
498 if ( pattern == NULL ) {
502 ad.bv_val = strchr( oc.bv_val, '/' );
503 if ( ad.bv_val != NULL ) {
504 const char *text = NULL;
507 oc.bv_len = ad.bv_val - oc.bv_val;
510 ad.bv_len = pattern - ad.bv_val;
511 rc = slap_bv2ad( &ad, &group_ad, &text );
512 if ( rc != LDAP_SUCCESS ) {
517 oc.bv_len = pattern - oc.bv_val;
520 group_oc = oc_bvfind( &oc );
521 if ( group_oc == NULL ) {
526 if ( group_oc == NULL ) {
527 group_oc = oc_find( SLAPD_GROUP_CLASS );
528 if ( group_oc == NULL ) {
534 if ( group_ad == NULL ) {
535 const char *text = NULL;
538 rc = slap_str2ad( SLAPD_GROUP_ATTR, &group_ad, &text );
540 if ( rc != LDAP_SUCCESS ) {
546 flags = SLAP_LIMITS_TYPE_GROUP | SLAP_LIMITS_EXACT;
548 if ( pattern[0] != '=' ) {
549 Debug( LDAP_DEBUG_ANY,
550 "%s : line %d: missing '=' in "
551 "\"group[/objectClass[/attributeType]]"
553 "\"limits <pattern> <limits>\" line.\n",
558 /* skip '=' (required) */
563 for ( i = 2; i < argc; i++ ) {
564 if ( limits_parse_one( argv[i], &limit ) ) {
566 Debug( LDAP_DEBUG_ANY,
567 "%s : line %d: unknown limit values \"%s\" in "
568 "\"limits <pattern> <limits>\" line.\n",
569 fname, lineno, argv[i] );
578 * FIXME: add warnings?
580 if ( limit.lms_t_hard > 0 &&
581 ( limit.lms_t_hard < limit.lms_t_soft
582 || limit.lms_t_soft == -1 ) ) {
583 limit.lms_t_hard = limit.lms_t_soft;
586 if ( limit.lms_s_hard > 0 &&
587 ( limit.lms_s_hard < limit.lms_s_soft
588 || limit.lms_s_soft == -1 ) ) {
589 limit.lms_s_hard = limit.lms_s_soft;
598 * > 0 => limit (in seconds)
603 * > 0 => limit (in entries)
606 * -2 => disable the control
609 * > 0 => limit (in entries)
614 * > 0 => limit size (in entries)
616 if ( limit.lms_s_pr_total > 0 &&
617 limit.lms_s_pr > limit.lms_s_pr_total ) {
618 limit.lms_s_pr = limit.lms_s_pr_total;
621 rc = limits_add( be, flags, pattern, group_oc, group_ad, &limit );
624 Debug( LDAP_DEBUG_ANY,
625 "%s : line %d: unable to add limit in "
626 "\"limits <pattern> <limits>\" line.\n",
636 struct slap_limits_set *limit
639 assert( arg != NULL );
640 assert( limit != NULL );
642 if ( strncasecmp( arg, "time", STRLENOF( "time" ) ) == 0 ) {
643 arg += STRLENOF( "time" );
645 if ( arg[0] == '.' ) {
647 if ( strncasecmp( arg, "soft=", STRLENOF( "soft=" ) ) == 0 ) {
648 arg += STRLENOF( "soft=" );
649 if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
650 limit->lms_t_soft = -1;
655 if ( lutil_atoi( &soft, arg ) != 0 || soft < -1 ) {
660 /* FIXME: use "unlimited" instead; issue warning? */
663 limit->lms_t_soft = soft;
666 } else if ( strncasecmp( arg, "hard=", STRLENOF( "hard=" ) ) == 0 ) {
667 arg += STRLENOF( "hard=" );
668 if ( strcasecmp( arg, "soft" ) == 0 ) {
669 limit->lms_t_hard = 0;
671 } else if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
672 limit->lms_t_hard = -1;
677 if ( lutil_atoi( &hard, arg ) != 0 || hard < -1 ) {
682 /* FIXME: use "unlimited" instead */
686 /* FIXME: use "soft" instead */
689 limit->lms_t_hard = hard;
696 } else if ( arg[0] == '=' ) {
698 if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
699 limit->lms_t_soft = -1;
702 if ( lutil_atoi( &limit->lms_t_soft, arg ) != 0
703 || limit->lms_t_soft < -1 )
708 limit->lms_t_hard = 0;
714 } else if ( strncasecmp( arg, "size", STRLENOF( "size" ) ) == 0 ) {
715 arg += STRLENOF( "size" );
717 if ( arg[0] == '.' ) {
719 if ( strncasecmp( arg, "soft=", STRLENOF( "soft=" ) ) == 0 ) {
720 arg += STRLENOF( "soft=" );
721 if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
722 limit->lms_s_soft = -1;
727 if ( lutil_atoi( &soft, arg ) != 0 || soft < -1 ) {
732 /* FIXME: use "unlimited" instead */
735 limit->lms_s_soft = soft;
738 } else if ( strncasecmp( arg, "hard=", STRLENOF( "hard=" ) ) == 0 ) {
739 arg += STRLENOF( "hard=" );
740 if ( strcasecmp( arg, "soft" ) == 0 ) {
741 limit->lms_s_hard = 0;
743 } else if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
744 limit->lms_s_hard = -1;
749 if ( lutil_atoi( &hard, arg ) != 0 || hard < -1 ) {
754 /* FIXME: use "unlimited" instead */
758 /* FIXME: use "soft" instead */
761 limit->lms_s_hard = hard;
764 } else if ( strncasecmp( arg, "unchecked=", STRLENOF( "unchecked=" ) ) == 0 ) {
765 arg += STRLENOF( "unchecked=" );
766 if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
767 limit->lms_s_unchecked = -1;
769 } else if ( strcasecmp( arg, "disabled" ) == 0 ) {
770 limit->lms_s_unchecked = 0;
775 if ( lutil_atoi( &unchecked, arg ) != 0 || unchecked < -1 ) {
779 if ( unchecked == -1 ) {
780 /* FIXME: use "unlimited" instead */
783 limit->lms_s_unchecked = unchecked;
786 } else if ( strncasecmp( arg, "pr=", STRLENOF( "pr=" ) ) == 0 ) {
787 arg += STRLENOF( "pr=" );
788 if ( strcasecmp( arg, "noEstimate" ) == 0 ) {
789 limit->lms_s_pr_hide = 1;
791 } else if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
792 limit->lms_s_pr = -1;
797 if ( lutil_atoi( &pr, arg ) != 0 || pr < -1 ) {
802 /* FIXME: use "unlimited" instead */
805 limit->lms_s_pr = pr;
808 } else if ( strncasecmp( arg, "prtotal=", STRLENOF( "prtotal=" ) ) == 0 ) {
809 arg += STRLENOF( "prtotal=" );
811 if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
812 limit->lms_s_pr_total = -1;
814 } else if ( strcasecmp( arg, "disabled" ) == 0 ) {
815 limit->lms_s_pr_total = -2;
817 } else if ( strcasecmp( arg, "hard" ) == 0 ) {
818 limit->lms_s_pr_total = 0;
823 if ( lutil_atoi( &total, arg ) != 0 || total < -1 ) {
828 /* FIXME: use "unlimited" instead */
832 /* FIXME: use "pr=disable" instead */
835 limit->lms_s_pr_total = total;
842 } else if ( arg[0] == '=' ) {
844 if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
845 limit->lms_s_soft = -1;
848 if ( lutil_atoi( &limit->lms_s_soft, arg ) != 0
849 || limit->lms_s_soft < -1 )
854 limit->lms_s_hard = 0;
864 static const char *lmpats[] = {
876 #define WHATSLEFT ( buflen - ( ptr - bv->bv_val ) )
878 /* Caller must provide an adequately sized buffer in bv */
880 limits_unparse( struct slap_limits *lim, struct berval *bv, ber_len_t buflen )
886 if ( !bv || !bv->bv_val ) return -1;
890 if (( lim->lm_flags & SLAP_LIMITS_TYPE_MASK ) == SLAP_LIMITS_TYPE_GROUP ) {
891 if ( WHATSLEFT <= STRLENOF( "group/" "/" "=\"" "\"" )
892 + lim->lm_group_oc->soc_cname.bv_len
893 + lim->lm_group_ad->ad_cname.bv_len
894 + lim->lm_pat.bv_len ) return -1;
896 ptr = lutil_strcopy( ptr, "group/" );
897 ptr = lutil_strcopy( ptr, lim->lm_group_oc->soc_cname.bv_val );
899 ptr = lutil_strcopy( ptr, lim->lm_group_ad->ad_cname.bv_val );
900 ptr = lutil_strcopy( ptr, "=\"" );
901 ptr = lutil_strcopy( ptr, lim->lm_pat.bv_val );
904 lm = lim->lm_flags & SLAP_LIMITS_MASK;
906 case SLAP_LIMITS_ANONYMOUS:
907 case SLAP_LIMITS_USERS:
908 case SLAP_LIMITS_ANY:
909 if ( WHATSLEFT <= strlen( lmpats[lm] ) ) return -1;
910 ptr = lutil_strcopy( ptr, lmpats[lm] );
912 case SLAP_LIMITS_UNDEFINED:
913 case SLAP_LIMITS_EXACT:
914 case SLAP_LIMITS_ONE:
915 case SLAP_LIMITS_SUBTREE:
916 case SLAP_LIMITS_CHILDREN:
917 case SLAP_LIMITS_REGEX:
918 if ( WHATSLEFT <= STRLENOF( "dn." "=" "\"" "\"" )
919 + strlen( lmpats[lm] ) + lim->lm_pat.bv_len ) return -1;
920 ptr = lutil_strcopy( ptr, "dn." );
921 ptr = lutil_strcopy( ptr, lmpats[lm] );
924 ptr = lutil_strcopy( ptr, lim->lm_pat.bv_val );
929 if ( WHATSLEFT <= STRLENOF( " " ) ) return -1;
931 bv->bv_len = ptr - bv->bv_val;
934 if ( limits_unparse_one( &lim->lm_limits,
935 SLAP_LIMIT_SIZE|SLAP_LIMIT_TIME,
940 bv->bv_len += btmp.bv_len;
944 /* Caller must provide an adequately sized buffer in bv */
946 limits_unparse_one( struct slap_limits_set *lim, int which, struct berval *bv, ber_len_t buflen )
950 if ( !bv || !bv->bv_val ) return;
954 if ( which & SLAP_LIMIT_SIZE ) {
955 if ( lim->lms_s_soft != SLAPD_DEFAULT_SIZELIMIT ) {
957 /* If same as global limit, drop it */
958 if ( lim != &frontendDB->be_def_limit &&
959 lim->lms_s_soft == frontendDB->be_def_limit.lms_s_soft )
962 /* If there's also a hard limit, fully qualify this one */
963 } else if ( lim->lms_s_hard ) {
964 if ( WHATSLEFT <= STRLENOF( " size.soft=" ) ) return -1;
965 ptr = lutil_strcopy( ptr, " size.soft=" );
967 /* If doing both size & time, qualify this */
968 } else if ( which & SLAP_LIMIT_TIME ) {
969 if ( WHATSLEFT <= STRLENOF( " size=" ) ) return -1;
970 ptr = lutil_strcopy( ptr, " size=" );
973 if ( lim->lms_s_soft == -1 ) {
974 if ( WHATSLEFT <= STRLENOF( "unlimited" ) ) return -1;
975 ptr = lutil_strcopy( ptr, "unlimited" );
977 ptr += snprintf( ptr, WHATSLEFT, "%d", lim->lms_s_soft );
978 if ( WHATSLEFT < 0 ) return -1;
983 if ( lim->lms_s_hard ) {
984 if ( WHATSLEFT <= STRLENOF( " size.hard=" ) ) return -1;
985 ptr = lutil_strcopy( ptr, " size.hard=" );
986 if ( lim->lms_s_hard == -1 ) {
987 if ( WHATSLEFT <= STRLENOF( "unlimited" ) ) return -1;
988 ptr = lutil_strcopy( ptr, "unlimited" );
990 ptr += snprintf( ptr, WHATSLEFT, "%d", lim->lms_s_hard );
991 if ( WHATSLEFT < 0 ) return -1;
995 if ( lim->lms_s_unchecked != -1 ) {
996 if ( WHATSLEFT <= STRLENOF( " size.unchecked=" ) ) return -1;
997 ptr = lutil_strcopy( ptr, " size.unchecked=" );
998 if ( lim->lms_s_unchecked == 0 ) {
999 if ( WHATSLEFT <= STRLENOF( "disabled" ) ) return -1;
1000 ptr = lutil_strcopy( ptr, "disabled" );
1002 ptr += snprintf( ptr, WHATSLEFT, "%d", lim->lms_s_unchecked );
1003 if ( WHATSLEFT < 0 ) return -1;
1007 if ( lim->lms_s_pr_hide ) {
1008 if ( WHATSLEFT <= STRLENOF( " size.pr=noEstimate " ) ) return -1;
1009 ptr = lutil_strcopy( ptr, " size.pr=noEstimate " );
1011 if ( lim->lms_s_pr ) {
1012 if ( WHATSLEFT <= STRLENOF( " size.pr=" ) ) return -1;
1013 ptr = lutil_strcopy( ptr, " size.pr=" );
1014 if ( lim->lms_s_pr == -1 ) {
1015 if ( WHATSLEFT <= STRLENOF( "unlimited" ) ) return -1;
1016 ptr = lutil_strcopy( ptr, "unlimited" );
1018 ptr += snprintf( ptr, WHATSLEFT, "%d", lim->lms_s_pr );
1019 if ( WHATSLEFT < 0 ) return -1;
1023 if ( lim->lms_s_pr_total ) {
1024 if ( WHATSLEFT <= STRLENOF( " size.prtotal=" ) ) return -1;
1025 ptr = lutil_strcopy( ptr, " size.prtotal=" );
1026 if ( lim->lms_s_pr_total == -1 ) {
1027 if ( WHATSLEFT <= STRLENOF( "unlimited" ) ) return -1;
1028 ptr = lutil_strcopy( ptr, "unlimited" );
1029 } else if ( lim->lms_s_pr_total == -2 ) {
1030 if ( WHATSLEFT <= STRLENOF( "disabled" ) ) return -1;
1031 ptr = lutil_strcopy( ptr, "disabled" );
1033 ptr += snprintf( ptr, WHATSLEFT, "%d", lim->lms_s_pr_total );
1034 if ( WHATSLEFT < 0 ) return -1;
1040 if ( which & SLAP_LIMIT_TIME ) {
1041 if ( lim->lms_t_soft != SLAPD_DEFAULT_TIMELIMIT ) {
1043 /* If same as global limit, drop it */
1044 if ( lim != &frontendDB->be_def_limit &&
1045 lim->lms_t_soft == frontendDB->be_def_limit.lms_t_soft )
1049 /* If there's also a hard limit, fully qualify this one */
1050 } else if ( lim->lms_t_hard ) {
1051 if ( WHATSLEFT <= STRLENOF( " time.soft=" ) ) return -1;
1052 ptr = lutil_strcopy( ptr, " time.soft=" );
1054 /* If doing both size & time, qualify this */
1055 } else if ( which & SLAP_LIMIT_SIZE ) {
1056 if ( WHATSLEFT <= STRLENOF( " time=" ) ) return -1;
1057 ptr = lutil_strcopy( ptr, " time=" );
1060 if ( lim->lms_t_soft == -1 ) {
1061 if ( WHATSLEFT <= STRLENOF( "unlimited" ) ) return -1;
1062 ptr = lutil_strcopy( ptr, "unlimited" );
1064 ptr += snprintf( ptr, WHATSLEFT, "%d", lim->lms_t_soft );
1065 if ( WHATSLEFT < 0 ) return -1;
1070 if ( lim->lms_t_hard ) {
1071 if ( WHATSLEFT <= STRLENOF( " time.hard=" ) ) return -1;
1072 ptr = lutil_strcopy( ptr, " time.hard=" );
1073 if ( lim->lms_t_hard == -1 ) {
1074 if ( WHATSLEFT <= STRLENOF( "unlimited" ) ) return -1;
1075 ptr = lutil_strcopy( ptr, "unlimited" );
1077 ptr += snprintf( ptr, WHATSLEFT, "%d", lim->lms_t_hard );
1078 if ( WHATSLEFT < 0 ) return -1;
1083 if ( ptr != bv->bv_val ) {
1086 bv->bv_len = ptr - bv->bv_val;
1091 limits_check( Operation *op, SlapReply *rs )
1093 assert( op != NULL );
1094 assert( rs != NULL );
1095 /* FIXME: should this be always true? */
1096 assert( op->o_tag == LDAP_REQ_SEARCH);
1098 /* protocol only allows 0..maxInt;
1100 * internal searches:
1101 * - may use SLAP_NO_LIMIT ( = -1 ) to indicate no limits;
1102 * - should use slimit = N and tlimit = SLAP_NO_LIMIT to
1103 * indicate searches that should return exactly N matches,
1104 * and handle errors thru a callback (see for instance
1105 * slap_sasl_match() and slap_sasl2dn())
1107 if ( op->ors_tlimit == SLAP_NO_LIMIT && op->ors_slimit == SLAP_NO_LIMIT ) {
1111 /* allow root to set no limit */
1112 if ( be_isroot( op ) ) {
1113 op->ors_limit = NULL;
1115 if ( op->ors_tlimit == 0 ) {
1116 op->ors_tlimit = SLAP_NO_LIMIT;
1119 if ( op->ors_slimit == 0 ) {
1120 op->ors_slimit = SLAP_NO_LIMIT;
1123 /* if not root, get appropriate limits */
1125 ( void ) limits_get( op, &op->o_ndn, &op->ors_limit );
1127 assert( op->ors_limit != NULL );
1129 /* if no limit is required, use soft limit */
1130 if ( op->ors_tlimit == 0 ) {
1131 op->ors_tlimit = op->ors_limit->lms_t_soft;
1133 /* limit required: check if legal */
1135 if ( op->ors_limit->lms_t_hard == 0 ) {
1136 if ( op->ors_limit->lms_t_soft > 0
1137 && ( op->ors_tlimit > op->ors_limit->lms_t_soft ) ) {
1138 op->ors_tlimit = op->ors_limit->lms_t_soft;
1141 } else if ( op->ors_limit->lms_t_hard > 0 ) {
1142 #ifdef ABOVE_HARD_LIMIT_IS_ERROR
1143 if ( op->ors_tlimit == SLAP_MAX_LIMIT ) {
1144 op->ors_tlimit = op->ors_limit->lms_t_hard;
1146 } else if ( op->ors_tlimit > op->ors_limit->lms_t_hard ) {
1147 /* error if exceeding hard limit */
1148 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1149 send_ldap_result( op, rs );
1150 rs->sr_err = LDAP_SUCCESS;
1153 #else /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1154 if ( op->ors_tlimit > op->ors_limit->lms_t_hard ) {
1155 op->ors_tlimit = op->ors_limit->lms_t_hard;
1157 #endif /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1161 /* else leave as is */
1163 /* don't even get to backend if candidate check is disabled */
1164 if ( op->ors_limit->lms_s_unchecked == 0 ) {
1165 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1166 send_ldap_result( op, rs );
1167 rs->sr_err = LDAP_SUCCESS;
1171 /* if paged results is requested */
1172 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
1175 PagedResultsState *ps = op->o_pagedresults_state;
1177 /* paged results is not allowed */
1178 if ( op->ors_limit->lms_s_pr_total == -2 ) {
1179 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1180 rs->sr_text = "pagedResults control not allowed";
1181 send_ldap_result( op, rs );
1182 rs->sr_err = LDAP_SUCCESS;
1187 if ( op->ors_limit->lms_s_pr > 0 && ps->ps_size > op->ors_limit->lms_s_pr ) {
1188 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1189 rs->sr_text = "illegal pagedResults page size";
1190 send_ldap_result( op, rs );
1191 rs->sr_err = LDAP_SUCCESS;
1196 if ( op->ors_limit->lms_s_pr_total == 0 ) {
1197 if ( op->ors_limit->lms_s_hard == 0 ) {
1198 pr_total = op->ors_limit->lms_s_soft;
1200 pr_total = op->ors_limit->lms_s_hard;
1203 pr_total = op->ors_limit->lms_s_pr_total;
1206 if ( pr_total == -1 ) {
1207 if ( op->ors_slimit == 0 || op->ors_slimit == SLAP_MAX_LIMIT ) {
1211 slimit = op->ors_slimit - ps->ps_count;
1214 #ifdef ABOVE_HARD_LIMIT_IS_ERROR
1215 } else if ( pr_total > 0 && op->ors_slimit != SLAP_MAX_LIMIT
1216 && ( op->ors_slimit == SLAP_NO_LIMIT || op->ors_slimit > pr_total ) )
1218 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1219 send_ldap_result( op, rs );
1220 rs->sr_err = LDAP_SUCCESS;
1222 #endif /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1225 /* if no limit is required, use soft limit */
1229 /* first round of pagedResults: set count to any appropriate limit */
1231 /* if the limit is set, check that it does not violate any server-side limit */
1232 #ifdef ABOVE_HARD_LIMIT_IS_ERROR
1233 if ( op->ors_slimit == SLAP_MAX_LIMIT ) {
1234 slimit2 = op->ors_slimit = pr_total;
1235 #else /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1236 if ( op->ors_slimit == SLAP_MAX_LIMIT || op->ors_slimit > pr_total ) {
1237 slimit2 = op->ors_slimit = pr_total;
1238 #endif /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1240 } else if ( op->ors_slimit == 0 ) {
1244 slimit2 = op->ors_slimit;
1247 total = slimit2 - ps->ps_count;
1250 if ( op->ors_limit->lms_s_pr > 0 ) {
1251 /* use the smallest limit set by total/per page */
1252 if ( total < op->ors_limit->lms_s_pr ) {
1256 /* use the perpage limit if any
1257 * NOTE: + 1 because the given value must be legal */
1258 slimit = op->ors_limit->lms_s_pr + 1;
1262 /* use the total limit if any */
1266 } else if ( op->ors_limit->lms_s_pr > 0 ) {
1267 /* use the perpage limit if any
1268 * NOTE: + 1 because the given value must be legal */
1269 slimit = op->ors_limit->lms_s_pr + 1;
1272 /* use the standard hard/soft limit if any */
1273 slimit = op->ors_limit->lms_s_hard;
1277 /* if got any limit, use it */
1278 if ( slimit != -2 ) {
1279 if ( op->ors_slimit == 0 ) {
1280 op->ors_slimit = slimit;
1282 } else if ( slimit > 0 ) {
1283 if ( op->ors_slimit - ps->ps_count > slimit ) {
1284 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1285 send_ldap_result( op, rs );
1286 rs->sr_err = LDAP_SUCCESS;
1289 op->ors_slimit = slimit;
1291 } else if ( slimit == 0 ) {
1296 /* use the standard hard/soft limit if any */
1297 op->ors_slimit = pr_total;
1300 /* no limit requested: use soft, whatever it is */
1301 } else if ( op->ors_slimit == 0 ) {
1302 op->ors_slimit = op->ors_limit->lms_s_soft;
1304 /* limit requested: check if legal */
1306 /* hard limit as soft (traditional behavior) */
1307 if ( op->ors_limit->lms_s_hard == 0 ) {
1308 if ( op->ors_limit->lms_s_soft > 0
1309 && op->ors_slimit > op->ors_limit->lms_s_soft ) {
1310 op->ors_slimit = op->ors_limit->lms_s_soft;
1313 /* explicit hard limit: error if violated */
1314 } else if ( op->ors_limit->lms_s_hard > 0 ) {
1315 #ifdef ABOVE_HARD_LIMIT_IS_ERROR
1316 if ( op->ors_slimit == SLAP_MAX_LIMIT ) {
1317 op->ors_slimit = op->ors_limit->lms_s_hard;
1319 } else if ( op->ors_slimit > op->ors_limit->lms_s_hard ) {
1320 /* if limit exceeds hard, error */
1321 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1322 send_ldap_result( op, rs );
1323 rs->sr_err = LDAP_SUCCESS;
1326 #else /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1327 if ( op->ors_slimit > op->ors_limit->lms_s_hard ) {
1328 op->ors_slimit = op->ors_limit->lms_s_hard;
1330 #endif /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1334 /* else leave as is */
1342 struct slap_limits **lm )
1350 for ( i = 0; lm[ i ]; i++ ) {
1351 switch ( lm[ i ]->lm_flags & SLAP_LIMITS_MASK ) {
1352 case SLAP_LIMITS_REGEX:
1353 regfree( &lm[ i ]->lm_regex );
1356 case SLAP_LIMITS_EXACT:
1357 case SLAP_LIMITS_ONE:
1358 case SLAP_LIMITS_SUBTREE:
1359 case SLAP_LIMITS_CHILDREN:
1360 if ( !BER_BVISNULL( &lm[ i ]->lm_pat ) ) {
1361 ch_free( lm[ i ]->lm_pat.bv_val );