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 d = ndn->bv_len - lm[0]->lm_dn_pat.bv_len;
59 /* ndn shorter than dn_pat */
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] )
72 || DN_ESCAPE( ndn->bv_val[d-2] ) )
78 /* in case of (sub)match ... */
79 if ( lm[0]->lm_dn_pat.bv_len == ( ndn->bv_len - d )
80 && strcmp( lm[0]->lm_dn_pat.bv_val, &ndn->bv_val[d] ) == 0 ) {
81 /* check for exactly one rdn in case of ONE */
82 if ( lm[0]->lm_type == SLAP_LIMITS_ONE ) {
84 * if ndn is more that one rdn
85 * below dn_pat, continue
87 if ( (size_t) dn_rdnlen( NULL, ndn ) != d - 1 ) {
92 *limit = &lm[0]->lm_limits;
99 case SLAP_LIMITS_REGEX:
100 if ( ndn->bv_len == 0 ) {
103 if ( regexec( &lm[0]->lm_dn_regex, ndn->bv_val, 0, NULL, 0 )
106 *limit = &lm[0]->lm_limits;
111 case SLAP_LIMITS_ANONYMOUS:
112 if ( ndn->bv_len == 0 ) {
113 *limit = &lm[0]->lm_limits;
118 case SLAP_LIMITS_USERS:
119 if ( ndn->bv_len != 0 ) {
120 *limit = &lm[0]->lm_limits;
126 assert( 0 ); /* unreachable */
139 struct slap_limits_set *limit
143 struct slap_limits *lm;
148 lm = ( struct slap_limits * )ch_calloc( sizeof( struct slap_limits ), 1 );
151 case SLAP_LIMITS_EXACT:
152 case SLAP_LIMITS_ONE:
153 case SLAP_LIMITS_SUBTREE:
154 case SLAP_LIMITS_CHILDREN:
159 bv.bv_val = (char *) pattern;
160 bv.bv_len = strlen( pattern );
162 rc = dnNormalize2( NULL, &bv, &lm->lm_dn_pat );
163 if ( rc != LDAP_SUCCESS ) {
170 case SLAP_LIMITS_REGEX:
171 case SLAP_LIMITS_UNDEFINED:
172 lm->lm_type = SLAP_LIMITS_REGEX;
173 ber_str2bv( pattern, 0, 1, &lm->lm_dn_pat );
174 if ( regcomp( &lm->lm_dn_regex, lm->lm_dn_pat.bv_val,
175 REG_EXTENDED | REG_ICASE ) ) {
176 free( lm->lm_dn_pat.bv_val );
182 case SLAP_LIMITS_ANONYMOUS:
183 case SLAP_LIMITS_USERS:
185 lm->lm_dn_pat.bv_val = NULL;
186 lm->lm_dn_pat.bv_len = 0;
190 lm->lm_limits = *limit;
193 if ( be->be_limits != NULL ) {
194 for ( ; be->be_limits[i]; i++ );
197 be->be_limits = ( struct slap_limits ** )ch_realloc( be->be_limits,
198 sizeof( struct slap_limits * ) * ( i + 2 ) );
199 be->be_limits[i] = lm;
200 be->be_limits[i+1] = NULL;
214 int type = SLAP_LIMITS_UNDEFINED;
216 struct slap_limits_set limit;
223 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
224 "%s : line %d: missing arg(s) in "
225 "\"limits <pattern> <limits>\" line.\n",
228 Debug( LDAP_DEBUG_ANY,
229 "%s : line %d: missing arg(s) in "
230 "\"limits <pattern> <limits>\" line.\n%s",
236 limit = be->be_def_limit;
241 * "limits" <pattern> <limit> [ ... ]
248 * [ "dn" [ "." { "exact" | "base" | "one" | "sub" | children"
249 * | "regex" | "anonymous" } ] "=" ] <dn pattern>
252 * "exact" and "base" are the same (exact match);
253 * "one" means exactly one rdn below, NOT including the pattern
254 * "sub" means any rdn below, including the pattern
255 * "children" means any rdn below, NOT including the pattern
257 * "anonymous" may be deprecated in favour
258 * of the pattern = "anonymous" form
263 * "time" [ "." { "soft" | "hard" } ] "=" <integer>
265 * "size" [ "." { "soft" | "hard" | "unchecked" } ] "=" <integer>
269 if ( strcasecmp( pattern, "anonymous" ) == 0 ) {
270 type = SLAP_LIMITS_ANONYMOUS;
272 } else if ( strcasecmp( pattern, "users" ) == 0 ) {
273 type = SLAP_LIMITS_USERS;
275 } else if ( strncasecmp( pattern, "dn", 2 ) == 0 ) {
277 if ( pattern[0] == '.' ) {
279 if ( strncasecmp( pattern, "exact", 5 ) == 0 ) {
280 type = SLAP_LIMITS_EXACT;
283 } else if ( strncasecmp( pattern, "base", 4 ) == 0 ) {
284 type = SLAP_LIMITS_BASE;
287 } else if ( strncasecmp( pattern, "one", 3 ) == 0 ) {
288 type = SLAP_LIMITS_ONE;
291 } else if ( strncasecmp( pattern, "subtree", 7 ) == 0 ) {
292 type = SLAP_LIMITS_SUBTREE;
295 } else if ( strncasecmp( pattern, "children", 8 ) == 0 ) {
296 type = SLAP_LIMITS_CHILDREN;
299 } else if ( strncasecmp( pattern, "regex", 5 ) == 0 ) {
300 type = SLAP_LIMITS_REGEX;
304 * this could be deprecated in favour
305 * of the pattern = "anonymous" form
307 } else if ( strncasecmp( pattern, "anonymous", 9 ) == 0 ) {
308 type = SLAP_LIMITS_ANONYMOUS;
313 /* pre-check the data */
315 case SLAP_LIMITS_ANONYMOUS:
316 case SLAP_LIMITS_USERS:
318 /* no need for pattern */
323 if ( pattern[0] != '=' ) {
325 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
326 "%s : line %d: missing '=' in "
327 "\"dn[.{exact|base|one|subtree"
328 "|children|regex|anonymous}]"
330 "\"limits <pattern> <limits>\" line.\n",
333 Debug( LDAP_DEBUG_ANY,
334 "%s : line %d: missing '=' in "
335 "\"dn[.{exact|base|one|subtree"
336 "|children|regex|anonymous}]"
338 "\"limits <pattern> <limits>\" "
345 /* skip '=' (required) */
351 for ( i = 2; i < argc; i++ ) {
352 if ( parse_limit( argv[i], &limit ) ) {
355 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
356 "%s : line %d: unknown limit type \"%s\" in "
357 "\"limits <pattern> <limits>\" line.\n",
358 fname, lineno, argv[i] ));
360 Debug( LDAP_DEBUG_ANY,
361 "%s : line %d: unknown limit type \"%s\" in "
362 "\"limits <pattern> <limits>\" line.\n",
363 fname, lineno, argv[i] );
373 if ( limit.lms_t_hard > 0 && limit.lms_t_hard < limit.lms_t_soft ) {
374 limit.lms_t_hard = limit.lms_t_soft;
377 if ( limit.lms_s_hard > 0 && limit.lms_s_hard < limit.lms_s_soft ) {
378 limit.lms_s_hard = limit.lms_s_soft;
381 return( add_limits( be, type, pattern, &limit ) );
387 struct slap_limits_set *limit
393 if ( strncasecmp( arg, "time", 4 ) == 0 ) {
396 if ( arg[0] == '.' ) {
398 if ( strncasecmp( arg, "soft", 4 ) == 0 ) {
400 if ( arg[0] != '=' ) {
404 limit->lms_t_soft = atoi( arg );
406 } else if ( strncasecmp( arg, "hard", 4 ) == 0 ) {
408 if ( arg[0] != '=' ) {
412 if ( strcasecmp( arg, "soft" ) == 0 ) {
413 limit->lms_t_hard = 0;
414 } else if ( strcasecmp( arg, "none" ) == 0 ) {
415 limit->lms_t_hard = -1;
417 limit->lms_t_hard = atoi( arg );
424 } else if ( arg[0] == '=' ) {
425 limit->lms_t_soft = atoi( arg );
426 limit->lms_t_hard = 0;
432 } else if ( strncasecmp( arg, "size", 4 ) == 0 ) {
435 if ( arg[0] == '.' ) {
437 if ( strncasecmp( arg, "soft", 4 ) == 0 ) {
439 if ( arg[0] != '=' ) {
443 limit->lms_s_soft = atoi( arg );
445 } else if ( strncasecmp( arg, "hard", 4 ) == 0 ) {
447 if ( arg[0] != '=' ) {
451 if ( strcasecmp( arg, "soft" ) == 0 ) {
452 limit->lms_s_hard = 0;
453 } else if ( strcasecmp( arg, "none" ) == 0 ) {
454 limit->lms_s_hard = -1;
456 limit->lms_s_hard = atoi( arg );
459 } else if ( strncasecmp( arg, "unchecked", 9 ) == 0 ) {
461 if ( arg[0] != '=' ) {
465 if ( strcasecmp( arg, "none" ) == 0 ) {
466 limit->lms_s_unchecked = -1;
468 limit->lms_s_unchecked = atoi( arg );
475 } else if ( arg[0] == '=' ) {
476 limit->lms_s_soft = atoi( arg );
477 limit->lms_s_hard = 0;