2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2000-2008 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
19 #include <ac/string.h>
24 static BerVarray set_chase( SLAP_SET_GATHER gatherer,
25 SetCookie *cookie, BerVarray set, AttributeDescription *desc, int closure );
28 slap_set_size( BerVarray set )
34 while ( !BER_BVISNULL( &set[ i ] ) ) {
42 slap_set_isempty( BerVarray set )
48 if ( !BER_BVISNULL( &set[ 0 ] ) ) {
56 slap_set_dispose( SetCookie *cp, BerVarray set, unsigned flags )
58 if ( flags & SLAP_SET_REFVAL ) {
59 if ( ! ( flags & SLAP_SET_REFARR ) ) {
60 cp->set_op->o_tmpfree( set, cp->set_op->o_tmpmemctx );
64 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
69 set_dup( SetCookie *cp, BerVarray set, unsigned flags )
71 BerVarray newset = NULL;
77 if ( flags & SLAP_SET_REFARR ) {
80 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ )
82 newset = cp->set_op->o_tmpcalloc( i + 1,
83 sizeof( struct berval ),
84 cp->set_op->o_tmpmemctx );
85 if ( newset == NULL ) {
89 if ( flags & SLAP_SET_REFVAL ) {
90 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
91 ber_dupbv_x( &newset[ i ], &set[ i ],
92 cp->set_op->o_tmpmemctx );
96 AC_MEMCPY( newset, set, ( i + 1 ) * sizeof( struct berval ) );
114 long i, j, last, rlast;
115 unsigned op = ( op_flags & SLAP_SET_OPMASK );
119 case '|': /* union */
120 if ( lset == NULL || BER_BVISNULL( &lset[ 0 ] ) ) {
121 if ( rset == NULL ) {
122 if ( lset == NULL ) {
123 set = cp->set_op->o_tmpcalloc( 1,
124 sizeof( struct berval ),
125 cp->set_op->o_tmpmemctx );
126 BER_BVZERO( &set[ 0 ] );
129 return set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
131 slap_set_dispose( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
132 return set_dup( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
134 if ( rset == NULL || BER_BVISNULL( &rset[ 0 ] ) ) {
135 slap_set_dispose( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
136 return set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
139 /* worst scenario: no duplicates */
140 rlast = slap_set_size( rset );
141 i = slap_set_size( lset ) + rlast + 1;
142 set = cp->set_op->o_tmpcalloc( i, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
144 /* set_chase() depends on this routine to
145 * keep the first elements of the result
146 * set the same (and in the same order)
149 for ( i = 0; !BER_BVISNULL( &lset[ i ] ); i++ ) {
150 if ( op_flags & SLAP_SET_LREFVAL ) {
151 ber_dupbv_x( &set[ i ], &lset[ i ], cp->set_op->o_tmpmemctx );
154 set[ i ] = lset[ i ];
158 /* pointers to values have been used in set - don't free twice */
159 op_flags |= SLAP_SET_LREFVAL;
163 for ( i = 0; !BER_BVISNULL( &rset[ i ] ); i++ ) {
166 for ( j = 0; !BER_BVISNULL( &set[ j ] ); j++ ) {
167 if ( bvmatch( &rset[ i ], &set[ j ] ) )
169 if ( !( op_flags & SLAP_SET_RREFVAL ) ) {
170 cp->set_op->o_tmpfree( rset[ i ].bv_val, cp->set_op->o_tmpmemctx );
171 rset[ i ] = rset[ --rlast ];
172 BER_BVZERO( &rset[ rlast ] );
180 if ( op_flags & SLAP_SET_RREFVAL ) {
181 ber_dupbv_x( &set[ last ], &rset[ i ], cp->set_op->o_tmpmemctx );
184 set[ last ] = rset[ i ];
190 /* pointers to values have been used in set - don't free twice */
191 op_flags |= SLAP_SET_RREFVAL;
193 BER_BVZERO( &set[ last ] );
197 case '&': /* intersection */
198 if ( lset == NULL || BER_BVISNULL( &lset[ 0 ] )
199 || rset == NULL || BER_BVISNULL( &rset[ 0 ] ) )
201 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
202 cp->set_op->o_tmpmemctx );
203 BER_BVZERO( &set[ 0 ] );
210 llen = slap_set_size( lset );
211 rlen = slap_set_size( rset );
213 /* dup the shortest */
216 set = set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
222 set = set_dup( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
231 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
232 for ( j = 0; !BER_BVISNULL( &sset[ j ] ); j++ ) {
233 if ( bvmatch( &set[ i ], &sset[ j ] ) ) {
238 if ( BER_BVISNULL( &sset[ j ] ) ) {
239 cp->set_op->o_tmpfree( set[ i ].bv_val, cp->set_op->o_tmpmemctx );
240 set[ i ] = set[ --last ];
241 BER_BVZERO( &set[ last ] );
248 case '+': /* string concatenation */
249 i = slap_set_size( rset );
250 j = slap_set_size( lset );
252 /* handle empty set cases */
255 set = cp->set_op->o_tmpcalloc( i * j + 1, sizeof( struct berval ),
256 cp->set_op->o_tmpmemctx );
260 BER_BVZERO( &set[ 0 ] );
264 set = set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
269 } else if ( j == 0 ) {
270 set = set_dup( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
275 set = cp->set_op->o_tmpcalloc( i * j + 1, sizeof( struct berval ),
276 cp->set_op->o_tmpmemctx );
281 for ( last = 0, i = 0; !BER_BVISNULL( &lset[ i ] ); i++ ) {
282 for ( j = 0; !BER_BVISNULL( &rset[ j ] ); j++ ) {
286 /* don't concatenate with the empty string */
287 if ( BER_BVISEMPTY( &lset[ i ] ) ) {
288 ber_dupbv_x( &bv, &rset[ j ], cp->set_op->o_tmpmemctx );
289 if ( bv.bv_val == NULL ) {
290 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
295 } else if ( BER_BVISEMPTY( &rset[ j ] ) ) {
296 ber_dupbv_x( &bv, &lset[ i ], cp->set_op->o_tmpmemctx );
297 if ( bv.bv_val == NULL ) {
298 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
304 bv.bv_len = lset[ i ].bv_len + rset[ j ].bv_len;
305 bv.bv_val = cp->set_op->o_tmpalloc( bv.bv_len + 1,
306 cp->set_op->o_tmpmemctx );
307 if ( bv.bv_val == NULL ) {
308 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
312 AC_MEMCPY( bv.bv_val, lset[ i ].bv_val, lset[ i ].bv_len );
313 AC_MEMCPY( &bv.bv_val[ lset[ i ].bv_len ], rset[ j ].bv_val, rset[ j ].bv_len );
314 bv.bv_val[ bv.bv_len ] = '\0';
317 for ( k = 0; k < last; k++ ) {
318 if ( bvmatch( &set[ k ], &bv ) ) {
319 cp->set_op->o_tmpfree( bv.bv_val, cp->set_op->o_tmpmemctx );
329 BER_BVZERO( &set[ last ] );
337 if ( lset ) slap_set_dispose( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
338 if ( rset ) slap_set_dispose( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
344 set_chase( SLAP_SET_GATHER gatherer,
345 SetCookie *cp, BerVarray set, AttributeDescription *desc, int closure )
347 BerVarray vals, nset;
351 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
352 cp->set_op->o_tmpmemctx );
354 BER_BVZERO( &set[ 0 ] );
359 if ( BER_BVISNULL( set ) ) {
363 nset = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
364 if ( nset == NULL ) {
365 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
368 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
369 vals = gatherer( cp, &set[ i ], desc );
370 if ( vals != NULL ) {
371 nset = slap_set_join( cp, nset, '|', vals );
374 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
377 for ( i = 0; !BER_BVISNULL( &nset[ i ] ); i++ ) {
378 vals = gatherer( cp, &nset[ i ], desc );
379 if ( vals != NULL ) {
380 nset = slap_set_join( cp, nset, '|', vals );
381 if ( nset == NULL ) {
392 slap_set_filter( SLAP_SET_GATHER gatherer,
393 SetCookie *cp, struct berval *fbv,
394 struct berval *user, struct berval *target, BerVarray *results )
396 #define STACK_SIZE 64
397 #define IS_SET(x) ( (unsigned long)(x) >= 256 )
398 #define IS_OP(x) ( (unsigned long)(x) < 256 )
399 #define SF_ERROR(x) do { rc = -1; goto _error; } while ( 0 )
400 #define SF_TOP() ( (BerVarray)( ( stp < 0 ) ? 0 : stack[ stp ] ) )
401 #define SF_POP() ( (BerVarray)( ( stp < 0 ) ? 0 : stack[ stp-- ] ) )
402 #define SF_PUSH(x) do { \
403 if ( stp >= ( STACK_SIZE - 1 ) ) SF_ERROR( overflow ); \
404 stack[ ++stp ] = (BerVarray)(long)(x); \
408 BerVarray stack[ STACK_SIZE ] = { 0 };
411 char c, *filter = fbv->bv_val;
418 while ( ( c = *filter++ ) ) {
428 if ( IS_SET( SF_TOP() ) ) {
436 if ( IS_OP( set ) ) {
439 if ( SF_TOP() == (void *)'(' /* ) */ ) {
444 } else if ( IS_OP( SF_TOP() ) ) {
445 op = (unsigned long)SF_POP();
448 set = slap_set_join( cp, lset, op, set );
460 case '|': /* union */
461 case '&': /* intersection */
462 case '+': /* string concatenation */
464 if ( IS_OP( set ) ) {
467 if ( SF_TOP() == 0 || SF_TOP() == (void *)'(' /* ) */ ) {
471 } else if ( IS_OP( SF_TOP() ) ) {
472 op = (unsigned long)SF_POP();
474 set = slap_set_join( cp, lset, op, set );
488 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
491 for ( len = 0; ( c = *filter++ ) && ( c != /* [ */ ']' ); len++ )
497 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
498 cp->set_op->o_tmpmemctx );
502 set->bv_val = cp->set_op->o_tmpcalloc( len + 1, sizeof( char ),
503 cp->set_op->o_tmpmemctx );
504 if ( BER_BVISNULL( set ) ) {
507 AC_MEMCPY( set->bv_val, &filter[ - len - 1 ], len );
518 /* fall through to next case */
521 if ( IS_OP( SF_TOP() ) ) {
529 && ( c < 'A' || c > 'Z' )
530 && ( c < 'a' || c > 'z' ) )
536 ( c = filter[ len ] )
537 && ( ( c >= '0' && c <= '9' )
538 || ( c >= 'A' && c <= 'Z' )
539 || ( c >= 'a' && c <= 'z' ) );
543 && memcmp( "this", filter, len ) == 0 )
545 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
548 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
549 cp->set_op->o_tmpmemctx );
553 ber_dupbv_x( set, target, cp->set_op->o_tmpmemctx );
554 if ( BER_BVISNULL( set ) ) {
557 BER_BVZERO( &set[ 1 ] );
560 && memcmp( "user", filter, len ) == 0 )
562 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
565 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
566 cp->set_op->o_tmpmemctx );
570 ber_dupbv_x( set, user, cp->set_op->o_tmpmemctx );
571 if ( BER_BVISNULL( set ) ) {
574 BER_BVZERO( &set[ 1 ] );
576 } else if ( SF_TOP() != (void *)'/' ) {
581 AttributeDescription *ad = NULL;
582 const char *text = NULL;
588 if ( slap_bv2ad( &fb2, &ad, &text ) != LDAP_SUCCESS ) {
592 /* NOTE: ad must have distinguishedName syntax
593 * or expand in an LDAP URI if c == '*'
596 set = set_chase( gatherer,
597 cp, SF_POP(), ad, c == '*' );
613 if ( IS_OP( set ) ) {
616 if ( SF_TOP() == 0 ) {
619 } else if ( IS_OP( SF_TOP() ) ) {
620 op = (unsigned long)SF_POP();
622 set = slap_set_join( cp, lset, op, set );
631 rc = slap_set_isempty( set ) ? 0 : 1;
638 if ( IS_SET( set ) ) {
639 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
641 while ( ( set = SF_POP() ) ) {
642 if ( IS_SET( set ) ) {
643 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );