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 ) );
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 */
215 set = set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
220 set = set_dup( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
229 last = slap_set_size( set );
230 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
231 for ( j = 0; !BER_BVISNULL( &sset[ j ] ); j++ ) {
232 if ( bvmatch( &set[ i ], &sset[ j ] ) ) {
237 if ( BER_BVISNULL( &sset[ j ] ) ) {
238 cp->set_op->o_tmpfree( set[ i ].bv_val, cp->set_op->o_tmpmemctx );
239 set[ i ] = set[ --last ];
240 BER_BVZERO( &set[ last ] );
247 case '+': /* string concatenation */
248 i = slap_set_size( rset );
249 j = slap_set_size( lset );
251 /* handle empty set cases */
254 set = cp->set_op->o_tmpcalloc( i * j + 1, sizeof( struct berval ),
255 cp->set_op->o_tmpmemctx );
259 BER_BVZERO( &set[ 0 ] );
263 set = set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
267 } else if ( j == 0 ) {
268 set = set_dup( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
272 set = cp->set_op->o_tmpcalloc( i * j + 1, sizeof( struct berval ),
273 cp->set_op->o_tmpmemctx );
278 for ( last = 0, i = 0; !BER_BVISNULL( &lset[ i ] ); i++ ) {
279 for ( j = 0; !BER_BVISNULL( &rset[ j ] ); j++ ) {
283 /* don't concatenate with the empty string */
284 if ( BER_BVISEMPTY( &lset[ i ] ) ) {
285 ber_dupbv_x( &bv, &rset[ j ], cp->set_op->o_tmpmemctx );
286 if ( bv.bv_val == NULL ) {
287 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
292 } else if ( BER_BVISEMPTY( &rset[ j ] ) ) {
293 ber_dupbv_x( &bv, &lset[ i ], cp->set_op->o_tmpmemctx );
294 if ( bv.bv_val == NULL ) {
295 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
301 bv.bv_len = lset[ i ].bv_len + rset[ j ].bv_len;
302 bv.bv_val = cp->set_op->o_tmpalloc( bv.bv_len + 1,
303 cp->set_op->o_tmpmemctx );
304 if ( bv.bv_val == NULL ) {
305 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
309 AC_MEMCPY( bv.bv_val, lset[ i ].bv_val, lset[ i ].bv_len );
310 AC_MEMCPY( &bv.bv_val[ lset[ i ].bv_len ], rset[ j ].bv_val, rset[ j ].bv_len );
311 bv.bv_val[ bv.bv_len ] = '\0';
314 for ( k = 0; k < last; k++ ) {
315 if ( bvmatch( &set[ k ], &bv ) ) {
316 cp->set_op->o_tmpfree( bv.bv_val, cp->set_op->o_tmpmemctx );
326 BER_BVZERO( &set[ last ] );
334 if ( lset ) slap_set_dispose( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
335 if ( rset ) slap_set_dispose( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
341 set_chase( SLAP_SET_GATHER gatherer,
342 SetCookie *cp, BerVarray set, AttributeDescription *desc, int closure )
344 BerVarray vals, nset;
348 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
349 cp->set_op->o_tmpmemctx );
351 BER_BVZERO( &set[ 0 ] );
356 if ( BER_BVISNULL( set ) ) {
360 nset = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
361 if ( nset == NULL ) {
362 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
365 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
366 vals = gatherer( cp, &set[ i ], desc );
367 if ( vals != NULL ) {
368 nset = slap_set_join( cp, nset, '|', vals );
371 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
374 for ( i = 0; !BER_BVISNULL( &nset[ i ] ); i++ ) {
375 vals = gatherer( cp, &nset[ i ], desc );
376 if ( vals != NULL ) {
377 nset = slap_set_join( cp, nset, '|', vals );
378 if ( nset == NULL ) {
389 slap_set_filter( SLAP_SET_GATHER gatherer,
390 SetCookie *cp, struct berval *fbv,
391 struct berval *user, struct berval *target, BerVarray *results )
393 #define STACK_SIZE 64
394 #define IS_SET(x) ( (unsigned long)(x) >= 256 )
395 #define IS_OP(x) ( (unsigned long)(x) < 256 )
396 #define SF_ERROR(x) do { rc = -1; goto _error; } while ( 0 )
397 #define SF_TOP() ( (BerVarray)( ( stp < 0 ) ? 0 : stack[ stp ] ) )
398 #define SF_POP() ( (BerVarray)( ( stp < 0 ) ? 0 : stack[ stp-- ] ) )
399 #define SF_PUSH(x) do { \
400 if ( stp >= ( STACK_SIZE - 1 ) ) SF_ERROR( overflow ); \
401 stack[ ++stp ] = (BerVarray)(long)(x); \
405 BerVarray stack[ STACK_SIZE ] = { 0 };
408 char c, *filter = fbv->bv_val;
415 while ( ( c = *filter++ ) ) {
425 if ( IS_SET( SF_TOP() ) ) {
433 if ( IS_OP( set ) ) {
436 if ( SF_TOP() == (void *)'(' /* ) */ ) {
441 } else if ( IS_OP( SF_TOP() ) ) {
442 op = (unsigned long)SF_POP();
445 set = slap_set_join( cp, lset, op, set );
457 case '|': /* union */
458 case '&': /* intersection */
459 case '+': /* string concatenation */
461 if ( IS_OP( set ) ) {
464 if ( SF_TOP() == 0 || SF_TOP() == (void *)'(' /* ) */ ) {
468 } else if ( IS_OP( SF_TOP() ) ) {
469 op = (unsigned long)SF_POP();
471 set = slap_set_join( cp, lset, op, set );
485 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
488 for ( len = 0; ( c = *filter++ ) && ( c != /* [ */ ']' ); len++ )
494 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
495 cp->set_op->o_tmpmemctx );
499 set->bv_val = cp->set_op->o_tmpcalloc( len + 1, sizeof( char ),
500 cp->set_op->o_tmpmemctx );
501 if ( BER_BVISNULL( set ) ) {
504 AC_MEMCPY( set->bv_val, &filter[ - len - 1 ], len );
515 /* fall through to next case */
518 if ( IS_OP( SF_TOP() ) ) {
526 && ( c < 'A' || c > 'Z' )
527 && ( c < 'a' || c > 'z' ) )
533 ( c = filter[ len ] )
534 && ( ( c >= '0' && c <= '9' )
535 || ( c >= 'A' && c <= 'Z' )
536 || ( c >= 'a' && c <= 'z' ) );
540 && memcmp( "this", filter, len ) == 0 )
542 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
545 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
546 cp->set_op->o_tmpmemctx );
550 ber_dupbv_x( set, target, cp->set_op->o_tmpmemctx );
551 if ( BER_BVISNULL( set ) ) {
554 BER_BVZERO( &set[ 1 ] );
557 && memcmp( "user", filter, len ) == 0 )
559 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
562 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
563 cp->set_op->o_tmpmemctx );
567 ber_dupbv_x( set, user, cp->set_op->o_tmpmemctx );
568 if ( BER_BVISNULL( set ) ) {
571 BER_BVZERO( &set[ 1 ] );
573 } else if ( SF_TOP() != (void *)'/' ) {
578 AttributeDescription *ad = NULL;
579 const char *text = NULL;
585 if ( slap_bv2ad( &fb2, &ad, &text ) != LDAP_SUCCESS ) {
589 /* NOTE: ad must have distinguishedName syntax
590 * or expand in an LDAP URI if c == '*'
593 set = set_chase( gatherer,
594 cp, SF_POP(), ad, c == '*' );
610 if ( IS_OP( set ) ) {
613 if ( SF_TOP() == 0 ) {
616 } else if ( IS_OP( SF_TOP() ) ) {
617 op = (unsigned long)SF_POP();
619 set = slap_set_join( cp, lset, op, set );
628 rc = slap_set_isempty( set ) ? 0 : 1;
635 if ( IS_SET( set ) ) {
636 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
638 while ( ( set = SF_POP() ) ) {
639 if ( IS_SET( set ) ) {
640 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );