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-2008 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
30 static const struct berval lmpats[] = {
33 BER_BVC( "onelevel" ),
35 BER_BVC( "children" ),
37 BER_BVC( "anonymous" ),
43 static const char *const dn_source[2] = { "DN", "DN.THIS" };
44 static const char *const lmpats_out[] = {
57 limits2str( unsigned i )
59 return i < (sizeof( lmpats_out ) / sizeof( lmpats_out[0] ))
60 ? lmpats_out[i] : "UNKNOWN";
62 #endif /* LDAP_DEBUG */
67 struct slap_limits_set **limit
70 static struct berval empty_dn = BER_BVC( "" );
71 struct slap_limits **lm;
72 struct berval *ndns[2];
75 assert( limit != NULL );
78 ndns[1] = &op->o_req_ndn;
80 Debug( LDAP_DEBUG_TRACE, "==> limits_get: %s self=\"%s\" this=\"%s\"\n",
82 BER_BVISNULL( ndns[0] ) ? "[anonymous]" : ndns[0]->bv_val,
83 BER_BVISNULL( ndns[1] ) ? "" : ndns[1]->bv_val );
87 *limit = &op->o_bd->be_def_limit;
89 if ( op->o_bd->be_limits == NULL ) {
93 for ( lm = op->o_bd->be_limits; lm[0] != NULL; lm++ ) {
94 unsigned style = lm[0]->lm_flags & SLAP_LIMITS_MASK;
95 unsigned type = lm[0]->lm_flags & SLAP_LIMITS_TYPE_MASK;
96 unsigned isthis = type == SLAP_LIMITS_TYPE_THIS;
97 struct berval *ndn = ndns[isthis];
99 if ( style == SLAP_LIMITS_ANY )
102 if ( BER_BVISEMPTY( ndn ) ) {
103 if ( style == SLAP_LIMITS_ANONYMOUS )
111 case SLAP_LIMITS_EXACT:
112 if ( type == SLAP_LIMITS_TYPE_GROUP ) {
113 int rc = backend_group( op, NULL,
116 lm[0]->lm_group_ad );
121 if ( dn_match( &lm[0]->lm_pat, ndn ) ) {
127 case SLAP_LIMITS_ONE:
128 case SLAP_LIMITS_SUBTREE:
129 case SLAP_LIMITS_CHILDREN: {
132 /* ndn shorter than lm_pat */
133 if ( ndn->bv_len < lm[0]->lm_pat.bv_len ) {
136 d = ndn->bv_len - lm[0]->lm_pat.bv_len;
139 /* allow exact match for SUBTREE only */
140 if ( style != SLAP_LIMITS_SUBTREE ) {
144 /* check for unescaped rdn separator */
145 if ( !DN_SEPARATOR( ndn->bv_val[d - 1] ) ) {
150 /* check that ndn ends with lm_pat */
151 if ( strcmp( lm[0]->lm_pat.bv_val, &ndn->bv_val[d] ) != 0 ) {
155 /* in case of ONE, require exactly one rdn below lm_pat */
156 if ( style == SLAP_LIMITS_ONE ) {
157 if ( dn_rdnlen( NULL, ndn ) != d - 1 ) {
165 case SLAP_LIMITS_REGEX:
166 if ( regexec( &lm[0]->lm_regex, ndn->bv_val, 0, NULL, 0 ) == 0 ) {
171 case SLAP_LIMITS_ANONYMOUS:
174 case SLAP_LIMITS_USERS:
176 Debug( LDAP_DEBUG_TRACE, "<== limits_get: type=%s match=%s\n",
177 dn_source[isthis], limits2str( style ), 0 );
179 *limit = &lm[0]->lm_limits;
183 Debug( LDAP_DEBUG_TRACE,
184 "<== limits_get: type=%s match=%s dn=\"%s\"\n",
185 dn_source[isthis], limits2str( style ), lm[0]->lm_pat.bv_val );
186 *limit = &lm[0]->lm_limits;
190 Debug( LDAP_DEBUG_TRACE, "<== limits_get: type=GROUP match=EXACT "
191 "dn=\"%s\" oc=\"%s\" ad=\"%s\"\n",
192 lm[0]->lm_pat.bv_val,
193 lm[0]->lm_group_oc->soc_cname.bv_val,
194 lm[0]->lm_group_ad->ad_cname.bv_val );
195 *limit = &lm[0]->lm_limits;
199 assert( 0 ); /* unreachable */
212 ObjectClass *group_oc,
213 AttributeDescription *group_ad,
214 struct slap_limits_set *limit
218 struct slap_limits *lm;
219 unsigned type, style;
221 assert( be != NULL );
222 assert( limit != NULL );
224 type = flags & SLAP_LIMITS_TYPE_MASK;
225 style = flags & SLAP_LIMITS_MASK;
228 case SLAP_LIMITS_ANONYMOUS:
229 case SLAP_LIMITS_USERS:
230 case SLAP_LIMITS_ANY:
231 /* For these styles, type == 0 (SLAP_LIMITS_TYPE_SELF). */
232 for ( i = 0; be->be_limits && be->be_limits[ i ]; i++ ) {
233 if ( be->be_limits[ i ]->lm_flags == style ) {
241 lm = ( struct slap_limits * )ch_calloc( sizeof( struct slap_limits ), 1 );
244 case SLAP_LIMITS_UNDEFINED:
245 style = SLAP_LIMITS_EXACT;
246 /* continue to next cases */
247 case SLAP_LIMITS_EXACT:
248 case SLAP_LIMITS_ONE:
249 case SLAP_LIMITS_SUBTREE:
250 case SLAP_LIMITS_CHILDREN:
251 lm->lm_flags = style | type;
256 ber_str2bv( pattern, 0, 0, &bv );
258 rc = dnNormalize( 0, NULL, NULL, &bv, &lm->lm_pat, NULL );
259 if ( rc != LDAP_SUCCESS ) {
266 case SLAP_LIMITS_REGEX:
267 lm->lm_flags = style | type;
268 ber_str2bv( pattern, 0, 1, &lm->lm_pat );
269 if ( regcomp( &lm->lm_regex, lm->lm_pat.bv_val,
270 REG_EXTENDED | REG_ICASE ) ) {
271 free( lm->lm_pat.bv_val );
277 case SLAP_LIMITS_ANONYMOUS:
278 case SLAP_LIMITS_USERS:
279 case SLAP_LIMITS_ANY:
280 lm->lm_flags = style | type;
281 BER_BVZERO( &lm->lm_pat );
286 case SLAP_LIMITS_TYPE_GROUP:
287 assert( group_oc != NULL );
288 assert( group_ad != NULL );
289 lm->lm_group_oc = group_oc;
290 lm->lm_group_ad = group_ad;
294 lm->lm_limits = *limit;
297 if ( be->be_limits != NULL ) {
298 for ( ; be->be_limits[i]; i++ );
301 be->be_limits = ( struct slap_limits ** )ch_realloc( be->be_limits,
302 sizeof( struct slap_limits * ) * ( i + 2 ) );
303 be->be_limits[i] = lm;
304 be->be_limits[i+1] = NULL;
309 #define STRSTART( s, m ) (strncasecmp( s, m, STRLENOF( "" m "" )) == 0)
320 int flags = SLAP_LIMITS_UNDEFINED;
322 struct slap_limits_set limit;
324 ObjectClass *group_oc = NULL;
325 AttributeDescription *group_ad = NULL;
327 assert( be != NULL );
330 Debug( LDAP_DEBUG_ANY,
331 "%s : line %d: missing arg(s) in "
332 "\"limits <pattern> <limits>\" line.\n%s",
337 limit = be->be_def_limit;
342 * "limits" <pattern> <limit> [ ... ]
349 * [ "dn" [ "." { "this" | "self" } ] [ "." { "exact" | "base" |
350 * "onelevel" | "subtree" | "children" | "regex" | "anonymous" } ]
354 * "this" is the baseobject, "self" (the default) is the bound DN
355 * "exact" and "base" are the same (exact match);
356 * "onelevel" means exactly one rdn below, NOT including pattern
357 * "subtree" means any rdn below, including pattern
358 * "children" means any rdn below, NOT including pattern
360 * "anonymous" may be deprecated in favour
361 * of the pattern = "anonymous" form
363 * "group[/objectClass[/attributeType]]" "=" "<dn pattern>"
367 * "time" [ "." { "soft" | "hard" } ] "=" <integer>
369 * "size" [ "." { "soft" | "hard" | "unchecked" } ] "=" <integer>
373 if ( strcmp( pattern, "*" ) == 0) {
374 flags = SLAP_LIMITS_ANY;
376 } else if ( strcasecmp( pattern, "anonymous" ) == 0 ) {
377 flags = SLAP_LIMITS_ANONYMOUS;
379 } else if ( strcasecmp( pattern, "users" ) == 0 ) {
380 flags = SLAP_LIMITS_USERS;
382 } else if ( STRSTART( pattern, "dn" ) ) {
383 pattern += STRLENOF( "dn" );
384 flags = SLAP_LIMITS_TYPE_SELF;
385 if ( pattern[0] == '.' ) {
387 if ( STRSTART( pattern, "this" ) ) {
388 flags = SLAP_LIMITS_TYPE_THIS;
389 pattern += STRLENOF( "this" );
390 } else if ( STRSTART( pattern, "self" ) ) {
391 pattern += STRLENOF( "self" );
396 if ( pattern[0] == '.' ) {
399 if ( STRSTART( pattern, "exact" ) ) {
400 flags |= SLAP_LIMITS_EXACT;
401 pattern += STRLENOF( "exact" );
403 } else if ( STRSTART( pattern, "base" ) ) {
404 flags |= SLAP_LIMITS_BASE;
405 pattern += STRLENOF( "base" );
407 } else if ( STRSTART( pattern, "one" ) ) {
408 flags |= SLAP_LIMITS_ONE;
409 pattern += STRLENOF( "one" );
410 if ( STRSTART( pattern, "level" ) ) {
411 pattern += STRLENOF( "level" );
414 Debug( LDAP_DEBUG_ANY,
415 "%s : line %d: deprecated \"one\" style "
416 "\"limits <pattern> <limits>\" line; "
417 "use \"onelevel\" instead.\n", fname, lineno, 0 );
420 } else if ( STRSTART( pattern, "sub" ) ) {
421 flags |= SLAP_LIMITS_SUBTREE;
422 pattern += STRLENOF( "sub" );
423 if ( STRSTART( pattern, "tree" ) ) {
424 pattern += STRLENOF( "tree" );
427 Debug( LDAP_DEBUG_ANY,
428 "%s : line %d: deprecated \"sub\" style "
429 "\"limits <pattern> <limits>\" line; "
430 "use \"subtree\" instead.\n", fname, lineno, 0 );
433 } else if ( STRSTART( pattern, "children" ) ) {
434 flags |= SLAP_LIMITS_CHILDREN;
435 pattern += STRLENOF( "children" );
437 } else if ( STRSTART( pattern, "regex" ) ) {
438 flags |= SLAP_LIMITS_REGEX;
439 pattern += STRLENOF( "regex" );
442 * this could be deprecated in favour
443 * of the pattern = "anonymous" form
445 } else if ( STRSTART( pattern, "anonymous" )
446 && flags == SLAP_LIMITS_TYPE_SELF )
448 flags = SLAP_LIMITS_ANONYMOUS;
453 /* pre-check the data */
454 if ( pattern != NULL ) {
455 if ( pattern[0] != '=' ) {
456 Debug( LDAP_DEBUG_ANY,
457 "%s : line %d: missing '=' in "
458 "\"dn[.{this|self}][.{exact|base"
459 "|onelevel|subtree|children|regex}]"
461 "\"limits <pattern> <limits>\" "
467 /* skip '=' (required) */
470 /* trim obvious cases */
471 if ( strcmp( pattern, "*" ) == 0 ) {
472 flags = SLAP_LIMITS_ANY;
475 } else if ( (flags & SLAP_LIMITS_MASK) == SLAP_LIMITS_REGEX
476 && strcmp( pattern, ".*" ) == 0 ) {
477 flags = SLAP_LIMITS_ANY;
482 } else if (STRSTART( pattern, "group" ) ) {
483 pattern += STRLENOF( "group" );
485 if ( pattern[0] == '/' ) {
486 struct berval oc, ad;
488 oc.bv_val = pattern + 1;
489 pattern = strchr( pattern, '=' );
490 if ( pattern == NULL ) {
494 ad.bv_val = strchr( oc.bv_val, '/' );
495 if ( ad.bv_val != NULL ) {
496 const char *text = NULL;
498 oc.bv_len = ad.bv_val - oc.bv_val;
501 ad.bv_len = pattern - ad.bv_val;
502 rc = slap_bv2ad( &ad, &group_ad, &text );
503 if ( rc != LDAP_SUCCESS ) {
508 oc.bv_len = pattern - oc.bv_val;
511 group_oc = oc_bvfind( &oc );
512 if ( group_oc == NULL ) {
517 if ( group_oc == NULL ) {
518 group_oc = oc_find( SLAPD_GROUP_CLASS );
519 if ( group_oc == NULL ) {
525 if ( group_ad == NULL ) {
526 const char *text = NULL;
528 rc = slap_str2ad( SLAPD_GROUP_ATTR, &group_ad, &text );
530 if ( rc != LDAP_SUCCESS ) {
536 flags = SLAP_LIMITS_TYPE_GROUP | SLAP_LIMITS_EXACT;
538 if ( pattern[0] != '=' ) {
539 Debug( LDAP_DEBUG_ANY,
540 "%s : line %d: missing '=' in "
541 "\"group[/objectClass[/attributeType]]"
543 "\"limits <pattern> <limits>\" line.\n",
548 /* skip '=' (required) */
553 for ( i = 2; i < argc; i++ ) {
554 if ( limits_parse_one( argv[i], &limit ) ) {
556 Debug( LDAP_DEBUG_ANY,
557 "%s : line %d: unknown limit values \"%s\" in "
558 "\"limits <pattern> <limits>\" line.\n",
559 fname, lineno, argv[i] );
568 * FIXME: add warnings?
570 if ( limit.lms_t_hard > 0 &&
571 ( limit.lms_t_hard < limit.lms_t_soft
572 || limit.lms_t_soft == -1 ) ) {
573 limit.lms_t_hard = limit.lms_t_soft;
576 if ( limit.lms_s_hard > 0 &&
577 ( limit.lms_s_hard < limit.lms_s_soft
578 || limit.lms_s_soft == -1 ) ) {
579 limit.lms_s_hard = limit.lms_s_soft;
588 * > 0 => limit (in seconds)
593 * > 0 => limit (in entries)
596 * -2 => disable the control
599 * > 0 => limit (in entries)
604 * > 0 => limit size (in entries)
606 if ( limit.lms_s_pr_total > 0 &&
607 limit.lms_s_pr > limit.lms_s_pr_total ) {
608 limit.lms_s_pr = limit.lms_s_pr_total;
611 rc = limits_add( be, flags, pattern, group_oc, group_ad, &limit );
614 Debug( LDAP_DEBUG_ANY,
615 "%s : line %d: unable to add limit in "
616 "\"limits <pattern> <limits>\" line.\n",
626 struct slap_limits_set *limit
629 assert( arg != NULL );
630 assert( limit != NULL );
632 if ( STRSTART( arg, "time" ) ) {
633 arg += STRLENOF( "time" );
635 if ( arg[0] == '.' ) {
637 if ( STRSTART( arg, "soft=" ) ) {
638 arg += STRLENOF( "soft=" );
639 if ( strcasecmp( arg, "unlimited" ) == 0
640 || strcasecmp( arg, "none" ) == 0 )
642 limit->lms_t_soft = -1;
647 if ( lutil_atoi( &soft, arg ) != 0 || soft < -1 ) {
652 /* FIXME: use "unlimited" instead; issue warning? */
655 limit->lms_t_soft = soft;
658 } else if ( STRSTART( arg, "hard=" ) ) {
659 arg += STRLENOF( "hard=" );
660 if ( strcasecmp( arg, "soft" ) == 0 ) {
661 limit->lms_t_hard = 0;
663 } else if ( strcasecmp( arg, "unlimited" ) == 0
664 || strcasecmp( arg, "none" ) == 0 )
666 limit->lms_t_hard = -1;
671 if ( lutil_atoi( &hard, arg ) != 0 || hard < -1 ) {
676 /* FIXME: use "unlimited" instead */
680 /* FIXME: use "soft" instead */
683 limit->lms_t_hard = hard;
690 } else if ( arg[0] == '=' ) {
692 if ( strcasecmp( arg, "unlimited" ) == 0
693 || strcasecmp( arg, "none" ) == 0 )
695 limit->lms_t_soft = -1;
698 if ( lutil_atoi( &limit->lms_t_soft, arg ) != 0
699 || limit->lms_t_soft < -1 )
704 limit->lms_t_hard = 0;
710 } else if ( STRSTART( arg, "size" ) ) {
711 arg += STRLENOF( "size" );
713 if ( arg[0] == '.' ) {
715 if ( STRSTART( arg, "soft=" ) ) {
716 arg += STRLENOF( "soft=" );
717 if ( strcasecmp( arg, "unlimited" ) == 0
718 || strcasecmp( arg, "none" ) == 0 )
720 limit->lms_s_soft = -1;
725 if ( lutil_atoi( &soft, arg ) != 0 || soft < -1 ) {
730 /* FIXME: use "unlimited" instead */
733 limit->lms_s_soft = soft;
736 } else if ( STRSTART( arg, "hard=" ) ) {
737 arg += STRLENOF( "hard=" );
738 if ( strcasecmp( arg, "soft" ) == 0 ) {
739 limit->lms_s_hard = 0;
741 } else if ( strcasecmp( arg, "unlimited" ) == 0
742 || 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 ( STRSTART( arg, "unchecked=" ) ) {
765 arg += STRLENOF( "unchecked=" );
766 if ( strcasecmp( arg, "unlimited" ) == 0
767 || strcasecmp( arg, "none" ) == 0 )
769 limit->lms_s_unchecked = -1;
771 } else if ( strcasecmp( arg, "disabled" ) == 0 ) {
772 limit->lms_s_unchecked = 0;
777 if ( lutil_atoi( &unchecked, arg ) != 0 || unchecked < -1 ) {
781 if ( unchecked == -1 ) {
782 /* FIXME: use "unlimited" instead */
785 limit->lms_s_unchecked = unchecked;
788 } else if ( STRSTART( arg, "pr=" ) ) {
789 arg += STRLENOF( "pr=" );
790 if ( strcasecmp( arg, "noEstimate" ) == 0 ) {
791 limit->lms_s_pr_hide = 1;
793 } else if ( strcasecmp( arg, "unlimited" ) == 0
794 || strcasecmp( arg, "none" ) == 0 )
796 limit->lms_s_pr = -1;
801 if ( lutil_atoi( &pr, arg ) != 0 || pr < -1 ) {
806 /* FIXME: use "unlimited" instead */
809 limit->lms_s_pr = pr;
812 } else if ( STRSTART( arg, "prtotal=" ) ) {
813 arg += STRLENOF( "prtotal=" );
815 if ( strcasecmp( arg, "unlimited" ) == 0
816 || strcasecmp( arg, "none" ) == 0 )
818 limit->lms_s_pr_total = -1;
820 } else if ( strcasecmp( arg, "disabled" ) == 0 ) {
821 limit->lms_s_pr_total = -2;
823 } else if ( strcasecmp( arg, "hard" ) == 0 ) {
824 limit->lms_s_pr_total = 0;
829 if ( lutil_atoi( &total, arg ) != 0 || total < -1 ) {
834 /* FIXME: use "unlimited" instead */
838 /* FIXME: use "pr=disable" instead */
841 limit->lms_s_pr_total = total;
848 } else if ( arg[0] == '=' ) {
850 if ( strcasecmp( arg, "unlimited" ) == 0
851 || strcasecmp( arg, "none" ) == 0 )
853 limit->lms_s_soft = -1;
856 if ( lutil_atoi( &limit->lms_s_soft, arg ) != 0
857 || limit->lms_s_soft < -1 )
862 limit->lms_s_hard = 0;
872 /* Helper macros for limits_unparse() and limits_unparse_one():
873 * Write to ptr, but not past bufEnd. Move ptr past the new text.
874 * Return (success && enough room ? 0 : -1).
876 #define ptr_APPEND_BV(bv) /* Append a \0-terminated berval */ \
877 (WHATSLEFT <= (bv).bv_len ? -1 : \
878 ((void) (ptr = lutil_strcopy( ptr, (bv).bv_val )), 0))
879 #define ptr_APPEND_LIT(str) /* Append a string literal */ \
880 (WHATSLEFT <= STRLENOF( "" str "" ) ? -1 : \
881 ((void) (ptr = lutil_strcopy( ptr, str )), 0))
882 #define ptr_APPEND_FMT(args) /* Append formatted text */ \
883 (WHATSLEFT <= (tmpLen = snprintf args) ? -1 : ((void) (ptr += tmpLen), 0))
884 #define ptr_APPEND_FMT1(fmt, arg) ptr_APPEND_FMT(( ptr, WHATSLEFT, fmt, arg ))
885 #define WHATSLEFT ((ber_len_t) (bufEnd - ptr))
887 /* Caller must provide an adequately sized buffer in bv */
889 limits_unparse( struct slap_limits *lim, struct berval *bv, ber_len_t buflen )
892 char *ptr, *bufEnd; /* Updated/used by ptr_APPEND_*()/WHATSLEFT */
893 ber_len_t tmpLen; /* Used by ptr_APPEND_FMT*() */
894 unsigned type, style;
897 if ( !bv || !bv->bv_val ) return -1;
900 bufEnd = ptr + buflen;
901 type = lim->lm_flags & SLAP_LIMITS_TYPE_MASK;
903 if ( type == SLAP_LIMITS_TYPE_GROUP ) {
904 rc = ptr_APPEND_FMT(( ptr, WHATSLEFT, "group/%s/%s=\"%s\"",
905 lim->lm_group_oc->soc_cname.bv_val,
906 lim->lm_group_ad->ad_cname.bv_val,
907 lim->lm_pat.bv_val ));
909 style = lim->lm_flags & SLAP_LIMITS_MASK;
911 case SLAP_LIMITS_ANONYMOUS:
912 case SLAP_LIMITS_USERS:
913 case SLAP_LIMITS_ANY:
914 rc = ptr_APPEND_BV( lmpats[style] );
916 case SLAP_LIMITS_UNDEFINED:
917 case SLAP_LIMITS_EXACT:
918 case SLAP_LIMITS_ONE:
919 case SLAP_LIMITS_SUBTREE:
920 case SLAP_LIMITS_CHILDREN:
921 case SLAP_LIMITS_REGEX:
922 rc = ptr_APPEND_FMT(( ptr, WHATSLEFT, "dn.%s%s=\"%s\"",
923 type == SLAP_LIMITS_TYPE_SELF ? "" : "this.",
924 lmpats[style].bv_val, lim->lm_pat.bv_val ));
929 bv->bv_len = ptr - bv->bv_val;
932 rc = limits_unparse_one( &lim->lm_limits,
933 SLAP_LIMIT_SIZE | SLAP_LIMIT_TIME,
936 bv->bv_len += btmp.bv_len;
941 /* Caller must provide an adequately sized buffer in bv */
944 struct slap_limits_set *lim,
949 char *ptr, *bufEnd; /* Updated/used by ptr_APPEND_*()/WHATSLEFT */
950 ber_len_t tmpLen; /* Used by ptr_APPEND_FMT*() */
952 if ( !bv || !bv->bv_val ) return -1;
955 bufEnd = ptr + buflen;
957 if ( which & SLAP_LIMIT_SIZE ) {
958 if ( lim->lms_s_soft != SLAPD_DEFAULT_SIZELIMIT ) {
960 /* If same as global limit, drop it */
961 if ( lim != &frontendDB->be_def_limit &&
962 lim->lms_s_soft == frontendDB->be_def_limit.lms_s_soft )
965 /* If there's also a hard limit, fully qualify this one */
966 } else if ( lim->lms_s_hard ) {
967 if ( ptr_APPEND_LIT( " size.soft=" ) ) return -1;
969 /* If doing both size & time, qualify this */
970 } else if ( which & SLAP_LIMIT_TIME ) {
971 if ( ptr_APPEND_LIT( " size=" ) ) return -1;
974 if ( lim->lms_s_soft == -1
975 ? ptr_APPEND_LIT( "unlimited " )
976 : ptr_APPEND_FMT1( "%d ", lim->lms_s_soft ) )
980 if ( lim->lms_s_hard ) {
981 if ( ptr_APPEND_LIT( " size.hard=" ) ) return -1;
982 if ( lim->lms_s_hard == -1
983 ? ptr_APPEND_LIT( "unlimited " )
984 : ptr_APPEND_FMT1( "%d ", lim->lms_s_hard ) )
987 if ( lim->lms_s_unchecked != -1 ) {
988 if ( ptr_APPEND_LIT( " size.unchecked=" ) ) return -1;
989 if ( lim->lms_s_unchecked == 0
990 ? ptr_APPEND_LIT( "disabled " )
991 : ptr_APPEND_FMT1( "%d ", lim->lms_s_unchecked ) )
994 if ( lim->lms_s_pr_hide ) {
995 if ( ptr_APPEND_LIT( " size.pr=noEstimate " ) ) return -1;
997 if ( lim->lms_s_pr ) {
998 if ( ptr_APPEND_LIT( " size.pr=" ) ) return -1;
999 if ( lim->lms_s_pr == -1
1000 ? ptr_APPEND_LIT( "unlimited " )
1001 : ptr_APPEND_FMT1( "%d ", lim->lms_s_pr ) )
1004 if ( lim->lms_s_pr_total ) {
1005 if ( ptr_APPEND_LIT( " size.prtotal=" ) ) return -1;
1006 if ( lim->lms_s_pr_total == -1 ? ptr_APPEND_LIT( "unlimited " )
1007 : lim->lms_s_pr_total == -2 ? ptr_APPEND_LIT( "disabled " )
1008 : ptr_APPEND_FMT1( "%d ", lim->lms_s_pr_total ) )
1013 if ( which & SLAP_LIMIT_TIME ) {
1014 if ( lim->lms_t_soft != SLAPD_DEFAULT_TIMELIMIT ) {
1016 /* If same as global limit, drop it */
1017 if ( lim != &frontendDB->be_def_limit &&
1018 lim->lms_t_soft == frontendDB->be_def_limit.lms_t_soft )
1022 /* If there's also a hard limit, fully qualify this one */
1023 } else if ( lim->lms_t_hard ) {
1024 if ( ptr_APPEND_LIT( " time.soft=" ) ) return -1;
1026 /* If doing both size & time, qualify this */
1027 } else if ( which & SLAP_LIMIT_SIZE ) {
1028 if ( ptr_APPEND_LIT( " time=" ) ) return -1;
1031 if ( lim->lms_t_soft == -1
1032 ? ptr_APPEND_LIT( "unlimited " )
1033 : ptr_APPEND_FMT1( "%d ", lim->lms_t_soft ) )
1037 if ( lim->lms_t_hard ) {
1038 if ( ptr_APPEND_LIT( " time.hard=" ) ) return -1;
1039 if ( lim->lms_t_hard == -1
1040 ? ptr_APPEND_LIT( "unlimited " )
1041 : ptr_APPEND_FMT1( "%d ", lim->lms_t_hard ) )
1045 if ( ptr != bv->bv_val ) {
1048 bv->bv_len = ptr - bv->bv_val;
1055 limits_check( Operation *op, SlapReply *rs )
1057 assert( op != NULL );
1058 assert( rs != NULL );
1059 /* FIXME: should this be always true? */
1060 assert( op->o_tag == LDAP_REQ_SEARCH);
1062 /* protocol only allows 0..maxInt;
1064 * internal searches:
1065 * - may use SLAP_NO_LIMIT ( = -1 ) to indicate no limits;
1066 * - should use slimit = N and tlimit = SLAP_NO_LIMIT to
1067 * indicate searches that should return exactly N matches,
1068 * and handle errors thru a callback (see for instance
1069 * slap_sasl_match() and slap_sasl2dn())
1071 if ( op->ors_tlimit == SLAP_NO_LIMIT && op->ors_slimit == SLAP_NO_LIMIT ) {
1075 /* allow root to set no limit */
1076 if ( be_isroot( op ) ) {
1077 op->ors_limit = NULL;
1079 if ( op->ors_tlimit == 0 ) {
1080 op->ors_tlimit = SLAP_NO_LIMIT;
1083 if ( op->ors_slimit == 0 ) {
1084 op->ors_slimit = SLAP_NO_LIMIT;
1087 /* if paged results and slimit are requested */
1088 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED &&
1089 op->ors_slimit != SLAP_NO_LIMIT ) {
1090 PagedResultsState *ps = op->o_pagedresults_state;
1091 int total = op->ors_slimit - ps->ps_count;
1093 op->ors_slimit = total;
1099 /* if not root, get appropriate limits */
1101 ( void ) limits_get( op, &op->ors_limit );
1103 assert( op->ors_limit != NULL );
1105 /* if no limit is required, use soft limit */
1106 if ( op->ors_tlimit == 0 ) {
1107 op->ors_tlimit = op->ors_limit->lms_t_soft;
1109 /* limit required: check if legal */
1111 if ( op->ors_limit->lms_t_hard == 0 ) {
1112 if ( op->ors_limit->lms_t_soft > 0
1113 && ( op->ors_tlimit > op->ors_limit->lms_t_soft ) ) {
1114 op->ors_tlimit = op->ors_limit->lms_t_soft;
1117 } else if ( op->ors_limit->lms_t_hard > 0 ) {
1118 #ifdef ABOVE_HARD_LIMIT_IS_ERROR
1119 if ( op->ors_tlimit == SLAP_MAX_LIMIT ) {
1120 op->ors_tlimit = op->ors_limit->lms_t_hard;
1122 } else if ( op->ors_tlimit > op->ors_limit->lms_t_hard ) {
1123 /* error if exceeding hard limit */
1124 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1125 send_ldap_result( op, rs );
1126 rs->sr_err = LDAP_SUCCESS;
1129 #else /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1130 if ( op->ors_tlimit > op->ors_limit->lms_t_hard ) {
1131 op->ors_tlimit = op->ors_limit->lms_t_hard;
1133 #endif /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1137 /* else leave as is */
1139 /* don't even get to backend if candidate check is disabled */
1140 if ( op->ors_limit->lms_s_unchecked == 0 ) {
1141 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1142 send_ldap_result( op, rs );
1143 rs->sr_err = LDAP_SUCCESS;
1147 /* if paged results is requested */
1148 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
1151 PagedResultsState *ps = op->o_pagedresults_state;
1153 /* paged results is not allowed */
1154 if ( op->ors_limit->lms_s_pr_total == -2 ) {
1155 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1156 rs->sr_text = "pagedResults control not allowed";
1157 send_ldap_result( op, rs );
1158 rs->sr_err = LDAP_SUCCESS;
1163 if ( op->ors_limit->lms_s_pr > 0
1164 && ps->ps_size > op->ors_limit->lms_s_pr )
1166 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1167 rs->sr_text = "illegal pagedResults page size";
1168 send_ldap_result( op, rs );
1169 rs->sr_err = LDAP_SUCCESS;
1174 if ( op->ors_limit->lms_s_pr_total == 0 ) {
1175 if ( op->ors_limit->lms_s_hard == 0 ) {
1176 pr_total = op->ors_limit->lms_s_soft;
1178 pr_total = op->ors_limit->lms_s_hard;
1181 pr_total = op->ors_limit->lms_s_pr_total;
1184 if ( pr_total == -1 ) {
1185 if ( op->ors_slimit == 0 || op->ors_slimit == SLAP_MAX_LIMIT ) {
1189 slimit = op->ors_slimit - ps->ps_count;
1192 #ifdef ABOVE_HARD_LIMIT_IS_ERROR
1193 } else if ( pr_total > 0 && op->ors_slimit != SLAP_MAX_LIMIT
1194 && ( op->ors_slimit == SLAP_NO_LIMIT
1195 || op->ors_slimit > pr_total ) )
1197 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1198 send_ldap_result( op, rs );
1199 rs->sr_err = LDAP_SUCCESS;
1201 #endif /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1204 /* if no limit is required, use soft limit */
1208 /* first round of pagedResults:
1209 * set count to any appropriate limit */
1211 /* if the limit is set, check that it does
1212 * not violate any server-side limit */
1213 #ifdef ABOVE_HARD_LIMIT_IS_ERROR
1214 if ( op->ors_slimit == SLAP_MAX_LIMIT )
1215 #else /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1216 if ( op->ors_slimit == SLAP_MAX_LIMIT
1217 || op->ors_slimit > pr_total )
1218 #endif /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1220 slimit2 = op->ors_slimit = pr_total;
1222 } else if ( op->ors_slimit == 0 ) {
1226 slimit2 = op->ors_slimit;
1229 total = slimit2 - ps->ps_count;
1232 if ( op->ors_limit->lms_s_pr > 0 ) {
1233 /* use the smallest limit set by total/per page */
1234 if ( total < op->ors_limit->lms_s_pr ) {
1238 /* use the perpage limit if any
1239 * NOTE: + 1 because given value must be legal */
1240 slimit = op->ors_limit->lms_s_pr + 1;
1244 /* use the total limit if any */
1248 } else if ( op->ors_limit->lms_s_pr > 0 ) {
1249 /* use the perpage limit if any
1250 * NOTE: + 1 because the given value must be legal */
1251 slimit = op->ors_limit->lms_s_pr + 1;
1254 /* use the standard hard/soft limit if any */
1255 slimit = op->ors_limit->lms_s_hard;
1259 /* if got any limit, use it */
1260 if ( slimit != -2 ) {
1261 if ( op->ors_slimit == 0 ) {
1262 op->ors_slimit = slimit;
1264 } else if ( slimit > 0 ) {
1265 if ( op->ors_slimit - ps->ps_count > slimit ) {
1266 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1267 send_ldap_result( op, rs );
1268 rs->sr_err = LDAP_SUCCESS;
1271 op->ors_slimit = slimit;
1273 } else if ( slimit == 0 ) {
1278 /* use the standard hard/soft limit if any */
1279 op->ors_slimit = pr_total;
1282 /* no limit requested: use soft, whatever it is */
1283 } else if ( op->ors_slimit == 0 ) {
1284 op->ors_slimit = op->ors_limit->lms_s_soft;
1286 /* limit requested: check if legal */
1288 /* hard limit as soft (traditional behavior) */
1289 if ( op->ors_limit->lms_s_hard == 0 ) {
1290 if ( op->ors_limit->lms_s_soft > 0
1291 && op->ors_slimit > op->ors_limit->lms_s_soft ) {
1292 op->ors_slimit = op->ors_limit->lms_s_soft;
1295 /* explicit hard limit: error if violated */
1296 } else if ( op->ors_limit->lms_s_hard > 0 ) {
1297 #ifdef ABOVE_HARD_LIMIT_IS_ERROR
1298 if ( op->ors_slimit == SLAP_MAX_LIMIT ) {
1299 op->ors_slimit = op->ors_limit->lms_s_hard;
1301 } else if ( op->ors_slimit > op->ors_limit->lms_s_hard ) {
1302 /* if limit exceeds hard, error */
1303 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1304 send_ldap_result( op, rs );
1305 rs->sr_err = LDAP_SUCCESS;
1308 #else /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1309 if ( op->ors_slimit > op->ors_limit->lms_s_hard ) {
1310 op->ors_slimit = op->ors_limit->lms_s_hard;
1312 #endif /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1316 /* else leave as is */
1324 struct slap_limits **lm )
1332 for ( i = 0; lm[ i ]; i++ ) {
1333 switch ( lm[ i ]->lm_flags & SLAP_LIMITS_MASK ) {
1334 case SLAP_LIMITS_REGEX:
1335 regfree( &lm[ i ]->lm_regex );
1338 case SLAP_LIMITS_EXACT:
1339 case SLAP_LIMITS_ONE:
1340 case SLAP_LIMITS_SUBTREE:
1341 case SLAP_LIMITS_CHILDREN:
1342 if ( !BER_BVISNULL( &lm[ i ]->lm_pat ) ) {
1343 ch_free( lm[ i ]->lm_pat.bv_val );