2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2000-2005 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 cp->set_op->o_tmpfree( lset, cp->set_op->o_tmpmemctx );
271 if ( !( op_flags & SLAP_SET_RREFARR ) && rset != NULL ) {
272 cp->set_op->o_tmpfree( rset, cp->set_op->o_tmpmemctx );
279 set_chase( SLAP_SET_GATHER gatherer,
280 SetCookie *cp, BerVarray set, AttributeDescription *desc, int closure )
282 BerVarray vals, nset;
286 set = cp->set_op->o_tmpcalloc( 1, sizeof(struct berval),
287 cp->set_op->o_tmpmemctx );
292 if ( BER_BVISNULL( set ) ) {
296 nset = cp->set_op->o_tmpcalloc( 1, sizeof(struct berval), cp->set_op->o_tmpmemctx );
297 if ( nset == NULL ) {
298 slap_set_dispose( cp, set, 0 );
301 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
302 vals = (gatherer)( cp, &set[ i ], desc );
303 if ( vals != NULL ) {
304 nset = slap_set_join( cp, nset, '|', vals );
307 slap_set_dispose( cp, set, 0 );
310 for ( i = 0; !BER_BVISNULL( &nset[ i ] ); i++ ) {
311 vals = (gatherer)( cp, &nset[ i ], desc );
312 if ( vals != NULL ) {
313 nset = slap_set_join( cp, nset, '|', vals );
314 if ( nset == NULL ) {
325 slap_set_filter( SLAP_SET_GATHER gatherer,
326 SetCookie *cp, struct berval *fbv,
327 struct berval *user, struct berval *target, BerVarray *results )
329 #define STACK_SIZE 64
330 #define IS_SET(x) ( (unsigned long)(x) >= 256 )
331 #define IS_OP(x) ( (unsigned long)(x) < 256 )
332 #define SF_ERROR(x) do { rc = -1; goto _error; } while (0)
333 #define SF_TOP() ( (BerVarray)( (stp < 0) ? 0 : stack[ stp ] ) )
334 #define SF_POP() ( (BerVarray)( (stp < 0) ? 0 : stack[ stp-- ] ) )
335 #define SF_PUSH(x) do { \
336 if (stp >= (STACK_SIZE - 1)) SF_ERROR(overflow); \
337 stack[ ++stp ] = (BerVarray)(long)(x); \
341 BerVarray stack[ STACK_SIZE ] = { 0 };
344 char c, *filter = fbv->bv_val;
351 while ( ( c = *filter++ ) ) {
361 if ( IS_SET( SF_TOP() ) ) {
369 if ( IS_OP( set ) ) {
372 if ( SF_TOP() == (void *)'(' /* ) */ ) {
377 } else if ( IS_OP( SF_TOP() ) ) {
378 op = (unsigned long)SF_POP();
381 set = slap_set_join( cp, lset, op, set );
393 case '|': /* union */
394 case '&': /* intersection */
395 case '+': /* string concatenation */
397 if ( IS_OP( set ) ) {
400 if ( SF_TOP() == 0 || SF_TOP() == (void *)'(' /* ) */ ) {
404 } else if ( IS_OP( SF_TOP() ) ) {
405 op = (unsigned long)SF_POP();
407 set = slap_set_join( cp, lset, op, set );
421 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
424 for ( len = 0; ( c = *filter++ ) && (c != /* [ */ ']'); len++ )
430 set = cp->set_op->o_tmpcalloc( 2, sizeof(struct berval),
431 cp->set_op->o_tmpmemctx );
435 set->bv_val = cp->set_op->o_tmpcalloc( len + 1, sizeof(char),
436 cp->set_op->o_tmpmemctx );
437 if ( BER_BVISNULL( set ) ) {
440 AC_MEMCPY( set->bv_val, &filter[ - len - 1 ], len );
451 /* fall through to next case */
454 if ( IS_OP( SF_TOP() ) ) {
462 && ( c < 'A' || c > 'Z' )
463 && ( c < 'a' || c > 'z' ) )
469 ( c = filter[ len ] )
470 && ( ( c >= '0' && c <= '9' )
471 || ( c >= 'A' && c <= 'Z' )
472 || ( c >= 'a' && c <= 'z' ) );
476 && memcmp( "this", filter, len ) == 0 )
478 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
481 set = cp->set_op->o_tmpcalloc( 2, sizeof(struct berval),
482 cp->set_op->o_tmpmemctx );
486 ber_dupbv_x( set, target, cp->set_op->o_tmpmemctx );
487 if ( BER_BVISNULL( set ) ) {
490 BER_BVZERO( &set[ 1 ] );
493 && memcmp( "user", filter, len ) == 0 )
495 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
498 set = cp->set_op->o_tmpcalloc( 2, sizeof(struct berval),
499 cp->set_op->o_tmpmemctx );
503 ber_dupbv_x( set, user, cp->set_op->o_tmpmemctx );
504 if ( BER_BVISNULL( set ) ) {
507 BER_BVZERO( &set[ 1 ] );
509 } else if ( SF_TOP() != (void *)'/' ) {
514 AttributeDescription *ad = NULL;
515 const char *text = NULL;
521 if ( slap_bv2ad( &fb2, &ad, &text ) != LDAP_SUCCESS ) {
525 /* NOTE: ad must have distinguishedName syntax
526 * or expand in an LDAP URI if c == '*'
529 set = set_chase( gatherer,
530 cp, SF_POP(), ad, c == '*' );
546 if ( IS_OP( set ) ) {
549 if ( SF_TOP() == 0 ) {
552 } else if ( IS_OP( SF_TOP() ) ) {
553 op = (unsigned long)SF_POP();
555 set = slap_set_join( cp, lset, op, set );
564 rc = slap_set_isempty( set ) ? 0 : 1;
571 if ( IS_SET( set ) ) {
572 slap_set_dispose( cp, set, 0 );
574 while ( ( set = SF_POP() ) ) {
575 if ( IS_SET( set ) ) {
576 slap_set_dispose( cp, set, 0 );