]> git.sur5r.net Git - openldap/blob - servers/slapd/limits.c
Sync with HEAD
[openldap] / servers / slapd / limits.c
1 /* limits.c - routines to handle regex-based size and time limits */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/regex.h>
22 #include <ac/string.h>
23
24 #include "slap.h"
25 #include "lutil.h"
26
27 /* define to get an error if requesting limit higher than hard */
28 #undef ABOVE_HARD_LIMIT_IS_ERROR
29
30 static char *
31 limits2str( unsigned i )
32 {
33         switch ( i ) {
34         case SLAP_LIMITS_UNDEFINED:
35                 return "UNDEFINED";
36
37         case SLAP_LIMITS_EXACT:
38                 return "EXACT";
39                         
40         case SLAP_LIMITS_ONE:
41                 return "ONELEVEL";      
42
43         case SLAP_LIMITS_SUBTREE:
44                 return "SUBTREE";
45
46         case SLAP_LIMITS_CHILDREN:
47                 return "CHILDREN";
48
49         case SLAP_LIMITS_REGEX:
50                 return "REGEX";
51
52         case SLAP_LIMITS_ANONYMOUS:
53                 return "ANONYMOUS";
54                 
55         case SLAP_LIMITS_USERS:
56                 return "USERS";
57                 
58         case SLAP_LIMITS_ANY:
59                 return "ANY";
60
61         default:
62                 return "UNKNOWN";
63         }
64 }
65
66 int
67 limits_get( 
68         Operation               *op,
69         struct berval           *ndn, 
70         struct slap_limits_set  **limit
71 )
72 {
73         struct slap_limits **lm;
74
75         assert( op != NULL );
76         assert( limit != NULL );
77
78         Debug( LDAP_DEBUG_TRACE, "==> limits_get: %s dn=\"%s\"\n",
79                         op->o_log_prefix,
80                         BER_BVISNULL( ndn ) ? "[anonymous]" : ndn->bv_val, 0 );
81         /*
82          * default values
83          */
84         *limit = &op->o_bd->be_def_limit;
85
86         if ( op->o_bd->be_limits == NULL ) {
87                 return( 0 );
88         }
89
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;
93
94                 switch ( style ) {
95                 case SLAP_LIMITS_EXACT:
96                         if ( BER_BVISEMPTY( ndn ) ) {
97                                 break;
98                         }
99
100                         if ( type == SLAP_LIMITS_TYPE_GROUP ) {
101                                 int     rc;
102
103                                 rc = backend_group( op, NULL,
104                                                 &lm[0]->lm_pat, ndn,
105                                                 lm[0]->lm_group_oc,
106                                                 lm[0]->lm_group_ad );
107                                 if ( rc == 0 ) {
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 );
114
115                                         return( 0 );
116                                 }
117                         } else {
118                         
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 );
123                                         return( 0 );
124                                 }
125                         }
126                         break;
127
128                 case SLAP_LIMITS_ONE:
129                 case SLAP_LIMITS_SUBTREE:
130                 case SLAP_LIMITS_CHILDREN: {
131                         size_t d;
132                         
133                         if ( BER_BVISEMPTY( ndn ) ) {
134                                 break;
135                         }
136
137                         /* ndn shorter than dn_pat */
138                         if ( ndn->bv_len < lm[0]->lm_pat.bv_len ) {
139                                 break;
140                         }
141                         d = ndn->bv_len - lm[0]->lm_pat.bv_len;
142
143                         /* allow exact match for SUBTREE only */
144                         if ( d == 0 ) {
145                                 if ( style != SLAP_LIMITS_SUBTREE ) {
146                                         break;
147                                 }
148                         } else {
149                                 /* check for unescaped rdn separator */
150                                 if ( !DN_SEPARATOR( ndn->bv_val[d - 1] ) ) {
151                                         break;
152                                 }
153                         }
154
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 )
159                         {
160                                 /* check for exactly one rdn in case of ONE */
161                                 if ( style == SLAP_LIMITS_ONE ) {
162                                         /*
163                                          * if ndn is more that one rdn
164                                          * below dn_pat, continue
165                                          */
166                                         if ( (size_t) dn_rdnlen( NULL, ndn )
167                                                         != d - 1 )
168                                         {
169                                                 break;
170                                         }
171                                 }
172
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 );
176                                 return( 0 );
177                         }
178
179                         break;
180                 }
181
182                 case SLAP_LIMITS_REGEX:
183                         if ( BER_BVISEMPTY( ndn ) ) {
184                                 break;
185                         }
186                         if ( regexec( &lm[0]->lm_regex, ndn->bv_val,
187                                                 0, NULL, 0 ) == 0 )
188                         {
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 );
192                                 return( 0 );
193                         }
194                         break;
195
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;
201                                 return( 0 );
202                         }
203                         break;
204
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 );
210                                 return( 0 );
211                         }
212                         break;
213
214                 case SLAP_LIMITS_ANY:
215                         *limit = &lm[0]->lm_limits;
216                         return( 0 );
217
218                 default:
219                         assert( 0 );    /* unreachable */
220                         return( -1 );
221                 }
222         }
223
224         return( 0 );
225 }
226
227 static int
228 limits_add(
229         Backend                 *be,
230         unsigned                flags,
231         const char              *pattern,
232         ObjectClass             *group_oc,
233         AttributeDescription    *group_ad,
234         struct slap_limits_set  *limit
235 )
236 {
237         int                     i;
238         struct slap_limits      *lm;
239         unsigned                type, style;
240         
241         assert( be != NULL );
242         assert( limit != NULL );
243
244         type = flags & SLAP_LIMITS_TYPE_MASK;
245         style = flags & SLAP_LIMITS_MASK;
246
247         switch ( style ) {
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 ) {
253                                 return( -1 );
254                         }
255                 }
256                 break;
257         }
258
259
260         lm = ( struct slap_limits * )ch_calloc( sizeof( struct slap_limits ), 1 );
261
262         switch ( style ) {
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;
271                 {
272                         int rc;
273                         struct berval bv;
274
275                         ber_str2bv( pattern, 0, 0, &bv );
276
277                         rc = dnNormalize( 0, NULL, NULL, &bv, &lm->lm_pat, NULL );
278                         if ( rc != LDAP_SUCCESS ) {
279                                 ch_free( lm );
280                                 return( -1 );
281                         }
282                 }
283                 break;
284                 
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 );
291                         ch_free( lm );
292                         return( -1 );
293                 }
294                 break;
295
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 );
301                 break;
302         }
303
304         switch ( type ) {
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;
310                 break;
311         }
312
313         lm->lm_limits = *limit;
314
315         i = 0;
316         if ( be->be_limits != NULL ) {
317                 for ( ; be->be_limits[i]; i++ );
318         }
319
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;
324         
325         return( 0 );
326 }
327
328 int
329 limits_parse(
330         Backend     *be,
331         const char  *fname,
332         int         lineno,
333         int         argc,
334         char        **argv
335 )
336 {
337         int                     flags = SLAP_LIMITS_UNDEFINED;
338         char                    *pattern;
339         struct slap_limits_set  limit;
340         int                     i, rc = 0;
341         ObjectClass             *group_oc = NULL;
342         AttributeDescription    *group_ad = NULL;
343
344         assert( be != NULL );
345
346         if ( argc < 3 ) {
347                 Debug( LDAP_DEBUG_ANY,
348                         "%s : line %d: missing arg(s) in "
349                         "\"limits <pattern> <limits>\" line.\n%s",
350                         fname, lineno, "" );
351                 return( -1 );
352         }
353
354         limit = be->be_def_limit;
355
356         /*
357          * syntax:
358          *
359          * "limits" <pattern> <limit> [ ... ]
360          * 
361          * 
362          * <pattern>:
363          * 
364          * "anonymous"
365          * "users"
366          * [ "dn" [ "." { "exact" | "base" | "onelevel" | "subtree" | children"
367          *      | "regex" | "anonymous" } ] "=" ] <dn pattern>
368          *
369          * Note:
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
374          *      
375          *      "anonymous" may be deprecated in favour 
376          *      of the pattern = "anonymous" form
377          *
378          * "group[/objectClass[/attributeType]]" "=" "<dn pattern>"
379          *
380          * <limit>:
381          *
382          * "time" [ "." { "soft" | "hard" } ] "=" <integer>
383          *
384          * "size" [ "." { "soft" | "hard" | "unchecked" } ] "=" <integer>
385          */
386         
387         pattern = argv[1];
388         if ( strcmp( pattern, "*" ) == 0) {
389                 flags = SLAP_LIMITS_ANY;
390
391         } else if ( strcasecmp( pattern, "anonymous" ) == 0 ) {
392                 flags = SLAP_LIMITS_ANONYMOUS;
393
394         } else if ( strcasecmp( pattern, "users" ) == 0 ) {
395                 flags = SLAP_LIMITS_USERS;
396                 
397         } else if ( strncasecmp( pattern, "dn", STRLENOF( "dn" ) ) == 0 ) {
398                 pattern += STRLENOF( "dn" );
399                 if ( pattern[0] == '.' ) {
400                         pattern++;
401                         if ( strncasecmp( pattern, "exact", STRLENOF( "exact" )) == 0 ) {
402                                 flags = SLAP_LIMITS_EXACT;
403                                 pattern += STRLENOF( "exact" );
404
405                         } else if ( strncasecmp( pattern, "base", STRLENOF( "base" ) ) == 0 ) {
406                                 flags = SLAP_LIMITS_BASE;
407                                 pattern += STRLENOF( "base" );
408
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" );
414
415                                 } else {
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 );
420                                 }
421
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" );
427
428                                 } else {
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 );
433                                 }
434
435                         } else if ( strncasecmp( pattern, "children", STRLENOF( "children" ) ) == 0 ) {
436                                 flags = SLAP_LIMITS_CHILDREN;
437                                 pattern += STRLENOF( "children" );
438
439                         } else if ( strncasecmp( pattern, "regex", STRLENOF( "regex" ) ) == 0 ) {
440                                 flags = SLAP_LIMITS_REGEX;
441                                 pattern += STRLENOF( "regex" );
442
443                         /* 
444                          * this could be deprecated in favour
445                          * of the pattern = "anonymous" form
446                          */
447                         } else if ( strncasecmp( pattern, "anonymous", STRLENOF( "anonymous" ) ) == 0 ) {
448                                 flags = SLAP_LIMITS_ANONYMOUS;
449                                 pattern = NULL;
450                         }
451                 }
452
453                 /* pre-check the data */
454                 switch ( flags ) {
455                 case SLAP_LIMITS_ANONYMOUS:
456                 case SLAP_LIMITS_USERS:
457
458                         /* no need for pattern */
459                         pattern = NULL;
460                         break;
461
462                 default:
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}]"
468                                         "=<pattern>\" in "
469                                         "\"limits <pattern> <limits>\" "
470                                         "line.\n%s",
471                                         fname, lineno, "" );
472                                 return( -1 );
473                         }
474
475                         /* skip '=' (required) */
476                         pattern++;
477
478                         /* trim obvious cases */
479                         if ( strcmp( pattern, "*" ) == 0 ) {
480                                 flags = SLAP_LIMITS_ANY;
481                                 pattern = NULL;
482
483                         } else if ( flags == SLAP_LIMITS_REGEX
484                                         && strcmp( pattern, ".*" ) == 0 ) {
485                                 flags = SLAP_LIMITS_ANY;
486                                 pattern = NULL;
487                         }
488                 }
489
490         } else if (strncasecmp( pattern, "group", STRLENOF( "group" ) ) == 0 ) {
491                 pattern += STRLENOF( "group" );
492
493                 if ( pattern[0] == '/' ) {
494                         struct berval   oc, ad;
495
496                         oc.bv_val = pattern + 1;
497                         pattern = strchr( pattern, '=' );
498                         if ( pattern == NULL ) {
499                                 return -1;
500                         }
501
502                         ad.bv_val = strchr( oc.bv_val, '/' );
503                         if ( ad.bv_val != NULL ) {
504                                 const char      *text = NULL;
505                                 int             rc;
506
507                                 oc.bv_len = ad.bv_val - oc.bv_val;
508
509                                 ad.bv_val++;
510                                 ad.bv_len = pattern - ad.bv_val;
511                                 rc = slap_bv2ad( &ad, &group_ad, &text );
512                                 if ( rc != LDAP_SUCCESS ) {
513                                         goto no_ad;
514                                 }
515
516                         } else {
517                                 oc.bv_len = pattern - oc.bv_val;
518                         }
519
520                         group_oc = oc_bvfind( &oc );
521                         if ( group_oc == NULL ) {
522                                 goto no_oc;
523                         }
524                 }
525
526                 if ( group_oc == NULL ) {
527                         group_oc = oc_find( SLAPD_GROUP_CLASS );
528                         if ( group_oc == NULL ) {
529 no_oc:;
530                                 return( -1 );
531                         }
532                 }
533
534                 if ( group_ad == NULL ) {
535                         const char      *text = NULL;
536                         int             rc;
537                         
538                         rc = slap_str2ad( SLAPD_GROUP_ATTR, &group_ad, &text );
539
540                         if ( rc != LDAP_SUCCESS ) {
541 no_ad:;
542                                 return( -1 );
543                         }
544                 }
545
546                 flags = SLAP_LIMITS_TYPE_GROUP | SLAP_LIMITS_EXACT;
547
548                 if ( pattern[0] != '=' ) {
549                         Debug( LDAP_DEBUG_ANY,
550                                 "%s : line %d: missing '=' in "
551                                 "\"group[/objectClass[/attributeType]]"
552                                 "=<pattern>\" in "
553                                 "\"limits <pattern> <limits>\" line.\n",
554                                 fname, lineno, 0 );
555                         return( -1 );
556                 }
557
558                 /* skip '=' (required) */
559                 pattern++;
560         }
561
562         /* get the limits */
563         for ( i = 2; i < argc; i++ ) {
564                 if ( limits_parse_one( argv[i], &limit ) ) {
565
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] );
570
571                         return( 1 );
572                 }
573         }
574
575         /*
576          * sanity checks ...
577          *
578          * FIXME: add warnings?
579          */
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;
584         }
585         
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;
590         }
591
592         /*
593          * defaults ...
594          * 
595          * lms_t_hard:
596          *      -1      => no limits
597          *      0       => same as soft
598          *      > 0     => limit (in seconds)
599          *
600          * lms_s_hard:
601          *      -1      => no limits
602          *      0       0> same as soft
603          *      > 0     => limit (in entries)
604          *
605          * lms_s_pr_total:
606          *      -2      => disable the control
607          *      -1      => no limits
608          *      0       => same as soft
609          *      > 0     => limit (in entries)
610          *
611          * lms_s_pr:
612          *      -1      => no limits
613          *      0       => no limits?
614          *      > 0     => limit size (in entries)
615          */
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;
619         }
620
621         rc = limits_add( be, flags, pattern, group_oc, group_ad, &limit );
622         if ( rc ) {
623
624                 Debug( LDAP_DEBUG_ANY,
625                         "%s : line %d: unable to add limit in "
626                         "\"limits <pattern> <limits>\" line.\n",
627                 fname, lineno, 0 );
628         }
629
630         return( rc );
631 }
632
633 int
634 limits_parse_one(
635         const char              *arg,
636         struct slap_limits_set  *limit
637 )
638 {
639         assert( arg != NULL );
640         assert( limit != NULL );
641
642         if ( strncasecmp( arg, "time", STRLENOF( "time" ) ) == 0 ) {
643                 arg += STRLENOF( "time" );
644
645                 if ( arg[0] == '.' ) {
646                         arg++;
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;
651
652                                 } else {
653                                         char    *next = NULL;
654                                         int     soft = strtol( arg, &next, 10 );
655
656                                         if ( next == arg || next[ 0 ] != '\0' ) {
657                                                 return( 1 );
658                                         }
659
660                                         if ( soft < -1 ) {
661                                                 return( 1 );
662                                         }
663
664                                         if ( soft == -1 ) {
665                                                 /* FIXME: use "unlimited" instead; issue warning? */
666                                         }
667
668                                         limit->lms_t_soft = soft;
669                                 }
670                                 
671                         } else if ( strncasecmp( arg, "hard=", STRLENOF( "hard=" ) ) == 0 ) {
672                                 arg += STRLENOF( "hard=" );
673                                 if ( strcasecmp( arg, "soft" ) == 0 ) {
674                                         limit->lms_t_hard = 0;
675
676                                 } else if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
677                                         limit->lms_t_hard = -1;
678
679                                 } else {
680                                         char    *next = NULL;
681                                         int     hard = strtol( arg, &next, 10 );
682
683                                         if ( next == arg || next[ 0 ] != '\0' ) {
684                                                 return( 1 );
685                                         }
686
687                                         if ( hard < -1 ) {
688                                                 return( 1 );
689                                         }
690
691                                         if ( hard == -1 ) {
692                                                 /* FIXME: use "unlimited" instead */
693                                         }
694
695                                         if ( hard == 0 ) {
696                                                 /* FIXME: use "soft" instead */
697                                         }
698
699                                         limit->lms_t_hard = hard;
700                                 }
701                                 
702                         } else {
703                                 return( 1 );
704                         }
705                         
706                 } else if ( arg[0] == '=' ) {
707                         arg++;
708                         if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
709                                 limit->lms_t_soft = -1;
710
711                         } else {
712                                 char    *next = NULL;
713
714                                 limit->lms_t_soft = strtol( arg, &next, 10 );
715                                 if ( next == arg || limit->lms_t_soft < -1 ) {
716                                         return( 1 );
717                                 }
718                         }
719                         limit->lms_t_hard = 0;
720                         
721                 } else {
722                         return( 1 );
723                 }
724
725         } else if ( strncasecmp( arg, "size", STRLENOF( "size" ) ) == 0 ) {
726                 arg += STRLENOF( "size" );
727                 
728                 if ( arg[0] == '.' ) {
729                         arg++;
730                         if ( strncasecmp( arg, "soft=", STRLENOF( "soft=" ) ) == 0 ) {
731                                 arg += STRLENOF( "soft=" );
732                                 if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
733                                         limit->lms_s_soft = -1;
734
735                                 } else {
736                                         char    *next = NULL;
737                                         int     soft = strtol( arg, &next, 10 );
738
739                                         if ( next == arg || next[ 0 ] != '\0' ) {
740                                                 return( 1 );
741                                         }
742
743                                         if ( soft < -1 ) {
744                                                 return( 1 );
745                                         }
746
747                                         if ( soft == -1 ) {
748                                                 /* FIXME: use "unlimited" instead */
749                                         }
750
751                                         limit->lms_s_soft = soft;
752                                 }
753                                 
754                         } else if ( strncasecmp( arg, "hard=", STRLENOF( "hard=" ) ) == 0 ) {
755                                 arg += STRLENOF( "hard=" );
756                                 if ( strcasecmp( arg, "soft" ) == 0 ) {
757                                         limit->lms_s_hard = 0;
758
759                                 } else if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
760                                         limit->lms_s_hard = -1;
761
762                                 } else {
763                                         char    *next = NULL;
764                                         int     hard = strtol( arg, &next, 10 );
765
766                                         if ( next == arg || next[ 0 ] != '\0' ) {
767                                                 return( 1 );
768                                         }
769
770                                         if ( hard < -1 ) {
771                                                 return( 1 );
772                                         }
773
774                                         if ( hard == -1 ) {
775                                                 /* FIXME: use "unlimited" instead */
776                                         }
777
778                                         if ( hard == 0 ) {
779                                                 /* FIXME: use "soft" instead */
780                                         }
781
782                                         limit->lms_s_hard = hard;
783                                 }
784                                 
785                         } else if ( strncasecmp( arg, "unchecked=", STRLENOF( "unchecked=" ) ) == 0 ) {
786                                 arg += STRLENOF( "unchecked=" );
787                                 if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
788                                         limit->lms_s_unchecked = -1;
789
790                                 } else if ( strcasecmp( arg, "disabled" ) == 0 ) {
791                                         limit->lms_s_unchecked = 0;
792
793                                 } else {
794                                         char    *next = NULL;
795                                         int     unchecked = strtol( arg, &next, 10 );
796
797                                         if ( next == arg || next[ 0 ] != '\0' ) {
798                                                 return( 1 );
799                                         }
800
801                                         if ( unchecked < -1 ) {
802                                                 return( 1 );
803                                         }
804
805                                         if ( unchecked == -1 ) {
806                                                 /*  FIXME: use "unlimited" instead */
807                                         }
808
809                                         limit->lms_s_unchecked = unchecked;
810                                 }
811
812                         } else if ( strncasecmp( arg, "pr=", STRLENOF( "pr=" ) ) == 0 ) {
813                                 arg += STRLENOF( "pr=" );
814                                 if ( strcasecmp( arg, "noEstimate" ) == 0 ) {
815                                         limit->lms_s_pr_hide = 1;
816
817                                 } else if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
818                                         limit->lms_s_pr = -1;
819
820                                 } else {
821                                         char    *next = NULL;
822                                         int     pr = strtol( arg, &next, 10 );
823
824                                         if ( next == arg || next[ 0 ] != '\0' ) {
825                                                 return( 1 );
826                                         }
827
828                                         if ( pr < -1 ) {
829                                                 return( 1 );
830                                         }
831
832                                         if ( pr == -1 ) {
833                                                 /* FIXME: use "unlimited" instead */
834                                         }
835
836                                         limit->lms_s_pr = pr;
837                                 }
838
839                         } else if ( strncasecmp( arg, "prtotal=", STRLENOF( "prtotal=" ) ) == 0 ) {
840                                 arg += STRLENOF( "prtotal=" );
841
842                                 if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
843                                         limit->lms_s_pr_total = -1;
844
845                                 } else if ( strcasecmp( arg, "disabled" ) == 0 ) {
846                                         limit->lms_s_pr_total = -2;
847
848                                 } else if ( strcasecmp( arg, "hard" ) == 0 ) {
849                                         limit->lms_s_pr_total = 0;
850
851                                 } else {
852                                         char    *next = NULL;
853                                         int     total;
854
855                                         total = strtol( arg, &next, 10 );
856                                         if ( next == arg || next[ 0 ] != '\0' ) {
857                                                 return( 1 );
858                                         }
859
860                                         if ( total < -1 ) {
861                                                 return( 1 );
862                                         }
863
864                                         if ( total == -1 ) {
865                                                 /* FIXME: use "unlimited" instead */
866                                         }
867
868                                         if ( total == 0 ) {
869                                                 /* FIXME: use "pr=disable" instead */
870                                         }
871
872                                         limit->lms_s_pr_total = total;
873                                 }
874
875                         } else {
876                                 return( 1 );
877                         }
878                         
879                 } else if ( arg[0] == '=' ) {
880                         arg++;
881                         if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) {
882                                 limit->lms_s_soft = -1;
883
884                         } else {
885                                 char    *next = NULL;
886
887                                 limit->lms_s_soft = strtol( arg, &next, 10 );
888                                 if ( next == arg || limit->lms_s_soft < -1 ) {
889                                         return( 1 );
890                                 }
891                         }
892                         limit->lms_s_hard = 0;
893                         
894                 } else {
895                         return( 1 );
896                 }
897         }
898
899         return 0;
900 }
901
902 static const char *lmpats[] = {
903         "base",
904         "base",
905         "onelevel",
906         "subtree",
907         "children",
908         "regex",
909         "anonymous",
910         "users",
911         "*"
912 };
913
914 /* Caller must provide an adequately sized buffer in bv */
915 void
916 limits_unparse( struct slap_limits *lim, struct berval *bv )
917 {
918         struct berval btmp;
919         char *ptr;
920         int lm;
921
922         if ( !bv || !bv->bv_val ) return;
923
924         ptr = bv->bv_val;
925
926         if (( lim->lm_flags & SLAP_LIMITS_TYPE_MASK ) == SLAP_LIMITS_TYPE_GROUP ) {
927                 ptr = lutil_strcopy( ptr, "group/" );
928                 ptr = lutil_strcopy( ptr, lim->lm_group_oc->soc_cname.bv_val );
929                 *ptr++ = '/';
930                 ptr = lutil_strcopy( ptr, lim->lm_group_ad->ad_cname.bv_val );
931                 ptr = lutil_strcopy( ptr, "=\"" );
932                 ptr = lutil_strcopy( ptr, lim->lm_pat.bv_val );
933                 *ptr++ = '"';
934         } else {
935                 lm = lim->lm_flags & SLAP_LIMITS_MASK;
936                 switch( lm ) {
937                 case SLAP_LIMITS_ANONYMOUS:
938                 case SLAP_LIMITS_USERS:
939                 case SLAP_LIMITS_ANY:
940                         ptr = lutil_strcopy( ptr, lmpats[lm] );
941                         break;
942                 case SLAP_LIMITS_UNDEFINED:
943                 case SLAP_LIMITS_EXACT:
944                 case SLAP_LIMITS_ONE:
945                 case SLAP_LIMITS_SUBTREE:
946                 case SLAP_LIMITS_CHILDREN:
947                 case SLAP_LIMITS_REGEX:
948                         ptr = lutil_strcopy( ptr, "dn." );
949                         ptr = lutil_strcopy( ptr, lmpats[lm] );
950                         *ptr++ = '=';
951                         *ptr++ = '"';
952                         ptr = lutil_strcopy( ptr, lim->lm_pat.bv_val );
953                         *ptr++ = '"';
954                         break;
955                 }
956         }
957         *ptr++ = ' ';
958         bv->bv_len = ptr - bv->bv_val;
959         btmp.bv_val = ptr;
960         btmp.bv_len = 0;
961         limits_unparse_one( &lim->lm_limits, SLAP_LIMIT_SIZE|SLAP_LIMIT_TIME, &btmp );
962         bv->bv_len += btmp.bv_len;
963 }
964
965 /* Caller must provide an adequately sized buffer in bv */
966 void
967 limits_unparse_one( struct slap_limits_set *lim, int which, struct berval *bv )
968 {
969         char *ptr;
970
971         if ( !bv || !bv->bv_val ) return;
972
973         ptr = bv->bv_val;
974
975         if ( which & SLAP_LIMIT_SIZE ) {
976                 if ( lim->lms_s_soft != SLAPD_DEFAULT_SIZELIMIT ) {
977
978                         /* If same as global limit, drop it */
979                         if ( lim != &frontendDB->be_def_limit &&
980                                 lim->lms_s_soft == frontendDB->be_def_limit.lms_s_soft )
981                                 goto s_hard;
982                         /* If there's also a hard limit, fully qualify this one */
983                         else if ( lim->lms_s_hard )
984                                 ptr = lutil_strcopy( ptr, " size.soft=" );
985
986                         /* If doing both size & time, qualify this */
987                         else if ( which & SLAP_LIMIT_TIME )
988                                 ptr = lutil_strcopy( ptr, " size=" );
989
990                         if ( lim->lms_s_soft == -1 )
991                                 ptr = lutil_strcopy( ptr, "unlimited" );
992                         else
993                                 ptr += sprintf( ptr, "%d", lim->lms_s_soft );
994                         *ptr++ = ' ';
995                 }
996 s_hard:
997                 if ( lim->lms_s_hard ) {
998                         ptr = lutil_strcopy( ptr, " size.hard=" );
999                         if ( lim->lms_s_hard == -1 )
1000                                 ptr = lutil_strcopy( ptr, "unlimited" );
1001                         else
1002                                 ptr += sprintf( ptr, "%d", lim->lms_s_hard );
1003                         *ptr++ = ' ';
1004                 }
1005                 if ( lim->lms_s_unchecked != -1 ) {
1006                         ptr = lutil_strcopy( ptr, " size.unchecked=" );
1007                         if ( lim->lms_s_unchecked == 0 )
1008                                 ptr = lutil_strcopy( ptr, "disabled" );
1009                         else
1010                                 ptr += sprintf( ptr, "%d", lim->lms_s_unchecked );
1011                         *ptr++ = ' ';
1012                 }
1013                 if ( lim->lms_s_pr_hide ) {
1014                         ptr = lutil_strcopy( ptr, " size.pr=noEstimate " );
1015                 }
1016                 if ( lim->lms_s_pr ) {
1017                         ptr = lutil_strcopy( ptr, " size.pr=" );
1018                         if ( lim->lms_s_pr == -1 )
1019                                 ptr = lutil_strcopy( ptr, "unlimited" );
1020                         else
1021                                 ptr += sprintf( ptr, "%d", lim->lms_s_pr );
1022                         *ptr++ = ' ';
1023                 }
1024                 if ( lim->lms_s_pr_total ) {
1025                         ptr = lutil_strcopy( ptr, " size.prtotal=" );
1026                         if ( lim->lms_s_pr_total == -1 )
1027                                 ptr = lutil_strcopy( ptr, "unlimited" );
1028                         else if ( lim->lms_s_pr_total == -2 )
1029                                 ptr = lutil_strcopy( ptr, "disabled" );
1030                         else 
1031                                 ptr += sprintf( ptr, "%d", lim->lms_s_pr_total );
1032                         *ptr++ = ' ';
1033                 }
1034         }
1035         if ( which & SLAP_LIMIT_TIME ) {
1036                 if ( lim->lms_t_soft != SLAPD_DEFAULT_TIMELIMIT ) {
1037
1038                         /* If same as global limit, drop it */
1039                         if ( lim != &frontendDB->be_def_limit &&
1040                                 lim->lms_t_soft == frontendDB->be_def_limit.lms_t_soft )
1041                                 goto t_hard;
1042
1043                         /* If there's also a hard limit, fully qualify this one */
1044                         else if ( lim->lms_t_hard ) 
1045                                 ptr = lutil_strcopy( ptr, " time.soft=" );
1046
1047                         /* If doing both size & time, qualify this */
1048                         else if ( which & SLAP_LIMIT_SIZE )
1049                                 ptr = lutil_strcopy( ptr, " time=" );
1050
1051                         if ( lim->lms_t_soft == -1 )
1052                                 ptr = lutil_strcopy( ptr, "unlimited" );
1053                         else
1054                                 ptr += sprintf( ptr, "%d", lim->lms_t_soft );
1055                         *ptr++ = ' ';
1056                 }
1057 t_hard:
1058                 if ( lim->lms_t_hard ) {
1059                         ptr = lutil_strcopy( ptr, " time.hard=" );
1060                         if ( lim->lms_t_hard == -1 )
1061                                 ptr = lutil_strcopy( ptr, "unlimited" );
1062                         else
1063                                 ptr += sprintf( ptr, "%d", lim->lms_t_hard );
1064                         *ptr++ = ' ';
1065                 }
1066         }
1067         if ( ptr != bv->bv_val ) {
1068                 ptr--;
1069                 *ptr = '\0';
1070                 bv->bv_len = ptr - bv->bv_val;
1071         }
1072 }
1073
1074 int
1075 limits_check( Operation *op, SlapReply *rs )
1076 {
1077         assert( op != NULL );
1078         assert( rs != NULL );
1079         /* FIXME: should this be always true? */
1080         assert( op->o_tag == LDAP_REQ_SEARCH);
1081
1082         /* protocol only allows 0..maxInt;
1083          *
1084          * internal searches:
1085          * - may use SLAP_NO_LIMIT ( = -1 ) to indicate no limits;
1086          * - should use slimit = N and tlimit = SLAP_NO_LIMIT to
1087          *   indicate searches that should return exactly N matches,
1088          *   and handle errors thru a callback (see for instance
1089          *   slap_sasl_match() and slap_sasl2dn())
1090          */
1091         if ( op->ors_tlimit == SLAP_NO_LIMIT && op->ors_slimit == SLAP_NO_LIMIT ) {
1092                 return 0;
1093         }
1094
1095         /* allow root to set no limit */
1096         if ( be_isroot( op ) ) {
1097                 op->ors_limit = NULL;
1098
1099                 if ( op->ors_tlimit == 0 ) {
1100                         op->ors_tlimit = SLAP_NO_LIMIT;
1101                 }
1102
1103                 if ( op->ors_slimit == 0 ) {
1104                         op->ors_slimit = SLAP_NO_LIMIT;
1105                 }
1106
1107         /* if not root, get appropriate limits */
1108         } else {
1109                 ( void ) limits_get( op, &op->o_ndn, &op->ors_limit );
1110
1111                 assert( op->ors_limit != NULL );
1112
1113                 /* if no limit is required, use soft limit */
1114                 if ( op->ors_tlimit == 0 ) {
1115                         op->ors_tlimit = op->ors_limit->lms_t_soft;
1116
1117                 /* limit required: check if legal */
1118                 } else {
1119                         if ( op->ors_limit->lms_t_hard == 0 ) {
1120                                 if ( op->ors_limit->lms_t_soft > 0
1121                                                 && ( op->ors_tlimit > op->ors_limit->lms_t_soft ) ) {
1122                                         op->ors_tlimit = op->ors_limit->lms_t_soft;
1123                                 }
1124
1125                         } else if ( op->ors_limit->lms_t_hard > 0 ) {
1126 #ifdef ABOVE_HARD_LIMIT_IS_ERROR
1127                                 if ( op->ors_tlimit == SLAP_MAX_LIMIT ) {
1128                                         op->ors_tlimit = op->ors_limit->lms_t_hard;
1129
1130                                 } else if ( op->ors_tlimit > op->ors_limit->lms_t_hard ) {
1131                                         /* error if exceeding hard limit */
1132                                         rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1133                                         send_ldap_result( op, rs );
1134                                         rs->sr_err = LDAP_SUCCESS;
1135                                         return -1;
1136                                 }
1137 #else /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1138                                 if ( op->ors_tlimit > op->ors_limit->lms_t_hard ) {
1139                                         op->ors_tlimit = op->ors_limit->lms_t_hard;
1140                                 }
1141 #endif /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1142                         }
1143                 }
1144
1145                 /* else leave as is */
1146
1147                 /* don't even get to backend if candidate check is disabled */
1148                 if ( op->ors_limit->lms_s_unchecked == 0 ) {
1149                         rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1150                         send_ldap_result( op, rs );
1151                         rs->sr_err = LDAP_SUCCESS;
1152                         return -1;
1153                 }
1154
1155                 /* if paged results is requested */     
1156                 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
1157                         int     slimit = -2;
1158                         int     pr_total;
1159                         PagedResultsState *ps = op->o_pagedresults_state;
1160
1161                         /* paged results is not allowed */
1162                         if ( op->ors_limit->lms_s_pr_total == -2 ) {
1163                                 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1164                                 rs->sr_text = "pagedResults control not allowed";
1165                                 send_ldap_result( op, rs );
1166                                 rs->sr_err = LDAP_SUCCESS;
1167                                 rs->sr_text = NULL;
1168                                 return -1;
1169                         }
1170                         
1171                         if ( op->ors_limit->lms_s_pr > 0 && ps->ps_size > op->ors_limit->lms_s_pr ) {
1172                                 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1173                                 rs->sr_text = "illegal pagedResults page size";
1174                                 send_ldap_result( op, rs );
1175                                 rs->sr_err = LDAP_SUCCESS;
1176                                 rs->sr_text = NULL;
1177                                 return -1;
1178                         }
1179
1180                         if ( op->ors_limit->lms_s_pr_total == 0 ) {
1181                                 if ( op->ors_limit->lms_s_hard == 0 ) {
1182                                         pr_total = op->ors_limit->lms_s_soft;
1183                                 } else {
1184                                         pr_total = op->ors_limit->lms_s_hard;
1185                                 }
1186                         } else {
1187                                 pr_total = op->ors_limit->lms_s_pr_total;
1188                         }
1189
1190                         if ( pr_total == -1 ) {
1191                                 if ( op->ors_slimit == 0 || op->ors_slimit == SLAP_MAX_LIMIT ) {
1192                                         slimit = -1;
1193
1194                                 } else {
1195                                         slimit = op->ors_slimit - ps->ps_count;
1196                                 }
1197
1198 #ifdef ABOVE_HARD_LIMIT_IS_ERROR
1199                         } else if ( pr_total > 0 && op->ors_slimit != SLAP_MAX_LIMIT
1200                                         && ( op->ors_slimit == SLAP_NO_LIMIT || op->ors_slimit > pr_total ) )
1201                         {
1202                                 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1203                                 send_ldap_result( op, rs );
1204                                 rs->sr_err = LDAP_SUCCESS;
1205                                 return -1;
1206 #endif /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1207         
1208                         } else {
1209                                 /* if no limit is required, use soft limit */
1210                                 int     total;
1211                                 int     slimit2;
1212
1213                                 /* first round of pagedResults: set count to any appropriate limit */
1214
1215                                 /* if the limit is set, check that it does not violate any server-side limit */
1216 #ifdef ABOVE_HARD_LIMIT_IS_ERROR
1217                                 if ( op->ors_slimit == SLAP_MAX_LIMIT ) {
1218                                         slimit2 = op->ors_slimit = pr_total;
1219 #else /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1220                                 if ( op->ors_slimit == SLAP_MAX_LIMIT || op->ors_slimit > pr_total ) {
1221                                         slimit2 = op->ors_slimit = pr_total;
1222 #endif /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1223
1224                                 } else if ( op->ors_slimit == 0 ) {
1225                                         slimit2 = pr_total;
1226
1227                                 } else {
1228                                         slimit2 = op->ors_slimit;
1229                                 }
1230
1231                                 total = slimit2 - ps->ps_count;
1232
1233                                 if ( total >= 0 ) {
1234                                         if ( op->ors_limit->lms_s_pr > 0 ) {
1235                                                 /* use the smallest limit set by total/per page */
1236                                                 if ( total < op->ors_limit->lms_s_pr ) {
1237                                                         slimit = total;
1238         
1239                                                 } else {
1240                                                         /* use the perpage limit if any 
1241                                                          * NOTE: + 1 because the given value must be legal */
1242                                                         slimit = op->ors_limit->lms_s_pr + 1;
1243                                                 }
1244
1245                                         } else {
1246                                                 /* use the total limit if any */
1247                                                 slimit = total;
1248                                         }
1249
1250                                 } else if ( op->ors_limit->lms_s_pr > 0 ) {
1251                                         /* use the perpage limit if any 
1252                                          * NOTE: + 1 because the given value must be legal */
1253                                         slimit = op->ors_limit->lms_s_pr + 1;
1254
1255                                 } else {
1256                                         /* use the standard hard/soft limit if any */
1257                                         slimit = op->ors_limit->lms_s_hard;
1258                                 }
1259                         }
1260                 
1261                         /* if got any limit, use it */
1262                         if ( slimit != -2 ) {
1263                                 if ( op->ors_slimit == 0 ) {
1264                                         op->ors_slimit = slimit;
1265
1266                                 } else if ( slimit > 0 ) {
1267                                         if ( op->ors_slimit - ps->ps_count > slimit ) {
1268                                                 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1269                                                 send_ldap_result( op, rs );
1270                                                 rs->sr_err = LDAP_SUCCESS;
1271                                                 return -1;
1272                                         }
1273                                         op->ors_slimit = slimit;
1274                                 }
1275
1276                         } else {
1277                                 /* use the standard hard/soft limit if any */
1278                                 op->ors_slimit = pr_total;
1279                         }
1280
1281                 /* no limit requested: use soft, whatever it is */
1282                 } else if ( op->ors_slimit == 0 ) {
1283                         op->ors_slimit = op->ors_limit->lms_s_soft;
1284
1285                 /* limit requested: check if legal */
1286                 } else {
1287                         /* hard limit as soft (traditional behavior) */
1288                         if ( op->ors_limit->lms_s_hard == 0 ) {
1289                                 if ( op->ors_limit->lms_s_soft > 0
1290                                                 && op->ors_slimit > op->ors_limit->lms_s_soft ) {
1291                                         op->ors_slimit = op->ors_limit->lms_s_soft;
1292                                 }
1293
1294                         /* explicit hard limit: error if violated */
1295                         } else if ( op->ors_limit->lms_s_hard > 0 ) {
1296 #ifdef ABOVE_HARD_LIMIT_IS_ERROR
1297                                 if ( op->ors_slimit == SLAP_MAX_LIMIT ) {
1298                                         op->ors_slimit = op->ors_limit->lms_s_hard;
1299
1300                                 } else if ( op->ors_slimit > op->ors_limit->lms_s_hard ) {
1301                                         /* if limit exceeds hard, error */
1302                                         rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1303                                         send_ldap_result( op, rs );
1304                                         rs->sr_err = LDAP_SUCCESS;
1305                                         return -1;
1306                                 }
1307 #else /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1308                                 if ( op->ors_slimit > op->ors_limit->lms_s_hard ) {
1309                                         op->ors_slimit = op->ors_limit->lms_s_hard;
1310                                 }
1311 #endif /* ! ABOVE_HARD_LIMIT_IS_ERROR */
1312                         }
1313                 }
1314
1315                 /* else leave as is */
1316         }
1317
1318         return 0;
1319 }
1320
1321 void
1322 limits_destroy( 
1323         struct slap_limits      **lm )
1324 {
1325         int             i;
1326
1327         if ( lm == NULL ) {
1328                 return;
1329         }
1330
1331         for ( i = 0; lm[ i ]; i++ ) {
1332                 switch ( lm[ i ]->lm_flags & SLAP_LIMITS_MASK ) {
1333                 case SLAP_LIMITS_REGEX:
1334                         regfree( &lm[ i ]->lm_regex );
1335                         break;
1336
1337                 case SLAP_LIMITS_EXACT:
1338                 case SLAP_LIMITS_ONE:
1339                 case SLAP_LIMITS_SUBTREE:
1340                 case SLAP_LIMITS_CHILDREN:
1341                         if ( !BER_BVISNULL( &lm[ i ]->lm_pat ) ) {
1342                                 ch_free( lm[ i ]->lm_pat.bv_val );
1343                         }
1344                         break;
1345
1346                 default:
1347                         break;
1348                 }
1349
1350                 ch_free( lm[ i ] );
1351         }
1352
1353         ch_free( lm );
1354 }