1 /* limits.c - routines to handle regex-based size and time limits */
3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
20 struct slap_limits_set **limit
23 struct slap_limits **lm;
31 *limit = &be->be_def_limit;
33 if ( be->be_limits == NULL ) {
37 for ( lm = be->be_limits; lm[0] != NULL; lm++ ) {
38 switch ( lm[0]->lm_type ) {
39 case SLAP_LIMITS_EXACT:
40 if ( ndn->bv_len == 0 ) {
43 if ( dn_match( &lm[0]->lm_dn_pat, ndn ) ) {
44 *limit = &lm[0]->lm_limits;
50 case SLAP_LIMITS_SUBTREE:
51 case SLAP_LIMITS_CHILDREN: {
54 if ( ndn->bv_len == 0 ) {
58 /* ndn shorter than dn_pat */
59 if ( ndn->bv_len < lm[0]->lm_dn_pat.bv_len ) {
62 d = ndn->bv_len - lm[0]->lm_dn_pat.bv_len;
64 /* allow exact match for SUBTREE only */
66 if ( lm[0]->lm_type != SLAP_LIMITS_SUBTREE ) {
70 /* check for unescaped rdn separator */
71 if ( !DN_SEPARATOR( ndn->bv_val[d-1] ) ) {
76 /* in case of (sub)match ... */
77 if ( lm[0]->lm_dn_pat.bv_len == ( ndn->bv_len - d )
78 && strcmp( lm[0]->lm_dn_pat.bv_val, &ndn->bv_val[d] ) == 0 ) {
79 /* check for exactly one rdn in case of ONE */
80 if ( lm[0]->lm_type == SLAP_LIMITS_ONE ) {
82 * if ndn is more that one rdn
83 * below dn_pat, continue
85 if ( (size_t) dn_rdnlen( NULL, ndn ) != d - 1 ) {
90 *limit = &lm[0]->lm_limits;
97 case SLAP_LIMITS_REGEX:
98 if ( ndn->bv_len == 0 ) {
101 if ( regexec( &lm[0]->lm_dn_regex, ndn->bv_val, 0, NULL, 0 )
104 *limit = &lm[0]->lm_limits;
109 case SLAP_LIMITS_ANONYMOUS:
110 if ( ndn->bv_len == 0 ) {
111 *limit = &lm[0]->lm_limits;
116 case SLAP_LIMITS_USERS:
117 if ( ndn->bv_len != 0 ) {
118 *limit = &lm[0]->lm_limits;
123 case SLAP_LIMITS_ANY:
124 *limit = &lm[0]->lm_limits;
128 assert( 0 ); /* unreachable */
141 struct slap_limits_set *limit
145 struct slap_limits *lm;
150 lm = ( struct slap_limits * )ch_calloc( sizeof( struct slap_limits ), 1 );
153 case SLAP_LIMITS_EXACT:
154 case SLAP_LIMITS_ONE:
155 case SLAP_LIMITS_SUBTREE:
156 case SLAP_LIMITS_CHILDREN:
161 bv.bv_val = (char *) pattern;
162 bv.bv_len = strlen( pattern );
164 rc = dnNormalize2( NULL, &bv, &lm->lm_dn_pat );
165 if ( rc != LDAP_SUCCESS ) {
172 case SLAP_LIMITS_REGEX:
173 case SLAP_LIMITS_UNDEFINED:
174 lm->lm_type = SLAP_LIMITS_REGEX;
175 ber_str2bv( pattern, 0, 1, &lm->lm_dn_pat );
176 if ( regcomp( &lm->lm_dn_regex, lm->lm_dn_pat.bv_val,
177 REG_EXTENDED | REG_ICASE ) ) {
178 free( lm->lm_dn_pat.bv_val );
184 case SLAP_LIMITS_ANONYMOUS:
185 case SLAP_LIMITS_USERS:
186 case SLAP_LIMITS_ANY:
188 lm->lm_dn_pat.bv_val = NULL;
189 lm->lm_dn_pat.bv_len = 0;
193 lm->lm_limits = *limit;
196 if ( be->be_limits != NULL ) {
197 for ( ; be->be_limits[i]; i++ );
200 be->be_limits = ( struct slap_limits ** )ch_realloc( be->be_limits,
201 sizeof( struct slap_limits * ) * ( i + 2 ) );
202 be->be_limits[i] = lm;
203 be->be_limits[i+1] = NULL;
217 int type = SLAP_LIMITS_UNDEFINED;
219 struct slap_limits_set limit;
226 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
227 "%s : line %d: missing arg(s) in "
228 "\"limits <pattern> <limits>\" line.\n",
231 Debug( LDAP_DEBUG_ANY,
232 "%s : line %d: missing arg(s) in "
233 "\"limits <pattern> <limits>\" line.\n%s",
239 limit = be->be_def_limit;
244 * "limits" <pattern> <limit> [ ... ]
251 * [ "dn" [ "." { "exact" | "base" | "one" | "sub" | children"
252 * | "regex" | "anonymous" } ] "=" ] <dn pattern>
255 * "exact" and "base" are the same (exact match);
256 * "one" means exactly one rdn below, NOT including the pattern
257 * "sub" means any rdn below, including the pattern
258 * "children" means any rdn below, NOT including the pattern
260 * "anonymous" may be deprecated in favour
261 * of the pattern = "anonymous" form
266 * "time" [ "." { "soft" | "hard" } ] "=" <integer>
268 * "size" [ "." { "soft" | "hard" | "unchecked" } ] "=" <integer>
272 if ( strcmp( pattern, "*" ) == 0) {
273 type = SLAP_LIMITS_ANY;
275 } else if ( strcasecmp( pattern, "anonymous" ) == 0 ) {
276 type = SLAP_LIMITS_ANONYMOUS;
278 } else if ( strcasecmp( pattern, "users" ) == 0 ) {
279 type = SLAP_LIMITS_USERS;
281 } else if ( strncasecmp( pattern, "dn", 2 ) == 0 ) {
283 if ( pattern[0] == '.' ) {
285 if ( strncasecmp( pattern, "exact", 5 ) == 0 ) {
286 type = SLAP_LIMITS_EXACT;
289 } else if ( strncasecmp( pattern, "base", 4 ) == 0 ) {
290 type = SLAP_LIMITS_BASE;
293 } else if ( strncasecmp( pattern, "one", 3 ) == 0 ) {
294 type = SLAP_LIMITS_ONE;
297 } else if ( strncasecmp( pattern, "subtree", 7 ) == 0 ) {
298 type = SLAP_LIMITS_SUBTREE;
301 } else if ( strncasecmp( pattern, "children", 8 ) == 0 ) {
302 type = SLAP_LIMITS_CHILDREN;
305 } else if ( strncasecmp( pattern, "regex", 5 ) == 0 ) {
306 type = SLAP_LIMITS_REGEX;
310 * this could be deprecated in favour
311 * of the pattern = "anonymous" form
313 } else if ( strncasecmp( pattern, "anonymous", 9 ) == 0 ) {
314 type = SLAP_LIMITS_ANONYMOUS;
319 /* pre-check the data */
321 case SLAP_LIMITS_ANONYMOUS:
322 case SLAP_LIMITS_USERS:
324 /* no need for pattern */
329 if ( pattern[0] != '=' ) {
331 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
332 "%s : line %d: missing '=' in "
333 "\"dn[.{exact|base|one|subtree"
334 "|children|regex|anonymous}]"
336 "\"limits <pattern> <limits>\" line.\n",
339 Debug( LDAP_DEBUG_ANY,
340 "%s : line %d: missing '=' in "
341 "\"dn[.{exact|base|one|subtree"
342 "|children|regex|anonymous}]"
344 "\"limits <pattern> <limits>\" "
351 /* skip '=' (required) */
354 /* trim obvious cases */
355 if ( strcmp( pattern, "*" ) == 0 ) {
356 type = SLAP_LIMITS_ANY;
359 } else if ( ( type == SLAP_LIMITS_REGEX || type == SLAP_LIMITS_UNDEFINED )
360 && strcmp( pattern, ".*" ) == 0 ) {
361 type = SLAP_LIMITS_ANY;
368 for ( i = 2; i < argc; i++ ) {
369 if ( parse_limit( argv[i], &limit ) ) {
372 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
373 "%s : line %d: unknown limit type \"%s\" in "
374 "\"limits <pattern> <limits>\" line.\n",
375 fname, lineno, argv[i] ));
377 Debug( LDAP_DEBUG_ANY,
378 "%s : line %d: unknown limit type \"%s\" in "
379 "\"limits <pattern> <limits>\" line.\n",
380 fname, lineno, argv[i] );
390 if ( limit.lms_t_hard > 0 && limit.lms_t_hard < limit.lms_t_soft ) {
391 limit.lms_t_hard = limit.lms_t_soft;
394 if ( limit.lms_s_hard > 0 && limit.lms_s_hard < limit.lms_s_soft ) {
395 limit.lms_s_hard = limit.lms_s_soft;
398 return( add_limits( be, type, pattern, &limit ) );
404 struct slap_limits_set *limit
410 if ( strncasecmp( arg, "time", 4 ) == 0 ) {
413 if ( arg[0] == '.' ) {
415 if ( strncasecmp( arg, "soft", 4 ) == 0 ) {
417 if ( arg[0] != '=' ) {
421 limit->lms_t_soft = atoi( arg );
423 } else if ( strncasecmp( arg, "hard", 4 ) == 0 ) {
425 if ( arg[0] != '=' ) {
429 if ( strcasecmp( arg, "soft" ) == 0 ) {
430 limit->lms_t_hard = 0;
431 } else if ( strcasecmp( arg, "none" ) == 0 ) {
432 limit->lms_t_hard = -1;
434 limit->lms_t_hard = atoi( arg );
441 } else if ( arg[0] == '=' ) {
442 limit->lms_t_soft = atoi( arg );
443 limit->lms_t_hard = 0;
449 } else if ( strncasecmp( arg, "size", 4 ) == 0 ) {
452 if ( arg[0] == '.' ) {
454 if ( strncasecmp( arg, "soft", 4 ) == 0 ) {
456 if ( arg[0] != '=' ) {
460 limit->lms_s_soft = atoi( arg );
462 } else if ( strncasecmp( arg, "hard", 4 ) == 0 ) {
464 if ( arg[0] != '=' ) {
468 if ( strcasecmp( arg, "soft" ) == 0 ) {
469 limit->lms_s_hard = 0;
470 } else if ( strcasecmp( arg, "none" ) == 0 ) {
471 limit->lms_s_hard = -1;
473 limit->lms_s_hard = atoi( arg );
476 } else if ( strncasecmp( arg, "unchecked", 9 ) == 0 ) {
478 if ( arg[0] != '=' ) {
482 if ( strcasecmp( arg, "none" ) == 0 ) {
483 limit->lms_s_unchecked = -1;
485 limit->lms_s_unchecked = atoi( arg );
492 } else if ( arg[0] == '=' ) {
493 limit->lms_s_soft = atoi( arg );
494 limit->lms_s_hard = 0;