]> git.sur5r.net Git - openldap/blob - servers/slapd/limits.c
83b47788ff3e79f6de2e21d2505efbfdd6ae0d74
[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-2004 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
26 int
27 limits_get( 
28         Operation               *op,
29         struct berval           *ndn, 
30         struct slap_limits_set  **limit
31 )
32 {
33         struct slap_limits **lm;
34
35         assert( op );
36         assert( limit );
37
38         /*
39          * default values
40          */
41         *limit = &op->o_bd->be_def_limit;
42
43         if ( op->o_bd->be_limits == NULL ) {
44                 return( 0 );
45         }
46
47         for ( lm = op->o_bd->be_limits; lm[0] != NULL; lm++ ) {
48                 unsigned        style = lm[0]->lm_flags & SLAP_LIMITS_MASK;
49                 unsigned        type = lm[0]->lm_flags & SLAP_LIMITS_TYPE_MASK;
50
51                 switch ( style ) {
52                 case SLAP_LIMITS_EXACT:
53                         if ( ndn->bv_len == 0 ) {
54                                 break;
55                         }
56
57                         if ( type == SLAP_LIMITS_TYPE_GROUP ) {
58                                 int     rc;
59
60                                 rc = backend_group( op, NULL,
61                                                 &lm[0]->lm_pat, ndn,
62                                                 lm[0]->lm_group_oc,
63                                                 lm[0]->lm_group_ad );
64                                 if ( rc == 0 ) {
65                                         *limit = &lm[0]->lm_limits;
66                                         return( 0 );
67                                 }
68                         }
69                         
70                         if ( dn_match( &lm[0]->lm_pat, ndn ) ) {
71                                 *limit = &lm[0]->lm_limits;
72                                 return( 0 );
73                         }
74                         break;
75
76                 case SLAP_LIMITS_ONE:
77                 case SLAP_LIMITS_SUBTREE:
78                 case SLAP_LIMITS_CHILDREN: {
79                         size_t d;
80                         
81                         if ( ndn->bv_len == 0 ) {
82                                 break;
83                         }
84
85                         /* ndn shorter than dn_pat */
86                         if ( ndn->bv_len < lm[0]->lm_pat.bv_len ) {
87                                 break;
88                         }
89                         d = ndn->bv_len - lm[0]->lm_pat.bv_len;
90
91                         /* allow exact match for SUBTREE only */
92                         if ( d == 0 ) {
93                                 if ( style != SLAP_LIMITS_SUBTREE ) {
94                                         break;
95                                 }
96                         } else {
97                                 /* check for unescaped rdn separator */
98                                 if ( !DN_SEPARATOR( ndn->bv_val[d-1] ) ) {
99                                         break;
100                                 }
101                         }
102
103                         /* in case of (sub)match ... */
104                         if ( lm[0]->lm_pat.bv_len == ( ndn->bv_len - d )
105                                         && strcmp( lm[0]->lm_pat.bv_val,
106                                                 &ndn->bv_val[d] ) == 0 )
107                         {
108                                 /* check for exactly one rdn in case of ONE */
109                                 if ( style == SLAP_LIMITS_ONE ) {
110                                         /*
111                                          * if ndn is more that one rdn
112                                          * below dn_pat, continue
113                                          */
114                                         if ( (size_t) dn_rdnlen( NULL, ndn )
115                                                         != d - 1 )
116                                         {
117                                                 break;
118                                         }
119                                 }
120
121                                 *limit = &lm[0]->lm_limits;
122                                 return( 0 );
123                         }
124
125                         break;
126                 }
127
128                 case SLAP_LIMITS_REGEX:
129                         if ( ndn->bv_len == 0 ) {
130                                 break;
131                         }
132                         if ( regexec( &lm[0]->lm_regex, ndn->bv_val,
133                                                 0, NULL, 0 ) == 0 )
134                         {
135                                 *limit = &lm[0]->lm_limits;
136                                 return( 0 );
137                         }
138                         break;
139
140                 case SLAP_LIMITS_ANONYMOUS:
141                         if ( ndn->bv_len == 0 ) {
142                                 *limit = &lm[0]->lm_limits;
143                                 return( 0 );
144                         }
145                         break;
146
147                 case SLAP_LIMITS_USERS:
148                         if ( ndn->bv_len != 0 ) {
149                                 *limit = &lm[0]->lm_limits;
150                                 return( 0 );
151                         }
152                         break;
153
154                 case SLAP_LIMITS_ANY:
155                         *limit = &lm[0]->lm_limits;
156                         return( 0 );
157
158                 default:
159                         assert( 0 );    /* unreachable */
160                         return( -1 );
161                 }
162         }
163
164         return( 0 );
165 }
166
167 static int
168 limits_add(
169         Backend                 *be,
170         unsigned                flags,
171         const char              *pattern,
172         ObjectClass             *group_oc,
173         AttributeDescription    *group_ad,
174         struct slap_limits_set  *limit
175 )
176 {
177         int                     i;
178         struct slap_limits      *lm;
179         unsigned                type, style;
180         
181         assert( be );
182         assert( limit );
183
184         type = flags & SLAP_LIMITS_TYPE_MASK;
185         style = flags & SLAP_LIMITS_MASK;
186
187         switch ( style ) {
188         case SLAP_LIMITS_ANONYMOUS:
189         case SLAP_LIMITS_USERS:
190         case SLAP_LIMITS_ANY:
191                 for ( i = 0; be->be_limits && be->be_limits[ i ]; i++ ) {
192                         if ( be->be_limits[ i ]->lm_flags == style ) {
193                                 return( -1 );
194                         }
195                 }
196                 break;
197         }
198
199
200         lm = ( struct slap_limits * )ch_calloc( sizeof( struct slap_limits ), 1 );
201
202         switch ( style ) {
203         case SLAP_LIMITS_UNDEFINED:
204                 style = SLAP_LIMITS_EXACT;
205                 /* continue to next cases */
206         case SLAP_LIMITS_EXACT:
207         case SLAP_LIMITS_ONE:
208         case SLAP_LIMITS_SUBTREE:
209         case SLAP_LIMITS_CHILDREN:
210                 lm->lm_flags = style | type;
211                 {
212                         int rc;
213                         struct berval bv;
214                         bv.bv_val = (char *) pattern;
215                         bv.bv_len = strlen( pattern );
216
217                         rc = dnNormalize( 0, NULL, NULL, &bv, &lm->lm_pat, NULL );
218                         if ( rc != LDAP_SUCCESS ) {
219                                 ch_free( lm );
220                                 return( -1 );
221                         }
222                 }
223                 break;
224                 
225         case SLAP_LIMITS_REGEX:
226                 lm->lm_flags = style | type;
227                 ber_str2bv( pattern, 0, 1, &lm->lm_pat );
228                 if ( regcomp( &lm->lm_regex, lm->lm_pat.bv_val, 
229                                         REG_EXTENDED | REG_ICASE ) ) {
230                         free( lm->lm_pat.bv_val );
231                         ch_free( lm );
232                         return( -1 );
233                 }
234                 break;
235
236         case SLAP_LIMITS_ANONYMOUS:
237         case SLAP_LIMITS_USERS:
238         case SLAP_LIMITS_ANY:
239                 lm->lm_flags = style | type;
240                 lm->lm_pat.bv_val = NULL;
241                 lm->lm_pat.bv_len = 0;
242                 break;
243         }
244
245         switch ( type ) {
246         case SLAP_LIMITS_TYPE_GROUP:
247                 assert( group_oc );
248                 assert( group_ad );
249                 lm->lm_group_oc = group_oc;
250                 lm->lm_group_ad = group_ad;
251                 break;
252         }
253
254         lm->lm_limits = *limit;
255
256         i = 0;
257         if ( be->be_limits != NULL ) {
258                 for ( ; be->be_limits[i]; i++ );
259         }
260
261         be->be_limits = ( struct slap_limits ** )ch_realloc( be->be_limits,
262                         sizeof( struct slap_limits * ) * ( i + 2 ) );
263         be->be_limits[i] = lm;
264         be->be_limits[i+1] = NULL;
265         
266         return( 0 );
267 }
268
269 int
270 limits_parse(
271         Backend     *be,
272         const char  *fname,
273         int         lineno,
274         int         argc,
275         char        **argv
276 )
277 {
278         int                     flags = SLAP_LIMITS_UNDEFINED;
279         char                    *pattern;
280         struct slap_limits_set  limit;
281         int                     i, rc = 0;
282         ObjectClass             *group_oc = NULL;
283         AttributeDescription    *group_ad = NULL;
284
285         assert( be );
286
287         if ( argc < 3 ) {
288 #ifdef NEW_LOGGING
289                 LDAP_LOG( CONFIG, CRIT, 
290                         "%s : line %d: missing arg(s) in "
291                         "\"limits <pattern> <limits>\" line.\n", fname, lineno, 0 );
292 #else
293                 Debug( LDAP_DEBUG_ANY,
294                         "%s : line %d: missing arg(s) in "
295                         "\"limits <pattern> <limits>\" line.\n%s",
296                         fname, lineno, "" );
297 #endif
298                 return( -1 );
299         }
300
301         limit = be->be_def_limit;
302
303         /*
304          * syntax:
305          *
306          * "limits" <pattern> <limit> [ ... ]
307          * 
308          * 
309          * <pattern>:
310          * 
311          * "anonymous"
312          * "users"
313          * [ "dn" [ "." { "exact" | "base" | "onelevel" | "subtree" | children"
314          *      | "regex" | "anonymous" } ] "=" ] <dn pattern>
315          *
316          * Note:
317          *      "exact" and "base" are the same (exact match);
318          *      "onelevel" means exactly one rdn below, NOT including pattern
319          *      "subtree" means any rdn below, including pattern
320          *      "children" means any rdn below, NOT including pattern
321          *      
322          *      "anonymous" may be deprecated in favour 
323          *      of the pattern = "anonymous" form
324          *
325          * "group[/objectClass[/attributeType]]" "=" "<dn pattern>"
326          *
327          * <limit>:
328          *
329          * "time" [ "." { "soft" | "hard" } ] "=" <integer>
330          *
331          * "size" [ "." { "soft" | "hard" | "unchecked" } ] "=" <integer>
332          */
333         
334         pattern = argv[1];
335         if ( strcmp( pattern, "*" ) == 0) {
336                 flags = SLAP_LIMITS_ANY;
337
338         } else if ( strcasecmp( pattern, "anonymous" ) == 0 ) {
339                 flags = SLAP_LIMITS_ANONYMOUS;
340
341         } else if ( strcasecmp( pattern, "users" ) == 0 ) {
342                 flags = SLAP_LIMITS_USERS;
343                 
344         } else if ( strncasecmp( pattern, "dn", STRLENOF( "dn" ) ) == 0 ) {
345                 pattern += STRLENOF( "dn" );
346                 if ( pattern[0] == '.' ) {
347                         pattern++;
348                         if ( strncasecmp( pattern, "exact", STRLENOF( "exact" )) == 0 ) {
349                                 flags = SLAP_LIMITS_EXACT;
350                                 pattern += STRLENOF( "exact" );
351
352                         } else if ( strncasecmp( pattern, "base", STRLENOF( "base" ) ) == 0 ) {
353                                 flags = SLAP_LIMITS_BASE;
354                                 pattern += STRLENOF( "base" );
355
356                         } else if ( strncasecmp( pattern, "one", STRLENOF( "one" ) ) == 0 ) {
357                                 flags = SLAP_LIMITS_ONE;
358                                 pattern += STRLENOF( "one" );
359                                 if ( strncasecmp( pattern, "level", STRLENOF( "level" ) ) == 0 ) {
360                                         pattern += STRLENOF( "level" );
361
362                                 } else {
363 #ifdef NEW_LOGGING
364                                         LDAP_LOG( CONFIG, WARNING , 
365                                                 "%s : line %d: deprecated \"one\" style "
366                                                 "\"limits <pattern> <limits>\" line; "
367                                                 "use \"onelevel\" instead.\n", fname, lineno, 0 );
368 #else
369                                         Debug( LDAP_DEBUG_ANY,
370                                                 "%s : line %d: deprecated \"one\" style "
371                                                 "\"limits <pattern> <limits>\" line; "
372                                                 "use \"onelevel\" instead.\n", fname, lineno, 0 );
373 #endif
374                                 }
375
376                         } else if ( strncasecmp( pattern, "sub", STRLENOF( "sub" ) ) == 0 ) {
377                                 flags = SLAP_LIMITS_SUBTREE;
378                                 pattern += STRLENOF( "sub" );
379                                 if ( strncasecmp( pattern, "tree", STRLENOF( "tree" ) ) == 0 ) {
380                                         pattern += STRLENOF( "tree" );
381
382                                 } else {
383 #ifdef NEW_LOGGING
384                                         LDAP_LOG( CONFIG, WARNING , 
385                                                 "%s : line %d: deprecated \"sub\" style "
386                                                 "\"limits <pattern> <limits>\" line; "
387                                                 "use \"subtree\" instead.\n", fname, lineno, 0 );
388 #else
389                                         Debug( LDAP_DEBUG_ANY,
390                                                 "%s : line %d: deprecated \"sub\" style "
391                                                 "\"limits <pattern> <limits>\" line; "
392                                                 "use \"subtree\" instead.\n", fname, lineno, 0 );
393 #endif
394                                 }
395
396                         } else if ( strncasecmp( pattern, "children", STRLENOF( "children" ) ) == 0 ) {
397                                 flags = SLAP_LIMITS_CHILDREN;
398                                 pattern += STRLENOF( "children" );
399
400                         } else if ( strncasecmp( pattern, "regex", STRLENOF( "regex" ) ) == 0 ) {
401                                 flags = SLAP_LIMITS_REGEX;
402                                 pattern += STRLENOF( "regex" );
403
404                         /* 
405                          * this could be deprecated in favour
406                          * of the pattern = "anonymous" form
407                          */
408                         } else if ( strncasecmp( pattern, "anonymous", STRLENOF( "anonymous" ) ) == 0 ) {
409                                 flags = SLAP_LIMITS_ANONYMOUS;
410                                 pattern = NULL;
411                         }
412                 }
413
414                 /* pre-check the data */
415                 switch ( flags ) {
416                 case SLAP_LIMITS_ANONYMOUS:
417                 case SLAP_LIMITS_USERS:
418
419                         /* no need for pattern */
420                         pattern = NULL;
421                         break;
422
423                 default:
424                         if ( pattern[0] != '=' ) {
425 #ifdef NEW_LOGGING
426                                 LDAP_LOG( CONFIG, CRIT, 
427                                         "%s : line %d: missing '=' in "
428                                         "\"dn[.{exact|base|onelevel|subtree"
429                                         "|children|regex|anonymous}]" "=<pattern>\" in "
430                                         "\"limits <pattern> <limits>\" line.\n", fname, lineno, 0 );
431 #else
432                                 Debug( LDAP_DEBUG_ANY,
433                                         "%s : line %d: missing '=' in "
434                                         "\"dn[.{exact|base|onelevel|subtree"
435                                         "|children|regex|anonymous}]"
436                                         "=<pattern>\" in "
437                                         "\"limits <pattern> <limits>\" "
438                                         "line.\n%s",
439                                         fname, lineno, "" );
440 #endif
441                                 return( -1 );
442                         }
443
444                         /* skip '=' (required) */
445                         pattern++;
446
447                         /* trim obvious cases */
448                         if ( strcmp( pattern, "*" ) == 0 ) {
449                                 flags = SLAP_LIMITS_ANY;
450                                 pattern = NULL;
451
452                         } else if ( flags == SLAP_LIMITS_REGEX
453                                         && strcmp( pattern, ".*" ) == 0 ) {
454                                 flags = SLAP_LIMITS_ANY;
455                                 pattern = NULL;
456                         }
457                 }
458
459         } else if (strncasecmp( pattern, "group", STRLENOF( "group" ) ) == 0 ) {
460                 pattern += STRLENOF( "group" );
461
462                 if ( pattern[0] == '/' ) {
463                         struct berval   oc, ad;
464
465                         oc.bv_val = pattern + 1;
466
467                         ad.bv_val = strchr(pattern, '/');
468                         if ( ad.bv_val != NULL ) {
469                                 const char      *text = NULL;
470                                 int             rc;
471
472                                 oc.bv_len = ad.bv_val - oc.bv_val;
473
474                                 ad.bv_val++;
475                                 ad.bv_len = strlen( ad.bv_val );
476                                 rc = slap_bv2ad( &ad, &group_ad, &text );
477                                 if ( rc != LDAP_SUCCESS ) {
478                                         goto no_ad;
479                                 }
480
481                                 pattern = ad.bv_val + ad.bv_len;
482
483                         } else {
484                                 oc.bv_len = strlen( oc.bv_val );
485
486                                 pattern = oc.bv_val + oc.bv_len;
487                         }
488
489                         group_oc = oc_bvfind( &oc );
490                         if ( group_oc == NULL ) {
491                                 goto no_oc;
492                         }
493                 }
494
495                 if ( group_oc == NULL ) {
496                         group_oc = oc_find( SLAPD_GROUP_CLASS );
497                         if ( group_oc == NULL ) {
498 no_oc:;
499                                 return( -1 );
500                         }
501                 }
502
503                 if ( group_ad == NULL ) {
504                         const char      *text = NULL;
505                         int             rc;
506                         
507                         rc = slap_str2ad( SLAPD_GROUP_ATTR, &group_ad, &text );
508
509                         if ( rc != LDAP_SUCCESS ) {
510 no_ad:;
511                                 return( -1 );
512                         }
513                 }
514
515                 flags = SLAP_LIMITS_TYPE_GROUP | SLAP_LIMITS_EXACT;
516
517                 if ( pattern[0] != '=' ) {
518 #ifdef NEW_LOGGING
519                         LDAP_LOG( CONFIG, CRIT, 
520                                 "%s : line %d: missing '=' in "
521                                 "\"group[/objectClass[/attributeType]]"
522                                 "=<pattern>\" in "
523                                 "\"limits <pattern> <limits>\" line.\n",
524                                 fname, lineno, 0 );
525 #else
526                         Debug( LDAP_DEBUG_ANY,
527                                 "%s : line %d: missing '=' in "
528                                 "\"group[/objectClass[/attributeType]]"
529                                 "=<pattern>\" in "
530                                 "\"limits <pattern> <limits>\" line.\n",
531                                 fname, lineno, 0 );
532 #endif
533                         return( -1 );
534                 }
535
536                 /* skip '=' (required) */
537                 pattern++;
538         }
539
540         /* get the limits */
541         for ( i = 2; i < argc; i++ ) {
542                 if ( limits_parse_one( argv[i], &limit ) ) {
543
544 #ifdef NEW_LOGGING
545                         LDAP_LOG( CONFIG, CRIT, 
546                                 "%s : line %d: unknown limit values \"%s\" in "
547                                 "\"limits <pattern> <limits>\" line.\n",
548                                 fname, lineno, argv[i] );
549 #else
550                         Debug( LDAP_DEBUG_ANY,
551                                 "%s : line %d: unknown limit values \"%s\" in "
552                                 "\"limits <pattern> <limits>\" line.\n",
553                         fname, lineno, argv[i] );
554 #endif
555
556                         return( 1 );
557                 }
558         }
559
560         /*
561          * sanity checks ...
562          */
563         if ( limit.lms_t_hard > 0 && 
564                         ( limit.lms_t_hard < limit.lms_t_soft 
565                           || limit.lms_t_soft == -1 ) ) {
566                 limit.lms_t_hard = limit.lms_t_soft;
567         }
568         
569         if ( limit.lms_s_hard > 0 && 
570                         ( limit.lms_s_hard < limit.lms_s_soft 
571                           || limit.lms_s_soft == -1 ) ) {
572                 limit.lms_s_hard = limit.lms_s_soft;
573         }
574
575         /*
576          * defaults ...
577          */
578         if ( limit.lms_t_hard == 0 ) {
579                 limit.lms_t_hard = limit.lms_t_soft;
580         }
581
582         if ( limit.lms_s_hard == 0 ) {
583                 limit.lms_s_hard = limit.lms_s_soft;
584         }
585
586         if ( limit.lms_s_pr_total == 0 ) {
587                 limit.lms_s_pr_total = limit.lms_s_hard;
588         }
589
590         rc = limits_add( be, flags, pattern, group_oc, group_ad, &limit );
591         if ( rc ) {
592
593 #ifdef NEW_LOGGING
594                 LDAP_LOG( CONFIG, CRIT, 
595                         "%s : line %d: unable to add limit in "
596                         "\"limits <pattern> <limits>\" line.\n",
597                         fname, lineno, 0 );
598 #else
599                 Debug( LDAP_DEBUG_ANY,
600                         "%s : line %d: unable to add limit in "
601                         "\"limits <pattern> <limits>\" line.\n",
602                 fname, lineno, 0 );
603 #endif
604         }
605
606         return( rc );
607 }
608
609 int
610 limits_parse_one(
611         const char              *arg,
612         struct slap_limits_set  *limit
613 )
614 {
615         assert( arg );
616         assert( limit );
617
618         if ( strncasecmp( arg, "time", STRLENOF( "time" ) ) == 0 ) {
619                 arg += STRLENOF( "time" );
620
621                 if ( arg[0] == '.' ) {
622                         arg++;
623                         if ( strncasecmp( arg, "soft=", STRLENOF( "soft=" ) ) == 0 ) {
624                                 arg += STRLENOF( "soft=" );
625                                 if ( strcasecmp( arg, "none" ) == 0 ) {
626                                         limit->lms_t_soft = -1;
627
628                                 } else {
629                                         char    *next = NULL;
630                                         int     soft = strtol( arg, &next, 10 );
631
632                                         if ( next == arg || next[ 0 ] != '\0' ) {
633                                                 return( 1 );
634                                         }
635
636                                         if ( soft < -1 ) {
637                                                 return( 1 );
638                                         }
639
640                                         if ( soft == -1 ) {
641                                                 /* FIXME: use "none" instead */
642                                         }
643
644                                         limit->lms_t_soft = soft;
645                                 }
646                                 
647                         } else if ( strncasecmp( arg, "hard=", STRLENOF( "hard=" ) ) == 0 ) {
648                                 arg += STRLENOF( "hard=" );
649                                 if ( strcasecmp( arg, "soft" ) == 0 ) {
650                                         limit->lms_t_hard = 0;
651
652                                 } else if ( strcasecmp( arg, "none" ) == 0 ) {
653                                         limit->lms_t_hard = -1;
654
655                                 } else {
656                                         char    *next = NULL;
657                                         int     hard = strtol( arg, &next, 10 );
658
659                                         if ( next == arg || next[ 0 ] != '\0' ) {
660                                                 return( 1 );
661                                         }
662
663                                         if ( hard < -1 ) {
664                                                 return( 1 );
665                                         }
666
667                                         if ( hard == -1 ) {
668                                                 /* FIXME: use "none" instead */
669                                         }
670
671                                         if ( hard == 0 ) {
672                                                 /* FIXME: use "soft" instead */
673                                         }
674
675                                         limit->lms_t_hard = hard;
676                                 }
677                                 
678                         } else {
679                                 return( 1 );
680                         }
681                         
682                 } else if ( arg[0] == '=' ) {
683                         arg++;
684                         if ( strcasecmp( arg, "none" ) == 0 ) {
685                                 limit->lms_t_soft = -1;
686                         } else {
687                                 char    *next = NULL;
688
689                                 limit->lms_t_soft = strtol( arg, &next, 10 );
690                                 if ( next == arg || limit->lms_t_soft < -1 ) {
691                                         return( 1 );
692                                 }
693                         }
694                         limit->lms_t_hard = 0;
695                         
696                 } else {
697                         return( 1 );
698                 }
699
700         } else if ( strncasecmp( arg, "size", STRLENOF( "size" ) ) == 0 ) {
701                 arg += STRLENOF( "size" );
702                 
703                 if ( arg[0] == '.' ) {
704                         arg++;
705                         if ( strncasecmp( arg, "soft=", STRLENOF( "soft=" ) ) == 0 ) {
706                                 arg += STRLENOF( "soft=" );
707                                 if ( strcasecmp( arg, "none" ) == 0 ) {
708                                         limit->lms_s_soft = -1;
709                                 } else {
710                                         char    *next = NULL;
711                                         int     soft = strtol( arg, &next, 10 );
712
713                                         if ( next == arg || next[ 0 ] != '\0' ) {
714                                                 return( 1 );
715                                         }
716
717                                         if ( soft < -1 ) {
718                                                 return( 1 );
719                                         }
720
721                                         if ( soft == -1 ) {
722                                                 /* FIXME: use "none" instead */
723                                         }
724
725                                         limit->lms_s_soft = soft;
726                                 }
727                                 
728                         } else if ( strncasecmp( arg, "hard=", STRLENOF( "hard=" ) ) == 0 ) {
729                                 arg += STRLENOF( "hard=" );
730                                 if ( strcasecmp( arg, "soft" ) == 0 ) {
731                                         limit->lms_s_hard = 0;
732
733                                 } else if ( strcasecmp( arg, "none" ) == 0 ) {
734                                         limit->lms_s_hard = -1;
735
736                                 } else {
737                                         char    *next = NULL;
738                                         int     hard = strtol( arg, &next, 10 );
739
740                                         if ( next == arg || next[ 0 ] != '\0' ) {
741                                                 return( 1 );
742                                         }
743
744                                         if ( hard < -1 ) {
745                                                 return( 1 );
746                                         }
747
748                                         if ( hard == -1 ) {
749                                                 /* FIXME: use "none" instead */
750                                         }
751
752                                         if ( hard == 0 ) {
753                                                 /* FIXME: use "soft" instead */
754                                         }
755
756                                         limit->lms_s_hard = hard;
757                                 }
758                                 
759                         } else if ( strncasecmp( arg, "unchecked=", STRLENOF( "unchecked=" ) ) == 0 ) {
760                                 arg += STRLENOF( "unchecked=" );
761                                 if ( strcasecmp( arg, "none" ) == 0 ) {
762                                         limit->lms_s_unchecked = -1;
763
764                                 } else if ( strcasecmp( arg, "disabled" ) == 0 ) {
765                                         limit->lms_s_unchecked = 0;
766
767                                 } else {
768                                         char    *next = NULL;
769                                         int     unchecked = strtol( arg, &next, 10 );
770
771                                         if ( next == arg || next[ 0 ] != '\0' ) {
772                                                 return( 1 );
773                                         }
774
775                                         if ( unchecked < -1 ) {
776                                                 return( 1 );
777                                         }
778
779                                         if ( unchecked == -1 ) {
780                                                 /*  FIXME: use "none" instead */
781                                         }
782
783                                         limit->lms_s_unchecked = unchecked;
784                                 }
785
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;
790
791                                 } else if ( strcasecmp( arg, "none" ) == 0 ) {
792                                         limit->lms_s_pr = -1;
793
794                                 } else if ( strcasecmp( arg, "disabled" ) == 0 ) {
795                                         limit->lms_s_pr_total = -2;
796
797                                 } else {
798                                         char    *next = NULL;
799                                         int     pr = strtol( arg, &next, 10 );
800
801                                         if ( next == arg || next[ 0 ] != '\0' ) {
802                                                 return( 1 );
803                                         }
804
805                                         if ( pr < -1 ) {
806                                                 return( 1 );
807                                         }
808
809                                         if ( pr == -1 ) {
810                                                 /* FIXME: use "none" instead */
811                                         }
812
813                                         limit->lms_s_pr = pr;
814                                 }
815
816                         } else if ( strncasecmp( arg, "prtotal=", STRLENOF( "prtotal=" ) ) == 0 ) {
817                                 arg += STRLENOF( "prtotal=" );
818
819                                 if ( strcasecmp( arg, "none" ) == 0 ) {
820                                         limit->lms_s_pr_total = -1;
821
822                                 } else if ( strcasecmp( arg, "hard" ) == 0 ) {
823                                         limit->lms_s_pr_total = 0;
824
825                                 } else {
826                                         char    *next = NULL;
827                                         int     total;
828
829                                         total = strtol( arg, &next, 10 );
830                                         if ( next == arg || next[ 0 ] != '\0' ) {
831                                                 return( 1 );
832                                         }
833
834                                         if ( total < -1 ) {
835                                                 return( 1 );
836                                         }
837
838                                         if ( total == -1 ) {
839                                                 /* FIXME: use "none" instead */
840                                         }
841
842                                         if ( total == 0 ) {
843                                                 /* FIXME: use "pr=disable" instead */
844                                         }
845
846                                         limit->lms_s_pr_total = total;
847                                 }
848
849                         } else {
850                                 return( 1 );
851                         }
852                         
853                 } else if ( arg[0] == '=' ) {
854                         arg++;
855                         if ( strcasecmp( arg, "none" ) == 0 ) {
856                                 limit->lms_s_soft = -1;
857                         } else {
858                                 char    *next = NULL;
859
860                                 limit->lms_s_soft = strtol( arg, &next, 10 );
861                                 if ( next == arg || limit->lms_s_soft < -1 ) {
862                                         return( 1 );
863                                 }
864                         }
865                         limit->lms_s_hard = 0;
866                         
867                 } else {
868                         return( 1 );
869                 }
870         }
871
872         return 0;
873 }
874
875
876 int
877 limits_check( Operation *op, SlapReply *rs )
878 {
879         assert( op );
880         assert( rs );
881         /* FIXME: should this be always true? */
882         assert( op->o_tag == LDAP_REQ_SEARCH);
883         
884         /* allow root to set no limit */
885         if ( be_isroot( op ) ) {
886                 op->ors_limit = NULL;
887
888                 if ( op->ors_tlimit == 0 ) {
889                         op->ors_tlimit = -1;
890                 }
891
892                 if ( op->ors_slimit == 0 ) {
893                         op->ors_slimit = -1;
894                 }
895
896         /* if not root, get appropriate limits */
897         } else {
898                 ( void ) limits_get( op, &op->o_ndn, &op->ors_limit );
899
900                 assert( op->ors_limit != NULL );
901
902                 /* if no limit is required, use soft limit */
903                 if ( op->ors_tlimit <= 0 ) {
904                         op->ors_tlimit = op->ors_limit->lms_t_soft;
905
906                 } else {
907                         /* no hard limit means use soft instead */
908                         if ( op->ors_limit->lms_t_hard == 0 ) {
909                                 if ( op->ors_limit->lms_t_soft > -1
910                                                 && op->ors_tlimit > op->ors_limit->lms_t_soft ) {
911                                         op->ors_tlimit = op->ors_limit->lms_t_soft;
912                                 }
913
914                         /* -1 means no hard limit */
915                         } else if ( op->ors_limit->lms_t_hard == -1 ) {
916                                 op->ors_tlimit = -1;
917                 
918                         /* error if exceeding hard limit */     
919                         } else if ( op->ors_tlimit > op->ors_limit->lms_t_hard ) {
920                                 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
921                                 send_ldap_result( op, rs );
922                                 rs->sr_err = LDAP_SUCCESS;
923                                 return -1;
924                         }
925                 }
926
927                 /* don't even get to backend if candidate check is disabled */
928                 if ( op->ors_limit->lms_s_unchecked == 0 ) {
929                         rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
930                         send_ldap_result( op, rs );
931                         rs->sr_err = LDAP_SUCCESS;
932                         return -1;
933                 }
934
935                 /* if paged results is requested */     
936                 if ( get_pagedresults( op ) ) {
937                         int     slimit = -2;
938
939                         /* paged results is not allowed */
940                         if ( op->ors_limit->lms_s_pr_total == -2 ) {
941                                 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
942                                 rs->sr_text = "pagedResults control not allowed";
943                                 send_ldap_result( op, rs );
944                                 rs->sr_err = LDAP_SUCCESS;
945                                 rs->sr_text = NULL;
946                                 return -1;
947         
948                         } else {
949                                 /* if no limit is required, use soft limit */
950                                 int     total;
951                                 int     slimit2 = -1;
952
953                                 /* first round of pagedResults: set count to any appropriate limit */
954
955                                 /* if the limit is set, check that it does not violate any limit */
956                                 if ( op->ors_slimit > 0 ) {
957                                         slimit2 = op->ors_slimit;
958                                         if ( op->ors_limit->lms_s_pr_total > 0 ) {
959                                                 if ( op->ors_slimit > op->ors_limit->lms_s_pr_total ) {
960                                                         slimit2 = -2;
961                                                 }
962
963                                         } else if ( op->ors_limit->lms_s_hard > 0 ) {
964                                                 if ( op->ors_slimit > op->ors_limit->lms_s_hard ) {
965                                                         slimit2 = -2;
966                                                 }
967
968                                         } else if ( op->ors_limit->lms_s_soft > 0 ) {
969                                                 if ( op->ors_slimit > op->ors_limit->lms_s_soft ) {
970                                                         slimit2 = -2;
971                                                 }
972                                         }
973
974                                         if ( slimit2 == -2 ) {
975                                                 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
976                                                 send_ldap_result( op, rs );
977                                                 rs->sr_err = LDAP_SUCCESS;
978                                                 return -1;
979                                         }
980
981                                 } else {
982                                         if ( op->ors_limit->lms_s_pr_total > 0 ) {
983                                                 slimit2 = op->ors_limit->lms_s_pr_total;
984
985                                         } else if ( op->ors_limit->lms_s_hard > 0 ) {
986                                                 slimit2 = op->ors_limit->lms_s_hard;
987
988                                         } else if ( op->ors_limit->lms_s_soft > 0 ) {
989                                                 slimit2 = op->ors_limit->lms_s_soft;
990                                         }
991                                 }
992
993                                 total = slimit2 - op->o_pagedresults_state.ps_count;
994
995                                 if ( total >= 0 && op->ors_limit->lms_s_pr > 0 ) {
996                                         /* use the smallest limit set by total/per page */
997                                         if ( total < op->ors_limit->lms_s_pr ) {
998                                                 slimit = total;
999
1000                                         } else {
1001                                                 /* use the perpage limit if any 
1002                                                  * NOTE: + 1 because the given value must be legal */
1003                                                 slimit = op->ors_limit->lms_s_pr + 1;
1004                                         }
1005
1006                                 } else if ( total >= 0 ) {
1007                                         /* use the total limit if any */
1008                                         slimit = total;
1009
1010                                 } else if ( op->ors_limit->lms_s_pr != 0 ) {
1011                                         /* use the perpage limit if any 
1012                                          * NOTE: + 1 because the given value must be legal */
1013                                         slimit = op->ors_limit->lms_s_pr + 1;
1014
1015                                 } else {
1016                                         /* use the standard hard/soft limit if any */
1017                                         slimit = op->ors_limit->lms_s_hard;
1018                                 }
1019                         }
1020                 
1021                         /* if got any limit, use it */
1022                         if ( slimit != -2 ) {
1023                                 if ( op->ors_slimit <= 0 ) {
1024                                         op->ors_slimit = slimit;
1025
1026                                 } else if ( op->ors_slimit - op->o_pagedresults_state.ps_count > slimit ) {
1027                                         rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1028                                         send_ldap_result( op, rs );
1029                                         rs->sr_err = LDAP_SUCCESS;
1030                                         return -1;
1031                                 } else {
1032                                         op->ors_slimit = slimit;
1033                                 }
1034
1035                         } else {
1036                                 /* use the standard hard/soft limit if any */
1037                                 op->ors_slimit = op->ors_limit->lms_s_hard;
1038                         }
1039
1040                 } else if ( op->ors_slimit == 0 ) {
1041                         op->ors_slimit = op->ors_limit->lms_s_soft;
1042
1043                 } else {
1044                         /* no hard limit means use soft instead */
1045                         if ( op->ors_limit->lms_s_hard == 0 ) {
1046                                 if ( op->ors_limit->lms_s_soft > -1
1047                                                 && op->ors_slimit > op->ors_limit->lms_s_soft ) {
1048                                         op->ors_slimit = op->ors_limit->lms_s_soft;
1049                                 }
1050
1051                         /* -1 means no hard limit */
1052                         } else if ( op->ors_limit->lms_s_hard == -1 ) {
1053                                 op->ors_slimit = -1;
1054                                         
1055                         /* error if exceeding hard limit */     
1056                         } else if ( op->ors_slimit > op->ors_limit->lms_s_hard ) {
1057                                 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1058                                 send_ldap_result( op, rs );
1059                                 rs->sr_err = LDAP_SUCCESS;
1060                                 return -1;
1061                         }
1062                 }
1063         }
1064
1065         return 0;
1066 }
1067