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 ) {
329 slap_set_filter( SLAP_SET_GATHER gatherer,
330 SetCookie *cp, struct berval *fbv,
331 struct berval *user, struct berval *target, BerVarray *results )
333 #define STACK_SIZE 64
334 #define IS_SET(x) ( (unsigned long)(x) >= 256 )
335 #define IS_OP(x) ( (unsigned long)(x) < 256 )
336 #define SF_ERROR(x) do { rc = -1; goto _error; } while ( 0 )
337 #define SF_TOP() ( (BerVarray)( ( stp < 0 ) ? 0 : stack[ stp ] ) )
338 #define SF_POP() ( (BerVarray)( ( stp < 0 ) ? 0 : stack[ stp-- ] ) )
339 #define SF_PUSH(x) do { \
340 if ( stp >= ( STACK_SIZE - 1 ) ) SF_ERROR( overflow ); \
341 stack[ ++stp ] = (BerVarray)(long)(x); \
345 BerVarray stack[ STACK_SIZE ] = { 0 };
348 char c, *filter = fbv->bv_val;
355 while ( ( c = *filter++ ) ) {
365 if ( IS_SET( SF_TOP() ) ) {
373 if ( IS_OP( set ) ) {
376 if ( SF_TOP() == (void *)'(' /* ) */ ) {
381 } else if ( IS_OP( SF_TOP() ) ) {
382 op = (unsigned long)SF_POP();
385 set = slap_set_join( cp, lset, op, set );
397 case '|': /* union */
398 case '&': /* intersection */
399 case '+': /* string concatenation */
401 if ( IS_OP( set ) ) {
404 if ( SF_TOP() == 0 || SF_TOP() == (void *)'(' /* ) */ ) {
408 } else if ( IS_OP( SF_TOP() ) ) {
409 op = (unsigned long)SF_POP();
411 set = slap_set_join( cp, lset, op, set );
425 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
428 for ( len = 0; ( c = *filter++ ) && ( c != /* [ */ ']' ); len++ )
434 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
435 cp->set_op->o_tmpmemctx );
439 set->bv_val = cp->set_op->o_tmpcalloc( len + 1, sizeof( char ),
440 cp->set_op->o_tmpmemctx );
441 if ( BER_BVISNULL( set ) ) {
444 AC_MEMCPY( set->bv_val, &filter[ - len - 1 ], len );
455 /* fall through to next case */
458 if ( IS_OP( SF_TOP() ) ) {
466 && ( c < 'A' || c > 'Z' )
467 && ( c < 'a' || c > 'z' ) )
473 ( c = filter[ len ] )
474 && ( ( c >= '0' && c <= '9' )
475 || ( c >= 'A' && c <= 'Z' )
476 || ( c >= 'a' && c <= 'z' ) );
480 && memcmp( "this", filter, len ) == 0 )
482 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
485 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
486 cp->set_op->o_tmpmemctx );
490 ber_dupbv_x( set, target, cp->set_op->o_tmpmemctx );
491 if ( BER_BVISNULL( set ) ) {
494 BER_BVZERO( &set[ 1 ] );
497 && memcmp( "user", filter, len ) == 0 )
499 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
502 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
503 cp->set_op->o_tmpmemctx );
507 ber_dupbv_x( set, user, cp->set_op->o_tmpmemctx );
508 if ( BER_BVISNULL( set ) ) {
511 BER_BVZERO( &set[ 1 ] );
513 } else if ( SF_TOP() != (void *)'/' ) {
518 AttributeDescription *ad = NULL;
519 const char *text = NULL;
525 if ( slap_bv2ad( &fb2, &ad, &text ) != LDAP_SUCCESS ) {
529 /* NOTE: ad must have distinguishedName syntax
530 * or expand in an LDAP URI if c == '*'
533 set = set_chase( gatherer,
534 cp, SF_POP(), ad, c == '*' );
550 if ( IS_OP( set ) ) {
553 if ( SF_TOP() == 0 ) {
556 } else if ( IS_OP( SF_TOP() ) ) {
557 op = (unsigned long)SF_POP();
559 set = slap_set_join( cp, lset, op, set );
568 rc = slap_set_isempty( set ) ? 0 : 1;
575 if ( IS_SET( set ) ) {
576 slap_set_dispose( cp, set, 0 );
578 while ( ( set = SF_POP() ) ) {
579 if ( IS_SET( set ) ) {
580 slap_set_dispose( cp, set, 0 );