2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2000-2007 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 ) );
115 unsigned op = ( op_flags & SLAP_SET_OPMASK );
119 case '|': /* union */
120 if ( lset == NULL || BER_BVISNULL( lset ) ) {
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 );
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 ) ) {
135 slap_set_dispose( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
136 return set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
139 i = slap_set_size( lset ) + slap_set_size( rset ) + 1;
140 set = cp->set_op->o_tmpcalloc( i, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
142 /* set_chase() depends on this routine to
143 * keep the first elements of the result
144 * set the same (and in the same order)
147 for ( i = 0; !BER_BVISNULL( &lset[ i ] ); i++ ) {
148 if ( op_flags & SLAP_SET_LREFVAL ) {
149 ber_dupbv_x( &set[ i ], &lset[ i ], cp->set_op->o_tmpmemctx );
152 set[ i ] = lset[ i ];
158 for ( i = 0; !BER_BVISNULL( &rset[ i ] ); i++ ) {
161 for ( j = 0; !BER_BVISNULL( &set[ j ] ); j++ ) {
162 if ( bvmatch( &rset[ i ], &set[ j ] ) )
164 if ( !( op_flags & SLAP_SET_RREFVAL ) ) {
165 cp->set_op->o_tmpfree( rset[ i ].bv_val, cp->set_op->o_tmpmemctx );
166 BER_BVZERO( &rset[ i ] );
174 if ( op_flags & SLAP_SET_RREFVAL ) {
175 ber_dupbv_x( &set[ last ], &rset[ i ], cp->set_op->o_tmpmemctx );
178 set[ last ] = rset[ i ];
183 BER_BVZERO( &set[ last ] );
187 case '&': /* intersection */
188 if ( lset == NULL || BER_BVISNULL( lset )
189 || rset == NULL || BER_BVISNULL( rset ) )
191 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
192 cp->set_op->o_tmpmemctx );
196 set = set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
201 last = slap_set_size( set ) - 1;
202 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
203 for ( j = 0; !BER_BVISNULL( &rset[ j ] ); j++ ) {
204 if ( bvmatch( &set[ i ], &rset[ j ] ) ) {
209 if ( BER_BVISNULL( &rset[ j ] ) ) {
210 cp->set_op->o_tmpfree( set[ i ].bv_val, cp->set_op->o_tmpmemctx );
211 set[ i ] = set[ last ];
212 BER_BVZERO( &set[ last ] );
220 case '+': /* string concatenation */
221 i = slap_set_size( rset );
222 j = slap_set_size( lset );
224 set = cp->set_op->o_tmpcalloc( i * j + 1, sizeof( struct berval ),
225 cp->set_op->o_tmpmemctx );
230 for ( last = 0, i = 0; !BER_BVISNULL( &lset[ i ] ); i++ ) {
231 for ( j = 0; !BER_BVISNULL( &rset[ j ] ); j++ ) {
235 bv.bv_len = lset[ i ].bv_len + rset[ j ].bv_len;
236 bv.bv_val = cp->set_op->o_tmpalloc( bv.bv_len + 1,
237 cp->set_op->o_tmpmemctx );
238 if ( bv.bv_val == NULL ) {
239 slap_set_dispose( cp, set, 0 );
243 AC_MEMCPY( bv.bv_val, lset[ i ].bv_val, lset[ i ].bv_len );
244 AC_MEMCPY( &bv.bv_val[ lset[ i ].bv_len ], rset[ j ].bv_val, rset[ j ].bv_len );
245 bv.bv_val[ bv.bv_len ] = '\0';
247 for ( k = 0; k < last; k++ ) {
248 if ( bvmatch( &set[ k ], &bv ) ) {
249 cp->set_op->o_tmpfree( bv.bv_val, cp->set_op->o_tmpmemctx );
259 BER_BVZERO( &set[ last ] );
267 if ( !( op_flags & SLAP_SET_LREFARR ) && lset != NULL ) {
268 if ( !( op_flags & SLAP_SET_LREFVAL ))
269 cp->set_op->o_tmpfree( lset->bv_val, cp->set_op->o_tmpmemctx );
270 cp->set_op->o_tmpfree( lset, cp->set_op->o_tmpmemctx );
273 if ( !( op_flags & SLAP_SET_RREFARR ) && rset != NULL ) {
274 if ( !( op_flags & SLAP_SET_RREFVAL ))
275 cp->set_op->o_tmpfree( rset->bv_val, cp->set_op->o_tmpmemctx );
276 cp->set_op->o_tmpfree( rset, cp->set_op->o_tmpmemctx );
283 set_chase( SLAP_SET_GATHER gatherer,
284 SetCookie *cp, BerVarray set, AttributeDescription *desc, int closure )
286 BerVarray vals, nset;
290 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
291 cp->set_op->o_tmpmemctx );
296 if ( BER_BVISNULL( set ) ) {
300 nset = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
301 if ( nset == NULL ) {
302 slap_set_dispose( cp, set, 0 );
305 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
306 vals = gatherer( cp, &set[ i ], desc );
307 if ( vals != NULL ) {
308 nset = slap_set_join( cp, nset, '|', vals );
311 slap_set_dispose( cp, set, 0 );
314 for ( i = 0; !BER_BVISNULL( &nset[ i ] ); i++ ) {
315 vals = gatherer( cp, &nset[ i ], desc );
316 if ( vals != NULL ) {
317 nset = slap_set_join( cp, nset, '|', vals );
318 if ( nset == NULL ) {
330 set_parents( SetCookie *cp, BerVarray set )
333 struct berval bv, pbv;
334 BerVarray nset, vals;
337 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
338 cp->set_op->o_tmpmemctx );
340 BER_BVZERO( &set[ 0 ] );
345 if ( BER_BVISNULL( &set[ 0 ] ) ) {
349 nset = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
350 if ( nset == NULL ) {
351 slap_set_dispose( cp, set, 0 );
355 BER_BVZERO( &nset[ 0 ] );
357 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
361 for ( ; !BER_BVISEMPTY( &pbv ); dnParent( &bv, &pbv ) ) {
366 vals = cp->set_op->o_tmpcalloc( level + 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
367 if ( vals == NULL ) {
368 slap_set_dispose( cp, set, 0 );
369 slap_set_dispose( cp, nset, 0 );
372 BER_BVZERO( &vals[ 0 ] );
376 for ( j = 0 ; j < level ; j++ ) {
377 ber_dupbv_x( &vals[ last ], &bv, cp->set_op->o_tmpmemctx );
379 dnParent( &bv, &bv );
381 BER_BVZERO( &vals[ last ] );
383 nset = slap_set_join( cp, nset, '|', vals );
386 slap_set_dispose( cp, set, 0 );
394 set_parent( SetCookie *cp, BerVarray set, int level )
401 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
402 cp->set_op->o_tmpmemctx );
404 BER_BVZERO( &set[ 0 ] );
409 if ( BER_BVISNULL( &set[ 0 ] ) ) {
413 nset = cp->set_op->o_tmpcalloc( slap_set_size( set ) + 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
414 if ( nset == NULL ) {
415 slap_set_dispose( cp, set, 0 );
419 BER_BVZERO( &nset[ 0 ] );
422 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
425 for ( j = 0 ; j < level ; j++ ) {
426 dnParent( &bv, &bv );
429 for ( j = 0; !BER_BVISNULL( &nset[ j ] ); j++ ) {
430 if ( bvmatch( &bv, &nset[ j ] ) )
436 if ( BER_BVISNULL( &nset[ j ] ) ) {
437 ber_dupbv_x( &nset[ last ], &bv, cp->set_op->o_tmpmemctx );
442 BER_BVZERO( &nset[ last ] );
444 slap_set_dispose( cp, set, 0 );
450 slap_set_filter( SLAP_SET_GATHER gatherer,
451 SetCookie *cp, struct berval *fbv,
452 struct berval *user, struct berval *target, BerVarray *results )
454 #define STACK_SIZE 64
455 #define IS_SET(x) ( (unsigned long)(x) >= 256 )
456 #define IS_OP(x) ( (unsigned long)(x) < 256 )
457 #define SF_ERROR(x) do { rc = -1; goto _error; } while ( 0 )
458 #define SF_TOP() ( (BerVarray)( ( stp < 0 ) ? 0 : stack[ stp ] ) )
459 #define SF_POP() ( (BerVarray)( ( stp < 0 ) ? 0 : stack[ stp-- ] ) )
460 #define SF_PUSH(x) do { \
461 if ( stp >= ( STACK_SIZE - 1 ) ) SF_ERROR( overflow ); \
462 stack[ ++stp ] = (BerVarray)(long)(x); \
466 BerVarray stack[ STACK_SIZE ] = { 0 };
469 char c, *filter = fbv->bv_val;
476 while ( ( c = *filter++ ) ) {
486 if ( IS_SET( SF_TOP() ) ) {
494 if ( IS_OP( set ) ) {
497 if ( SF_TOP() == (void *)'(' /* ) */ ) {
502 } else if ( IS_OP( SF_TOP() ) ) {
503 op = (unsigned long)SF_POP();
506 set = slap_set_join( cp, lset, op, set );
518 case '|': /* union */
519 case '&': /* intersection */
520 case '+': /* string concatenation */
522 if ( IS_OP( set ) ) {
525 if ( SF_TOP() == 0 || SF_TOP() == (void *)'(' /* ) */ ) {
529 } else if ( IS_OP( SF_TOP() ) ) {
530 op = (unsigned long)SF_POP();
532 set = slap_set_join( cp, lset, op, set );
546 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
549 for ( len = 0; ( c = *filter++ ) && ( c != /* [ */ ']' ); len++ )
555 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
556 cp->set_op->o_tmpmemctx );
560 set->bv_val = cp->set_op->o_tmpcalloc( len + 1, sizeof( char ),
561 cp->set_op->o_tmpmemctx );
562 if ( BER_BVISNULL( set ) ) {
565 AC_MEMCPY( set->bv_val, &filter[ - len - 1 ], len );
572 if ( ( SF_TOP() == (void *)'/' )
573 && ( *filter >= '0' || *filter <= '9' ) )
579 ( c = filter[ len ] )
580 && ( c >= '0' && c <= '9' );
583 for ( i = len ; i > 0 ; i-- ) {
584 parent += (int)( filter[ i - 1 ] - '0' ) * count;
589 set = set_parents( cp, SF_POP() );
591 set = set_parent( cp, SF_POP(), parent );
602 /* fall through to next case */
606 if ( IS_OP( SF_TOP() ) ) {
614 && ( c < 'A' || c > 'Z' )
615 && ( c < 'a' || c > 'z' ) )
621 ( c = filter[ len ] )
622 && ( ( c >= '0' && c <= '9' )
623 || ( c >= 'A' && c <= 'Z' )
624 || ( c >= 'a' && c <= 'z' ) );
628 && memcmp( "this", filter, len ) == 0 )
630 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
633 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
634 cp->set_op->o_tmpmemctx );
638 ber_dupbv_x( set, target, cp->set_op->o_tmpmemctx );
639 if ( BER_BVISNULL( set ) ) {
642 BER_BVZERO( &set[ 1 ] );
645 && memcmp( "user", filter, len ) == 0 )
647 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
650 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
651 cp->set_op->o_tmpmemctx );
655 ber_dupbv_x( set, user, cp->set_op->o_tmpmemctx );
656 if ( BER_BVISNULL( set ) ) {
659 BER_BVZERO( &set[ 1 ] );
661 } else if ( SF_TOP() != (void *)'/' ) {
666 AttributeDescription *ad = NULL;
667 const char *text = NULL;
673 if ( slap_bv2ad( &fb2, &ad, &text ) != LDAP_SUCCESS ) {
677 /* NOTE: ad must have distinguishedName syntax
678 * or expand in an LDAP URI if c == '*'
681 set = set_chase( gatherer,
682 cp, SF_POP(), ad, c == '*' );
698 if ( IS_OP( set ) ) {
701 if ( SF_TOP() == 0 ) {
704 } else if ( IS_OP( SF_TOP() ) ) {
705 op = (unsigned long)SF_POP();
707 set = slap_set_join( cp, lset, op, set );
716 rc = slap_set_isempty( set ) ? 0 : 1;
723 if ( IS_SET( set ) ) {
724 slap_set_dispose( cp, set, 0 );
726 while ( ( set = SF_POP() ) ) {
727 if ( IS_SET( set ) ) {
728 slap_set_dispose( cp, set, 0 );