1 /* limits.c - routines to handle regex-based size and time limits */
3 * Copyright 1998-2000 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 == NULL || ndn[0] == '\0' ) {
43 if ( strcmp( lm[0]->lm_dn_pat->bv_val, ndn ) == 0 ) {
44 *limit = &lm[0]->lm_limits;
50 case SLAP_LIMITS_SUBTREE:
51 case SLAP_LIMITS_CHILDREN: {
54 if ( ndn == NULL || ndn[0] == '\0' ) {
58 d = strlen( ndn ) - 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[d-1] ) || DN_ESCAPE( ndn[d-2] ) ) {
76 /* in case of (sub)match ... */
77 if ( strcmp( lm[0]->lm_dn_pat->bv_val, &ndn[d] ) == 0 ) {
78 /* check for exactly one rdn in case of ONE */
79 if ( lm[0]->lm_type == SLAP_LIMITS_ONE ) {
81 * if ndn is more that one rdn
82 * below dn_pat, continue
84 if ( dn_rdnlen( NULL, ndn ) != d - 1 ) {
89 *limit = &lm[0]->lm_limits;
96 case SLAP_LIMITS_REGEX:
97 if ( ndn == NULL || ndn[0] == '\0' ) {
100 if ( regexec( &lm[0]->lm_dn_regex, ndn, 0, NULL, 0 ) == 0 ) {
101 *limit = &lm[0]->lm_limits;
106 case SLAP_LIMITS_ANONYMOUS:
107 if ( ndn == NULL || ndn[0] == '\0' ) {
108 *limit = &lm[0]->lm_limits;
113 case SLAP_LIMITS_USERS:
114 if ( ndn != NULL && ndn[0] != '\0' ) {
115 *limit = &lm[0]->lm_limits;
121 assert( 0 ); /* unreachable */
134 struct slap_limits_set *limit
138 struct slap_limits *lm;
143 lm = ( struct slap_limits * )ch_calloc( sizeof( struct slap_limits ), 1 );
146 case SLAP_LIMITS_EXACT:
147 case SLAP_LIMITS_ONE:
148 case SLAP_LIMITS_SUBTREE:
149 case SLAP_LIMITS_CHILDREN:
151 lm->lm_dn_pat = ber_bvstrdup( pattern );
152 if ( dn_normalize( lm->lm_dn_pat->bv_val ) == NULL ) {
153 ber_bvfree( lm->lm_dn_pat );
159 case SLAP_LIMITS_REGEX:
160 case SLAP_LIMITS_UNDEFINED:
161 lm->lm_type = SLAP_LIMITS_REGEX;
162 lm->lm_dn_pat = ber_bvstrdup( pattern );
163 if ( regcomp( &lm->lm_dn_regex, lm->lm_dn_pat->bv_val,
164 REG_EXTENDED | REG_ICASE ) ) {
165 ber_bvfree( lm->lm_dn_pat );
171 case SLAP_LIMITS_ANONYMOUS:
172 case SLAP_LIMITS_USERS:
174 lm->lm_dn_pat = NULL;
178 lm->lm_limits = *limit;
181 if ( be->be_limits != NULL ) {
182 for ( ; be->be_limits[i]; i++ );
185 be->be_limits = ( struct slap_limits ** )ch_realloc( be->be_limits,
186 sizeof( struct slap_limits * ) * ( i + 2 ) );
187 be->be_limits[i] = lm;
188 be->be_limits[i+1] = NULL;
202 int type = SLAP_LIMITS_UNDEFINED;
204 struct slap_limits_set limit;
211 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
212 "%s : line %d: missing arg(s) in "
213 "\"limits <pattern> <limits>\" line.\n",
216 Debug( LDAP_DEBUG_ANY,
217 "%s : line %d: missing arg(s) in "
218 "\"limits <pattern> <limits>\" line.\n%s",
224 limit = be->be_def_limit;
229 * "limits" <pattern> <limit> [ ... ]
236 * [ "dn" [ "." { "exact" | "base" | "one" | "sub" | children"
237 * | "regex" | "anonymous" } ] "=" ] <dn pattern>
240 * "exact" and "base" are the same (exact match);
241 * "one" means exactly one rdn below, NOT including the pattern
242 * "sub" means any rdn below, including the pattern
243 * "children" means any rdn below, NOT including the pattern
245 * "anonymous" may be deprecated in favour
246 * of the pattern = "anonymous" form
251 * "time" [ "." { "soft" | "hard" } ] "=" <integer>
253 * "size" [ "." { "soft" | "hard" | "unchecked" } ] "=" <integer>
257 if ( strcasecmp( pattern, "anonymous" ) == 0 ) {
258 type = SLAP_LIMITS_ANONYMOUS;
260 } else if ( strcasecmp( pattern, "users" ) == 0 ) {
261 type = SLAP_LIMITS_USERS;
263 } else if ( strncasecmp( pattern, "dn", 2 ) == 0 ) {
265 if ( pattern[0] == '.' ) {
267 if ( strncasecmp( pattern, "exact", 5 ) == 0 ) {
268 type = SLAP_LIMITS_EXACT;
271 } else if ( strncasecmp( pattern, "base", 4 ) == 0 ) {
272 type = SLAP_LIMITS_BASE;
275 } else if ( strncasecmp( pattern, "one", 3 ) == 0 ) {
276 type = SLAP_LIMITS_ONE;
279 } else if ( strncasecmp( pattern, "subtree", 7 ) == 0 ) {
280 type = SLAP_LIMITS_SUBTREE;
283 } else if ( strncasecmp( pattern, "children", 8 ) == 0 ) {
284 type = SLAP_LIMITS_CHILDREN;
287 } else if ( strncasecmp( pattern, "regex", 5 ) == 0 ) {
288 type = SLAP_LIMITS_REGEX;
292 * this could be deprecated in favour
293 * of the pattern = "anonymous" form
295 } else if ( strncasecmp( pattern, "anonymous", 9 ) == 0 ) {
296 type = SLAP_LIMITS_ANONYMOUS;
301 /* pre-check the data */
303 case SLAP_LIMITS_ANONYMOUS:
304 case SLAP_LIMITS_USERS:
306 /* no need for pattern */
311 if ( pattern[0] != '=' ) {
313 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
314 "%s : line %d: missing '=' in "
315 "\"dn[.{exact|base|one|subtree"
316 "|children|regex|anonymous}]"
318 "\"limits <pattern> <limits>\" line.\n",
321 Debug( LDAP_DEBUG_ANY,
322 "%s : line %d: missing '=' in "
323 "\"dn[.{exact|base|one|subtree"
324 "|children|regex|anonymous}]"
326 "\"limits <pattern> <limits>\" "
333 /* skip '=' (required) */
339 for ( i = 2; i < argc; i++ ) {
340 if ( parse_limit( argv[i], &limit ) ) {
343 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
344 "%s : line %d: unknown limit type \"%s\" in "
345 "\"limits <pattern> <limits>\" line.\n",
346 fname, lineno, argv[i] ));
348 Debug( LDAP_DEBUG_ANY,
349 "%s : line %d: unknown limit type \"%s\" in "
350 "\"limits <pattern> <limits>\" line.\n",
351 fname, lineno, argv[i] );
361 if ( limit.lms_t_hard > 0 && limit.lms_t_hard < limit.lms_t_soft ) {
362 limit.lms_t_hard = limit.lms_t_soft;
365 if ( limit.lms_s_hard > 0 && limit.lms_s_hard < limit.lms_s_soft ) {
366 limit.lms_s_hard = limit.lms_s_soft;
369 return( add_limits( be, type, pattern, &limit ) );
375 struct slap_limits_set *limit
381 if ( strncasecmp( arg, "time", 4 ) == 0 ) {
384 if ( arg[0] == '.' ) {
386 if ( strncasecmp( arg, "soft", 4 ) == 0 ) {
388 if ( arg[0] != '=' ) {
392 limit->lms_t_soft = atoi( arg );
394 } else if ( strncasecmp( arg, "hard", 4 ) == 0 ) {
396 if ( arg[0] != '=' ) {
400 if ( strcasecmp( arg, "soft" ) == 0 ) {
401 limit->lms_t_hard = 0;
402 } else if ( strcasecmp( arg, "none" ) == 0 ) {
403 limit->lms_t_hard = -1;
405 limit->lms_t_hard = atoi( arg );
412 } else if ( arg[0] == '=' ) {
413 limit->lms_t_soft = atoi( arg );
414 limit->lms_t_hard = 0;
420 } else if ( strncasecmp( arg, "size", 4 ) == 0 ) {
423 if ( arg[0] == '.' ) {
425 if ( strncasecmp( arg, "soft", 4 ) == 0 ) {
427 if ( arg[0] != '=' ) {
431 limit->lms_s_soft = atoi( arg );
433 } else if ( strncasecmp( arg, "hard", 4 ) == 0 ) {
435 if ( arg[0] != '=' ) {
439 if ( strcasecmp( arg, "soft" ) == 0 ) {
440 limit->lms_s_hard = 0;
441 } else if ( strcasecmp( arg, "none" ) == 0 ) {
442 limit->lms_s_hard = -1;
444 limit->lms_s_hard = atoi( arg );
447 } else if ( strncasecmp( arg, "unchecked", 9 ) == 0 ) {
449 if ( arg[0] != '=' ) {
453 if ( strcasecmp( arg, "none" ) == 0 ) {
454 limit->lms_s_unchecked = -1;
456 limit->lms_s_unchecked = atoi( arg );
463 } else if ( arg[0] == '=' ) {
464 limit->lms_s_soft = atoi( arg );
465 limit->lms_s_hard = 0;