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 ( strcmp( lm[0]->lm_dn_pat.bv_val, ndn->bv_val ) == 0 ) {
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 ( strcmp( lm[0]->lm_dn_pat.bv_val, &ndn->bv_val[d] ) == 0 ) {
80 /* check for exactly one rdn in case of ONE */
81 if ( lm[0]->lm_type == SLAP_LIMITS_ONE ) {
83 * if ndn is more that one rdn
84 * below dn_pat, continue
86 if ( (size_t) dn_rdnlen( NULL, ndn ) != d - 1 ) {
91 *limit = &lm[0]->lm_limits;
98 case SLAP_LIMITS_REGEX:
99 if ( ndn->bv_len == 0 ) {
102 if ( regexec( &lm[0]->lm_dn_regex, ndn->bv_val, 0, NULL, 0 )
105 *limit = &lm[0]->lm_limits;
110 case SLAP_LIMITS_ANONYMOUS:
111 if ( ndn->bv_len == 0 ) {
112 *limit = &lm[0]->lm_limits;
117 case SLAP_LIMITS_USERS:
118 if ( ndn->bv_len != 0 ) {
119 *limit = &lm[0]->lm_limits;
125 assert( 0 ); /* unreachable */
138 struct slap_limits_set *limit
142 struct slap_limits *lm;
147 lm = ( struct slap_limits * )ch_calloc( sizeof( struct slap_limits ), 1 );
150 case SLAP_LIMITS_EXACT:
151 case SLAP_LIMITS_ONE:
152 case SLAP_LIMITS_SUBTREE:
153 case SLAP_LIMITS_CHILDREN:
158 bv.bv_val = (char *) pattern;
159 bv.bv_len = strlen( pattern );
161 rc = dnNormalize2( NULL, &bv, &lm->lm_dn_pat );
162 if ( rc != LDAP_SUCCESS ) {
169 case SLAP_LIMITS_REGEX:
170 case SLAP_LIMITS_UNDEFINED:
171 lm->lm_type = SLAP_LIMITS_REGEX;
172 ber_str2bv( pattern, 0, 1, &lm->lm_dn_pat );
173 if ( regcomp( &lm->lm_dn_regex, lm->lm_dn_pat.bv_val,
174 REG_EXTENDED | REG_ICASE ) ) {
175 free( lm->lm_dn_pat.bv_val );
181 case SLAP_LIMITS_ANONYMOUS:
182 case SLAP_LIMITS_USERS:
184 lm->lm_dn_pat.bv_val = NULL;
185 lm->lm_dn_pat.bv_len = 0;
189 lm->lm_limits = *limit;
192 if ( be->be_limits != NULL ) {
193 for ( ; be->be_limits[i]; i++ );
196 be->be_limits = ( struct slap_limits ** )ch_realloc( be->be_limits,
197 sizeof( struct slap_limits * ) * ( i + 2 ) );
198 be->be_limits[i] = lm;
199 be->be_limits[i+1] = NULL;
213 int type = SLAP_LIMITS_UNDEFINED;
215 struct slap_limits_set limit;
222 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
223 "%s : line %d: missing arg(s) in "
224 "\"limits <pattern> <limits>\" line.\n",
227 Debug( LDAP_DEBUG_ANY,
228 "%s : line %d: missing arg(s) in "
229 "\"limits <pattern> <limits>\" line.\n%s",
235 limit = be->be_def_limit;
240 * "limits" <pattern> <limit> [ ... ]
247 * [ "dn" [ "." { "exact" | "base" | "one" | "sub" | children"
248 * | "regex" | "anonymous" } ] "=" ] <dn pattern>
251 * "exact" and "base" are the same (exact match);
252 * "one" means exactly one rdn below, NOT including the pattern
253 * "sub" means any rdn below, including the pattern
254 * "children" means any rdn below, NOT including the pattern
256 * "anonymous" may be deprecated in favour
257 * of the pattern = "anonymous" form
262 * "time" [ "." { "soft" | "hard" } ] "=" <integer>
264 * "size" [ "." { "soft" | "hard" | "unchecked" } ] "=" <integer>
268 if ( strcasecmp( pattern, "anonymous" ) == 0 ) {
269 type = SLAP_LIMITS_ANONYMOUS;
271 } else if ( strcasecmp( pattern, "users" ) == 0 ) {
272 type = SLAP_LIMITS_USERS;
274 } else if ( strncasecmp( pattern, "dn", 2 ) == 0 ) {
276 if ( pattern[0] == '.' ) {
278 if ( strncasecmp( pattern, "exact", 5 ) == 0 ) {
279 type = SLAP_LIMITS_EXACT;
282 } else if ( strncasecmp( pattern, "base", 4 ) == 0 ) {
283 type = SLAP_LIMITS_BASE;
286 } else if ( strncasecmp( pattern, "one", 3 ) == 0 ) {
287 type = SLAP_LIMITS_ONE;
290 } else if ( strncasecmp( pattern, "subtree", 7 ) == 0 ) {
291 type = SLAP_LIMITS_SUBTREE;
294 } else if ( strncasecmp( pattern, "children", 8 ) == 0 ) {
295 type = SLAP_LIMITS_CHILDREN;
298 } else if ( strncasecmp( pattern, "regex", 5 ) == 0 ) {
299 type = SLAP_LIMITS_REGEX;
303 * this could be deprecated in favour
304 * of the pattern = "anonymous" form
306 } else if ( strncasecmp( pattern, "anonymous", 9 ) == 0 ) {
307 type = SLAP_LIMITS_ANONYMOUS;
312 /* pre-check the data */
314 case SLAP_LIMITS_ANONYMOUS:
315 case SLAP_LIMITS_USERS:
317 /* no need for pattern */
322 if ( pattern[0] != '=' ) {
324 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
325 "%s : line %d: missing '=' in "
326 "\"dn[.{exact|base|one|subtree"
327 "|children|regex|anonymous}]"
329 "\"limits <pattern> <limits>\" line.\n",
332 Debug( LDAP_DEBUG_ANY,
333 "%s : line %d: missing '=' in "
334 "\"dn[.{exact|base|one|subtree"
335 "|children|regex|anonymous}]"
337 "\"limits <pattern> <limits>\" "
344 /* skip '=' (required) */
350 for ( i = 2; i < argc; i++ ) {
351 if ( parse_limit( argv[i], &limit ) ) {
354 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
355 "%s : line %d: unknown limit type \"%s\" in "
356 "\"limits <pattern> <limits>\" line.\n",
357 fname, lineno, argv[i] ));
359 Debug( LDAP_DEBUG_ANY,
360 "%s : line %d: unknown limit type \"%s\" in "
361 "\"limits <pattern> <limits>\" line.\n",
362 fname, lineno, argv[i] );
372 if ( limit.lms_t_hard > 0 && limit.lms_t_hard < limit.lms_t_soft ) {
373 limit.lms_t_hard = limit.lms_t_soft;
376 if ( limit.lms_s_hard > 0 && limit.lms_s_hard < limit.lms_s_soft ) {
377 limit.lms_s_hard = limit.lms_s_soft;
380 return( add_limits( be, type, pattern, &limit ) );
386 struct slap_limits_set *limit
392 if ( strncasecmp( arg, "time", 4 ) == 0 ) {
395 if ( arg[0] == '.' ) {
397 if ( strncasecmp( arg, "soft", 4 ) == 0 ) {
399 if ( arg[0] != '=' ) {
403 limit->lms_t_soft = atoi( arg );
405 } else if ( strncasecmp( arg, "hard", 4 ) == 0 ) {
407 if ( arg[0] != '=' ) {
411 if ( strcasecmp( arg, "soft" ) == 0 ) {
412 limit->lms_t_hard = 0;
413 } else if ( strcasecmp( arg, "none" ) == 0 ) {
414 limit->lms_t_hard = -1;
416 limit->lms_t_hard = atoi( arg );
423 } else if ( arg[0] == '=' ) {
424 limit->lms_t_soft = atoi( arg );
425 limit->lms_t_hard = 0;
431 } else if ( strncasecmp( arg, "size", 4 ) == 0 ) {
434 if ( arg[0] == '.' ) {
436 if ( strncasecmp( arg, "soft", 4 ) == 0 ) {
438 if ( arg[0] != '=' ) {
442 limit->lms_s_soft = atoi( arg );
444 } else if ( strncasecmp( arg, "hard", 4 ) == 0 ) {
446 if ( arg[0] != '=' ) {
450 if ( strcasecmp( arg, "soft" ) == 0 ) {
451 limit->lms_s_hard = 0;
452 } else if ( strcasecmp( arg, "none" ) == 0 ) {
453 limit->lms_s_hard = -1;
455 limit->lms_s_hard = atoi( arg );
458 } else if ( strncasecmp( arg, "unchecked", 9 ) == 0 ) {
460 if ( arg[0] != '=' ) {
464 if ( strcasecmp( arg, "none" ) == 0 ) {
465 limit->lms_s_unchecked = -1;
467 limit->lms_s_unchecked = atoi( arg );
474 } else if ( arg[0] == '=' ) {
475 limit->lms_s_soft = atoi( arg );
476 limit->lms_s_hard = 0;