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] ) ) {
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;
124 assert( 0 ); /* unreachable */
137 struct slap_limits_set *limit
141 struct slap_limits *lm;
146 lm = ( struct slap_limits * )ch_calloc( sizeof( struct slap_limits ), 1 );
149 case SLAP_LIMITS_EXACT:
150 case SLAP_LIMITS_ONE:
151 case SLAP_LIMITS_SUBTREE:
152 case SLAP_LIMITS_CHILDREN:
157 bv.bv_val = (char *) pattern;
158 bv.bv_len = strlen( pattern );
160 rc = dnNormalize2( NULL, &bv, &lm->lm_dn_pat );
161 if ( rc != LDAP_SUCCESS ) {
168 case SLAP_LIMITS_REGEX:
169 case SLAP_LIMITS_UNDEFINED:
170 lm->lm_type = SLAP_LIMITS_REGEX;
171 ber_str2bv( pattern, 0, 1, &lm->lm_dn_pat );
172 if ( regcomp( &lm->lm_dn_regex, lm->lm_dn_pat.bv_val,
173 REG_EXTENDED | REG_ICASE ) ) {
174 free( lm->lm_dn_pat.bv_val );
180 case SLAP_LIMITS_ANONYMOUS:
181 case SLAP_LIMITS_USERS:
183 lm->lm_dn_pat.bv_val = NULL;
184 lm->lm_dn_pat.bv_len = 0;
188 lm->lm_limits = *limit;
191 if ( be->be_limits != NULL ) {
192 for ( ; be->be_limits[i]; i++ );
195 be->be_limits = ( struct slap_limits ** )ch_realloc( be->be_limits,
196 sizeof( struct slap_limits * ) * ( i + 2 ) );
197 be->be_limits[i] = lm;
198 be->be_limits[i+1] = NULL;
212 int type = SLAP_LIMITS_UNDEFINED;
214 struct slap_limits_set limit;
221 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
222 "%s : line %d: missing arg(s) in "
223 "\"limits <pattern> <limits>\" line.\n",
226 Debug( LDAP_DEBUG_ANY,
227 "%s : line %d: missing arg(s) in "
228 "\"limits <pattern> <limits>\" line.\n%s",
234 limit = be->be_def_limit;
239 * "limits" <pattern> <limit> [ ... ]
246 * [ "dn" [ "." { "exact" | "base" | "one" | "sub" | children"
247 * | "regex" | "anonymous" } ] "=" ] <dn pattern>
250 * "exact" and "base" are the same (exact match);
251 * "one" means exactly one rdn below, NOT including the pattern
252 * "sub" means any rdn below, including the pattern
253 * "children" means any rdn below, NOT including the pattern
255 * "anonymous" may be deprecated in favour
256 * of the pattern = "anonymous" form
261 * "time" [ "." { "soft" | "hard" } ] "=" <integer>
263 * "size" [ "." { "soft" | "hard" | "unchecked" } ] "=" <integer>
267 if ( strcasecmp( pattern, "anonymous" ) == 0 ) {
268 type = SLAP_LIMITS_ANONYMOUS;
270 } else if ( strcasecmp( pattern, "users" ) == 0 ) {
271 type = SLAP_LIMITS_USERS;
273 } else if ( strncasecmp( pattern, "dn", 2 ) == 0 ) {
275 if ( pattern[0] == '.' ) {
277 if ( strncasecmp( pattern, "exact", 5 ) == 0 ) {
278 type = SLAP_LIMITS_EXACT;
281 } else if ( strncasecmp( pattern, "base", 4 ) == 0 ) {
282 type = SLAP_LIMITS_BASE;
285 } else if ( strncasecmp( pattern, "one", 3 ) == 0 ) {
286 type = SLAP_LIMITS_ONE;
289 } else if ( strncasecmp( pattern, "subtree", 7 ) == 0 ) {
290 type = SLAP_LIMITS_SUBTREE;
293 } else if ( strncasecmp( pattern, "children", 8 ) == 0 ) {
294 type = SLAP_LIMITS_CHILDREN;
297 } else if ( strncasecmp( pattern, "regex", 5 ) == 0 ) {
298 type = SLAP_LIMITS_REGEX;
302 * this could be deprecated in favour
303 * of the pattern = "anonymous" form
305 } else if ( strncasecmp( pattern, "anonymous", 9 ) == 0 ) {
306 type = SLAP_LIMITS_ANONYMOUS;
311 /* pre-check the data */
313 case SLAP_LIMITS_ANONYMOUS:
314 case SLAP_LIMITS_USERS:
316 /* no need for pattern */
321 if ( pattern[0] != '=' ) {
323 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
324 "%s : line %d: missing '=' in "
325 "\"dn[.{exact|base|one|subtree"
326 "|children|regex|anonymous}]"
328 "\"limits <pattern> <limits>\" line.\n",
331 Debug( LDAP_DEBUG_ANY,
332 "%s : line %d: missing '=' in "
333 "\"dn[.{exact|base|one|subtree"
334 "|children|regex|anonymous}]"
336 "\"limits <pattern> <limits>\" "
343 /* skip '=' (required) */
349 for ( i = 2; i < argc; i++ ) {
350 if ( parse_limit( argv[i], &limit ) ) {
353 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
354 "%s : line %d: unknown limit type \"%s\" in "
355 "\"limits <pattern> <limits>\" line.\n",
356 fname, lineno, argv[i] ));
358 Debug( LDAP_DEBUG_ANY,
359 "%s : line %d: unknown limit type \"%s\" in "
360 "\"limits <pattern> <limits>\" line.\n",
361 fname, lineno, argv[i] );
371 if ( limit.lms_t_hard > 0 && limit.lms_t_hard < limit.lms_t_soft ) {
372 limit.lms_t_hard = limit.lms_t_soft;
375 if ( limit.lms_s_hard > 0 && limit.lms_s_hard < limit.lms_s_soft ) {
376 limit.lms_s_hard = limit.lms_s_soft;
379 return( add_limits( be, type, pattern, &limit ) );
385 struct slap_limits_set *limit
391 if ( strncasecmp( arg, "time", 4 ) == 0 ) {
394 if ( arg[0] == '.' ) {
396 if ( strncasecmp( arg, "soft", 4 ) == 0 ) {
398 if ( arg[0] != '=' ) {
402 limit->lms_t_soft = atoi( arg );
404 } else if ( strncasecmp( arg, "hard", 4 ) == 0 ) {
406 if ( arg[0] != '=' ) {
410 if ( strcasecmp( arg, "soft" ) == 0 ) {
411 limit->lms_t_hard = 0;
412 } else if ( strcasecmp( arg, "none" ) == 0 ) {
413 limit->lms_t_hard = -1;
415 limit->lms_t_hard = atoi( arg );
422 } else if ( arg[0] == '=' ) {
423 limit->lms_t_soft = atoi( arg );
424 limit->lms_t_hard = 0;
430 } else if ( strncasecmp( arg, "size", 4 ) == 0 ) {
433 if ( arg[0] == '.' ) {
435 if ( strncasecmp( arg, "soft", 4 ) == 0 ) {
437 if ( arg[0] != '=' ) {
441 limit->lms_s_soft = atoi( arg );
443 } else if ( strncasecmp( arg, "hard", 4 ) == 0 ) {
445 if ( arg[0] != '=' ) {
449 if ( strcasecmp( arg, "soft" ) == 0 ) {
450 limit->lms_s_hard = 0;
451 } else if ( strcasecmp( arg, "none" ) == 0 ) {
452 limit->lms_s_hard = -1;
454 limit->lms_s_hard = atoi( arg );
457 } else if ( strncasecmp( arg, "unchecked", 9 ) == 0 ) {
459 if ( arg[0] != '=' ) {
463 if ( strcasecmp( arg, "none" ) == 0 ) {
464 limit->lms_s_unchecked = -1;
466 limit->lms_s_unchecked = atoi( arg );
473 } else if ( arg[0] == '=' ) {
474 limit->lms_s_soft = atoi( arg );
475 limit->lms_s_hard = 0;